版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上二 2.1 #include <iostream.h> void main() /本題原考慮在 16 位機器上實驗目前多為 32 位機器故已過時。 int a = 42486; cout <<oct <<a <<endl <<hex <<a <<endl; unsigned b = 42486; cout << dec <<(signed)b <<endl; 2.2 #include <iostream.h> #include <i
2、omanip.h> const double pi = 3.; void main() double radius1, radius2; cout <<"please input two numbers:n" cin >>radius1 >>radius2; cout <<setw(10) <<pi <<setw(10) <<radius1 <<setw(10) <<(pi*radius1*radius1) <<endl <<setw(1
3、0) <<pi <<setw(10) <<radius2 <<setw(10) <<(pi*radius2*radius2) <<endl; 2.3 #include <iostream.h> #include <iomanip.h> const double e = 2.; void main() cout <<setprecision(10) <<e <<endl <<setiosflags(ios:fixed) <<setprecis
4、ion(8) <<e <<endl <<setiosflags(ios:scientific) <<e <<endl; 2.4 #include <iostream.h> void main() cout <<""How many students here?"n" <<""500"n" 2.5 #include <iostream.h> void main() cout <<"size
5、 of char " <<sizeof(char) <<" byten" <<"size of unsigned char " <<sizeof(unsigned char) <<" byten" <<"size of signed char " <<sizeof(signed char) <<" byten" <<"size of int " <<
6、;sizeof(int) <<" byten" <<"size of unsigned " <<sizeof(unsigned) <<" byten" <<"size of signed " <<sizeof(signed) <<" byten" <<"size of short " <<sizeof(short) <<" byten"
7、<<"size of unsigned short " <<sizeof(unsigned short) <<" byten" <<"size of long " <<sizeof(long) <<" byten" <<"size of signed long " <<sizeof(signed long) <<" byten" <<"size o
8、f unsigned long " <<sizeof(unsigned long) <<" byten" <<"size of float " <<sizeof(float) <<" byten" <<"size of double " <<sizeof(double) <<" byten" <<"size of long double " <<s
9、izeof(long double) <<" byten" 2.6 1) please input 3 sides of one triangle: 6,6,8 a= 6.00,b= 6.00,c= 8.00 area of triangle is 17.88854 2) 該程序計算三角形的面積前后分為三部分輸入處理輸出。 3) /#include <stdio.h> #include <iostream.h> #include <iomanip.h> #include <math.h> void main() f
10、loat a,b,c,s,area; /printf("please input 3 sides of one triangle:n"); cout <<"please input 3 sides of one triangle:n" /scanf("%f,%f,%f",&a,&b,&c); /輸入時以逗號作為數(shù)據(jù)間隔 cin >>a >>b >>c; /輸入時以空格作為數(shù)據(jù)間隔 s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)
11、; /printf("a=%7.2f,b=%7.2f,c=%7.2fn",a,b,c); cout <<setiosflags(ios:fixed) <<setprecision(2) <<"a=" <<setw(7) <<a <<",b=" <<setw(7) <<b <<",c=" <<setw(7) <<c <<endl; /printf("area of
12、triangle is %10.5f",area); cout <<"area of triangle is " <<setw(10) <<setprecision(5) <<area <<endl; 4) #include <iostream.h> #include <iomanip.h> #include <math.h> float area(float a, float b, float c); /函數(shù)聲明 void main() float a,b,c; co
13、ut <<"please input 3 sides of one triangle:n" cin >>a >>b >>c; /輸入時以空格作為數(shù)據(jù)間隔 float result = area(a,b,c); /函數(shù)調(diào)用 cout <<setiosflags(ios:fixed) <<setprecision(2) <<"a=" <<setw(7) <<a <<",b=" <<setw(7) <&l
14、t;b <<",c=" <<setw(7) <<c <<endl; cout <<"area of triangle is " <<setw(10) <<setprecision(5) <<result <<endl; float area(float a, float b, float c) /函數(shù)定義 float s=(a+b+c)/2; return sqrt(s*(s-a)*(s-b)*(s-c); 2.7In main(): Enter
15、two numbers: 3 8 Calling add(): In add(),received 3 and 8 and return 11 Back in main(): c was set to 11 Exiting. 2.8 #include <iostream.h> #include <math.h> double Cylinder(double r, double h); void main() double radius, height; cout <<"請輸入圓柱體的半徑和高:n" cin >>radius &
16、gt;>height; double volume = Cylinder(radius, height); cout <<"該圓柱體的體積為" <<volume <<endl; double Cylinder(double r, double h) return r*r*M_PI*h; 三 3.1 (1) sqrt(pow(sin(x),2.5) (2) (a*x+(a+x)/(4*a)/2 (3) pow(c,x*x)/sqrt(2*M_PI) /M_PI 為 BC 中 math.h 中的圓周率常數(shù) 3.2 13.7 2.5 9
17、3.3 (1) a1=1 a2=1 (2) 1.1 (3) 2,0.0 (4) 20 3.4 #include <iostream.h> void main() int x; cout <<"please input x:n" cin >>x; if(x<=-1) cout <<(x-1) <<endl; if(x>-1 && x<=2) cout <<2*x <<endl; if(2<x && x<=10) cout <&
18、lt;x*(x+2); 3.5 #include <iostream.h> void main() int a; cout <<"please input a number:n" cin >>a; int c1 = a%3 =0; int c2 = a%5 =0; int c3 = a%7 =0; switch(c1<<2)+(c2<<1)+c3) case 0: cout <<"不能被 3,5,7 整除.n" break; case 1: cout <<"只能
19、被 7 整除.n" break; case 2: cout <<"只能被 5 整除.n" break; case 3: cout <<"可以被 5,7 整除.n" break; case 4: cout <<"只能被 3 整除.n" break; case 5: cout <<"可以被 3,7 整除.n" break; case 6: cout <<"可以被 3,5 整除.n" break; case 7: cout <
20、<"可以被 3,5,7 整除.n" break; 3.6 #include <iostream.h> void main() int grade; cout <<"please input a number:n" cin >>grade; if(grade>100|grade<0) cout <<"錯誤的成績.n" else if(grade>=90) cout <<"A.n" else if(grade>=80) cout
21、<<"B.n" else if(grade>=70) cout <<"C.n" else if(grade>=60) cout <<"D.n" else cout <<"E.n" 四 4.1 1 #include <iostream.h> #include <math.h> void main() double sum=1, t=-1, x; int i=1; cout <<"please input a va
22、lue:n" cin >>x; do t*=(-1)*x/i; sum+=t; i+; while(fabs(t)>1e-8); cout <<"sum=" <<sum<<endl; 2 #include <iostream.h> #include <math.h> void main() double sum=1, t=-1, x; cout <<"please input a value:n" cin >>x; int i=1; while
23、(fabs(t)>1e-8) t*=(-1)*x/i; sum+=t; i+; cout <<"sum=" <<sum<<endl; 3 #include <iostream.h> #include <math.h> void main() double sum=1, t=-1, x; cout <<"please input a value:n" cin >>x; for(int i=1; fabs(t)>1e-8; i+) t*=(-1)*x/i; sum
24、+=t; cout <<"sum=" <<sum<<endl; 4.2 #include <iostream.h> void main() long sum=0, t=1; for(int i=1; i<=15; i+) t*=i; sum+=t; cout <<"sum=" <<sum <<endl; 4.3 #include <iostream.h> void main() for(int i=1; i<=9; i+) for(int j=0;
25、 j<=9; j+) for(int k=0; k<=9; k+) if(i*i*i+j*j*j+k*k*k = 100*i+10*j+k) cout <<(100*i+10*j+k) <<"是水仙花數(shù).n" 4.4 #include <iostream.h> void main() for(int i=1; i<1000; i+) int sum=0; for(int j=1; j<=i/2; j+) if(i%j=0) sum+=j; if(sum=i) cout <<i<<"
26、是完數(shù).n" 4.5 #include <iostream.h> void main() float s=100,h=100; for(int i=1; i<10; i+) s+=h; h/=2; cout <<"共經(jīng)過" <<s <<"米第 10 次反彈" <<h <<"米高.n" 4.6 #include <iostream.h> void main() int peachs=1; for(int i=1; i<10; i+)
27、 peachs=(peachs+1)*2; cout <<"第一天共摘下" <<peachs <<"個桃子.n" 4.7 #include <iostream.h> #include <math.h> void main() double x, a; cout <<"please input a value:n" cin >>a; x = a/2; while(fabs(x-a/x)/2)>1e-7) x=(x+a/x)/2; cout <
28、<a <<"的平方根是" <<x <<endl; 4.8 1 #include <iostream.h> void main() for(int i=1; i<=10; i+) for(int j=1; j<=10-i; j+) cout <<" " for(int j=1; j<=2*i-1; j+) cout <<"#" cout <<endl; 2 #include <iostream.h> void main
29、() for(int i=1; i<=8; i+) for(int j=1; j<=i; j+) cout <<" " for(int j=1; j<=18-i; j+) cout <<"#" cout <<endl; 4.9 1 #include <iostream.h> #include <iomanip.h> void main() cout <<" *" for(int i=1; i<=9; i+) cout <<set
30、w(4) <<i; cout <<"n-n" for(int i=1; i<=9; i+) cout <<setw(3) <<i; for(int j=1; j<=9; j+) cout <<setw(4) <<i*j; cout <<endl; 2 #include <iostream.h> #include <iomanip.h> void main() cout <<" *" for(int i=1; i<=9;
31、 i+) cout <<setw(4) <<i; cout <<"n-n" for(int i=1; i<=9; i+) cout <<setw(3) <<i; for(int j=1; j<=i; j+) cout <<setw(4) <<i*j; cout <<endl; 3 #include <iostream.h> #include <iomanip.h> void main() cout <<" *"
32、for(int i=1; i<=9; i+) cout <<setw(4) <<i; cout <<"n-n" for(int i=1; i<=9; i+) cout <<setw(3) <<i; if(i!=1) cout <<setw(4*i-4) <<" " for(int j=i; j<=9; j+) cout <<setw(4) <<i*j; cout <<endl; 4.10 #include <io
33、stream.h> void main() int n; long a=1, b=1, c=1, temp; cout <<"please input a value:n" cin >>n; for(int i=4; i<=n; i+) temp=a+c; a=b; b=c; c=temp; cout << c <<endl; 五 5.1 #include <iostream.h> #include <iomanip.h> #include <math.h> bool ispri
34、me(long n); void main() /input long a,b,l=0; cout <<"please input two numbers:n" cin >>a >>b; cout <<"primes from " <<a <<" to " <<b <<" is n" /process if(a%2=0) a+; for(long m=a; m<=b; m+=2) if(isprime(m) /ou
35、tput if(l+%10=0) cout <<endl; cout <<setw(5) <<m; bool isprime(long n) int sqrtm=sqrt(n); for(int i=2; i<=sqrtm; i+) /判明素數(shù) if(n%i=0) return false; return true; 5.2 #include <iostream.h> #include <iomanip.h> #include <math.h> double f(double x); double integral(
36、double a, double b); const double eps = 1e-8; void main() double a=0, b=1; cout <<"the integral of f(x) from " <<a <<" to " <<b <<" is n" <<setiosflags(ios:fixed) <<setprecision(8) <<setw(8) <<integral(a,b) <<e
37、ndl; double f(double x) return exp(x)/(1+x*x); double integral(double a, double b) int n=1; double h,tn,t2n,i2n,in=0; h = b-a; t2n = i2n = h*(f(a)+f(b)/2; while(fabs(i2n-in)>=eps) tn = t2n; in = i2n; double sigma = 0.0; for(int k=0; k<n; k+) double x = a+(k+0.5)*h; sigma += f(x); t2n = (tn+h*s
38、igma)/2.0; /變步長梯形 i2n = (4*t2n-tn)/3.0; /辛普生公式 n *= 2; h /= 2; return i2n; 5.3 #include <iostream.h> #include <iomanip.h> void multab1(); void multab2(); void multab3(); void main() multab1(); multab2(); multab3(); void multab1() cout <<" *" for(int i=1; i<=9; i+) cout
39、 <<setw(4) <<i; cout <<"n-n" for(int i=1; i<=9; i+) cout <<setw(3) <<i; for(int j=1; j<=9; j+) cout <<setw(4) <<i*j; cout <<endl; cout <<endl <<endl; void multab2() cout <<" *" for(int i=1; i<=9; i+) cout
40、<<setw(4) <<i; cout <<"n-n" for(int i=1; i<=9; i+) cout <<setw(3) <<i; for(int j=1; j<=i; j+) cout <<setw(4) <<i*j; cout <<endl; cout <<endl <<endl; void multab3() cout <<" *" for(int i=1; i<=9; i+) cout &
41、lt;<setw(4) <<i; cout <<"n-n" for(int i=1; i<=9; i+) cout <<setw(3) <<i; if(i!=1) cout <<setw(4*i-4) <<" " for(int j=i; j<=9; j+) cout <<setw(4) <<i*j; cout <<endl; cout <<endl <<endl; 5.4 Main-x=5,y=1,n=1
42、 Func-x=6,y=21,n=11 Main-x=5,y=1,n=11 Func-x=8,y=31,n=21 5.5#include <iostream.h>void main() int n; long a=1, b=1, temp; cout <<"please input a number:n" cin >>n; for(int i=3; i<=n; i+) temp=a+b; a=b; b=temp; cout <<b <<endl; 5.6 double poly(int n, double)
43、if(n=0) return 1; if(n=0) return x; return (2*n-1)*x*poly(n-1,x)-(n-1)*poly(n-2,x)/n; 5.7 #include <iostream.h> #include <math.h> void main() double x, y; x = 3.14159/4; do y = x; /x-=(cos(x)-x)/(sin(x)-1); x = cos(x); while(fabs(x-y)>1e-6); cout <<x <<endl; /答案為: 0. 5.8 #
44、include <iostream.h> void display(double d) cout <<"A double: " <<d <<endl; void display(int i) cout <<"A int: " <<i <<endl; void display(char c) cout <<"A char: " <<c <<endl; void main() double a=100.0; float
45、f=1.0; int n=120; char ch='c' short s=50; display(a); display(f); display(n); display(ch); display(s); 5.9 #include <iostream.h> long cattle(int n); void main() int n; cout <<"please input a number:n" cin >>n; cout <<cattle(n) <<endl; long cattle(int n
46、) if(n<=0) return 0; if(n<=3) return 1; return cattle(n-1)+cattle(n-3); 六 6.1(1) /file1.cpp int x=1; int func() /. /file2.cpp extern int x; int func(); void g() x=func(); /file3.cpp extern int x=2; /error: extern int 變量若有賦值則成定義 int g(); /error: 函數(shù)聲明與前面不一致 void main() x=g(); /. (2) /file1.cpp i
47、nt x=5; int y=8; extern int z; /file2.cpp int x; /error: int x;重復定義 extern double y; /error: y 同一名字不同類型定義 extern int z; /error: z 只有聲明卻無定義 6.2 25 6.3 #include "multab.h" void main() multab1(); multab2(); multab3(); /6_3_1 #include "multab.h" void multab1() cout <<" *&q
48、uot; for(int i=1; i<=9; i+) cout <<setw(4) <<i; cout <<"n-n" for(int i=1; i<=9; i+) cout <<setw(3) <<i; for(int j=1; j<=9; j+) cout <<setw(4) <<i*j; cout <<endl; cout <<endl <<endl; /6_3_2 #include "multab.h" vo
49、id multab2() cout <<" *" for(int i=1; i<=9; i+) cout <<setw(4) <<i; cout <<"n-n" for(int i=1; i<=9; i+) cout <<setw(3) <<i; for(int j=1; j<=i; j+) cout <<setw(4) <<i*j; cout <<endl; cout <<endl <<endl; /6_
50、3_3 #include "multab.h" void multab3() cout <<" *" for(int i=1; i<=9; i+) cout <<setw(4) <<i; cout <<"n-n" for(int i=1; i<=9; i+) cout <<setw(3) <<i; if(i!=1) cout <<setw(4*i-4) <<" " for(int j=i; j<=9;
51、j+) cout <<setw(4) <<i*j; cout <<endl; cout <<endl <<endl; /6_3.h #include <iostream.h> #include <iomanip.h> void multab1(); void multab2(); void multab3(); 七 7.1 #include <iostream.h> int findMinIndex(int a, int n); void main() int array=34,91,83,56,2
52、9,93,56,12,88,72; int size=sizeof(array)/sizeof(*array); int minIndex = findMinIndex(array, size); cout <<"最小數(shù): " <<arrayminIndex <<endl <<"相應的下標: " <<minIndex <<endl; int findMinIndex(int a, int n) int index = 0; for(int i=1; i<n; i+) if(ai
53、ndex>ai) index = i; return index; 7.2 #include <iostream.h> int insert(int a, int n, int value); void main() int array=12,29,34,56,72,83,88,91; int size=sizeof(array)/sizeof(*array); cout <<"插入前的數(shù)組:n" for(int i=0; i<size; i+) cout <<arrayi <<" " int
54、aValue; cout <<"nplease input a number :n" cin >>aValue; int max=insert(array,size,aValue); cout <<"n 最大數(shù): " <<max <<endl; cout <<"插入后的數(shù)組:n" for(int i=0; i<size; i+) cout <<arrayi <<" " int insert(int a, int n
55、, int value) if(an-1<=value) return value; int retValue = an-1; int i; for(i=n-2; i>=0&&ai>value; i-) ai+1=ai; ai+1=value; return retValue; 7.3 #include <iostream.h> const int num=17; void main() int interval=3; int anum; for(int i=0; i<num; i+) cout <<(ai=i+1) <&l
56、t;"," cout <<endl; int i=(interval-1)%num; for(int k=1; k<num; k+) cout <<ai <<"," ai=0; for(int j=1; !(ai&&(j+=interval); i=(i+1)%num); /數(shù)數(shù) cout <<"nNo." <<ai <<" boy has won.n" /輸出勝利者 7.4 void Swap(int& a, i
57、nt& b) int temp=a; a=b; b=temp; void Bsort(int a, int n) bool dontLoopAgain=false; while(dontLoopAgain=!dontLoopAgain) for(int i=0,pass=-n; i<n; i+) if(ai>ai+1) dontLoopAgain=false; Swap(ai,ai+1); 7.5 #include <iostream.h> const int n=5; int sum(int a5, int size); void main() int arr
58、aynn=3,2,4,1,5, 8,7,2,5,6, 6,9,1,4,3, 5,5,3,6,2, 2,8,1,8,6; cout <<sum(array,n); int sum(int a5, int size) int s=0; for(int i=0; i<size; i+) s+=aii+aisize-i-1; if(size%2=1) s-=asize/2size/2; return s; 7.6 #include <iostream.h> void findMax(int a4, int row,int col); void findBad(int a4
59、, int row,int col); void average(int a4, int row,int col); void main() int array54=88,67,48,91, 61,65,37,77, 92,81,73,60, 51,55,60,60, 77,63,70,80; findMax(array,5,4); findBad(array,5,4); average(array,5,4); void findMax(int a4, int row,int col) int r=0,c=0; for(int i=0; i<row; i+) for(int j=0; j
60、<col; j+) if(aij>arc) r=i,c=j; cout <<"成績最高的學生序號: " <<(r+1) <<" " for(int i=0; i<col; i+) cout <<ari<<" " cout <<endl; void findBad(int a4, int row,int col) for(int i=0; i<row; i+) for(int j=0; j<col; j+) if(aij<60) cout <<"有不及格課程的學生序號: " <<(i+1) <<" " for(int k=0; k<col; k+) cout <<aik <<" " cout <<endl; break; void average(int a4, int row,int col) double
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 遼寧省葫蘆島市2025-2026學年高一上學期1月期末考試語文試卷(含答案)
- 湖南省長沙市望城區(qū)第二中學2025-2026學年高一上學期期末考試地理試卷(含答案)
- 安徽省合肥市琥珀中學2025-2026學年上學期期末八年級物理試卷及答案(含答案)
- 2025-2026學年滬科版八年級數(shù)學上冊期末測試卷(含答案)
- 飛盤介紹教學課件
- 飛機設計培訓課件
- 2026山東事業(yè)單位統(tǒng)考菏澤市定陶區(qū)招聘初級綜合類崗位人員考試備考題庫及答案解析
- 2026四川廣元市青川縣衛(wèi)生系統(tǒng)部分醫(yī)療衛(wèi)生機構(gòu)招聘編外專業(yè)技術(shù)人員9人備考考試題庫及答案解析
- 2026河南鄭州地鐵招聘安檢員備考考試試題及答案解析
- 2026臺州市椒江永誠置業(yè)有限公司招聘編外工作人員6人備考考試試題及答案解析
- 2025-2030中國低壓變頻器行業(yè)營銷渠道及投融資方式分析研究報告
- 2025山東恒豐銀行濟南分行社會招聘1人筆試歷年典型考題及考點剖析附帶答案詳解
- 渠道管理制度規(guī)范
- 2025年企業(yè)安全生產(chǎn)培訓講義
- GB/T 714-2025橋梁用結(jié)構(gòu)鋼
- 心臟瓣膜置換術(shù)護理查房
- 【診療方案】慢性阻塞性肺疾病診治指南(2025年修訂版)
- 初三上學期物理期末復習知識詳解(含答案)
- 營養(yǎng)員指導員培訓
- 期末模擬測試(試卷)2025-2026學年六年級語文上冊(統(tǒng)編版)
- 2025-2026學年蘇教版小學數(shù)學三年級上冊期末綜合測試卷及答案(三套)
評論
0/150
提交評論