版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Good is good, but better carries it.精益求精,善益求善。C语言下期考试题库-改错.1.程序D8.c的功能是打开文件d:te.c用于读并判断打开是否成功【此部分在计算中心已公布的题库中,未标明正确答案,同学们自己做】#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的功能是申请100个字节的内存空间,显示其首地址,然后释放申请到的内存空间#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的信息使用结构体存储,从键盘读入其各项信息并显示。#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中字符串拆分成一个个的单词。程序说明:(1)单词之间的分隔符由串divChar,程序中定义为“;?!,./”。(2)函数getStrFromBuf的功能是从Buf的开始处寻找第一个单词,将找到的单词作为一个字符串复制到Str处,divStr指明单词间的分隔符。GetStrFromBuf的返回值指向已找到单词的下一个字符。(3)函数charInStr的功能是判断字符t是否出现在串Str中。(4)主程序的功能是将in
5、Buf中的所有单词在屏幕上显示出来。#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;二、求数组中值最大的元素值及其位置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;三、求二维数组中每行元素的和#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+)/*二维数组的数据输入*/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行的数据相加*/if(i=1)*p2=*p2+
10、bij;/*第1行的数据相加*/if(i=2)*p3=*p3+bij;/*第2行的数据相加*/四、输出二维数组任意行任意列元素的值#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五、有一个班,3个学生,各学4门课,计算总平均分数,以及第n个学生的成绩。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);六、动态数组的实现。/*程序功能:实现动态数组*/#include“alloc.h”#include“stdlib.h”main()int*array=NULL,num,i;printf(“Inputthenumberofelement:”);scanf(“%d”,&num);/*申请动态数组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);七、用函数调用方式,实现字符串的复制。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);八、用指针变量实现将字符串a复制为字符串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”);九、找出十个数中的最大值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;十、用指针数组的各指针指向字符串数组、指向一维整型数组、指向二维整型数组。#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;/*将整型一维数组a的3个元素的地址传递给指针数组ptr2的三个元素*/for(i=0;i3;i+)/*输出3个整型变量的值*/printf(%4d,*ptr2i);printf(n);for(i=0;i3;i+)ptr2i=bi;for(i=0;i3;i+)printf(%4d%4dn,*ptr2i,*(ptr2i+1);十一。用冒泡排序法对4个
16、字符串数组按字典排序。#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个字符串*/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()函数为指针分配存储空间,实现字符串的排序#includestdlib.h“#includestring.hmain()voidsort(char*ptr1,intn);voidprint(char*ptr1,intn);char*ptr14;inti,j;for(i=0;i4;i+)ptr1i=malloc(20);/*让指针数组各指针指向动态
18、分配的20字节的存储空间*/if(ptr1i=NULL)/*内存申请失败:printf(“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);十三、用指向指针的指针变量访问一维和二维数组。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+)/*用指向指针的指针变量输出一维数组*/p3=&p1;printf(%4d,*p3);printf(n);for(i=0;i3;i+)/*用指向指针的指针变p2=bi;量输出二维数组*/
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);十六、编写一个creat()函数,按照规定的节点结构体,创建一个单链表(链表中的节点个数不限,以输入学号0作结束)。#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();十七、输出链表的某节点数据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;十八。该子函数的功能是统计链表节点的个数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);十九,该子函数的功能是删除链表中的某节点structnodeintnum;charstr20;structnode*next;structnode*delet(structnode*head,char*pstr)/*以head为头指针,删除pstr所在节点*/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;/*跟踪链表的节点,即指针后移*/if(strcmp(temp-str,pstr)=0)if(temp=head)printf(deletestring:%sn,temp-str);head=head-next;free(temp);else/*表中节点或最后节点*/p-next=temp-next;printf(deletestring:%sn,temp-str);free(temp);elseprintf(nnofindstring!n);/*没找到要删除的字符串*/return(head);/*返回表头指针*/二十、以学号为顺序
27、建立链表,插入某节点structnode*insert(structnode*head,char*pstr,intn)/*插入学号为n、姓名为pstr的节点*/structnode*p1,*p2,*p3;p1=(structnode*)malloc(sizeof(structnode);strcpy(p1-str,pstr);/*写入节点的姓名字串*/p1-num=n;p2=head;if(head=NULL)head=p1;p1-next=NULL;/*新节点插入表头*/else/*非空表*/while(np2-num&p2-next!=NULL)/*输入的学号小于节点的学号,并且未到表尾*
28、/p3=p2;p2=p2-next;/*跟踪链表增长*/if(nnum)/*找到插入位置*/if(head=p2)head=p1;p1-next=p2;/*插入位置在表头*/elsep3-next=p1;p1-next=p2;/*插入位置在表中*/elsep2-next=p1;p1-next=NULL;/*插入位置在表尾*/return(head);/*返回链表的头指针*/二十二、打开d盘根下的myfile.c文件,验证文件能否正确打开.includevoidmain()FILE*fa;if(fa=fopen(“d:myfile.c”,“r”)=NULL)printf(“nCannotopen
29、thefile!”);exit(0);elseprintf(“nOpen!”);fclose(fa);二十三【编程题】、将键盘上输入的一个字符串(以#作为结束字符),存储到一个文件中。#includestdio.h”main()charch,filename10;printf(“Enterthefilename:”);scanf(%s,filename);if(fp=fopen(filename,“w”)=NULL)printf(cannotopenthisfilen);exit(0);ch=getchar();/*输入字符并存储到文件中*/while(ch!=#)fputc(ch,fp);c
30、h=getchar();fclose(fp);二十四、【编程题】将一个磁盘文件的信息复制到另一个文件中并显示在屏幕上。#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);二十五、编写一个程序,实现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);二十六、从键盘读入字符串存入文件file1.txt,再将文本文件file1.txt中的内容全部读出并显示在屏幕上。#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的内容追加到文件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);二十八。从键盘输入4个学生的有关数据,然后把它们转存到一个写入磁盘文件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();二十九、【编程题】从文件中读入数据,然后在屏幕上输出。#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);三十、按指定的格式,将学生信息写入到文件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中,按指定的格式,将学生信息读出并显示到屏幕上.#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);三十二、【编程题】有一个文本文件,第一次将它的内容显示在屏幕上,第二次把它复制到另一个文件中#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);三十三。【编程题】在一个文件上存放了4个学生的数据,要求将第一个和第三个学生的信息在屏幕上显示出来。#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);三十四,求文件长度#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;/*这里填写什么语句*/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);题目取一个整数a从右端开始的47位。程序分析:【12章的位运算非重点但会涉及考查】(1)先使a右移4位。(2)设置一个低4位全为1,其余全为0的数。可用(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;题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。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个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的
46、m个数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个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问
47、最后留下的是原来第几号的那位。#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;题目:反向输出一个链表。#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】题目:连接两个链表。#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);题目:编写一个函数,输入n为偶数时,调用函数求1/2+?+.+1/n,当输入n为奇数时,调用函数1/1+1/3+.+1/n(利用指针函数)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);题目:八进制转换为十进制main()char*p,s6;intn;p=s;gets(p);n=0;while(*(p)!=0)n=n*8+*p-0;p+;printf(%d,n);题目:计算字符串中子串出现的次数#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();题目:从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个#为止。#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);题目:从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存。输入的字符串以!结束。#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);题目:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件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);题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,原有的数据和计算出的平均分数存放在磁盘文件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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026-2027学年小学音乐四年级上册(全册)苏少版(简谱)教学设计
- 小学三年级生命安全教育《科学课上不乱动》教学设计
- 初中七年级数学上册4.2第3课时解一元一次方程-去括号教学设计
- 有机氟残液焚烧工岗前复测考核试卷含答案
- 碳汇计量评估师安全培训测试考核试卷含答案
- 小学五年级道德与法治《我们神圣的国土 一方水土 一方生活》教学设计
- 小学五年级心理健康教学设计:做情绪的主人-调节愤怒的四个阶梯
- 高中生物高二选择性必修微生物的选择培养与计数教学设计
- 初中地理九年级教学设计:自然环境要素关联与区域认知构建
- 初中九年级科学教学设计:能的转化与能量守恒定律深度探究与核心素养培育
- 广东深圳市龙岗区实验学校2026-2027学年度第一学期 七年级9月阶段性反馈英语试卷(含答案)
- 2026中控证考试题库及答案解析
- 2026年版概论测试题及答案
- (2026年版)糖尿病患者合并心血管疾病诊治专家共识
- 无产权车位使用权转让协议书2026年模板
- 纪念抗美援朝队会课件
- 内科诊所规章制度
- 《千字文》硬笔楷书字帖
- 领航国家大剧院合唱团混声合唱谱
- (完整word版)6.4-简单RISC处理器设计
- LS/T 3219-2017大豆磷脂
评论
0/150
提交评论