高級語言程序設計C平時作業要點_第1頁
高級語言程序設計C平時作業要點_第2頁
高級語言程序設計C平時作業要點_第3頁
高級語言程序設計C平時作業要點_第4頁
高級語言程序設計C平時作業要點_第5頁
已閱讀5頁,還剩9頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、一、分析程序,寫輸出結果1 #include<iostream.h>#include<math.h>void main()int m, k, i ; for( m=1; m<=10; m+=2 ) k = m/3; for( i=2; i<=k; i+ ) if( m%i ) cout << m << " " 解:m 的取值為1,3,5,7,9 對應k的取值為0,1,1,2,3, 第二個for循環:只有當k=2和k=3時才執行, 當k=2,i=2,m%i等于1為真,輸出m為7 當k=3,i=2,m%i等于1為真,輸

2、出m為9, i=3,m%i等于0為假,無輸出 結果為: 7 92 #include<iostream.h>void fun();void main()int i; for(i=1;i<5;i+) fun(); cout<<endl;void fun() static int a; int b=2; a += 2 ; cout<< a+b <<'t'解:主函數循環4次調用fun(); fun()函數內定義了靜態局部變量a,所以a的值會保持到下一次調用。 結果為: 4 6 8 10 3 #include<iostream.

3、h>int fun(int n) if(n=0) return 1; return 2*fun(n-1);void main() int a=5; cout<<fun(a)<<endl;解:fun()函數用遞歸求出2的n次方, 結果為: 324 #include<iostream.h>void main() char *cp="word" for (int i=0 ; i<4; i+ ) cout<<cp+i << 't' 解:主函數4次循環中, 第一次輸出cp+i,此時cp指向“wor

4、d”的第一個字符,所以輸出“word”, 之后cp依次往后移一個位置,輸出此位置及后面的字符 結果為: word ord rd d二、根據程序功能填空。1. 程序把10個數存儲到一維數組a中,并求該數組中最大值。#include<iostream.h>void main() int max; int a10=76,55,95,87,85,83,65,90,77,85; int *p= a ; max=*p; for( ; p< &a10 ; p+) if( *p>max ) max= *p ; cout<<"max= "<&

5、lt;max<<endl;2下面程序的功能是輸出1至100之間每位數字的乘積大于每位數的和的數。例如,45兩位數字的乘積為4×5=20,和為4+5=9。#include<iostream.h>void main() int n, k=1, s=0, m; for(n=1; n<=100; n+) k=1; s=0; M=n ;while( m>0 ) k*=m%10; s+=m%10; m=m/10 ; if(k>s) cout<<n<<'t' 3程序對輸入的n求s = 1 + 1/23 + 1/33

6、+ + 1/n3 。#include<iostream.h>void main()double s; int i, n; cout<<" n= " cin>>n; s = 0; for (i=1; i<n ; i+) s= s+1/(i*i*i) ; cout<<"s="<<s<<endl;4函數create從鍵盤輸入整數序列,以輸入0為結束。按輸入順序建立一個以head為表頭的單向鏈表。struct nodeint data; node * next;create( node

7、 *head )node *p, *q; p=new node; cin>>p->data; q=p; while( p->data ) if(head=NULL) head=p; else q=q->next ; q=p; p=new node ; cin>>p->data; q->next=NULL; delete p;5以下程序求方程的全部整數解:3x + 2y - 7z = 5( 0 x, y, z 100 )#include<iostream.h>void main() int x, y, z ; for( x=0;

8、x<=100; x+ ) for( y=0; y<=100; y+ ) if( ( z=3*x+2*y-5 ) % 7 ) continue ; z=z/7 ;/求出z的值 if( z>=0 && z<101 ) /檢查z的范圍 cout << "x=" << x << " y=" << y << " z=" << z << endl ; 三、程序設計1. 編寫函數輸出以下形狀的圖形,其中構成圖形的數字和輸出的行

9、數通過參數傳送。12 2 23 3 3 3 34 4 4 4 4 4 4答: void PrintFigure(int num, int row) int i, j, k; num = num - row +1; for(i = 1; i <= row; i+) j = 2*i-1; for(k = 1;k <= j; k+) cout<<num<<" " num+; cout<<endl; 2. 請編程序,輸入兩個正整數啊a和b(a<b),輸出a、b之間所有整數的因數(除1和本身)。每行輸出數據不超過10個。例如,若輸

10、入a為6,b為8,則輸出格式要求如下:the factors of 6 :2 3the factors of 7 :no factorthe factors of 8 :2 4答: #include<iostream.h> #include<math.h> void out(int a, int b) int i, j, count; for(i = a; i <= b; i+) count = 0; printf("nthe factor of %dn", i); for(j = 2; j < i; j+) if(i % j = 0)

11、printf("%s%d", (count = 0 ? "":","), j); count+; if(count = 0)printf("no factor"); 3請編程序,找出1至99之間的全部同構數。同構數是這樣一組數:它出現在平方數的右邊。例如:5是25右邊的數,25是625右邊的數,所以5和25都是同構數。答: #include<iostream> using namespace std; void main() int i, j, k; k = 10; for(i = 1; i <=

12、 99; i+) if(i = k) k *= 10; j = i * i; if(j % k = i) cout<< i << " " cout<<endl; 4. 編寫一個程序,實現如下功能: (1)從鍵盤輸入a op b。其中a, b為數值;op為字符,限制為+、-、*、/ 。 (2)調用函數count(op,a,b),計算表達式a op b的值。由主函數輸出結果。答:#include "stdafx.h" #include <iostream> using namespace std; class

13、cal public: int add(int x,int y) /加法定義函數 return x+y; int sub(int x,int y) /減法定義函數 return x-y; int mul(int x,int y) /乘法定義函數 return x*y; int div(int x,int y) /除法定義函數 if(y=0) cout<<"0不能作除數"<<endl; else return x/y; ; int main() cal c; /定義對象 int x,y; char ID; cout<<"請輸入要計

14、算的兩個數及運算符,中間用空格隔開比如2 3 +"<<endl; cin>>x>>y>>ID; switch (ID) case '+': cout<<c.add(x,y)<<endl; break; case '-': cout<<c.sub(x,y)<<endl; break; case '*': cout<<c.mul(x,y)<<endl; break; case '/': cout<&l

15、t;c.div(x,y)<<endl; ; return 0; 5. 編寫一個程序,實現如下功能:(1)輸入k(<100)個整數到數組x100中;(2)計算k個數的平均值及大于平均值的元素個數。答: #include<iostream.h> voidmain() Int x100,k,i,n; Double sum=0.0,ave; cout<<"HowmanyData?n" cin>>k; for(i=0;i<k;i+) cin>>xi;sum+=xi; ave=sum/k; n=0; for(i=0

16、;i<k;i+)/求大于平均值的元素個數 if(xi>ave)n+; cout<<"average="<<ave<<"n" cout<<"Thereare"<<n<<"elementslargethanaverage.n" 6. 定義函數void reversion(int ary,int size);逆置數組ary的元素。例如實參數組原來為 1,5,3,2,6,8,9,4 ,調用函數reversion后變成為 4,9,8,6,2,

17、3,5,1 。答: void reversion(int ary,int size) int temp; for(int i = 0; i < size/2; i+) temp = aryi; aryi = arysize -1 -i; arysize -1 -i = temp; 7. 數組a包含50個整數,把a中所有的后項除以前項之商取整后存入數組b(即bi=ai/ai-1,并且b50=a20/a1),最后按每行5個元素的格式輸出數組b。答: # include <stdio,h> void main() int a50,b5,i,j; printf ("請輸入5

18、0個數"); for (i=0;i<50;i+); scanf("%d",&ai); for (i=9;i>0;i-2) for (j=0;j<5;j+) bj=ai%ai-1; for(j=0;j<5;j+) for (i=1;i<5;i+) printf("%d",bi; printf("n"); 8. 編程輸出所有不超過100 的其平方具有對稱性質的正整數(也稱回文數)。輸出格式如下:number square1 12 43 911 12122 48426 676答:#include<stdio.h> Int main<void> Int j=0; For (i=0;j<100;j+) If (mj!=mi) Break; If (j>=i) Printf(n,n*n); Return o; 9. 編寫程序,打印如下楊輝三角。11 1 1 2 11 3 3 11 4 6 4 1答: #include<iostream> #include<iomanip> using namespace std; vo

溫馨提示

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

評論

0/150

提交評論