11、人員的記錄由編號和出生年、月、日組成,N名人員的資料已在主函式中存入結構體陣列std中,函式fun的功能是:找出指定出生年份的人員,將其資料放在形參k所指的陣列中,由主函式輸出,同時由函式值回傳滿足指定條件的人數,
請在程式的下劃線處填入正確的內容并把下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> #define N 8 typedef struct { int num; int year,month,day ; }STU; int fun(STU *std, STU *k, int year) { int i,n=0; for (i=0; i<N; i++) /**********found**********/ if( ___1___==year) /**********found**********/ k[n++]= ___2___; /**********found**********/ return (___3___); } int main() { STU std[N]={ {1,1984,2,15},{2,1983,9,21},{3,1984,9,1},{4,1983,7,15}, {5,1985,9,28},{6,1982,11,15},{7,1982,6,22},{8,1984,8,19}}; STU k[N]; int i,n,year; printf("Enter a year : "); scanf("%d",&year); n=fun(std,k,year); if(n==0) printf("\nNo person was born in %d \n",year); else { printf("\nThese persons were born in %d \n",year); for(i=0; i<n; i++) printf("%d %d-%d-%d\n",k[i].num,k[i].year,k[i].month,k[i].day); } return 0; }
12、給定程式中,函式fun的功能是:將形參指標所指結構體陣列中的三個元素按num成員進行升序排列,
請在程式的下劃線處填入正確的內容并把下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> typedef struct { int num; char name[10]; }PERSON; /**********found**********/ void fun(PERSON ___1___) { /**********found**********/ ___2___ temp; if(std[0].num>std[1].num) { temp=std[0]; std[0]=std[1]; std[1]=temp; } if(std[0].num>std[2].num) { temp=std[0]; std[0]=std[2]; std[2]=temp; } if(std[1].num>std[2].num) { temp=std[1]; std[1]=std[2]; std[2]=temp; } } int main() { PERSON std[ ]={ 5,"Zhanghu",2,"WangLi",6,"LinMin" }; int i; /**********found**********/ fun(___3___); printf("\nThe result is :\n"); for(i=0; i<3; i++) printf("%d,%s\n",std[i].num,std[i].name); return 0; }
13、下列給定程式中,函式fun的功能是:在帶頭結點的單向鏈表中,查找資料域中值為ch的結點,找到后通過函式值回傳該結點在鏈表中所處的順序號;若不存在值為ch的結點,函式回傳0值,
請在下劃線處填入正確的內容并將下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> #include <stdlib.h> #define N 8 typedef struct list { int data; struct list *next; } SLIST; SLIST *creatlist(char *); void outlist(SLIST *); int fun( SLIST *h, char ch) { SLIST *p; int n=0; p=h->next; /**********found**********/ while(p!=___1___) { n++; /**********found**********/ if (p->data=https://www.cnblogs.com/cs-whut/p/=ch) return ___2___; else p=p->next; } return 0; } int main() { SLIST *head; int k; char ch; char a[N]={'m','p','g','a','w','x','r','d'}; head=creatlist(a); outlist(head); printf("Enter a letter:"); scanf("%c",&ch); /**********found**********/ k=fun(___3___); if (k==0) printf("\nNot found!\n"); else printf("The sequence number is : %d\n",k); return 0; } SLIST *creatlist(char *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=https://www.cnblogs.com/cs-whut/p/a[i]; 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("->%c",p->data); p=p->next; } while(p!=NULL); printf("->End\n"); } }
14、給定程式中,函式fun的功能是將形參給定的字串、整數、浮點數寫到文本檔案中,再用字符方式從此文本檔案中逐個讀入并顯示在終端螢屏上,
請在程式的下劃線處填入正確的內容并把下劃線洗掉,使程式得出正確的結果,
注意:請勿改動main函式和其他函式中的任何內容,僅在函式fun的橫線上填入所撰寫的若干運算式或陳述句,
#include <stdio.h> void fun(char *s, int a, double f) { /**********found**********/ __1__ fp; char ch; fp = fopen("file1.txt", "w"); fprintf(fp, "%s %d %f\n", s, a, f); fclose(fp); fp = fopen("file1.txt", "r"); printf("\nThe result :\n"); ch = fgetc(fp); /**********found**********/ while (!feof(__2__)) { /**********found**********/ putchar(__3__); ch = fgetc(fp); } putchar('\n'); fclose(fp); } int main() { char a[10]="Hello!"; int b=12345; double c= 98.76; fun(a,b,c); return 0; }
15、下列給定程式的功能是:呼叫函式fun將指定源檔案中的內容復制到指定的目標檔案中,復制成功時函式回傳1,失敗時回傳0,在復制的程序中,把復制的內容輸出到螢屏,主函式中源檔案名放在變數sfname中,目標檔案名放在變數tfname中,
請在下劃線處填入正確的內容并將下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#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("\n"); return 1; } int main() { char sfname[20] ="myfile1",tfname[20]="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("\n"); if (fun(sfname, tfname)) printf("Succeed!"); else printf("Fail!"); return 0; }
16、給定程式中,函式fun的功能是:呼叫隨機函式產生20個互不相同的整數放在形參a所指陣列中(此陣列在主函式中已置0),
請在程式的下劃線處填入正確的內容并把下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdlib.h> #include <stdio.h> #define N 20 void fun(int *a) { int i, x, n=0; x=rand()%20; /**********found**********/ while (n<__1__) { for(i=0; i<n; i++ ) /**********found**********/ if( x==a[i] ) __2__; /**********found**********/ if( i==__3__) {a[n]=x; n++; } x=rand()%20; } } int main() { int x[N]={0} ,i; fun( x ); printf("The result : \n"); for( i=0; i<N; i++ ) { printf("%4d",x[i]); if((i+1)%5==0)printf("\n"); } printf("\n"); return 0; }
17、給定程式的主函式中,已給出由結構體構成的鏈表結點a、b、c,各結點的資料域中均存入字符,函式fun()的作用是:將a、b、c三個結點鏈接成一個單向鏈表,并輸出鏈表結點中的資料,
請在程式的下劃線處填入正確的內容并把下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> typedef struct list { char data; struct list *next; } Q; void fun( Q *pa, Q *pb, Q *pc) { Q *p; /**********found**********/ pa->next=___1___; pb->next=pc; p=pa; while( p ) { /**********found**********/ printf(" %c",____2_____); /**********found**********/ p=____3____; } printf("\n"); } int main() { Q a, b, c; a.data='E'; b.data=https://www.cnblogs.com/cs-whut/p/'F'; c.data=https://www.cnblogs.com/cs-whut/p/'G'; c.next=NULL; fun( &a, &b, &c ); return 0; }
18、程式通過定義學生結構體變數,存盤了學生的學號、姓名和三門課的成績,所有學生資料均以二進制方式輸出到檔案中,
函式fun的功能是從形參filename所指的檔案中讀入學生資料,并按照學號從小到大排序后,再用二進制方式把排序后的學生
資料輸出到filename所指的檔案中,覆寫原來的檔案內容,
請在程式的下劃線處填入正確的內容并把下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> #define N 5 typedef struct student { long sno; char name[10]; float score[3];} STU; void fun(char *filename) { FILE *fp; int i, j; STU s[N], t; /**********found**********/ fp=fopen(filename, __1__); fread(s,sizeof(STU),N,fp); fclose(fp); for (i=0; i<N-1; i++) for (j=i+1; j<N; j++) if (s[i].sno __2__ s[j].sno) { t = s[i]; s[i] = s[j]; s[j] = t; } fp=fopen(filename,"wb"); /**********found**********/ __3__(s, sizeof(STU), N, fp); fclose(fp); } int main() { STU t[N]={ {10005,"ZhangSan", 95, 80, 88}, {10003,"LiSi", 85, 70, 78},{10002,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87},{10001,"MaChao", 91, 92, 77}}, ss[N]; int i,j; FILE *fp; fp = fopen("student.dat", "wb"); fwrite(t, sizeof(STU), 5, fp); fclose(fp); printf("\nThe original data :\n"); for (j=0; j<N; j++) { printf("\nNo: %ld Name: %-8s Scores: ",t[j].sno, t[j].name); for (i=0; i<3; i++) printf("%6.2f ", t[j].score[i]); printf("\n"); } fun("student.dat"); printf("\nThe data after sorting :\n"); fp = fopen("student.dat", "rb"); fread(ss, sizeof(STU), 5, fp); fclose(fp); for (j=0; j<N; j++) { printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name); for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]); printf("\n"); } return 0; }
19、函式fun的功能是:在有n個元素的結構體陣列std中,查找有不及格科目的學生,找到后輸出學生的學號;函式的回傳值是有不及格科目的學生人數,
例如,主函式中給出了4名學生的資料,則程式運行的結果為:
學號:N1002 學號:N1006
共有2位學生有不及格科目
請在程式的下劃線處填入正確的內容,并把下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> typedef struct { char num[8]; double score[2]; /**********found**********/ } __(1)__ ; int fun(STU std[ ], int n) { int i, k=0; for(i=0; i<n; i++) /**********found**********/ if( std[i].score[0]<60__(2)__std[i].score[1]<60 ) { k++;printf("學號:%s ",std[i].num);} /**********found**********/ return __(3)__ ; } int main() { STU std[4]={ "N1001", 76.5,82.0 ,"N1002", 53.5,73.0, "N1005", 80.5,66.0,"N1006", 81.0,56.0 }; printf( "\n共有%d位學生有不及格科目\n" , fun(std,4) ); return 0; }
20、用篩選法可得到2~n(n<10000)之間的所有素數,方法是:首先從素數2開始,將所有2的倍數的數從數表中刪去(把數表中相應位置的值置成0);
接著從數表中找下一個非0數,并從數表中刪去該數的所有倍數;依此類推,直到所找的下一個數等于n為止,
這樣會得到一個序列:2,3,5,7,11,13,17,19,23,…
函式fun的作用是:用篩選法找出所有小于等于n的素數,并統計素數的個數作為函式值回傳,
請在程式的下劃線處填入正確的內容并把下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> int fun(int n) { int a[10000], i,j, count=0; for (i=2; i<=n; i++) a[i] = i; i = 2; while (i<n) { /**********found**********/ for (j=a[i]*2; j<=n; j+=___1___) a[j] = 0; i++; /**********found**********/ while (___2___==0) i++; } printf("\nThe prime number between 2 to %d\n", n); for (i=2; i<=n; i++) /**********found**********/ if (a[i]!=___3___) { count++; printf( count%15?"%5d":"\n%5d",a[i]); } return count; } int main() { int n=20, r; r = fun(n); printf("\nThe number of prime is : %d\n", r); return 0; }
11、(1)std[i].year (2)std[i] (3) n 12、(1)*std (2)PERSON (3)std 13、(1)NULL (2)n (3) head,ch 14、(1)FILE * (2)fp (3) ch 15、(1)"r" (2)fs (3) ft 16、(1)20 (2)break (3) n 17、(1)pb (2)p->data (3)p->nex 18、(1) "rb" (2)> (3) fwrite 19、(1)STU (2) || (3)k 20、(1)a[i] (2)a[i] (3)0參考答案
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/534055.html
標籤:C
上一篇:新手易犯,有幾人和我同樣中招過
