200行c語言
① 急求啊,寫一個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();
}