[其它考试]二级C语言09年的100原题.doc

上传人:音乐台 文档编号:1968507 上传时间:2019-01-27 格式:DOC 页数:229 大小:636KB
返回 下载 相关 举报
[其它考试]二级C语言09年的100原题.doc_第1页
第1页 / 共229页
[其它考试]二级C语言09年的100原题.doc_第2页
第2页 / 共229页
[其它考试]二级C语言09年的100原题.doc_第3页
第3页 / 共229页
亲,该文档总共229页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《[其它考试]二级C语言09年的100原题.doc》由会员分享,可在线阅读,更多相关《[其它考试]二级C语言09年的100原题.doc(229页珍藏版)》请在三一文库上搜索。

1、一1. 给定程序的功能是调用fun函数建立班级通讯录。通讯录中记录每位学生的编号、姓名和电话号码。班级的人数和学生的信息从键盘读入,每个人的信息作为一个数据块写到名为myfile5.dat的二进制文件中。#include #include #define N 5typedef struct int num; char name10; char tel10;STYPE;void check();/*found*/int fun(STYPE *std)/*found*/ FILE *fp; int i; if(fp=fopen(myfile5.dat,wb)=NULL) return(0); pr

2、intf(nOutput data to file !n); for(i=0; iN; i+)/*found*/ fwrite(&stdi, sizeof(STYPE), 1, fp); fclose(fp); return (1);main() STYPE s10= 1,aaaaa,111111,1,bbbbb,222222,1,ccccc,333333, 1,ddddd,444444,1,eeeee,555555; int k; k=fun(s); if (k=1) printf(Succeed!); check(); else printf(Fail!);void check() FIL

3、E *fp; int i; STYPE s10; if(fp=fopen(myfile5.dat,rb)=NULL) printf(Fail !n); exit(0); printf(nRead file and output to screen :n); printf(n num name teln); for(i=0; iN; i+) fread(&si,sizeof(STYPE),1, fp); printf(%6d %s %sn,si.num,si.name,si.tel); fclose(fp);2.给定程序的功能是调用fun函数建立班级通讯录。通讯录中记录每位学生的编号、姓名和电话

4、号码。班级的人数和学生的信息从键盘读入,每个人的信息作为一个数据块写到名为myfile5.dat的二进制文件中。例如:当s中的字符串为:“ABCDE”时,则t中的字符串为“ABCDEEDCBA”。#include #include void fun (char *s, char *t) int i, sl; sl = strlen(s);/*found*/ for(i = 0 ; i sl ; i+) ti = si; for (i=0; isl; i+)tsl+i = ssl-i-1;/*found*/ t2*sl = 0;main() char s100, t100; printf(nPl

5、ease enter string s:); scanf(%s, s); fun(s, t); printf(The result is: %sn, t);3. 函数fun的功能是:将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的千位和十位上,b数的十位和个位数依次放在c数的百位和个位上。例如,当a=45,b=12时,调用该函数后,c=4152。#include void fun(int a, int b, long *c) *c=(a/10)*1000+(b/10)*100+(a%10)*10+(b%10);main() int a,b;

6、long c; printf(Input a, b:); scanf(%d,%d, &a, &b); fun(a, b, &c); printf(The result is: %dn, c); NONO();NONO ( )/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *rf, *wf ; int i, a,b ; long c ; rf = fopen(C:WEXAM24990001in.dat, r) ; wf = fopen(C:WEXAM24990001out.dat,w) ; for(i = 0 ; i 10 ; i+) fscanf(rf, %

7、d,%d, &a, &b) ; fun(a, b, &c) ; fprintf(wf, a=%d,b=%d,c=%ldn, a, b, c) ; fclose(rf) ; fclose(wf) ;二1.从键盘输入若干行文本(每行不超过80个字符),写到文件myfile4.tet中,用-1(独立一行)作为字符串输入结束的标志。然后将文件的内容读出显示在屏幕上。文件的读写分别由自定义函数ReadText和WriteText实现。#include #include #include void WriteText(FILE *);void ReadText(FILE *);main() FILE *f

8、p; if(fp=fopen(myfile4.txt,w)=NULL) printf( open fail!n); exit(0); WriteText(fp); fclose(fp); if(fp=fopen(myfile4.txt,r)=NULL) printf( open fail!n); exit(0); ReadText(fp); fclose(fp);/*found*/void WriteText(FILE *fw) char str81; printf(nEnter string with -1 to end :n); gets(str); while(strcmp(str,-1

9、)!=0) /*found*/ fputs(str,fw); fputs(n,fw); gets(str); void ReadText(FILE *fr) char str81; printf(nRead file and output to screen :n); fgets(str,81,fr); while( !feof(fr) ) /*found*/ printf(%s,str); fgets(str,81,fr); 2.从低位开始取出长整形变量s中奇数位数上的数,依次构成一个新数放在t中。高位仍在高位,低位仍在低位。#include /*found*/void fun (long

10、s, long *t) long sl=10; *t = s % 10; while ( s 0) s = s/100; *t = s%10 * sl + *t;/*found*/sl = sl*10; main() long s, t; printf(nPlease enter s:); scanf(%ld, &s); fun(s, &t); printf(The result is: %ldn, t);3.将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的个位和百位上,b数的十位和个位数依次放在c数的千位和十位上。#include void

11、 fun(int a, int b, long *c) *c=(b/10)*1000+(a%10)*100+(b%10)*10+(a/10);main() int a,b; long c; printf(Input a, b:); scanf(%d,%d, &a, &b); fun(a, b, &c); printf(The result is: %ldn, c); NONO();NONO ( )/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *rf, *wf ; int i, a,b ; long c ; rf = fopen(C:WEXAM2499000

12、1in.dat, r) ; wf = fopen(C:WEXAM24990001out.dat,w) ; for(i = 0 ; i 10 ; i+) fscanf(rf, %d,%d, &a, &b) ; fun(a, b, &c) ; fprintf(wf, a=%d,b=%d,c=%ldn, a, b, c) ; fclose(rf) ; fclose(wf) ;三1.给定程序中,函数fun的功能是:将自然数110以及他们的平方根写到名为myfile3.txt的文本文件中,然后再顺序读出显示在屏幕上。#include #include int fun(char *fname ) FILE

13、 *fp; int i,n; float x; if(fp=fopen(fname, w)=NULL) return 0; for(i=1;i=10;i+)/*found*/ fprintf(fp,%d %fn,i,sqrt(double)i); printf(nSucceed!!n);/*found*/ fclose(fp); printf(nThe data in file :n);/*found*/ if(fp=fopen(fname,r)=NULL) return 0; fscanf(fp,%d%f,&n,&x); while(!feof(fp) printf(%d %fn,n,x);

14、 fscanf(fp,%d%f,&n,&x); fclose(fp); return 1;main() char fname=myfile3.txt; fun(fname);2.给定程序MODI1.C中的函数fun的功能是:将n个无序整数从小到大排序。#include #include fun ( int n, int *a ) int i, j, p, t; for ( j = 0; jn-1 ; j+ ) p = j;/*found*/ for ( i=j+1; iai )/*found*/ p=i; if ( p!=j ) t = aj; aj = ap; ap = t; putarr(

15、 int n, int *z ) int i; for ( i = 1; i = n; i+, z+ ) printf( %4d, *z ); if ( !( i%10 ) ) printf( n ); printf(n);main() int aa20=9,3,0,4,1,2,5,6,8,10,7, n=11; printf( nnBefore sorting %d numbers:n, n ); putarr( n, aa ); fun( n, aa ); printf( nAfter sorting %d numbers:n, n ); putarr( n, aa );3.函数fun的功

16、能是:将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在个位和百位上,b数的十位和个位数依次放在十位和千位上。#include void fun(int a, int b, long *c) *c=(b%10)*1000+(a%10)*100+(b/10)*10+(a/10);main() int a,b; long c; printf(Input a, b:); scanf(%d,%d, &a, &b); fun(a, b, &c); printf(The result is: %ldn, c); NONO();NONO ( )/* 本函数用于打开

17、文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *rf, *wf ; int i, a,b ; long c ; rf = fopen(C:WEXAM24990001in.dat, r) ; wf = fopen(C:WEXAM24990001out.dat,w) ; for(i = 0 ; i 10 ; i+) fscanf(rf, %d,%d, &a, &b) ; fun(a, b, &c) ; fprintf(wf, a=%d,b=%d,c=%ldn, a, b, c) ; fclose(rf) ; fclose(wf) ;四1调用函数fun就爱那个指定源文件中的内容复制

18、到指定的目标文件中,复制成功时函数返回值为1,失败时返回值为0。在复制的过程中,把复制的内容输出到终端屏幕。主函数中的源文件名放在变量sfname中,目标文件名放在变量tfname中。#include #include int fun(char *source, char *target) FILE *fs,*ft; char ch;/*found*/ if(fs=fopen(source, r)=NULL) return 0; if(ft=fopen(target, w)=NULL) return 0; printf(nThe data in file :n); ch=fgetc(fs);/

19、*found*/ while(!feof(fs) putchar( ch );/*found*/ fputc(ch,ft); ch=fgetc(fs); fclose(fs); fclose(ft); printf(nn); return 1;main() char sfname20 =myfile1,tfname20=myfile2; FILE *myf; int i; char c; myf=fopen(sfname,w); printf(nThe original data :n); for(i=1; i30; i+) c=A+rand()%25;fprintf(myf,%c,c); p

20、rintf(%c,c); fclose(myf);printf(nn); if (fun(sfname, tfname) printf(Succeed!); else printf(Fail!);2.给定程序MODI1.C中函数的功能是:将长整型数种每一位上为偶数的数依次取出,构成一个新数放在t中。高位仍在高位,低位仍在低位。#include void fun (long s, long *t) int d; long sl=1; *t = 0; while ( s 0) d = s%10;/*found*/ if (d%2=0) *t=d* sl+ *t; sl *= 10; /*found

21、*/ s /= 10; main() long s, t; printf(nPlease enter s:); scanf(%ld, &s); fun(s, &t); printf(The result is: %ldn, t);3.将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的十位和千位上,b数的十位和个位依次放在c数的额百位和个位上。#include void fun(int a, int b, long *c) *c=(a%10)*1000+(b/10)*100+(a/10)*10+(b%10);main() int a,b; lon

22、g c; printf(Input a, b:); scanf(%d,%d, &a, &b); fun(a, b, &c); printf(The result is: %ldn, c); NONO();NONO ( )/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *rf, *wf ; int i, a,b ; long c ; rf = fopen(C:WEXAM24990001in.dat, r) ; wf = fopen(C:WEXAM24990001out.dat,w) ; for(i = 0 ; i 10 ; i+) fscanf(rf, %d,

23、%d, &a, &b) ; fun(a, b, &c) ; fprintf(wf, a=%d,b=%d,c=%ldn, a, b, c) ; fclose(rf) ; fclose(wf) ;五1.给定程序中已建立一个带有头节点的单向链表,链表中的各结点按结点数据域中的数据递增有序链表。函数fun的功能是:把形参x的值放入一个新结点并插入到链表中,插入后各结点数据域的值仍保持递增有序。#include #include #define N 8typedef struct list int data; struct list *next; SLIST;void fun( SLIST *h, in

24、t x) SLIST *p, *q, *s; s=(SLIST *)malloc(sizeof(SLIST);/*found*/ s-data=x; q=h; p=h-next; while(p!=NULL & xp-data) /*found*/ q=p; p=p-next; s-next=p;/*found*/ q-next=s;SLIST *creatlist(int *a) SLIST *h,*p,*q; int i; h=p=(SLIST *)malloc(sizeof(SLIST); for(i=0; idata=ai; p-next=q; p=q; p-next=0; retur

25、n h;void outlist(SLIST *h) SLIST *p; p=h-next; if (p=NULL) printf(nThe list is NULL!n); else printf(nHead); do printf(-%d,p-data); p=p-next; while(p!=NULL); printf(-Endn); main() SLIST *head; int x; int aN=11,12,15,18,19,22,25,29; head=creatlist(a); printf(nThe list before inserting:n); outlist(head

26、); printf(nEnter a number : ); scanf(%d,&x); fun(head,x); printf(nThe list after inserting:n); outlist(head);2.计算正整数num的各位上的数字之积。例如,若输入:252,则输出应该是:20。若输入:202,则输入应该是:0。#include long fun (long num)/*found*/ long k=1; do k*=num%10 ;/*found*/ num/=10 ; while(num) ; return (k) ;main( ) long n ; printf(Pl

27、ease enter a number:) ; scanf(%ld,&n) ; printf(n%ldn,fun(n) ;3.请编写一个函数fun,它的功能是:计算n门课程的平均分,计算结果作为函数值返回。例如,若有5门课程的成绩是:90.5,72,80,61.5,55,则函数的值为:71.80。#include float fun ( float *a , int n ) int i; float ave=0.0; for(i=0; in; i+) ave=ave+ai ; ave=ave/n; return ave;main() float score30=90.5, 72, 80, 61

28、.5, 55, aver; aver = fun( score, 5 ); printf( nAverage score is: %5.2fn, aver); NONO ( );NONO ( )/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *fp, *wf ; int i, j ; float aver, score5 ; fp = fopen(C:WEXAM24990001in.dat,r) ; wf = fopen(C:WEXAM24990001out.dat,w) ; for(i = 0 ; i 10 ; i+) for(j = 0 ; j 5 ;

29、j+) fscanf(fp,%f,&scorej) ; aver = fun(score, 5) ; fprintf(wf, %5.2fn, aver) ; fclose(fp) ; fclose(wf) ;六1.给定程序中已建立一个带有头结点的单向链表,在main函数中将多次调用fun函数,每调用一次fun函数,输出链表尾部结点中的数据,并释放该结点,使链表缩短。#include #include #define N 8typedef struct list int data; struct list *next; SLIST;void fun( SLIST *p) SLIST *t, *s

30、; t=p-next; s=p; while(t-next != NULL) s=t;/*found*/ t=t-next; /*found*/ printf( %d ,t-data); s-next=NULL;/*found*/ free(t);SLIST *creatlist(int *a) SLIST *h,*p,*q; int i; h=p=(SLIST *)malloc(sizeof(SLIST); for(i=0; idata=ai; p-next=q; p=q; p-next=0; return h;void outlist(SLIST *h) SLIST *p; p=h-nex

31、t; if (p=NULL) printf(nThe list is NULL!n); else printf(nHead); do printf(-%d,p-data); p=p-next; while(p!=NULL); printf(-Endn); main() SLIST *head; int aN=11,12,15,18,19,22,25,29; head=creatlist(a); printf(nOutput from head:n); outlist(head); printf(nOutput from tail: n); while (head-next != NULL) f

32、un(head); printf(nn); printf(nOutput from head again :n); outlist(head); 2. 给定程序MODI1.C中函数fun的功能是:将字符串中的字符按逆序输出,但不改变字符串的内容。例如,若字符串为abcd则应输出dcba。#include /*found*/fun (char *a) if ( *a ) fun(a+1) ;/*found*/ printf(%c, *a) ; main( ) char s10=abcd; printf(处理前字符串=%sn处理后字符串=, s); fun(s); printf(n) ;3.请编写

33、一个函数fun,它的功能是:比较两个字符串的长度,(不得调用C语言提供的求字符串长度的函数),函数返回较长的字符串。若两个字符串长度相同,则返回第一个字符串。例如,输入beijingshanghai(为回车键),函数将返回shanghai。#include char *fun ( char *s, char *t) int i; char *p=s, *q=t; for(i=0;*p & *q; i+) p+; q+; if(*p = 0 & *q = 0) return s ; if(*p) return s ; else return t ;main( ) char a20,b20; pr

34、intf(Input 1th string:) ; gets( a); printf(Input 2th string:) ; gets( b); printf(%sn,fun (a, b ); NONO ();NONO ( )/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *fp, *wf ; int i ; char a20, b20 ; fp = fopen(C:WEXAM24990001in.dat,r) ; wf = fopen(C:WEXAM24990001out.dat,w) ; for(i = 0 ; i 10 ; i+) fscanf(fp

35、, %s %s, a, b) ; fprintf(wf, %sn, fun(a, b) ; fclose(fp) ; fclose(wf) ;七1.给定程序中已建立一个带有头结点的单向链表,链表中的各结点按数据域递增有序链接。函数fun的功能是:删除链表中数据域值的相同的结点,使之只保留一个。#include #include #define N 8typedef struct list int data; struct list *next; SLIST;void fun( SLIST *h) SLIST *p, *q; p=h-next; if (p!=NULL) q=p-next; while(q!=NULL) if (p-data=q-data) p-next=q-next;/*found*/ free(q);/*found*/ q=p-next; else p=q;/*found*/ q=q-next; SLIST *creatlist(int *a) SLIST *h,*p,*q; int i; h=p=(SLIST

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1