2015年電大C++語言程序設(shè)計第1-2-3-4次作業(yè)及答案參考小抄_第1頁
2015年電大C++語言程序設(shè)計第1-2-3-4次作業(yè)及答案參考小抄_第2頁
2015年電大C++語言程序設(shè)計第1-2-3-4次作業(yè)及答案參考小抄_第3頁
2015年電大C++語言程序設(shè)計第1-2-3-4次作業(yè)及答案參考小抄_第4頁
2015年電大C++語言程序設(shè)計第1-2-3-4次作業(yè)及答案參考小抄_第5頁
已閱讀5頁,還剩17頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1 電大 C+語言程序設(shè)計第 1、 2、 3、 4 次作業(yè)及答案 第一次作業(yè) 一、寫出下列每個程序運行后的輸出結(jié)果 1. #include void main() int x=5; switch(2*x-3) case 4:printf(%d ,x); case 7:printf(%d ,2*x+1); case 10:printf(%d ,3*x-1);break; default:printf(%s ,defaultn); printf(%sn,switch end.); 2. #include void main() int i,s=0; for(i=1;i=6;i+) s+=i*i; printf(s=%dn,s); 3. #include void main() int i,s1=0,s2=0; for(i=0;i10;i+) if(i%2)s1+=i; else s2+=i; printf(%d %dn,s1,s2); 2 4. #include void main() int n=10,y=1; while(n-)y+;y+; printf(y=%dn,y); 5. #include void main() int f,f1,f2,i; f1=f2=1; printf(%d %d ,f1,f2); for(i=3;i=10;i+) f=f1+f2; printf(%d ,f); if(i%5=0)printf(n); f1=f2; f2=f; printf(n); 6. #include #include void main() int i,n; for(n=2;n=20;n+) int temp=(int)sqrt(n);/sqrt(n)求出 n 的平方根并取整 for(i=2;itemp)printf(%d ,n); printf(n); 3 7. #include #include const int M=20; void main() int i,c2,c3,c5; c2=c3=c5=0; for(i=1;i=M;i+) if(i%2=0)c2+; if(i%3=0)c3+; if(i%5=0)c5+; printf(%d %d %dn,c2,c3,c5); 8. #include #include const int M=20; void main() int i,s; for(i=1,s=0;i5 的最小 n 值。 5編寫一個主函數(shù),求滿足不等式 22+42+ +n21000 的最大 n 值,假定分別用 i 和 s 為取偶數(shù)值和累加值的變量,并限定使用 do 循環(huán)編程。 6 6編寫一個主函數(shù),計算并輸出 n!的值,其中 n 值由鍵盤輸入。 參考答案: 1、 答案: #include void fun4(char* a,int b) do if(*a=0 & *a=9)b*a-48+; while(*a+); /*void main() char * a=122333444499888; int b10=0; fun4(a,b); for(int i=0;i10;i+) printf(%d ,bi); */ 2、 答案: #include 7 const int M=2,N=3; double Mean(double aMN, int m,int n ) double v=0; for(int i=0;im;i+) for(int j=0;jn;j+) v+=aij; return v/(m*n); /*void main() double a23=1,2,3,4,5,6; printf(%lfn,Mean(a,2,3); */ 3、 答案: #include int FF(int a , int n) int mul=1; if(n=1)mul*=a0; else mul=an-1*FF(a,n-1); return mul; /*void main() int a6=1,2,3,4,5,6; printf(%dn,FF(a,6); */ 4、 答案: 8 #include void main() double sum=0; int n=1; while(true) if(sum + 1/(double)n 5)break; else sum += 1/(double)n; n+; printf(%d, %lfn,n,sum); 5、 答案: #include void main() int s=0,i=2; do s+=i*i; if(s+(i+2)*(i+2)=1000)break; else i+=2; while(true); printf(i=%d,s=%d,i,s); 6、 答案: #include void main() 9 int s=0,n; printf(請輸入 n 的值: ); scanf(%d,&n); for(int i=1;i=n;i+) s=s*i; printf(n=%d,s=%d,n,s); 第三次作業(yè) 一、寫出下列每個程序運行后的輸出結(jié)果 1 程序代碼: #include void SB(char ch) switch(ch) case A: case a: printf(WW ); break; case B: case b: printf(GG ); break; case C: case c: printf(PP ); break; default: printf(BB ); break; void main() char a1 = b, a2 = C, a3 = f; SB(a1); SB(a2); SB(a3); SB(A); printf(n); 10 2 程序代碼: #include #include double SD(int a, int b, char op) double x; switch(op) case +: x = a + b; break; case -: x = a - b; break; case *: x = a * b; break; case /: if(b) x = (double)a/b; else exit(1); break; default: printf(運算符錯! n); exit(1); return x; void main() int x = 20, y = 8; printf(%3.2lf , SD(x, y, -); printf(%3.2lf , SD(x, y, *); printf(%3.2lfn, SD(x + y, y, /); 3 程序代碼: #include void WF(int x, int y) x = x + y; y = x + y; printf(subs: x, y = %d, %dn, x, y); void main() int x = 18, y = 23; printf(main: x, y = %d, %dn, x, y); 11 WF(x, y); x = 2 * x; printf(main: x, y = %d, %dn, x, y); 4 程序代碼: #include #include void fun(char ss); void main() char s15 = 567891234; fun(s); printf(%sn, s); void fun(char ss) int i, n = strlen(ss); for(i = 0; i n / 2; i +) char c = ssi; ssi = ssn - 1 - i; ssn - 1 - i = c; 5 程序代碼: #include void InsertSort(int a, int n) int i, j, x; for(i = 1; i = 0; j -) / 為 x 順序向前尋找合適的插入位置 12 if(x aj) aj + 1 = aj; else break; aj + 1 = x; void main() int i; int a6 = 20, 15, 32, 47, 36, 28 ; InsertSort(a, 6); for(i = 0; i 6; i +) printf(%d , ai); printf(n); 6 程序代碼: #include void main() int a8 = 3, 5, 7, 9, 11, 13, 15, 17 ; int i, * p = a; for(i = 0; i 8; i +) printf(%5d, * p +); if(i + 1) % 4 = 0) printf(n); 7 程序代碼: #include int LA(int * a, int n) int i, s = 0; for(i = 0; i n; i +) s += ai; return s; 13 void main() int a = 5, 10, 15, 20, 25, 30 ; int b = LA(a, 4); int c = LA(a + 2, 3); printf(%d %dn, b, c); 8 程序代碼: #include int LB(int * a, int n) int i, s = 1; for(i = 0; i n; i +) s *= * a +; return s; void main() int a = 1, 2, 3, 4, 2, 4, 5, 2 ; int b = LB(a, 4) + LB(&a3, 4); printf(b=%dn, b); 二、寫出下列每個函數(shù)的功能 1 程序代碼: int WB(int a, int n, int x) int i; for(i = 0; i n; i +) if(ai = x) return 1; return 0; 2 程序代碼: 14 int WC(int a, int n, int k) int c = 0, i; for(i = 0; i = k) c +; return c; 3 程序代碼: #include #include #include const int N = 10; int ff(int x, int y) int z; printf(%d + %d = , x, y); scanf(%d, &z); if(x + y = z) return 1; else return 0; void main() int i, a, b, c = 0; srand(time(0); / 初始化隨機數(shù)序列 for(i = 0; i N; i +) a = rand() % 20 + 1; / rand()函數(shù)產(chǎn)生 032767 之間的一個隨機數(shù) b = rand() % 20 + 1; c += ff(a, b); printf(得分: %dn, c * 10); 4 * 程序代碼: int fun6(int m, int n, int b) 15 if(m b & n b) return m * n; else if(m % b = 0 & n % b = 0) return b * fun6(m / b, n / b, b); else return fun6(m, n, + b); 5 程序代碼: #include #include void LI(int n) int * a = malloc(n * sizeof(int); int i; for(i = 0; i = 0; i -) printf(%d , * (a + i); printf(n); free(a); 6 程序代碼: int LK(double a, int n) double s = 0; int i, m = 0; for(i = 0; i n; i +) s += ai; s /= n; for(i = 0; i = s) m +; return m; 16 參考答案: 一、 1、答案:運行結(jié)果: GG PP BB WW 2、答案:運行結(jié)果: 12.00 160.00 3.50 3、答案:運行結(jié)果: main: x, y = 18, 23 subs: x, y = 41, 64 main: x, y = 36, 23 4、答案:運行結(jié)果: 432198765 5、運行結(jié)果: 47 36 32 28 20 15 6、答案:運行結(jié)果: 3 5 7 9 11 13 15 17 7、答案:運行結(jié)果: 50 60 8、答案:運行結(jié)果: b=184 二、 1、答案:在整型數(shù)組 a 的前 n 個元素中查找值為 x 的元素,找到返回 1,找不到返回 0。 2、答案:統(tǒng)計整型數(shù)組 a 的前 n 個元素中不小于 k 的元素個數(shù)并返回 3、答案:程序隨機產(chǎn)生 10 道 20 以內(nèi)整數(shù)加法題,請用戶回答。并統(tǒng)計得分, 4、答案:調(diào)用 fun6(m, n, 2)求 m 和 n 的最小公倍數(shù) 5、答案:讀入 n 個整數(shù),然后逆序輸出 17 6、答案:返回雙精度數(shù)數(shù)組 a 的前 n 個元素中不小于平均值的元素個數(shù)。 第四次作業(yè) 一、寫出下列每個程序運行 后的輸出結(jié)果 1 程序代碼: #include struct Worker char name15; / 姓名 int age; / 年齡 float pay; / 工資 ; void main() struct Worker x = wanghua, 52, 2350 ; struct Worker y, * p; y = x; p = &x; printf(%s %d %6.2fn, , y.age, y.pay); printf(%s %d %6.2fn, p-name, p-age + 1, p-pay + 20); 2 程序代碼: #include #include struct Worker char name15; / 姓名 int age; / 年齡 float pay; / 工資 ; void main() struct Worker x; char * t = liouting; 18 int d = 38; float f = 493; strcpy(, t); x.age = d; x.pay = f; x.age +; x.pay *= 2; printf(%s %d %6.2fn, , x.age, x.pay); 3 程序代碼: #include struct Worker char name15; / 姓名 int age; / 年齡 float pay; / 工資 ; int Less(struct Worker r1, struct Worker r2) if(r1.age r2.age) return 1; else return 0; void main() struct Worker a4 = abc, 25, 420 , def, 58, 638 , ghi, 49, 560 , jkl, 36, 375 ; struct Worker x = a0; int i; for(i = 1; i 4; i +) if(Less(x, ai) x = ai; printf(%s %d %6.2fn, , x.age, x.pay); 二、寫出下列每個函數(shù)的功能 1 程序代碼: struct Worker 19 char name15; / 姓名 int age; / 年齡 float pay; / 工資 ; void QA(struct Worker a, int n) int i; for(i = 1; i name); p = f; while(- n) p = p-next = malloc(sizeof(struct StrNode); scanf(%s, p-name); p-next = NULL; return f; 3 程序代碼: struct IntNode 20 int data; / 結(jié)點值域 struct IntNode * next; / 結(jié)點指針域 ; struct IntNode * FindMax(struct IntNode * f) struct IntNode * p = f; if(! f) return NULL; f = f-next; while(f) if(f-data data) p = f; f = f-next; return p; 4 * 程序代碼: struct IntNode int data; / 結(jié)點值域 struct IntNode * next; / 結(jié)點指針域 ; int Count(struct IntNode * f) int c = 0; while(f) c +; f = f-next; return c; 21 5 程序代碼: struct IntNode int data; / 結(jié)點值域 struct IntNode * next; / 結(jié)點指針域 ; struct IntNode * Input(int n) struct IntNode * f, * p; f = malloc(sizeof(struct IntNode); if(n = 0) return NULL; f-next = NULL; printf(從鍵盤輸入 %d 個整數(shù): , n); while(n -) scanf(%d, &(f-data); p = f;

溫馨提示

  • 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)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論