北京科技大學C實驗報告_第1頁
北京科技大學C實驗報告_第2頁
北京科技大學C實驗報告_第3頁
北京科技大學C實驗報告_第4頁
北京科技大學C實驗報告_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、實驗三、順序結(jié)構(gòu)和分支結(jié)構(gòu)程序設(shè)計2、 實驗內(nèi)容1、 已知三邊a、b、c、,求三角形面積。程序代碼:#include<iostream>using namespace std;#include"cmath"int main()int a,b,c;float s,area;cout<<"輸入三個邊長:"cin>>a>>b>>c;s=1.0/2*(a+b+c);area=sqrt(s*(s-a)*(s-b)*(s-c);cout<<"area="<<are

2、a<<endl;return 0;運行結(jié)果:2、 從鍵盤輸入兩個實型數(shù),并求他們的和、差、積、商。程序代碼:#include<iostream>#include<iomanip>using namespace std;int main()float x,y,z1,z2,z3,z4;cout<<"輸入兩個實數(shù):"cin>>x>>y;z1=x+y;z2+x-y;z3=x*y;z4=x/y;cout<<setiosflags(ios:fixed);cout<<setprecision(

3、2);cout<<x<<"+"<<y<<"="<<z1<<endl;cout<<x<<"-"<<y<<"="<<z2<<endl;cout<<x<<"*"<<y<<"="<<z3<<endl;cout<<x<<"/"&l

4、t;<y<<"="<<z4<<endl;return 0;運行結(jié)果:3、 編程序,計算下面分段函數(shù)的值。y=2x+10 (x<0)y=8x+5(0<<x<<10)y=4x-10(x>10)程序代碼:#include<iostream>using namespace std;int main()int x,y;cout<<"輸入x的值:"cin>>x;if(x<0)y=x+10;else if(x<=10)y=8*x+5;elsey

5、=4*x-10;cout<<"x="<<x<<",y="<<y<<endl;return 0;運行結(jié)果:4、 輸入并運行以下程序,分析程序運行結(jié)果程序代碼:#include<iostream>using namespace std;int main()float x,y;x=2.2;y=x/2.0;y=y*2.0;if (y=2.2)cout<<"x=y"<<endl;if (x=y)cout<<"這是一個邏輯錯誤!&

6、quot;<<endl;return 0;運行結(jié)果:5、 求一元二次方程ax*x+bx+c=0 的根。程序代碼:#include<iostream>#include<cmath>using namespace std;int main()float a,b,c,d,x1,x2,x3,x4,lp,ip;cin>>a>>b>>c;if(fabs(a)<1e-6)cout<<"is not quadratic"<<endl;elsed=b*b-4*a*c;if (fabs(d)&

7、lt;=1e-6)cout<<"has two equal roots:n"x1=x2=-b/(2*a);cout<<"x1=x2="<<x1<<endl;else if (d>1e-6)x1=(-b+sqrt(d)/(2*a);x2=(-b-sqrt(d)/(2*a);cout<<"has two real roots:n"cout<<"x1="<<x1<<",x2"<<x2<

8、;<endl;else lp=-b/(2*a);ip=sqrt(-d)/(2*a);cout<<"has two complex roots:n"cout<<"x1="<<lp<<"+"<<ip<<"in"cout<<"x2="<<lp<<"-"<<ip<<"in"return 0;運行結(jié)果:6、 編寫一個程序,輸出給定

9、的某年某月的天數(shù)。程序代碼:#include<iostream>using namespace std;int main()int year,month,days,leap;cout<<"年月:"cin>>year>>month;switch(month)case 1:case 3:case 5:case 7:case 8:case 10:case 12: days=31;break;case 4:case 6:case 9:case 11:days=30;break;case 2: if (year%4=0&&

10、;year%100!=0|year%400=0)leap=1;elseleap=0;if(leap)days=29;elsedays=28;cout<<year<<"年"<<month<<"月"<<"為"<<days<<"天"<<endl;return 0;運行結(jié)果:3、 自測練習1、 將40000s轉(zhuǎn)換成幾小時幾分幾秒的形式.程序代碼:#include<iostream>using namespace st

11、d;int main()int a,b,c,d;a=40000;b=a/3600;c=(a-3600*b)/60;d=a-3600*b-60*c;cout<<b<<"h"<<c<<"min"<<d<<"s"<<endl;return 0;運行結(jié)果:2、輸入三個邊長,判斷能否構(gòu)成三角形,如不能,輸出提示信息,否則判斷他是不是直角、等腰、全等或普通三角形。程序代碼:#include<iostream>using namespace std;in

12、t main()int a,b,c;cout<<"輸入三個邊長:"cin>>a>>b>>c;if (a+b<=c|a+c<=b|b+c<=a)cout<<"不能構(gòu)成三角形"<<endl;else if (a*a=b*b+c*c|b*b=a*a+c*c|c*c=a*a+b*b)cout<<"可以構(gòu)成一個直角三角形"<<endl;else if(a=b&&a=c)cout<<"可以構(gòu)成一個

13、等邊三角形"<<endl;else if(a=b|a=c|b=c)cout<<"可以構(gòu)成一個等腰三角形"<<endl;else if(a+b>c&&a+c>b&&b+c>a)cout<<"可以構(gòu)成一個普通三角形"<<endl;return 0;運行結(jié)果:3、輸入圓錐體的半徑和高,計算其體積。程序代碼:#include<iostream>using namespace std;int main()float r,h,v;cin

14、>>r>>h;v=3.14*r*r*h/3;cout<<"圓錐體的體積為:"<<v<<endl;return 0;運行結(jié)果:4、 輸入一個小于6位的數(shù),判斷他是幾位數(shù),并按照相反順序輸出程序代碼:#include<iostream>using namespace std;int main()int x,a,b,c,d,e,f;cout<<"請輸入一個數(shù)x:"cin>>x;a=x/100000;b=(x-a*100000)/10000;c=(x-a*100000

15、-b*10000)/1000;d=(x-a*100000-b*10000-c*1000)/100;e=(x-a*100000-b*10000-c*1000-d*100)/10;f=x-a*100000-b*10000-c*1000-d*100-e*10;if (a!=0)cout<<"x為6位數(shù),且倒序為"<<f<<e<<d<<c<<b<<a;else if (b!=0)cout<<"x為5位數(shù),且倒序為"<<f<<e<<d

16、<<c<<b;else if (c!=0)cout<<"x為4位數(shù),且倒序為"<<f<<e<<d<<c;else if (d!=0)cout<<"x為3位數(shù),且倒序為"<<f<<e<<d;else if (e!=0)cout<<"x為2位數(shù),且倒序為"<<f<<e;else if (f!=0)cout<<"x為1位數(shù),且倒序為"<&

17、lt;f;return 0;運行結(jié)果:5、設(shè)置整形變量,a,b,c,d分別存儲輸入的四個數(shù),按從大到小排列,是a為最大值,d為最小值,并順序輸出。程序代碼:#include<iostream>using namespace std;int main()int a,b,c,d,max,m;cin>>a>>b>>c>>d;if (a>b)max=a;else max=b,b=a;if (c>max)m=max,max=c,c=b,b=m;elseif (c>b)m=c,c=b,b=m;if (d>max)m=max

18、,max=d,d=c,c=b,b=m;else if (d>b)m=b,b=d,d=c,c=m;else if(d>c)m=c,c=d,d=m;cout<<max<<" "<<b<<" "<<c<<" "<<d<<endl;return 0;運行結(jié)果:6、 輸入某學生的考試成績,如果在90分以上,輸出"優(yōu)秀"8090分輸出"良好"7079分輸出"中等"6069輸出"及格"60分以下輸出"不及格".程序代碼:#include<iostream>using namespace std;int main(

溫馨提示

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

評論

0/150

提交評論