当前位置:首页 » 编程语言 » 200行c语言

200行c语言

发布时间: 2023-03-16 18:21:39

① 急求啊,写一个200行的c语言程序,什么都可以,带解释,哪位大神帮帮忙

#include<stdio.h>
#defineOK1
#defineERROR0
#defineTRUE1
#defineFALSE0
#defineMAXSIZE100
#defineLElemTypeint
#defineStatusint
#defineBOOLint
typedefstruct
{
LElemTypedata;
intcur;
}Component,SLinkList[MAXSIZE];
intMalloc(SLinkListspace)
{
//若备用链表非空,则返回分配的结点下标(备用链表的第一个结点),否则返回0
inti=space[0].cur;
if(i)
space[0].cur=space[i].cur;
returni;
}
StatusFree(SLinkListspace,intk)
{
//将下标为空的空闲结点回收到备用链表(成为备用链表的第一个结点)
space[k].cur=space[0].cur;
space[0].cur=k;
returnOK;
}
StatusInitList(SLinkListL)
{
//构造一个空的链表L,表头为L的最后一个单元L[MAXSIZE-1],其余单元链成
//一个备用链表,表头为L的第一个单元L[0],“0”表示空指针
inti;
L[MAXSIZE-1].cur=0;
for(i=0;i<MAXSIZE-2;i++)
L[i].cur=i+1;
L[MAXSIZE-2].cur=0;
returnOK;
}
StatusClearList(SLinkListL)
{
//初始条件:线性表L已存在。操作结果:将L重置为空表
inti,j,k;
i=L[MAXSIZE-1].cur;
L[MAXSIZE-1].cur=0;
k=L[0].cur;
L[0].cur=i;
while(i)
{
j=i;
i=L[i].cur;
}
L[j].cur=k;
returnOK;
}
BOOLListEmpty(SLinkListL)
{
//若L是空表,返回TRUE;否则返回FALSE
if(!L[MAXSIZE-1].cur)
returnTRUE;
else
returnFALSE;
}
intListLength(SLinkListL)
{
//返回L中数据元素个数
inti,len;
i=L[MAXSIZE-1].cur;
len=0;
while(i)
{
i=L[i].cur;
len++;
}
returnlen;
}
StatusGetElem(SLinkListL,inti,LElemType*e)
{
//用e返回L中第i个元素的值
intj,k=MAXSIZE-1;
if(i<1||i>ListLength(L))
returnERROR;
for(j=1;j<=i;j++)
k=L[k].cur;
*e=L[k].data;
returnOK;
}
intLocateElem(SLinkListL,LElemTypee)
{
//在静态单链线性表L中查找第1个值为e的元素。若找到,则返回它在L中的
//位序,否则返回0。(与其它LocateElem()的定义不同)
inti=L[MAXSIZE-1].cur;
while(i&&L[i].data!=e)
i=L[i].cur;
returni;
}
StatusPriorElem(SLinkListL,LElemTypecur_e,LElemType*pre_e)
{
//初始条件:线性表L已存在
//操作结果:若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,
//否则操作失败,pre_e无定义
inti,j;
i=L[MAXSIZE-1].cur;
do{
j=i;
i=L[i].cur;
}while(i&&L[i].data!=cur_e);
if(i)
{
*pre_e=L[j].data;
returnOK;
}
returnERROR;
}
StatusNextElem(SLinkListL,LElemTypecur_e,LElemType*next_e)
{
//初始条件:线性表L已存在
//操作结果:若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,
//否则操作失败,next_e无定义
inti,j;
i=LocateElem(L,cur_e);
if(i)
{
j=L[i].cur;
if(j)
{
*next_e=L[j].data;
returnOK;
}
}
returnERROR;
}
StatusListInsert(SLinkListL,inti,LElemTypee)
{
//在L中第i个元素之前插入新的数据元素e
intj,k,l;
k=MAXSIZE-1;
if(i<1||i>ListLength(L)+1)
returnERROR;
j=Malloc(L);
if(j)
{
L[j].data=e;
for(l=1;l<=i-1;l++)
k=L[k].cur;
L[j].cur=L[k].cur;
L[k].cur=j;
returnOK;
}
returnERROR;
}
StatusListDelete(SLinkListL,inti,LElemType*e)
{
//删除在L中第i个数据元素e,并返回其值
intj,k;
if(i<1||i>ListLength(L))
returnERROR;
k=MAXSIZE-1;
for(j=1;j<=i-1;j++)
k=L[k].cur;
j=L[k].cur;
L[k].cur=L[j].cur;
*e=L[j].data;
Free(L,j);
returnOK;
}
StatusListTraverse(SLinkListL,void(*visit)(LElemTypee))
{
inti=L[MAXSIZE-1].cur;
while(i)
{
visit(L[i].data);
i=L[i].cur;
}
returnOK;
}
voidVisit(LElemTypee)
{
printf("%d ",e);
}
intmain()
{
inti,j,k;
LElemTypee,e0;
SLinkListL;
InitList(L);
for(j=1;j<=5;j++)
i=ListInsert(L,1,j);
ListTraverse(L,Visit);
//判断链表是否为空
i=ListEmpty(L);
printf("%d ",i);
//打印链表长度
printf("%d ",ListLength(L));
//清空静态链表
ClearList(L);
ListTraverse(L,Visit);
for(j=1;j<=10;j++)
ListInsert(L,j,j);
//插入新节点后
ListTraverse(L,Visit);
//获取链表中的第5个元素
GetElem(L,5,&e);
printf("%d ",e);
for(j=0;j<=1;j++)
{
k=LocateElem(L,j);
if(k)
printf("%d%d ",j,k);//j在链表中的序号k
else
printf("Can'tfind%d ",j);//链表中不存在j
}
for(j=1;j<=2;j++)//测试头两个数据
{
GetElem(L,j,&e0);//把第j个数据赋给e0
i=PriorElem(L,e0,&e);//求e0的前驱
if(!i)
printf("Noelembefore%d ",e0);
else
printf("Elembefore%dis%d ",e0,e);//数据e0的前驱
}
for(j=ListLength(L)-1;j<=ListLength(L);j++)//最后两个数据
{
GetElem(L,j,&e0);//把第j个数据赋给e0
i=NextElem(L,e0,&e);//求e0的后继
if(!i)
printf("Noelemafter%d ",e0);
else
printf("Theelemafter%is%d ",e0,e);//数据e0的后继
}
k=ListLength(L);//k为表长
for(j=k+1;j>=k;j--)
{
i=ListDelete(L,j,&e);//删除第j个数据
if(i)
printf("DeleteSuccussfully ");
else
printf("Can'tfindtheelem ",j);
}
ListTraverse(L,Visit);
return0;
}

给你找了个静态链表的代码,能编译运行

② 单片机,c语言编写程序,现在是大四学生,一般我所需要的程序200行左右,是先写子函数呢,还是先写m

一般来说先写子函数,而且子函数中被调用的的子函数最好先写,不然会报错陵腊腔。另外也可以事先申明一下某个尺衫函数,那么局并此函数就可以放在此函数被调用的函数后面写。

③ 高分求一个200行左右的C语言程序要简单的我才学C语言

//C语言代码
#include <stdio.h>
void main()
{
printf("hello!");//此行复制200遍即可
}

④ 200行以上C语言程序举例

无聊,粘着玩 657行

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <ctype.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <Winsock2.h>
#include <ws2tcpip.h>
#include <io.h>

int raw2iframe(char *rawfile, char *ifile );
int Ts_pes(const char *src, const char *des ,unsigned short pid);
int pes_raw( char *pesfile, char *rawfile );
int es_pes(char *src, char *des);
int pes_ts(char *tsfile, char *pesfile);

static unsigned char m_buf[188 * 1024];
static unsigned char buf[188*1024];

/* 设置Dts时间戳(90khz) */
unsigned int SetDtsTimeStamp( unsigned char *buf, unsigned int time_stemp)
{

buf[0] = ((time_stemp >> 29) | 0x11 ) & 0x1f;
buf[1] = time_stemp >> 22;
buf[2] = (time_stemp >> 14) | 0x01;
buf[3] = time_stemp >> 7;
buf[4] = (time_stemp << 1) | 0x01;
return 0;
}

/* 设置Pts时间戳(90khz) */
unsigned int SetPtsTimeStamp( unsigned char *buf, unsigned int time_stemp)
{

buf[0] = ((time_stemp >> 29) | 0x31 ) & 0x3f;
buf[1] = time_stemp >> 22;
buf[2] = (time_stemp >> 14) | 0x01;
buf[3] = time_stemp >> 7;
buf[4] = (time_stemp << 1) | 0x01;
return 0;
}

/* 读取时戳(45khz) */
unsigned int GetTimeStamp( unsigned char *buf )
{
unsigned int ts;
unsigned char *p = buf;

ts = ((*p>>1) & 0x7) << 30;
p++;
ts += (*p) << 22;
p++;
ts += ((*p)>>1) << 15;
p++;
ts += (*p) << 7;
p++;
ts += (*p) >> 1;
p++;

return ts;
}

/*主函数*/
int main(void)
{
Ts_pes("shoes.ts", "641.pes", 641); //提取PES
pes_raw("641.pes","641.raw"); //提取ES
raw2iframe( "641.raw", "641.iframe" ); //提取I帧
es_pes("641.iframe", "es.pes"); //打包成PES
pes_ts("pes.ts","es.pes"); //打包成TS
return 0;
}

/*从视频中提取PES*/
int Ts_pes(const char *src, const char *des ,unsigned short pid)
{
unsigned char *p;
FILE *fpr, *fpw;
int size;
int ret;
unsigned short vpid;
unsigned char adaptation_field_control;
unsigned char continuity_counter;
unsigned char adaptation_field_length;
unsigned char *payload;
unsigned char payload_unit_start_indicator;
unsigned char last_counter = 0xff;
int start = 0;
fpr = fopen(src, "rb"); //打开文件
fpw = fopen(des, "wb"); //写入文件
if(NULL == fpr || NULL == fpw)
{
return -1;
}
size = sizeof(m_buf);

while(!feof(fpr))
{
ret = fread(m_buf, 1, size, fpr ); //读取文件
p = m_buf;
while(p < m_buf + ret)
{
vpid = (p[1] & 0x1f) << 8 | p[2];
if(pid == vpid)
{
adaptation_field_control = (p[3]>>4)&0x3; //判断是否有可调整字段
continuity_counter = p[3] & 0xf;
payload_unit_start_indicator = (p[1]>>6) & 0x1;
payload = NULL;
adaptation_field_length = p[4];
switch(adaptation_field_control)
{
case 0:
case 2:
break; /*0为保留,2为只有调整无负载*/
case 1:
payload = p + 4; /*无调整字段*/
break;
case 3:
payload = p + 5 + p[4];/*净荷*/
break;
}
if(1 == payload_unit_start_indicator)
{
start = 1;
}
if(start && payload)
{
fwrite(payload, 1, p + 188 - payload, fpw); //写入文件
}
if( last_counter!= 0xff && ((last_counter +1 )&0xf) != continuity_counter )
{
printf( "data lost\n" );
}
last_counter = continuity_counter;
}
p = p + 188;
}
}

printf("ts_pes_END\n");

fclose(fpr); //关闭
fclose(fpw);
return 0;
}

/*从PES中提取ES*/
int pes_raw( char *pesfile, char *rawfile )
{
FILE *fpd, *fp;
unsigned char *p, *payload, *tmp;
int size, num, rdsize;
unsigned int last = 0;
__int64 total = 0, wrsize = 0;

fp = fopen( pesfile, "rb" );
fpd = fopen( rawfile, "wb" );
if( fp == NULL || fpd == NULL )
return -1;

num = 0;
size = 0;
p = m_buf;

while( true )
{
REDO:
if( m_buf + size <= p )
{
p = m_buf;
size = 0;
}
else if( m_buf < p && p < m_buf + size )
{
size -= p - m_buf;
memmove( m_buf, p, size );
p = m_buf;
}

if( !feof(fp) && size < sizeof(m_buf) )
{
rdsize = fread( m_buf+size, 1, sizeof(m_buf)-size, fp );
size += rdsize;
total += rdsize;
}
if( size <= 0 )
break;

tmp = p;
/* 寻找PES-HEADER: 0X000001E0 */
while( p[0] != 0 || p[1] != 0 || p[2] != 0x01 ||
( ( p[3] & 0xe0 ) != 0xe0 && ( p[3] & 0xe0 ) != 0xc0 ) )
{
p++;
if( m_buf + size <= p )
goto REDO;
}
if( p != tmp )
{
printf( "pes skip size=%d\n", p - tmp );
}
/* PES_packet_length */
unsigned short len = (p[4]<<8) | p[5];
if( len == 0 )
{
unsigned char *end = p + 6;
while( end[0] != 0 || end[1] != 0 || end[2] != 0x01 ||
( ( end[3] & 0xe0 ) != 0xe0 && ( end[3] & 0xc0 ) != 0xc0 ) )
{
if( m_buf + size <= end )
{
if( feof(fp) )
break;
goto REDO;
}
end++;
}
len = end - p - 6;
}
if( m_buf + size < p + 6 + len )
{
if( feof(fp) )
break;
continue;
}
p += 6;

{
unsigned char PES_scrambling_control = (*p>>4)&0x3;
unsigned char PES_priority = (*p>>3)&0x1;
unsigned char data_alignment_indicator = (*p>>2)&0x1;
unsigned char right = (*p>>1)&0x1;
unsigned char original_or_ = (*p)&0x1;
p++;
unsigned char PTS_DTS_flags = (*p>>6)&0x3;
unsigned char ESCR_flag = (*p>>5)&0x1;
unsigned char ES_rate_flag = (*p>>4)&0x1;
unsigned char DSM_trick_mode_flag = (*p>>3)&0x1;
unsigned char additional__info_flag = (*p>>2)&0x1;
unsigned char PES_CRC_flag = (*p>>1)&0x1;
unsigned char PES_extension_flag = (*p)&0x1;
p++;
unsigned char PES_header_data_length = *p;
p++;

payload = p + PES_header_data_length;

if (PTS_DTS_flags == 0x2 )
{
unsigned int pts;
pts = (*p>>1) & 0x7;
pts = pts << 30;
p++;
pts += (*p)<<22;
p++;
pts += ((*p)>>1)<<15;
p++;
pts += (*p)<<7;
p++;
pts += (*p)>>1;
p++;

p -= 5;

if( pts < last )
{
printf( "?\n" );
}
last = pts;
}
else if( PTS_DTS_flags == 0x3 )
{
unsigned int pts, dts;

pts = (*p>>1) & 0x7;
pts = pts << 30;
p++;
pts += (*p)<<22;
p++;
pts += ((*p)>>1)<<15;
p++;
pts += (*p)<<7;
p++;
pts += (*p)>>1;
p++;

dts = (*p>>1) & 0x7;
dts = dts << 30;
p++;
dts += (*p)<<22;
p++;
dts += ((*p)>>1)<<15;
p++;
dts += (*p)<<7;
p++;
dts += (*p)>>1;
p++;

p -= 10;
printf( "num=%d ra=%d size=%d pts-dts=%d\n", num, pts - last, len-3-PES_header_data_length, (int)(pts-dts) );
if( pts < last )
{
printf( "?\n" );
}
last = pts;
}
else if( PTS_DTS_flags != 0 )
{
printf( "error\n" );
}

if( fpd )
{
fwrite( p + PES_header_data_length, 1, len - 3 - PES_header_data_length, fpd );
wrsize += len - 3 - PES_header_data_length;
}
num++;
p += len - 3;
}

payload = p;
size -= p - m_buf;
memmove( m_buf, p, size );
p = m_buf;
}

fclose( fp );
fclose( fpd );

printf("pes_raw_END\n");
return 0;
}

/*提取I帧*/
int raw2iframe(char *rawfile, char *ifile )
{
unsigned char *temp_p;
unsigned char *p;
unsigned char picture_coding_type;
unsigned char buf[188*1024] = {0};
unsigned char pes_buf[32*1024] = {0};
unsigned short pid = 641;
unsigned short t_pid = 0;
int i = 0;
int j = 0;
int size = 0;
int iLen = 0;
int wiLen = 0;
int temp_queue = 0;
int temp_ifrem = 0;

void *fps = CreateFile(rawfile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); //打开读文件
void *fpd = CreateFile(ifile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0 , NULL); //打开写文件

temp_p = NULL;

while(1)
{
if (0 == ReadFile(fps, buf+size, sizeof(buf)-size, (unsigned long *)&iLen, NULL)) //读取文件内容
{
CloseHandle((HANDLE)fps);
return -1;
}

if (0 == iLen)
{
break;
}

p = buf;

while( p + 6 < buf + iLen +size)
{
if (p[0] == 0x0 && p[1] == 0x0 && p[2] == 0x1) //是头进入
{

if (p[3] == 0x0 || p[3] == 0xB3 )
{
if ( (NULL != temp_p) && ((1 == temp_queue) || (1 == temp_ifrem)))
{
WriteFile(fpd, temp_p, p-temp_p, (unsigned long *)&wiLen, NULL); //写文件,先写序列头,再写I帧
temp_queue = 0;
temp_ifrem = 0;
temp_p = NULL;
}
}

if (p[3] == 0xB3) //判断是视频序列,则初始化
{
temp_queue = 1;
temp_p = p;
}

picture_coding_type = (p[5]>>3) & 0x7;
if (p[3] == 0x0 && 1 == picture_coding_type) //判断是I帧,则初始化
{
temp_ifrem = 1;
temp_p = p;
}
}

p++;
}
/*把多出来的内容写入下个BUF*/
if(NULL != temp_p)
{
size = buf + iLen + size - temp_p;
memmove(buf, temp_p, size);
}
else
{
size = buf + iLen + size - p;
memmove(buf, p, size);
}

temp_p = NULL;
}

CloseHandle((HANDLE)fps);
CloseHandle((HANDLE)fpd);

printf("raw_iframe_END\n");

return 0;
}

/*打包成PES*/
int es_pes(char *src, char *des)
{
FILE *iframe_fp, *pes_fp;
unsigned char *p;
unsigned char *temp_p = NULL;
unsigned char buf[188*1024] = {0};
unsigned char pes_header[19];
unsigned short int pes_packet_length = 0;
unsigned int framecnt = 0;
unsigned char flag = 0;
unsigned int Pts = 0;
unsigned int Dts = 0;
int i = 0;
int size = 0;
int iLen = 0;
int wiLen = 0;
int temp_que = 0;

iframe_fp = fopen( src, "rb" );
pes_fp = fopen( des, "wb" );
if( iframe_fp == NULL || pes_fp == NULL )
{
return -1;
}

while(!feof(iframe_fp))
{
iLen = fread(buf + size, 1, sizeof(buf) - size, iframe_fp);
p = buf;

while( p + 3 < buf + iLen +size)
{
memset(pes_header, 0, sizeof(pes_header));
if (p[0] == 0x0 && p[1] == 0x0 && p[2] == 0x1 && 0xB3 == p[3])
{
if ((NULL != temp_p) && (1 == temp_que))
{
LAST_I:
pes_packet_length = p - temp_p + 13;
pes_header[4] = (pes_packet_length&0xff00) >> 8;
pes_header[5] = pes_packet_length&0x00ff;
/*PES包头的相关数据*/
pes_header[0] = 0;
pes_header[1] = 0;
pes_header[2] = 0x01;
pes_header[3] = 0xE0;
pes_header[6] = 0x81;
pes_header[7] = 0xC0;
pes_header[8] = 0x0A;

Dts = (framecnt + 1) * 40 * 90;
Pts = framecnt * 40 * 90;
SetPtsTimeStamp(&pes_header[9], Pts); //设置时间戳 PTS
SetDtsTimeStamp(&pes_header[14], Dts); //设置时间戳 DTS
framecnt++;
if (0 == flag)
{
fwrite(pes_header, 1, sizeof(pes_header), pes_fp); //把PES包头写入文件
fwrite(temp_p, 1, p-temp_p, pes_fp); //把I帧写入文件
}
else
{
fwrite(pes_header, 1, sizeof(pes_header), pes_fp); //把PES包头写入文件
fwrite(temp_p, 1, p-temp_p+3, pes_fp); //把I帧写入文件
}

temp_p = NULL;

}

if (p[3] == 0xB3) //判断是否到了下一个序列头
{
temp_p = p;
temp_que = 1;
}
}

p++;
}

/*把多出来的内容写入下个BUF*/
if(NULL != temp_p)
{
if (feof(iframe_fp)) //把最后一帧写入文件
{
// flag = 1;
goto LAST_I;
}

size = buf + iLen + size - temp_p;
memmove(buf, temp_p, size);
}
else
{
size = buf + iLen + size - p;
memmove(buf, p, size);
}

temp_p = NULL;
}

printf("es_pes_END\n");

fclose(iframe_fp);
fclose(pes_fp);

return 0;
}

/*打包成TS包*/
int pes_ts(char *tsfile, char *pesfile)
{
FILE *ts_fp, *pes_fp;
int flag = 0;
int iLen = 0;
int size = 0;
int temp_pes = 0;
int pes_packet_len = 0;
unsigned char *p;
unsigned char counter = 0;
unsigned char *temp_p = NULL;
unsigned char ts_buf[188] = {0};
unsigned char start_indicator_flag = 0;

pes_fp = fopen(pesfile, "rb");
ts_fp = fopen(tsfile, "wb");
if( ts_fp == NULL || pes_fp == NULL )
{
return -1;
}

/*设ts参数*/
ts_buf[0] = 0x47;
ts_buf[1] = 0x62;
ts_buf[2] = 0x81;

while(!feof(pes_fp))
{
iLen = fread(buf+size, 1, sizeof(buf)-size, pes_fp); //读文件
p = buf;

while( p + 6 < buf + iLen +size)
{
if (0 == p[0] && 0 == p[1] && 0x01 == p[2] && 0xE0 == p[3]) //进入
{
if (flag == 0) //第一次找到PES包
{
temp_p = p;
flag = 1;
}
else
{
pes_packet_len = p - temp_p; //pes包长度
start_indicator_flag = 0;

while (1)
{
ts_buf[3] = counter;

if (1 != start_indicator_flag)
{
ts_buf[1] = ts_buf[1] | 0x40; //payload_unit_start_indicator置为1
start_indicator_flag = 1;
}
else
{
ts_buf[1] = ts_buf[1] & 0xBF; //payload_unit_start_indicator置为0
}

if (pes_packet_len > 184) //打包成TS包(188)
{
ts_buf[3] = ts_buf[3] & 0xDF;
ts_buf[3] = ts_buf[3] | 0x10;
memcpy(&ts_buf[4], temp_p, 184);
fwrite(ts_buf, 1, 188, ts_fp); //写文件
pes_packet_len -=184;
temp_p += 184;
}
else //不够184B的加入调整字段,为空
{
ts_buf[3] = ts_buf[3] | 0x30;
ts_buf[4] = 183 - pes_packet_len;
memcpy(&ts_buf[4] + 1 + ts_buf[4], temp_p, pes_packet_len);
fwrite(ts_buf, 1, 188, ts_fp); //写文件

break;
}

counter = (counter + 1) % 0x10; //ts包计数
}
}
temp_p = p;
}

p++;
}

if (1 == flag)
{
size = buf + iLen + size - temp_p;
memmove(buf, temp_p, size);
temp_p = NULL;
flag = 0;
}
else
{
size =buf + iLen + size - p;
memmove(m_buf , p , size);
}
}

printf("pes_ts_END\n");

fclose(pes_fp);
fclose(ts_fp);
return 0;
}

⑤ 200行的c语言代码,51单片机运行完要多长时间,没有循环结构

如果没有调用子程序的话,12M晶振时那么执行时间大概是200~600微妙

⑥ 求C语言200行以上的程序一个 急!!~~

给你一个通讯录管理系统程序 四百多行 够你用了吧!!! #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #define N 100 void input();//添加新用户函数 void amend();//修改用户信息函数 void delete_client();//删除用户信息函数 void demand_client();//用户信息查询函数 void collect_telephone();//用户信息汇总函数 void save_client(struct telephone message);//保存函数 void demand_name();//按用户名查询 void demand_telephone();//按电话号码查询 struct telephone { char client_name[20]; char client_address[30]; char client_telephone[15]; }; //添加新用户函数 void input() { struct telephone message; char reply='y'; char save='y'; while (reply=='y') { printf("用户姓名:"); scanf("%s",message.client_name); printf("电话号码:"); scanf("%s",message.client_telephone); save_client(message); printf("要继续吗?(y/n):"); scanf(" %c",&reply); } printf("按任意键返回主菜单……\n"); getchar(); getchar(); } //保存函数 void save_client(struct telephone message) { FILE *fp; fp=fopen("message.dat","a+"); if (fp!=NULL) { fwrite(&message,sizeof(struct telephone),1,fp); } else { printf("\n打开文件时出现错误,按任意键返回……\n"); getchar(); return; } fclose(fp); } //修改用户信息函数 void amend() { struct telephone message; FILE *fp; char amend_name[20]; char reply='y'; char found='y'; char save='y'; int size=sizeof(struct telephone); while (reply=='y') { found='n'; fp=fopen("message.dat","r+w"); if (fp!=NULL) { system("cls"); printf("\n请输入要修改的姓名:"); scanf("%s",amend_name); while ((fread(&message,size,1,fp))==1) { if ((strcmp(amend_name,message.client_name))==0) { found='y'; break; } } if (found=='y') { printf("==========================================\n"); printf("\n用户姓名:%s\n",message.client_name); printf("\n电话号码:%s\n",message.client_telephone); printf("==========================================\n"); printf("修改用户信息:\n"); printf("\n用户姓名:"); scanf("%s",message.client_name); printf("\n电话号码:"); scanf("%s",message.client_telephone); printf("\n要保存吗?(y/n):"); scanf(" %c",&save); if (save=='y') { fseek(fp,-size,1); fwrite(&message,sizeof(struct telephone),1,fp); } } else { printf("无此人信息!\n"); } } else { printf("打开文件时出现错误,按任意键返回……\n"); getchar(); return; } fclose(fp); printf("要继续吗?(y/n):"); scanf(" %c",&reply); } printf("按任意键返回主菜单……\n"); getchar(); getchar(); } //删除用户信息函数 void delete_client() { struct telephone message[N]; struct telephone temp_str; struct telephone delete_str; int i=0,j=0; char reply='y'; char found='y'; char confirm='y'; char delete_name[20]; FILE *fp; while (reply=='y') { system("cls"); fp=fopen("message.dat","r"); if (fp!=NULL) { i=0; found='n'; printf("\n请输入姓名:"); scanf("%s",delete_name); while ((fread(&temp_str,sizeof(struct telephone),1,fp))==1) { if ((strcmp(delete_name,temp_str.client_name))==0) { found='y'; delete_str=temp_str; }//查找要删除的记录 else { message[i]=temp_str; i++; }//将其它无关记录保存起来 } } else { printf("打开文件时出现错误,按任意键返回……\n"); getchar(); return; } fclose(fp); if (found=='y') { printf("==========================================\n"); printf("用户姓名:%s\n",delete_str.client_name); printf("电话号码:%s\n",delete_str.client_telephone); printf("==========================================\n"); } else { printf("无此人信息,按任意键返回……\n"); getchar(); break; } printf("确定要删除吗?(y/n):"); scanf(" %c",&confirm); if (confirm=='y') { fp=fopen("message.dat","w"); if (fp!=NULL) { for(j=0;j<i;j++) { fwrite(&message[j],sizeof(struct telephone),1,fp); } printf("记录已删除!!!\n"); } else { printf("打开文件时出现错误,按任意键返回……\n"); getchar(); return; } fclose(fp); } printf("要继续吗?(y/n):"); scanf(" %c",&reply); } printf("按任意键返回主菜单……\n"); getchar(); } //用户信息查询函数 void demand_client() { int choice=1; while (choice!=3) { system("cls"); printf("电话查询菜单\n"); printf(" 1 按联系人姓名查询\n"); printf(" 2 按联系人电话号码查询\n"); printf(" 3 返回主菜单\n"); printf("请选择(1-3):"); scanf("%d%*c",&choice); if (choice>3) { printf("请输入1-6之间的整数\n"); printf("按任意键返回菜单……\n"); getchar(); continue; } if (choice==1) { demand_name(); } else if (choice==2) { demand_telephone(); } } } //按用户名查询 void demand_name() { struct telephone message; FILE *fp; char amend_name[20]; char reply='y'; char found='y'; while (reply=='y') { found='n'; fp=fopen("message.dat","r+w"); if (fp!=NULL) { system("cls"); printf("\n请输入姓名:"); scanf("%s",amend_name); while ((fread(&message,sizeof(struct telephone),1,fp))==1) { if ((strcmp(amend_name,message.client_name))==0) { found='y'; break; } } if (found=='y') { printf("==========================================\n"); printf("用户姓名:%s\n",message.client_name); printf("电话号码:%s\n",message.client_telephone); printf("==========================================\n"); } else { printf("无此人信息!\n"); } } else { printf("打开文件时出现错误,按任意键返回……\n"); getchar(); return; } fclose(fp); printf("要继续吗?(y/n):"); scanf(" %c",&reply); } printf("按任意键返回主菜单……\n"); getchar(); getchar(); } //按电话号码查询 void demand_telephone() { struct telephone message; FILE *fp; char telephone[20]; char reply='y'; char found='y'; while (reply=='y') { found='n'; fp=fopen("message.dat","r+w"); if (fp!=NULL) { system("cls"); printf("\n请输入电话号码:"); scanf("%s",telephone); while ((fread(&message,sizeof(struct telephone),1,fp))==1) { if ((strcmp(telephone,message.client_telephone))==0) { found='y'; break; } } if (found=='y') { printf("==========================================\n"); printf("用户姓名:%s\n",message.client_name); printf("电话号码:%s\n",message.client_telephone); printf("==========================================\n"); } else { printf("无此电话号码的有关信息!\n"); } } else { printf("打开文件时出现错误,按任意键返回……\n"); getchar(); return; } fclose(fp); printf("要继续吗?(y/n):"); scanf(" %c",&reply); } printf("按任意键返回主菜单……\n"); getchar(); getchar(); } //用户信息汇总函数 void collect_telephone() { struct telephone message; FILE *fp; fp=fopen("message.dat","r"); if (fp!=NULL) { system("cls"); printf("\n用户姓名\t\t电话号码\n"); while ((fread(&message,sizeof(struct telephone),1,fp))==1) { printf("\n%-24s",message.client_name); printf("%-12s\n",message.client_telephone); } } else { printf("打开文件时出现错误,按任意键返回……\n"); getchar(); return; } fclose(fp); printf("按任意键返回主菜单……\n"); getch(); } void main() { char choice[10]=""; int len=0; while (choice[0]!='7') { printf("\t==========电话本号码查询系统=============\n"); printf("\t\t 1、添加新联系人\n"); printf("\t\t 2、修改联系人信息\n"); printf("\t\t 3、删除联系人信息\n"); printf("\t\t 4、联系人信息查询\n"); printf("\t\t 5、联系人信息汇总\n"); printf("\t\t 7、退出\n"); printf("\t=========================================\n"); printf("请选择(1-7):"); scanf("%s",choice); len=strlen(choice); if (len>1) { printf("请输入1-6之间的整数\n"); printf("按任意键返回主菜单……\n"); getchar(); getchar(); continue; } switch (choice[0]) { case '1': input(); break; case '2': amend(); break; case '3': delete_client(); break; case '4': demand_client(); break; case '5': collect_telephone(); break; default: break; } } }

⑦ 求大一C语言课程设计200行的程序

C语言编写的迷宫小游戏 源代码

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define N 20/*迷宫的大小,可改变*/
int oldmap[N][N];/*递归用的数组,用全局变量节约时间*/
int yes=0;/*yes是判断是否找到路的标志,1找到,0没找到*/
int way[100][2],wayn=0;/*way数组是显示路线用的,wayn是统计走了几个格子*/
void Init(void);/*图形初始化*/
void Close(void);/*图形关闭*/
void DrawPeople(int *x,int *y,int n);/*画人工探索物图*/
void PeopleFind(int (*x)[N]);/*人工探索*/
void WayCopy(int (*x)[N],int (*y)[N]);/*为了8个方向的递归,把旧迷宫图拷贝给新数组*/
int FindWay(int (*x)[N],int i,int j);/*自动探索函数*/
void MapRand(int (*x)[N]);/*随机生成迷宫函数*/
void PrMap(int (*x)[N]);/*输出迷宫图函数*/
void Result(void);/*输出结果处理*/
void Find(void);/*成功处理*/
void NotFind(void);/*失败处理*/
void main(void)/*主函数*/
{
int map[N][N]; /*迷宫数组*/
char ch;
clrscr();
printf("\n Please select hand(1) else auto\n");/*选择探索方式*/
scanf("%c",&ch);
Init(); /*初始化*/
MapRand(map);/*生成迷宫*/
PrMap(map);/*显示迷宫图*/
if(ch=='1')
PeopleFind(map);/*人工探索*/
else
FindWay(map,1,1);/*系统自动从下标1,1的地方开始探索*/
Result();/*输出结果*/
Close();
}
void Init(void)/*图形初始化*/
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
}
void DrawPeople(int *x,int *y,int n)/*画人工控制图*/
{/*如果将以下两句注释掉,则显示人工走过的路径,*/
setfillstyle(SOLID_FILL,WHITE); /*设置白色实体填充样式*/
bar(100+(*y)*15-6,50+(*x)*15-6,100+(*y)*15+6,50+(*x)*15+6);
/*恢复原通路*/
switch(n)/*判断x,y的变化,8个方向的变化*/
{
case 1: (*x)--;break; /*上*/
case 2: (*x)--;(*y)++;break ;/*右上*/
case 3: (*y)++;break; /*右*/
case 4: (*x)++;(*y)++;break; /*右下*/
case 5: (*x)++;break; /*下*/
case 6: (*x)++;(*y)--;break; /*左下*/
case 7: (*y)--;break; /*左*/
case 8: (*x)--;(*y)--;break; /*左上*/
}
setfillstyle(SOLID_FILL,RED);/*新位置显示探索物*/
bar(100+(*y)*15-6,50+(*x)*15-6,100+(*y)*15+6,50+(*x)*15+6);
}
void PeopleFind(int (*map)[N])/*人工手动查找*/
{
int x,y;
char c=0;/*接收按键的变量*/
x=y=1;/*人工查找的初始位置*/
setcolor(11);
line(500,200,550,200);
outtextxy(570,197,"d");
line(500,200,450,200);
outtextxy(430,197,"a");
line(500,200,500,150);
outtextxy(497,130,"w");
line(500,200,500,250);
outtextxy(497,270,"x");
line(500,200,450,150);
outtextxy(445,130,"q");
line(500,200,550,150);
outtextxy(550,130,"e");
line(500,200,450,250);
outtextxy(445,270,"z");
line(500,200,550,250);
outtextxy(550,270,"c");/*以上是画8个方向的控制介绍*/
setcolor(YELLOW);
outtextxy(420,290,"Press 'Enter' to end");/*压回车键结束*/
setfillstyle(SOLID_FILL,RED);
bar(100+y*15-6,50+x*15-6,100+y*15+6,50+x*15+6);/*入口位置显示*/
while(c!=13)/*如果按下的不是回车键*/
{
c=getch();/*接收字符后开始各个方向的探索*/
if(c=='w'&&map[x-1][y]!=1)
DrawPeople(&x,&y,1);/*上*/
else
if(c=='e'&&map[x-1][y+1]!=1)
DrawPeople(&x,&y,2);/*右上*/
else
if(c=='d'&&map[x][y+1]!=1)
DrawPeople(&x,&y,3);/*右*/
else
if(c=='c'&&map[x+1][y+1]!=1)
DrawPeople(&x,&y,4);/*右下*/
else
if(c=='x'&&map[x+1][y]!=1)
DrawPeople(&x,&y,5);/*下*/
else
if(c=='z'&&map[x+1][y-1]!=1)
DrawPeople(&x,&y,6); /*左下*/
else
if(c=='a'&&map[x][y-1]!=1)
DrawPeople(&x,&y,7); /*左*/
else if(c=='q'&&map[x-1][y-1]!=1)
DrawPeople(&x,&y,8); /*左上*/
}
setfillstyle(SOLID_FILL,WHITE); /*消去红色探索物,恢复原迷宫图*/
bar(100+y*15-6,50+x*15-6,100+y*15+6,50+x*15+6);
if(x==N-2&&y==N-2)/*人工控制找成功的话*/
yes=1; /*如果成功标志为1*/
}
void WayCopy(int (*oldmap)[N],int (*map)[N])/*拷贝迷宫数组 */
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
oldmap[i][j]=map[i][j];
}
int FindWay(int (*map)[N],int i,int j)/*递归找路*/
{
if(i==N-2&&j==N-2)/*走到出口*/
{
yes=1;/*标志为1,表示成功*/
return;
}
map[i][j]=1;/*走过的地方变为1*/
WayCopy(oldmap,map); /*拷贝迷宫图*/
if(oldmap[i+1][j+1]==0&&!yes)/*判断右下方是否可走*/
{
FindWay(oldmap,i+1,j+1);
if(yes)/*如果到达出口了,再把值赋给显示路线的way数组,也正是这个原因,所以具体路线是从最后开始保存*/
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i+1][j]==0&&!yes)/*判断下方是否可以走,如果标志yes已经是1也不用找下去了*/
{
FindWay(oldmap,i+1,j);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i][j+1]==0&&!yes)/*判断右方是否可以走*/
{
FindWay(oldmap,i,j+1);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i-1][j]==0&&!yes)/*判断上方是否可以走*/
{
FindWay(oldmap,i-1,j);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i-1][j+1]==0&&!yes)/*判断右上方是否可以走*/
{
FindWay(oldmap,i-1,j+1);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i+1][j-1]==0&&!yes)/*判断左下方是否可以走*/
{
FindWay(oldmap,i+1,j-1);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i][j-1]==0&&!yes)/*判断左方是否可以走*/
{
FindWay(oldmap,i,j-1);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
WayCopy(oldmap,map);
if(oldmap[i-1][j-1]==0&&!yes)/*判断左上方是否可以走*/
{
FindWay(oldmap,i-1,j-1);
if(yes)
{
way[wayn][0]=i;
way[wayn++][1]=j;
return;
}
}
return;
}
void MapRand(int (*map)[N])/*开始的随机迷宫图*/
{
int i,j;
cleardevice();/*清屏*/
randomize(); /*随机数发生器*/
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(i==0||i==N-1||j==0||j==N-1)/*最外面一圈为墙壁*/
map[i][j]=1;
else
if(i==1&&j==1||i==N-2&&j==N-2)/*出发点与终点表示为可走的*/
map[i][j]=0;
else
map[i][j]=random(2);/*其它的随机生成0或1*/
}
}
}
void PrMap(int (*map)[N])/*输出迷宫图*/
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(map[i][j]==0)
{
setfillstyle(SOLID_FILL,WHITE);/*白色为可走的路*/
bar(100+j*15-6,50+i*15-6,100+j*15+6,50+i*15+6);
}
else
{
setfillstyle(SOLID_FILL,BLUE);/*蓝色为墙壁*/
bar(100+j*15-6,50+i*15-6,100+j*15+6,50+i*15+6);
}
}
void Find(void)/*找到通路*/
{
int i;
setfillstyle(SOLID_FILL,RED);/*红色输出走的具体路线*/
wayn--;
for(i=wayn;i>=0;i--)
{
bar(100+way[i][1]*15-6,50+way[i][0]*15-6,100+
way[i][1]*15+6,50+way[i][0]*15+6);
sleep(1);/*控制显示时间*/
}
bar(100+(N-2)*15-6,50+(N-2)*15-6,100+
(N-2)*15+6,50+(N-2)*15+6); /*在目标点标红色*/
setcolor(GREEN);
settextstyle(0,0,2);/*设置字体大小*/
outtextxy(130,400,"Find a way!");
}
void NotFind(void)/*没找到通路*/
{
setcolor(GREEN);
settextstyle(0,0,2);/*设置字体大小*/
outtextxy(130,400,"Not find a way!");
}
void Result(void)/*结果处理*/
{
if(yes)/*如果找到*/
Find();
else/*没找到路*/
NotFind();
getch();
}
void Close(void)/*图形关闭*/
{
closegraph();
}

热点内容
动态规划01背包算法 发布:2024-11-05 22:17:40 浏览:849
nasm编译器如何安装 发布:2024-11-05 22:01:13 浏览:180
登录密码在微信的哪里 发布:2024-11-05 22:00:29 浏览:738
c防止反编译工具 发布:2024-11-05 21:56:14 浏览:247
安卓虚拟机怎么用 发布:2024-11-05 21:52:48 浏览:343
php时间搜索 发布:2024-11-05 20:58:36 浏览:478
燕山大学编译原理期末考试题 发布:2024-11-05 20:13:54 浏览:527
华为电脑出现临时服务器 发布:2024-11-05 20:05:08 浏览:408
斗战神免费挖矿脚本 发布:2024-11-05 19:53:25 浏览:665
网吧服务器分别是什么 发布:2024-11-05 19:45:32 浏览:392