C語(yǔ)言下期考試題庫(kù)知識(shí)分享_第1頁(yè)
C語(yǔ)言下期考試題庫(kù)知識(shí)分享_第2頁(yè)
C語(yǔ)言下期考試題庫(kù)知識(shí)分享_第3頁(yè)
C語(yǔ)言下期考試題庫(kù)知識(shí)分享_第4頁(yè)
C語(yǔ)言下期考試題庫(kù)知識(shí)分享_第5頁(yè)
已閱讀5頁(yè),還剩72頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Good is good, but better carries it.精益求精,善益求善。C語(yǔ)言下期考試題庫(kù)-改錯(cuò).1.程序D8.c的功能是打開(kāi)文件d:te.c用于讀并判斷打開(kāi)是否成功【此部分在計(jì)算中心已公布的題庫(kù)中,未標(biāo)明正確答案,同學(xué)們自己做】#includeintmain(void)FILE*fp;/*Found*/charfileName=d:te.c;/*Found*/fp=fopen(fileName,r);/*Found*/if(fp=NULL)puts(FileOpenError!);return1;putchar(fgetc(fp);fclose(fp);return0;2

2、.程序D9.c的功能是申請(qǐng)100個(gè)字節(jié)的內(nèi)存空間,顯示其首地址,然后釋放申請(qǐng)到的內(nèi)存空間#include#includeintmain(void)/*Found*/char*pch2;/*Found*/if(pch=(char*)malloc(100)=NULL)printf(mallocmemoryfail!n);return1;printf(%pn,pch);/*Found*/free(pch);return0;3.程序D10.c的功能是將字符串str2顯示在屏幕上#include/*Found*/#defineSTART/*Found*/#defineNEXTintmain(void)c

3、har*str2constinti=0;for(printf(n);str2i;)STARTputchar(str2i);/*Found*/p+;NEXTreturn0;4.程序D16.c的功能是worker2的信息使用結(jié)構(gòu)體存儲(chǔ),從鍵盤(pán)讀入其各項(xiàng)信息并顯示。#includeintmain(void)structWKERlongID;longintnum;charname20;charsex;/*Found*/worker2;worker2.ID=1L;/*Found*/scanf(%ld%s%c,&worker2.num,&worker2.sex);/*Found*

4、/printf(workersinfo:num=%ldname=%ssex=%cn,worker2.num,worker2.sex);return0;程序D19.c的功能是將inBuf中字符串拆分成一個(gè)個(gè)的單詞。程序說(shuō)明:(1)單詞之間的分隔符由串divChar,程序中定義為“;?!,./”。(2)函數(shù)getStrFromBuf的功能是從Buf的開(kāi)始處尋找第一個(gè)單詞,將找到的單詞作為一個(gè)字符串復(fù)制到Str處,divStr指明單詞間的分隔符。GetStrFromBuf的返回值指向已找到單詞的下一個(gè)字符。(3)函數(shù)charInStr的功能是判斷字符t是否出現(xiàn)在串Str中。(4)主程序的功能是將in

5、Buf中的所有單詞在屏幕上顯示出來(lái)。#include#defineYes1#defineNo0char*getStrFromBuf(char*Buf,char*Str,char*divStr);intcharInStr(char*Str,chart);intmain(void)charinBuf100=howoldare/you?,*point,oneWord20;chardivChar=;?!,./;point=inBuf;while(*point)/*Found*/P=getStrFromBuf(point,oneWord,divChar);if(*oneWord)puts(oneWord

6、);return0;intcharInStr(char*Str,chart)for(;*Str;Str+)/*Found*/if(*Str=*t)(去掉*)/*Found*/returnYes;/*Found*/returnNo;char*getStrFromBuf(char*Buf,char*Str,char*divStr)for(;*Buf;Buf+)if(charInStr(divStr,*Buf)=No)break;for(;*Buf;Buf+)if(charInStr(divStr,*Buf)=Yes)break;/*Found*/*Str=*Buf;Str+;/*Found*/*S

7、tr=0;/*Found*/returnBuf;填空題【附帶正確答案】一:#defineM10main()intsub_max();inti,aM,*ptr=a;intmax;for(i=0;i=9;i+)scanf(%d,a+i);max=sub_max(ptr,10);printf(max=%dn,max);intsub_max(int*b,inti)inttemp,j;temp=*b;for(j=1;j=i-1;j+)if(temp*(b+j)temp=*(b+j);returntemp;二、求數(shù)組中值最大的元素值及其位置main()inta34,*ptr,i,j,max,maxi,ma

8、xj;for(i=0;i3;i+)for(j=0;j4;j+)scanf(%d,&aij);ptr=a0;max_arr(ptr,&max,&maxi,12);maxj=maxi%4;maxi=maxi/4;printf(max=%d,maxi=%d,maxj=%d,max,maxi,maxj);intmax_arr(b,p1,p2,n)int*b,*p1,*p2,n;inti;*p1=b0;*p2=0;for(i=1;i*p1)*p1=bi;*p2=i;三、求二維數(shù)組中每行元素的和#defineM3#defineN4main()voidfun();floataMN;floatscore1=0

9、,score2=0,score3=0,*pa=a0;inti,j;for(i=0;iM;i+)for(j=0;jN;j+)/*二維數(shù)組的數(shù)據(jù)輸入*/scanf(%f,&aij);fun(pa,&score1,&score2,&score3);printf(%.2f,%.2f,%.2fn,score1,score2,score3);voidfun(b,p1,p2,p3)floatbN,*p1,*p2,*p3;inti,j;*p1=*p2=*p3=0;for(i=0;iM;i+)for(j=0;jN;j+)if(i=0)*p1=*p1+bij;/*第0行的數(shù)據(jù)相加*/if(i=1)*p2=*p2+

10、bij;/*第1行的數(shù)據(jù)相加*/if(i=2)*p3=*p3+bij;/*第2行的數(shù)據(jù)相加*/四、輸出二維數(shù)組任意行任意列元素的值#includestdio.hmain()inta34=1,3,5,7,9,11,13,15,17,21,23,56;int(*p)4,i,j;p=a;scanf(%d%d,&i,&j);printf(a%d,%d=%dn,i,j,*(*(p+i)+j);/或*(pi+j)或Pij五、有一個(gè)班,3個(gè)學(xué)生,各學(xué)4門(mén)課,計(jì)算總平均分?jǐn)?shù),以及第n個(gè)學(xué)生的成績(jī)。mian()voidaverage(float*p,intn);voidserch(float(*p)4,int)

11、;floatscore34=65,67,70,60,80,87,90,81,90,99,100,98;average(*score,12):serch(score,2);六、動(dòng)態(tài)數(shù)組的實(shí)現(xiàn)。/*程序功能:實(shí)現(xiàn)動(dòng)態(tài)數(shù)組*/#include“alloc.h”#include“stdlib.h”main()int*array=NULL,num,i;printf(“Inputthenumberofelement:”);scanf(“%d”,&num);/*申請(qǐng)動(dòng)態(tài)數(shù)組array=(int*)malloc(sizeof(int)*num);if(array=NULL)printf(“outofmemor

12、y,pressanykeytoquit”);exit(0);printf(“Input%delements:”,num);for(i=0;inum;i+)scanf(“%d”,&arrayi);printf(“%delementsare:”,num);for(i=0;inum;i+)printf(“%d,”,arrayi);printf(“b”);free(array);七、用函數(shù)調(diào)用方式,實(shí)現(xiàn)字符串的復(fù)制。voidstring_copy(char*str_from,char*str_to)inti=0;for(;(*(str_from+i)!=0;i+)*(str_to+i)=*(str_f

13、rom+i)*(str_to+i)=0;main()chararray_str120=”Iamateacher.”;chararray_str220;string_copy(array_str1,array_str2);printf(“array_str2=%sn”,array_str2);八、用指針變量實(shí)現(xiàn)將字符串a(chǎn)復(fù)制為字符串bmain()chara=“Iamaboy.”,b20,*p1,*p2;inti;p1=a;p2=b;for(;*p1!=0;p1+,p2+)*p2=*p1;*p2=0;printf(“stringais:%sn”,a);printf(“stringbis:”);fo

14、r(i=0;bi!=0;i+)printf(“%c”,bi);printf(“n”);九、找出十個(gè)數(shù)中的最大值main()int*max();intn,a10,*s,i;for(i=0;i10;i+)scanf(%d,a+i);s=max(a,10);printf(max=%d,index=%dn,*s,s-a);int*max(a,n)int*a,n;int*p,*t;for(p=a,t=a;p-a*t)t=p;returnt;十、用指針數(shù)組的各指針指向字符串?dāng)?shù)組、指向一維整型數(shù)組、指向二維整型數(shù)組。#includestdlib.hmain()char*ptr14=china,chengdu

15、,sichuang,chongqin;inti,*ptr23;inta3=1,2,3,b32=1,2,3,4,5,6;for(i=0;i4;i+)printf(n%s,ptr1i);printf(n);for(i=0;i3;i+)ptr2i=&ai;/*將整型一維數(shù)組a的3個(gè)元素的地址傳遞給指針數(shù)組ptr2的三個(gè)元素*/for(i=0;i3;i+)/*輸出3個(gè)整型變量的值*/printf(%4d,*ptr2i);printf(n);for(i=0;i3;i+)ptr2i=bi;for(i=0;i3;i+)printf(%4d%4dn,*ptr2i,*(ptr2i+1);十一。用冒泡排序法對(duì)4個(gè)

16、字符串?dāng)?shù)組按字典排序。#includestdlib.h#includestring.hmain()voidsort(char*n);voidprint(char*ptr1,intn);char*ptr14,str420;inti,j,n=4;for(i=0;in;i+)gets(stri);/*輸入4個(gè)字符串*/for(i=0;in;i+)ptr1i=stri;sort(ptr1,n);print(ptr1,n);voidsort(char*ptr1,intn)inti,j,temp20;for(i=0;in-1;i+)/*冒泡排序*/for(j=0;j0)strcpy(tem

17、p,ptr1j);strcpy(ptr1j,ptr1j+1);strcpy(ptr1j+1,temp);voidprint(char*ptr1,intn)inti;for(i=0;in;i+)printf(%sn,ptr1i);十二.利用malloc()函數(shù)為指針?lè)峙浯鎯?chǔ)空間,實(shí)現(xiàn)字符串的排序#includestdlib.h“#includestring.hmain()voidsort(char*ptr1,intn);voidprint(char*ptr1,intn);char*ptr14;inti,j;for(i=0;i4;i+)ptr1i=malloc(20);/*讓指針數(shù)組各指針指向動(dòng)態(tài)

18、分配的20字節(jié)的存儲(chǔ)空間*/if(ptr1i=NULL)/*內(nèi)存申請(qǐng)失?。簆rintf(“outofmemory,pressanykeytoquit”);exit(0);for(i=0;i4;i+)gets(ptr1i);/*輸入字符串*/sort(ptr1,4);print(ptr1,4);for(i=0;i4;i+)free(ptr1i);voidsort(char*ptr2,intn)char*temp;inti,j;for(i=0;in-1;i+)for(j=0;j0)temp=ptr2j;ptr2j=ptr2j+1;ptr2j+1=temp;voidprint(char*ptr1,i

19、ntn)inti;for(i=0;in;i+)printf(%sn,ptr1i);十三、用指向指針的指針變量訪問(wèn)一維和二維數(shù)組。main()inta10,b34,*p1,*p2,*p3,i,j;/*p3是指向指針的指針變量*/for(i=0;i10;i+)scanf(“%d”,&ai);for(i=0;i3;i+)for(j=0;j4;j+)scanf(%d,&bij);for(p1=a;p1-a10;p1+)/*用指向指針的指針變量輸出一維數(shù)組*/p3=&p1;printf(%4d,*p3);printf(n);for(i=0;i3;i+)/*用指向指針的指針變p2=bi;量輸出二維數(shù)組*/

20、for(;p2-biname);scanf(%ld,&student-num);scanf(%d%d%d,&student-birthday.year,&student-birthday.month,&student-birthday.day);printf(outputname,number,year,month,dayn);printf(%20s%10ld%10d/%d/%dn,student-name,student-num,student-birthday.year,student-birthday.month,student-birthday.day);十五structdataint

21、day,month,year;structstucharname20;longnum;structdatabirthday;main()inti;structstu*p,student4=liying,1,1978,5,23,wangping,2,1979,3,14,libo,3,1980,5,6,xuyan,4,1980,4,21;p=student;printf(n1-Outputname,number,year,month,dayn);for(i=0;iname,(p+i)-num,(p+i)-birthday.year,(p+i)-birthday.month,(p+i)-birthd

22、ay.day);十六、編寫(xiě)一個(gè)creat()函數(shù),按照規(guī)定的節(jié)點(diǎn)結(jié)構(gòu)體,創(chuàng)建一個(gè)單鏈表(鏈表中的節(jié)點(diǎn)個(gè)數(shù)不限,以輸入學(xué)號(hào)0作結(jié)束)。#include#include#defineLENsizeof(structstudent)#defineNULL0structstudentlongnum;intscore;structstudent*next;;intn;structstudent*creat()structstudent*head=NULL,*tail,*newp;n=0;newp=(structstudent*)malloc(LEN);printf(inputNumberandScore

23、n);scanf(“%ld,%d”,&newp-num,&newp-score);tail=newp;while(newp-num!=0)n+;if(n=1)head=newp;elsetail-next=newp;tail=newp;newp=(structstudent*)malloc(LEN);printf(inputNumberandScoren);scanf(“%ld,%d”,&newp-num,&newp-score);tail-next=NULL;return(head);main()structstudent*head;printf(Pleasecreatthelistn);h

24、ead=creat();十七、輸出鏈表的某節(jié)點(diǎn)數(shù)據(jù)typedefstructstudentcharname10;intscore;structstudent*next;NODE;voidprint_list(NODE*head)NODE*p=head;while(p!=NULL)printf(n%s%d,p-name,p-score);p=p-next;十八。該子函數(shù)的功能是統(tǒng)計(jì)鏈表節(jié)點(diǎn)的個(gè)數(shù)typedefstructstudentcharname10;intscore;structstudent*next;NODE;intcount_list(NODE*head)NODE*p=head;in

25、tcount=0;while(p!=NULL)count+;p=p-next;return(count);十九,該子函數(shù)的功能是刪除鏈表中的某節(jié)點(diǎn)structnodeintnum;charstr20;structnode*next;structnode*delet(structnode*head,char*pstr)/*以head為頭指針,刪除pstr所在節(jié)點(diǎn)*/structnode*temp,*p;temp=head;if(head=NULL)printf(nListisnull!n);elsetemp=head;while(strcmp(temp-str,pstr)!=0&temp-nex

26、t!=NULL)p=temp;temp=temp-next;/*跟蹤鏈表的節(jié)點(diǎn),即指針后移*/if(strcmp(temp-str,pstr)=0)if(temp=head)printf(deletestring:%sn,temp-str);head=head-next;free(temp);else/*表中節(jié)點(diǎn)或最后節(jié)點(diǎn)*/p-next=temp-next;printf(deletestring:%sn,temp-str);free(temp);elseprintf(nnofindstring!n);/*沒(méi)找到要?jiǎng)h除的字符串*/return(head);/*返回表頭指針*/二十、以學(xué)號(hào)為順序

27、建立鏈表,插入某節(jié)點(diǎn)structnode*insert(structnode*head,char*pstr,intn)/*插入學(xué)號(hào)為n、姓名為pstr的節(jié)點(diǎn)*/structnode*p1,*p2,*p3;p1=(structnode*)malloc(sizeof(structnode);strcpy(p1-str,pstr);/*寫(xiě)入節(jié)點(diǎn)的姓名字串*/p1-num=n;p2=head;if(head=NULL)head=p1;p1-next=NULL;/*新節(jié)點(diǎn)插入表頭*/else/*非空表*/while(np2-num&p2-next!=NULL)/*輸入的學(xué)號(hào)小于節(jié)點(diǎn)的學(xué)號(hào),并且未到表尾*

28、/p3=p2;p2=p2-next;/*跟蹤鏈表增長(zhǎng)*/if(nnum)/*找到插入位置*/if(head=p2)head=p1;p1-next=p2;/*插入位置在表頭*/elsep3-next=p1;p1-next=p2;/*插入位置在表中*/elsep2-next=p1;p1-next=NULL;/*插入位置在表尾*/return(head);/*返回鏈表的頭指針*/二十二、打開(kāi)d盤(pán)根下的myfile.c文件,驗(yàn)證文件能否正確打開(kāi).includevoidmain()FILE*fa;if(fa=fopen(“d:myfile.c”,“r”)=NULL)printf(“nCannotopen

29、thefile!”);exit(0);elseprintf(“nOpen!”);fclose(fa);二十三【編程題】、將鍵盤(pán)上輸入的一個(gè)字符串(以#作為結(jié)束字符),存儲(chǔ)到一個(gè)文件中。#includestdio.h”main()charch,filename10;printf(“Enterthefilename:”);scanf(%s,filename);if(fp=fopen(filename,“w”)=NULL)printf(cannotopenthisfilen);exit(0);ch=getchar();/*輸入字符并存儲(chǔ)到文件中*/while(ch!=#)fputc(ch,fp);c

30、h=getchar();fclose(fp);二十四、【編程題】將一個(gè)磁盤(pán)文件的信息復(fù)制到另一個(gè)文件中并顯示在屏幕上。#includestdio.h“main()FILE*in,*out;charch,infile20,outfile20;printf(Entertheinfilename:);scanf(%s,infile);printf(Entertheoutfilename:);scanf(%s,outfile);if(in=fopen(infile,r)=NULL)printf(cannotopeninfilen);exit(0);if(out=fopen(outfile,w)=NUL

31、L)printf(cannotopenoutfilen);exit(0);while(!feof(in)chfgetc(in);fputc(ch,out);putchar(ch);fclose(in);fclose(out);二十五、編寫(xiě)一個(gè)程序,實(shí)現(xiàn)DOS中的type命令的功能。命令格式如下:c:typed:file1.cpp#include#includevoidmain(intargc,char*argv)FILE*fp;charc;if(argc2)printf(“nNOfilename”);exit(0);if(fp=fopen(argv1,“r”)=NULL)printf(“nFi

32、lenotexist!”);exit(0);while(c=fgetc(fp)!=EOF)putchar(c);fclose(fp);二十六、從鍵盤(pán)讀入字符串存入文件file1.txt,再將文本文件file1.txt中的內(nèi)容全部讀出并顯示在屏幕上。#includemain()FILE*fp;charstring81;if(fp=fopen(file1.txt,w)=NULL)printf(canntopenfile);exit(0);while(strlen(gets(string)0)fputs(string,fp);fputs(n,fp);fclose(fp);if(fp=fopen(fi

33、le1.txt,r)=NULL)printf(canntopenfile);exit(0);while(fgets(string,81,fp)!=NULL)fputs(string,stdout);fclose(fp);二十七、【編程題】把文件1的內(nèi)容追加到文件2#include#include#defineBUFSIZE256voidmain(intargc,char*argv)inti;charbuffBUFSIZE;FILE*fp1,*fp2;if(argc=1)printf(Rsage:fcatfilenamelinke_fnamelink_fname.);printf(n);exit

34、(0);if(fp1=fopen(argv1,a)=NULL)printf(file%scannotopened.n,argv1);exit(1);for(i=2;iargc;i+)if(fp2=fopen(argvi,r)=NULL)printf(file%scannotopened.n);exit(1);while(fgets(buff,BUFSIZE,fp2)!=NULL)fputs(buff,fp1);fclose(fp2);fclose(fp1);二十八。從鍵盤(pán)輸入4個(gè)學(xué)生的有關(guān)數(shù)據(jù),然后把它們轉(zhuǎn)存到一個(gè)寫(xiě)入磁盤(pán)文件student.dat中。#includestdio.h”#defi

35、neSIZE4typedefstructintnum;charname10;stutype;stutypestudentSIZE;voidsave()FILE*fp;inti;if(fp=fopen(“student.dat,wb)=NULL)printf(“Cannotopenthisfilen);return;for(i=0;iSIZE;i+)if(fwrite(&studenti,sizeof(stutype),1,fp)!=1)printf(filewriteerrorn);fclose(fp);main()inti;printf(Enter%dstudentsNo.andname:n

36、“,SIZE);for(i=0;iSIZE;i+)scanf(%d%s,&studenti.num,);save();二十九、【編程題】從文件中讀入數(shù)據(jù),然后在屏幕上輸出。#includestdio.h”#defineSIZE4typedefstructintnum;charname10;stutype;stutypestudentSIZE;main()inti;FILE*fp;fp=fopen(“student.dat,rb);printf(%dstudentsNo.andname:n,SIZE);for(i=0;iSIZE;i+)fread(&studenti,s

37、izeof(stutype),1,fp);printf(%d%10sn,studenti.num,);fclose(fp);三十、按指定的格式,將學(xué)生信息寫(xiě)入到文件stud.dat中#includestructstudentcharname10;intnum;intage;charaddr15;boy2,*pp;voidmain()FILE*fp;charch,filename20;inti;gets(filename);pp=boy;if(fp=fopen(filename,w)=NULL)printf(Cannotopenfile!);getch();exit(1

38、);printf(ninputdatan);for(i=0;iname,&pp-num,&pp-age,pp-addr);pp=boy;for(i=0;iname,pp-num,pp-age,pp-addr);fclose(fp);從文件stud.dat中,按指定的格式,將學(xué)生信息讀出并顯示到屏幕上.#includestructstudentcharname10;intnum;intage;charaddr15;voidmain()FILE*fp;charch,filename20;inti;structstudentboy2,*pp=boy;printf(nfilename:);gets(f

39、ilename);fp=fopen(filename,r);for(i=0;iname,&pp-num,&pp-age,pp-addr);printf(nnnametnumberagetaddrn);pp=boy;for(i=0;iname,pp-num,pp-age,pp-addr);fclose(fp);三十二、【編程題】有一個(gè)文本文件,第一次將它的內(nèi)容顯示在屏幕上,第二次把它復(fù)制到另一個(gè)文件中#includestdio.hmain()FILE*fp1,*fp2;fp1=fopen(file1.c,r);fp2=fopen(file12.c,w);while(!feof(fp1)putch

40、ar(fgetc(fp1);rewind(fp1);while(!feof(fp1)fputc(fgetc(fp1),fp2);fclose(fp1);fclose(fp2);三十三?!揪幊填}】在一個(gè)文件上存放了4個(gè)學(xué)生的數(shù)據(jù),要求將第一個(gè)和第三個(gè)學(xué)生的信息在屏幕上顯示出來(lái)。#includestdio.h#defineSIZE4typedefstructcharname10;intnum;intage;charaddr15;stutype;stutypestudentSIZE;voidmain()FILE*fp;inti;if(fp=fopen(student.dat,rb)=NULL)pri

41、ntf(cannotopenthisfilen);exit(0);for(i=0;iSIZE;i+=2)fseek(fp,i*sizeof(stutype),0);fread(&studenti,sizeof(stutype),1,fp);printf(%d%6sn,studenti.num,);fclose(fp);三十四,求文件長(zhǎng)度#includestdio.hmain()FILE*fp;charfilename80;longlength;gets(filename);fp=fopen(filename,rb);if(fp=NULL)printf(filenotf

42、ound!n);elsefseek(fp,0L,SEEK_END);length=ftell(fp);printf(LengthofFileis%1dbytesn,length);fclose(fp);三十五題*填空!main()char*s=man,woman,girl,boy,sister;char*q;intk;for(k=0;k5;k+)q=s+k;/*這里填寫(xiě)什么語(yǔ)句*/printf(%sn,*q);三十六題、輸出年齡最大的人#defineN4#includestdio.hstaticstructmancharname20;intage;personN=li,18,wang,19,z

43、hang,20,sun,22;main()structman*q,*p;inti,m=0;p=person;for(i=0;iN;i+)if(mage)q=p+;m=q-age;printf(%s,%d,(*q).name,(*q).age);題目取一個(gè)整數(shù)a從右端開(kāi)始的47位。程序分析:【12章的位運(yùn)算非重點(diǎn)但會(huì)涉及考查】(1)先使a右移4位。(2)設(shè)置一個(gè)低4位全為1,其余全為0的數(shù)??捎?04;c=(0n2)swap(pointer1,pointer2);if(n1n3)swap(pointer1,pointer3);if(n2n3)swap(pointer2,pointer3);pri

44、ntf(thesortednumbersare:%d,%d,%dn,n1,n2,n3);swap(p1,p2)int*p1,*p2;intp;p=*p1;*p1=*p2;*p2=p;題目:輸入數(shù)組,最大的與第一個(gè)元素交換,最小的與最后一個(gè)元素交換,輸出數(shù)組。main()intnumber10;input(number);max_min(number);output(number);input(number)intnumber10;inti;for(i=0;i9;i+)scanf(%d,&numberi);scanf(%d,&number9);max_min(array)intarray10;i

45、nt*max,*min,k,l;int*p,*arr_end;arr_end=array+10;max=min=array;for(p=array+1;p*max)max=p;elseif(*p*min)min=p;k=*max;l=*min;*p=array0;array0=l;l=*p;*p=array9;array9=k;k=*p;return;output(array)intarray10;int*p;for(p=array;parray+9;p+)printf(%d,*p);printf(%dn,array9);題目:有n個(gè)整數(shù),使其前面各數(shù)順序向后移m個(gè)位置,最后m個(gè)數(shù)變成最前面的

46、m個(gè)數(shù)1.程序分析:2.程序源代碼:main()intnumber20,n,m,i;printf(thetotalnumbersis:);scanf(%d,&n);printf(backm:);scanf(%d,&m);for(i=0;in-1;i+)scanf(%d,&numberi);scanf(%d,&numbern-1);move(number,n,m);for(i=0;iarray;p-)*p=*(p-1);*array=array_end;m-;if(m0)move(array,n,m);題目:有n個(gè)人圍成一圈,順序排號(hào)。從第一個(gè)人開(kāi)始報(bào)數(shù)(從1到3報(bào)數(shù)),凡報(bào)到3的人退出圈子,問(wèn)

47、最后留下的是原來(lái)第幾號(hào)的那位。#definenmax50main()inti,k,m,n,numnmax,*p;printf(pleaseinputthetotalofnumbers:);scanf(%d,&n);p=num;for(i=0;in;i+)*(p+i)=i+1;i=0;k=0;m=0;while(mn);for(i=0;idata=num;ptr-next=(link)malloc(sizeof(node);if(i=4)ptr-next=NULL;elseptr=ptr-next;ptr=head;while(ptr!=NULL)printf(Thevalueis=%dn,pt

48、r-data);ptr=ptr-next;題目:反向輸出一個(gè)鏈表。#includestdlib.h#includestdio.hstructlistintdata;structlist*next;typedefstructlistnode;typedefnode*link;voidmain()linkptr,head,tail;intnum,i;tail=(link)malloc(sizeof(node);tail-next=NULL;ptr=tail;printf(npleaseinput5data=n);for(i=0;idata=num;head=(link)malloc(sizeof(

49、node);head-next=ptr;ptr=head;ptr=ptr-next;while(ptr!=NULL)printf(Thevalueis=%dn,ptr-data);ptr=ptr-next;【程序74】題目:連接兩個(gè)鏈表。#includestdlib.h#includestdio.hstructlistintdata;structlist*next;typedefstructlistnode;typedefnode*link;linkdelete_node(linkpointer,linktmp)if(tmp=NULL)/*deletefirstnode*/returnpoin

50、ter-next;elseif(tmp-next-next=NULL)/*deletelastnode*/tmp-next=NULL;else/*deletetheothernode*/tmp-next=tmp-next-next;returnpointer;voidselection_sort(linkpointer,intnum)linktmp,btmp;inti,min;for(i=0;idata;btmp=NULL;while(tmp-next)if(mintmp-next-data)min=tmp-next-data;btmp=tmp;tmp=tmp-next;printf(40:%

51、dn,min);pointer=delete_node(pointer,btmp);linkcreate_list(intarray,intnum)linktmp1,tmp2,pointer;inti;pointer=(link)malloc(sizeof(node);pointer-data=array0;tmp1=pointer;for(i=1;inext=NULL;tmp2-data=arrayi;tmp1-next=tmp2;tmp1=tmp1-next;returnpointer;linkconcatenate(linkpointer1,linkpointer2)linktmp;tm

52、p=pointer1;while(tmp-next)tmp=tmp-next;tmp-next=pointer2;returnpointer1;voidmain(void)intarr1=3,12,8,9,11;linkptr;ptr=create_list(arr1,5);selection_sort(ptr,5);題目:編寫(xiě)一個(gè)函數(shù),輸入n為偶數(shù)時(shí),調(diào)用函數(shù)求1/2+?+.+1/n,當(dāng)輸入n為奇數(shù)時(shí),調(diào)用函數(shù)1/1+1/3+.+1/n(利用指針函數(shù))main()#includestdio.hmain()floatpeven(),podd(),dcall();floatsum;intn;wh

53、ile(1)scanf(%d,&n);if(n1)break;if(n%2=0)printf(Even=);sum=dcall(peven,n);elseprintf(Odd=);sum=dcall(podd,n);printf(%f,sum);floatpeven(intn)floats;inti;s=1;for(i=2;i=n;i+=2)s+=1/(float)i;return(s);floatpodd(n)intn;floats;inti;s=0;for(i=1;i0)swap(str1,str2);if(strcmp(str1,str3)0)swap(str1,str3);if(str

54、cmp(str2,str3)0)swap(str2,str3);printf(afterbeingsortedn);printf(%sn%sn%sn,str1,str2,str3);charswap(p1,p2)char*p1,*p2;char*p20;strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);題目:八進(jìn)制轉(zhuǎn)換為十進(jìn)制main()char*p,s6;intn;p=s;gets(p);n=0;while(*(p)!=0)n=n*8+*p-0;p+;printf(%d,n);題目:計(jì)算字符串中子串出現(xiàn)的次數(shù)#includestring.h#includestd

55、io.hmain()charstr120,str220,*p1,*p2;intsum=0;printf(pleaseinputtwostringsn);scanf(%s%s,str1,str2);p1=str1;p2=str2;while(*p1!=0)if(*p1=*p2)while(*p1=*p2&*p2!=0)p1+;p2+;elsep1+;if(*p2=0)sum+;p2=str2;printf(%d,sum);getch();題目:從鍵盤(pán)輸入一些字符,逐個(gè)把它們送到磁盤(pán)上去,直到輸入一個(gè)#為止。#includestdio.hmain()FILE*fp;charch,filename1

56、0;scanf(%s,filename);if(fp=fopen(filename,w)=NULL)printf(cannotopenfilen);exit(0);ch=getchar();ch=getchar();while(ch!=#)fputc(ch,fp);putchar(ch);ch=getchar();fclose(fp);題目:從鍵盤(pán)輸入一個(gè)字符串,將小寫(xiě)字母全部轉(zhuǎn)換成大寫(xiě)字母,然后輸出到一個(gè)磁盤(pán)文件“test”中保存。輸入的字符串以!結(jié)束。#includestdio.hmain()FILE*fp;charstr100,filename10;inti=0;if(fp=fopen(

57、test,w)=NULL)printf(cannotopenthefilen);exit(0);printf(pleaseinputastring:n);gets(str);while(str!=!)if(str=a&str=z)str=str-32;fputc(str,fp);i+;fclose(fp);fp=fopen(test,r);fgets(str,strlen(str)+1,fp);printf(%sn,str);fclose(fp);題目:有兩個(gè)磁盤(pán)文件A和B,各存放一行字母,要求把這兩個(gè)文件中的信息合并(按字母順序排列),輸出到一個(gè)新文件C中.#includestdio.hma

58、in()FILE*fp;inti,j,n,ni;charc160,t,ch;if(fp=fopen(A,r)=NULL)printf(fileAcannotbeopenedn);exit(0);printf(nAcontentsare:n);for(i=0;(ch=fgetc(fp)!=EOF;i+)ci=ch;putchar(ci);fclose(fp);ni=i;if(fp=fopen(B,r)=NULL)printf(fileBcannotbeopenedn);exit(0);printf(nBcontentsare:n);for(i=0;(ch=fgetc(fp)!=EOF;i+)ci

59、=ch;putchar(ci);fclose(fp);n=i;for(i=0;in;i+)for(j=i+1;jcj)t=ci;ci=cj;cj=t;printf(nCfileis:n);fp=fopen(C,w);for(i=0;in;i+)putc(ci,fp);putchar(ci);fclose(fp);題目:有五個(gè)學(xué)生,每個(gè)學(xué)生有3門(mén)課的成績(jī),從鍵盤(pán)輸入以上數(shù)據(jù)(包括學(xué)生號(hào),姓名,三門(mén)課成績(jī)),計(jì)算出平均成績(jī),原有的數(shù)據(jù)和計(jì)算出的平均分?jǐn)?shù)存放在磁盤(pán)文件stud中.#includestdio.hstructstudentcharnum6;charname8;intscore3;floa

60、tavr;stu5;main()inti,j,sum;FILE*fp;for(i=0;i5;i+)printf(npleaseinputNo.%dscore:n,i);printf(stuNo:);scanf(%s,stui.num);printf(name:);scanf(%s,);sum=0;for(j=0;j3;j+)printf(score%d.,j+1);scanf(%d,&stui.scorej);sum+=stui.scorej;stui.avr=sum/3.0;fp=fopen(stud,w);for(i=0;i5;i+)if(fwrite(&stui,siz

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論