![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
江蘇省全國計算機等級考試二級C語言2009上機模擬5 |
一、填空題 請補充main函數(shù),該函數(shù)的功能是:從鍵盤輸入一個長整數(shù),如果這個數(shù)是負數(shù),則取它的絕對值,并顯示出來。例如,輸入:-666,結果為:666。 僅在橫線上填寫所需的若干表達式或語句,請勿改動函數(shù)中的其他任何內容。 注意:部分源程序給出如下。 # include # include main() { long int a; printf("Enter the data:\n"); scanf(___1___); printf("The origial data is %ld\n",a); if(a<0) ___2___; printf("\n"); printf(___3___); } 二、改錯題 下列給定程序中函數(shù)fun的功能是:求出在字符串中最后一次出現(xiàn)的子字符串的地址,通過函數(shù)值返回,在主函數(shù)中輸出從此地址開始的字符串;若未找到,則函數(shù)值為NULL。 例如,當字符串中的內容為abcdabfabcdx,t中的內容為ab時,輸出結果應是:abcdx。當字符串中的內容為abcdabfabcdx,t中的內容為abd時,則程序輸出未找到信息"not found!"。 請修改程序中的錯誤,使程序能得出正確的結果。 注意:不要改動main函數(shù),不能增行或刪行,也不能更改程序的結構。 # include # include # include char *fun(char *str,char *t) { char *p,*r,*s; /********error********/ s=NuLL; while(*str) { p=str; r=t; while(*r) /*******error*********/ if(r==p) { r++; p++; } else { break; } if(*r=='\0') s=str; str++; } return s; } main() { char str[100],t[100],*p; printf("\nplease enter string s:"); scanf("%s",str); printf("\nplease enter substring t:"); scanf("%s",t); p=fun(str,t); if(p) printf("\nthe result is:%s\n",p); else printf("\nnot found!\n"); } 三、編程題 編寫函數(shù)int fun(int mm,int b[MAX]),該函數(shù)的功能是求出小于或等于mm的所有素數(shù),并放在b數(shù)組中,該函數(shù)返回所求出的素數(shù)的個數(shù)。 請勿改動主函數(shù)main與其他函數(shù)中的任何內容,僅在函數(shù)fun的花括號中填入所編寫的若干語句。 注意:部分源程序給出如下。 # include # include # define MAX 100 int fun(int mm,int b[MAX]) { } main() { int m,i,sum; int b[MAX] ; FILE *out; printf("input a data:\n"); scanf(" %d",&m); sum=fun(m,b); for(i=0 ;i if(i==0 && i!=0) printf("\n"); printf("]",b[i]); } sum=fun(28,b); out=fopen("outfile.dat","w"); for(i=0 ;i fclose(out); } |