C程序設(shè)計題庫道含答案_第1頁
C程序設(shè)計題庫道含答案_第2頁
C程序設(shè)計題庫道含答案_第3頁
C程序設(shè)計題庫道含答案_第4頁
C程序設(shè)計題庫道含答案_第5頁
已閱讀5頁,還剩214頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、試卷編號:8172所屬語言:C+試卷方案:題庫182道設(shè)計題試卷總分:1820分共有題型:1種一、程序設(shè)計 共182題 (共計1820分)第1題 (10.0分) 題號:664/*-【程序設(shè)計】- 題目:已知有結(jié)構(gòu)體類型定義: struct node int data; struct node *nextPtr; ; 請編寫函數(shù)void computingList(node * head),對head 指向的單向鏈表,分別統(tǒng)計結(jié)點的data成員值為負(fù)數(shù)、0、 正數(shù)的結(jié)點個數(shù)分別存入變量negtive、zero、positive中。 - 注意:部分源程序給出如下。請勿改動主函數(shù)main和其它函數(shù)中

2、 的任何內(nèi)容,僅在函數(shù)的花括號中填入所編寫的若干語句。-*/#include #include using namespace std;void grading(int,int,int);struct node int data; struct node *nextPtr;void computingList(node * head) int positive=0,negtive=0,zero=0; /*Program*/ /* End */ /忽略閱讀 grading(positive,zero,negtive); /忽略閱讀結(jié)束struct node *createList(void) n

3、ode *head=NULL, *p1, *p2; int i; int a10 = -1,3,4,0,9,4,11,-6,2,-10; head=p2=p1= new node; p1-data = a0; for (i=1; idata = ai; p2-nextPtr=p1; p2=p1; p2-nextPtr=NULL; return (head);int main() struct node *head; head = createList(); computingList(head); return 0;/以下忽略閱讀void grading(int a,int b,int c)

4、fstream myfile; myfile.open(out.txt,ios:out); myfilea b cdata 0)positive+; else if (p-data data = 0)zero+; p=p-nextPtr;第3題 (10.0分) 題號:672/*-【程序設(shè)計】-題目:函數(shù)mySum(int a , int m, int n)返回二維 整數(shù)數(shù)組a中所有元素的和,m、n分別為數(shù)組a的行 數(shù)、列數(shù)。填寫適當(dāng)?shù)拇a,使得mySum()完成正確 的功能。-注意:部分源程序給出如下。請勿改動主函數(shù)main和其它 函數(shù)中的任何內(nèi)容,僅在函數(shù)的花括號中填入所編 寫的若干語句。-

5、*/#include#include using namespace std;int mySum(int a23, int m, int n);void gfkd();void main() int a23=1,2,3, 4,5,6; cout mySum(a, 2, 3) endl; / - main()函數(shù)此后部分可以不閱讀 - gfkd();int mySum(int a23, int m, int n)/* Program */* End */void gfkd() fstream myF; int a23; myF.open(in.dat,ios:in); myF.read(char

6、 *)a, sizeof(int)*2*3); myF.close(); myF.open(out.dat,ios:out); myF mySum(a, 2, 3); myF.close();答案:int i, j, s=0;for (i=0; im; i+)for (j=0; jn; j+)s = s + aij;return s;第4題 (10.0分) 題號:588/*-【程序設(shè)計】-題目: 求1-1000之間同構(gòu)數(shù)的個數(shù)n。同構(gòu)數(shù)是: 某數(shù)與其自身的平方數(shù)的后幾位相同。 例如:2525625,25是同構(gòu)數(shù)。-注意:部分源程序給出如下。請勿改動主函數(shù)main和其它 函數(shù)中的任何內(nèi)容,僅在函

7、數(shù)的花括號中填入所編 寫的若干語句。-*/#include#includeusing namespace std;void writeinfile(int n);void main() int m,k,j,n=0; for(m=1;m=1000;m+) /*Program*/ int i=1;k=m;j=m*m;while(k!=0)k/=10;i*=10;if(j%i=m)n+; /* End */ writeinfile(n);void writeinfile(int n) fstream myfile; myfile.open(f.txt,ios:out); myfilenendl; m

8、yfile.close();答案: int i=1;k=m;j=m*m;while(k!=0)k/=10;i*=10;if(j%i=m)n+;第9題 (10.0分) 題號:707/*-【程序設(shè)計】-題目:從文件中讀入n個字符串,將其中以字母A開頭的字符串復(fù)制 到二維字符數(shù)組str1中。-注意:部分源程序給出如下。請勿改動主函數(shù)main和其它 函數(shù)中的任何內(nèi)容,僅在函數(shù)的花括號中填入所編 寫的若干語句。-*/# include # include # include using namespace std;# define n 5# define M 20void wwjt(int len);c

9、har str1nM;void main() int i; char strnM; ifstream infile(in.txt,ios:in); if(!infile.is_open() coutFile Open Error!endl; return; for(i=0;istri; infile.close(); int j=0; /*Program*/ /* End */ wwjt(j);void wwjt(int len) ofstream outfile(out.txt,ios:out); if(!outfile.is_open() coutFile Open Errorendl;

10、return; for(int i=0;ilen;i+) outfilestr1in; outfile.close();答案:for(i=0;i=0)&(strcmp(stri,B)0) strcpy(str1j,stri); j+; 第10題 (10.0分) 題號:673/*-【程序設(shè)計】-題目:函數(shù)myStrCat(char *dst, char *src)將字符串src 連接到字符串dst的后面。填寫適當(dāng)?shù)拇a,使得 mySum()完成正確的功能。-注意:部分源程序給出如下。請勿改動主函數(shù)main和其它 函數(shù)中的任何內(nèi)容,僅在函數(shù)的花括號中填入所編 寫的若干語句。-*/#include#

11、include using namespace std;void myStrCat(char *dst, char *src);void gfkd();void main() char dst100=Hello, ; char src100=Good luck!; myStrCat(dst, src); cout dst endl; / - main()函數(shù)此后部分可以不閱讀 - gfkd();void myStrCat(char *dst, char *src)/* Program */* End */void gfkd() fstream myF; char dst100, src100;

12、 myF.open(in.dat,ios:in); myF.read(dst, 100); myF.read(src, 100); myF.close(); myF.open(out.dat,ios:out); myStrCat(dst, src); myF dst; myF.close();答案:int i=0, len;len = strlen(dst);while (srci)dstlen+ = srci+;dstlen = 0;第11題 (10.0分) 題號:723/*-【程序設(shè)計】-題目:編寫函數(shù)eachPrime,判斷兩個正整數(shù)m和n是否為 互質(zhì)數(shù)。若是,返回1;否則,返回-1。

13、互質(zhì)數(shù):最大公約數(shù)為1的兩個數(shù)稱為互質(zhì)數(shù)。-注意:部分源程序給出如下。請勿改動主函數(shù)main和其它 函數(shù)中的任何內(nèi)容,僅在函數(shù)的花括號中填入所編 寫的若干語句。-*/#include #include #include using namespace std;int eachPrime( int m, int n) /*Program*/ /* End */void grading();int main() int num1, num2, result; cout 0):n; cin num1 num2; if ( num1=0 | num2=0 ) cout Input error!n; r

14、esult = eachPrime(num1, num2); if(result = 1) cout num1 和 num2 是互質(zhì)數(shù)!n; else cout num1 和 num2 不是互質(zhì)數(shù)!n;/忽略閱讀 grading();/忽略閱讀結(jié)束 return 0;/以下忽略閱讀void grading() int n1, n2, result; fstream infile172,myfile; infile172.open(in172.txt,ios:in); myfile.open(out172.txt,ios:out); for(int i=0; i n1 n2; result =

15、eachPrime(n1, n2); myfile result endl; infile172.close(); myfile.close();答案:int temp;while (n != 0) temp = m; m = n; n = temp % n; if(m = 1)return 1;elsereturn -1;第12題 (10.0分) 題號:529/*-【程序設(shè)計】-題目:定義函數(shù)double max(double a,double b,double c)實現(xiàn)求三個實數(shù)中的最大值。-注意:1.部分源程序給出如下。請勿改動主函數(shù)main和其它函數(shù)中的任何內(nèi)容,僅在*program*

16、 和*end*所標(biāo)記的區(qū)域內(nèi)完成題干要求的函數(shù)功能。2./忽略閱讀與/忽略閱讀結(jié)束內(nèi)的程序可以忽略閱讀。-*/#include#includeusing namespace std;void wwjt();/*Program*/* End */void main() coutmax(3.2,7.4,4.5)endl; /忽略閱讀 wwjt(); /忽略閱讀結(jié)束/忽略閱讀void wwjt() double m,n,l; fstream infile, outfile; infile.open (in.txt,ios:in); outfile.open(out.txt,ios:out); for

17、 (int i=0;im; infilen; infilel; outfilemax(m,n,l)b&ac) return a;if(ba&bc) return b;return c;第13題 (10.0分) 題號:697/*-【程序設(shè)計】-題目:郵政匯款的收費標(biāo)準(zhǔn)是匯款金額的1%,但單筆收費上限是50元。 編寫一個函數(shù)double shouFei(double money),根據(jù)輸入的 匯款金額money,返回所要收取的費用。 -注意:僅在標(biāo)有Program和End的注釋行之間補充填寫代碼。 請勿改動主函數(shù)main和其它任何已有內(nèi)容。-*/#include #include void wri

18、teinfile( );/*Program*/* End */void main() coutm; cout本次匯款收費:shouFei(m)endl; writeinfile( );void writeinfile( ) double m; ifstream inFile(in.txt, ios:in | ios:nocreate); if(!inFile) cerr無法打開數(shù)據(jù)文件in.txt。需要核查!endl; return ; fstream myfile; myfile.open(out.txt,ios:out); if(!myfile.is_open() cerr無法打開數(shù)據(jù)文件

19、out.txt。需要核查!endl; return ; for(int i=0; im; if(inFile.fail() break; myfileshouFei(m)endl; inFile.close(); myfile.close();答案:double shouFei(double money)double sf = 0;if(money 50.0)sf = 50.0;return sf;第14題 (10.0分) 題號:695/*-【程序設(shè)計】-題目:請輸出個、十、百位數(shù)字各不相同的所有三位數(shù), 結(jié)果寫入f2.txt中,一個數(shù)一行。要求:輸出時按從小到大的順序輸出。-注意:部分源程序

20、給出如下。請勿改動主函數(shù)main和其它 函數(shù)中的任何內(nèi)容,僅在注釋形式標(biāo)識Program和 End之間填入所編寫的若干語句。-*/#include #include using namespace std;int main() int i, j, k, count = 0; ofstream output; output.open(f2.txt, ios:out); /*Program*/ /* End */ output.close(); cout Press the enter key to exit! in_avail() + 1); return 0;答案:for(i = 0; i 1

21、0; i+)for(j = 0; j 10; j+)for(k = 1; k 10; k+)if(i != j & j != k & i != k)output k j i;count+;if(count = 60)count = 0;output endl;elseoutput ;第15題 (10.0分) 題號:647/*-【程序設(shè)計】-題目:下面程序中的函數(shù)char *delc(char *s, char c);去掉字符串s中的字符c,并將新得到的字符串返回,完成該函數(shù)。-注意:部分源程序給出如下。請勿改動主函數(shù)main和其它 函數(shù)中的任何內(nèi)容,僅在函數(shù)的花括號中填入所編 寫的若干語句。-

22、*/#include#include#includeusing namespace std;char *delc(char *s, char c);int main() int i; char s100,c = a; fstream infile,outfile; infile.open(in.txt,ios:in); outfile.open(out.txt,ios:out); for (i=0;is; delc(s, c); outfilesendl; c+; infile.close(); outfile.close(); return 0;char *delc(char *s, cha

23、r c)/*Program*/* End */答案:char *p;while (p = strchr(s,c)strcpy(p,p+1);return s;第16題 (10.0分) 題號:595/*-【程序設(shè)計】-題目:已知三個數(shù)a,b,c,按由小到大的順序存入a,b, c中并輸出。 三個數(shù)a,b,c的值從已有文件infile596.txt中讀 取。-注意:部分源程序給出如下。請勿改動主函數(shù)main和其它 函數(shù)中的任何內(nèi)容,僅在函數(shù)的花括號中填入所編 寫的若干語句。-*/#include#includeusing namespace std;#include void writeinfile

24、(int a,int b,int c);void main() int a,b,c,t; ifstream ifile; ifile.open (infile596.txt,ios:in); if(! ifile ) coutabc; ifile.close(); /*Program*/ /* End */ writeinfile(a,b,c); return 0;void writeinfile(int a,int b,int c) fstream myfile; myfile.open(f.txt,ios:out); myfilea,b,cb)t=a;a=b;b=t;if(ac)t=a;a

25、=c;c=t;if(bc)t=b;b=c;c=t;第17題 (10.0分) 題號:699/*-【程序設(shè)計】-題目:編寫函數(shù)pfh,功能是返回兩個實數(shù)的平方和。-注意:僅在標(biāo)有Program和End的注釋行之間補充填寫代碼。 請勿改動主函數(shù)main和其它任何已有內(nèi)容。-*/#include #include /*Program*/* End */int grading( );int main() coutxy; cout它們的平方和是:pfh(x, y)endl;/忽略閱讀 grading( );/忽略閱讀結(jié)束 return 0;/以下忽略閱讀int grading( ) double x,y;

26、 ifstream inFile(in.txt, ios:in | ios:nocreate); if(!inFile) cerr無法打開數(shù)據(jù)文件in.txt。需要核查!endl; return 1; fstream myfile; myfile.open(out.txt,ios:out); if(!myfile.is_open() cerr無法打開數(shù)據(jù)文件out.txt。需要核查!endl; return 1; for (int i=0; ixy; myfile(int)pfh(x, y)endl; /在此僅輸出整數(shù)部分 inFile.close(); myfile.close(); ret

27、urn 0;答案:double pfh(double x, double y) return x*x+y*y;第18題 (10.0分) 題號:586/*-【程序設(shè)計】-題目: 編寫函數(shù)getSum()求整數(shù)m的各位數(shù)字之和并返回該值。 例如m252時,各位數(shù)字之和為9。-注意:部分源程序給出如下。請勿改動主函數(shù)main和其它 函數(shù)中的任何內(nèi)容,僅在函數(shù)的花括號中填入所編 寫的若干語句。-*/#include#includeusing namespace std;#include void writeinfile();int getSum(int m)/*Program*/* End */voi

28、d main()int m,s=0;m=252;s=getSum(m);couts;writeinfile();void writeinfile()fstream myfile;myfile.open(f2.txt,ios:out);myfilegetSum(252)endl;myfilegetSum(1457)endl;myfile.close();答案:-int s=0,n;while( m!=0)n=m%10;s+=n;m=m/10;return s;-第19題 (10.0分) 題號:636/*-【程序設(shè)計】-題目:約瑟夫問題這是17世紀(jì)的法國數(shù)學(xué)家加斯帕在數(shù)目的游戲問題中講的一個故事:

29、15個教徒和15 個非教徒在深海上遇險,必須將一半的人投入海中,其余的人才能幸免于難,于是想了一個辦法:30個人圍成一圓圈,從第一個人開始依次報數(shù),每數(shù)到第九個人就將他扔入大海,如此循環(huán)進行直到僅余15個人為止。問怎樣排法,才能使每次投入大海的都是非教徒。-注意:部分源程序給出如下。請勿改動主函數(shù)main和其它 函數(shù)中的任何內(nèi)容,僅在函數(shù)的花括號中填入所編 寫的若干語句。-*/#include #include using namespace std;void output(int i);void circle(struct node link );struct node int nextp;

30、 /*指向下一個人的指針(下一個人的數(shù)組下標(biāo))*/ int no_out; /*是否被扔下海的標(biāo)記。1:沒有被扔下海。0:已被扔下海*/link31; /*30個人,0號元素沒有使用*/int main() int i; for(i=1;i=30;i+) /*初始化結(jié)構(gòu)數(shù)組*/ linki.nextp=i+1; /*指針指向下一個人(數(shù)組元素下標(biāo))*/ linki.no_out=1; /*標(biāo)志置為1,表示人都在船上*/ link30.nextp=1; /*第30個人的指針指向第一個人以構(gòu)成環(huán)*/ circle(link); for(i=1;i=30;i+) /*輸出排列結(jié)果*/ cout(linki.no_out?教徒 :非教徒 ); output(linki.no_out

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論