[电脑基础知识]C语言上机题库及解析.doc
第一套一、填空#include <stdio.h>#include <stdlib.h>#define N 5typedef struct int num; char name10; char tel10;STYPE;void check();/*found*/int fun(_1_ *std)/*found*/ _2_ *fp; int i; if(fp=fopen("myfile5.dat","wb")=NULL) return(0); printf("nOutput data to file !n"); for(i=0; i<N; i+)/*found*/ fwrite(&stdi, sizeof(STYPE), 1, _3_); 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() FILE *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; i<N; i+) fread(&si,sizeof(STYPE),1, fp); printf("%6d %s %sn",si.num,si.name,si.tel); fclose(fp);答案:1.STYPE 2.FILE 3.fp二、改错#include <stdio.h>#include <string.h>void fun (char *s, char *t) int i, sl; sl = strlen(s);/*found*/ for( i=0; i<=s1; i +) ti = si; for (i=0; i<sl; i+)tsl+i = ssl-i-1;/*found*/ tsl = '0'main() char s100, t100; printf("nPlease enter string s:"); scanf("%s", s); fun(s, t); printf("The result is: %sn", t);答案:1.for(i=0;i<s1;i+) 2.t2*s1=0三、编程#include <stdio.h>void fun(int a, int b, long *c)main() int a,b; long c; void NONO ( ); printf("Input a, b:"); scanf("%d,%d", &a, &b); fun(a, b, &c); printf("The result is: %dn", c); NONO();void 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,%d", &a, &b) ; fun(a, b, &c) ; fprintf(wf, "a=%d,b=%d,c=%ldn", a, b, c) ; fclose(rf) ; fclose(wf) ;答案:#include <stdio.h>void fun(int a, int b, long *c) *c=(a/10)*1000+(b/10)*100+(a%10)*10+(b%10);main() int a,b; 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, "%d,%d", &a, &b) ; fun(a, b, &c) ; fprintf(wf, "a=%d,b=%d,c=%ldn", a, b, c) ; fclose(rf) ; fclose(wf) ;第二套一、填空#include <stdio.h>#include <string.h>#include <stdlib.h>void WriteText(FILE *);void ReadText(FILE *);main() FILE *fp; 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 _1_) char str81; printf("nEnter string with -1 to end :n"); gets(str); while(strcmp(str,"-1")!=0) /*found*/ fputs(_2_,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",_3_); fgets(str,81,fr); 答案:1.*fw 2.str 3.str二、改错#include <stdio.h>/*found*/void fun (long s, long t) long sl=10; *t = s % 10; while ( s > 0) s = s/100; *t = s%10 * sl + *t;/*found*/sl = sl*100; main() long s, t; printf("nPlease enter s:"); scanf("%ld", &s); fun(s, &t); printf("The result is: %ldn", t);答案:1.void fun(long s,long *t) 2.s1=s1*10;三、编程#include <stdio.h>void fun(int a, int b, long *c)main() int a,b; long c; void NONO ( ); printf("Input a, b:"); scanf("%d,%d", &a, &b); fun(a, b, &c); printf("The result is: %ldn", c); NONO();void 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,%d", &a, &b) ; fun(a, b, &c) ; fprintf(wf, "a=%d,b=%d,c=%ldn", a, b, c) ; fclose(rf) ; fclose(wf) ;答案:#include <stdio.h>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 ( )/* ±¾º¯ÊýÓÃÓÚ´ò¿ªÎļþ£¬ÊäÈëÊý¾Ý£¬µ÷Óú¯Êý£¬Êä³öÊý¾Ý£¬¹Ø±ÕÎļþ¡£ */ 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) ;第三套一、填空#include <math.h>#include <stdio.h>int fun(char *fname ) FILE *fp; int i,n; float x; if(fp=fopen(fname, "w")=NULL) return 0; for(i=1;i<=10;i+)/*found*/ fprintf(_1_,"%d %fn",i,sqrt(double)i); printf("nSucceed!£¡n");/*found*/ _2_; printf("nThe data in file :n");/*found*/ if(fp=fopen(_3_,"r")=NULL) return 0; fscanf(fp,"%d%f",&n,&x); while(!feof(fp) printf("%d %fn",n,x); fscanf(fp,"%d%f",&n,&x); fclose(fp); return 1;main() char fname="myfile3.txt" fun(fname);答案:1.fp 2.fclose(fp) 3.fname二、改错#include <stdio.h>#include <stdlib.h>fun ( int n, int *a ) int i, j, p, t; for ( j = 0; j<n-1 ; j+ ) p = j;/*found*/ for ( i=j+1; i<n-1 ; i+ ) if ( ap>ai )/*found*/ t=i; if ( p!=j ) t = aj; aj = ap; ap = t; putarr( 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 );答案:1.for(i=j+1;i<n;i+) 2.p=i;三、编程#include <stdio.h>void fun(int a, int b, long *c)main() int a,b; long c; void NONO ( ); printf("Input a, b:"); scanf("%d,%d", &a, &b); fun(a, b, &c); printf("The result is: %ldn", c); NONO();void 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,%d", &a, &b) ; fun(a, b, &c) ; fprintf(wf, "a=%d,b=%d,c=%ldn", a, b, c) ; fclose(rf) ; fclose(wf) ;答案:#include <stdio.h>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 ( )/* ±¾º¯ÊýÓÃÓÚ´ò¿ªÎļþ£¬ÊäÈëÊý¾Ý£¬µ÷Óú¯Êý£¬Êä³öÊý¾Ý£¬¹Ø±ÕÎļþ¡£ */ 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) ;第四套一、填空#include <stdio.h>#include <stdlib.h>int fun(char *source, char *target) FILE *fs,*ft; char ch;/*found*/ if(fs=fopen(source, _1_)=NULL) return 0; if(ft=fopen(target, "w")=NULL) return 0; printf("nThe data in file :n"); ch=fgetc(fs);/*found*/ while(!feof(_2_) putchar( ch );/*found*/ fputc(ch,_3_); 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; i<30; i+) c='A'+rand()%25;fprintf(myf,"%c",c); printf("%c",c); fclose(myf);printf("nn"); if (fun(sfname, tfname) printf("Succeed!"); else printf("Fail!");答案:1.”r” 2.fs 3.ft二、改错#include <stdio.h>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*/ s = 10; main() long s, t; printf("nPlease enter s:"); scanf("%ld", &s); fun(s, &t); printf("The result is: %ldn", t);答案:1.if(d%2=0) 2.s/=10;三、编程#include <stdio.h>void fun(int a, int b, long *c)main() int a,b; long c; void NONO ( ); printf("Input a, b:"); scanf("%d,%d", &a, &b); fun(a, b, &c); printf("The result is: %ldn", c); NONO();void 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,%d", &a, &b) ; fun(a, b, &c) ; fprintf(wf, "a=%d,b=%d,c=%ldn", a, b, c) ; fclose(rf) ; fclose(wf) ;答案:#include <stdio.h>void fun(int a, int b, long *c) *c=(a%10)*1000+(b/10)*100+(a/10)*10+(b%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: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) ;第五套一、填空#include <stdio.h>#include <stdlib.h>#define N 8typedef struct list int data; struct list *next; SLIST;void fun( SLIST *h, int x) SLIST *p, *q, *s; s=(SLIST *)malloc(sizeof(SLIST);/*found*/ s->data=_1_; q=h; p=h->next; while(p!=NULL && x>p->data) /*found*/ q=_2_; p=p->next; s->next=p;/*found*/ q->next=_3_;SLIST *creatlist(int *a) SLIST *h,*p,*q; int i; h=p=(SLIST *)malloc(sizeof(SLIST); for(i=0; i<N; i+) q=(SLIST *)malloc(sizeof(SLIST); q->data=ai; p->next=q; p=q; p->next=0; return 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); printf("nEnter a number : "); scanf("%d",&x); fun(head,x); printf("nThe list after inserting:n"); outlist(head);答案:1.x 2.p 3.s二、改错#include <stdio.h>long fun (long num)/*found*/ long k; do k*=num%10 ;/*found*/ num=10 ; while(num) ; return (k) ;main( ) long n ; printf("nPlease enter a number:") ; scanf("%ld",&n) ; printf("n%ldn",fun(n) ;答案:1.long k=1; 2.num/=10;三、编程#include <stdio.h>float fun ( float *a , int n )main() float score30=90.5, 72, 80, 61.5, 55, aver; void NONO ( ); aver = fun( score, 5 ); printf( "nAverage score is: %5.2fn", aver); NONO ( );void 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 ; j+) fscanf(fp,"%f,",&scorej) ; aver = fun(score, 5) ; fprintf(wf, "%5.2fn", aver) ; fclose(fp) ; fclose(wf) ;答案:#include <stdio.h>float fun ( float *a , int n ) int i; float ave=0.0; for(i=0; i<n; i+) ave=ave+ai ; ave=ave/n; return ave;main() float score30=90.5, 72, 80, 61.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 ; j+) fscanf(fp,"%f,",&scorej) ; aver = fun(score, 5) ; fprintf(wf, "%5.2fn", aver) ; fclose(fp) ; fclose(wf) ;第六套一、填空#include <stdio.h>#include <stdlib.h>#define N 8typedef struct list int data; struct list *next; SLIST;void fun( SLIST *p) SLIST *t, *s; t=p->next; s=p; while(t->next != NULL) s=t;/*found*/ t=t->_1_; /*found*/ printf(" %d ",_2_); s->next=NULL;/*found*/ free(_3_);SLIST *creatlist(int *a) SLIST *h,*p,*q; i