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

下載本文檔

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

文檔簡介

實驗三、順序結(jié)構(gòu)和分支結(jié)構(gòu)程序設(shè)計2、 實驗內(nèi)容1、 已知三邊a、b、c、,求三角形面積。程序代碼:#includeusing namespace std;#includecmathint main()int a,b,c;float s,area;coutabc;s=1.0/2*(a+b+c);area=sqrt(s*(s-a)*(s-b)*(s-c);coutarea=areaendl;return 0;運行結(jié)果:2、 從鍵盤輸入兩個實型數(shù),并求他們的和、差、積、商。程序代碼:#include#includeusing namespace std;int main()float x,y,z1,z2,z3,z4;coutxy;z1=x+y;z2+x-y;z3=x*y;z4=x/y;coutsetiosflags(ios:fixed);coutsetprecision(2);coutx+y=z1endl;coutx-y=z2endl;coutx*y=z3endl;coutx/y=z4endl;return 0;運行結(jié)果:3、 編程序,計算下面分段函數(shù)的值。y=2x+10 (x0)y=8x+5(0x10)程序代碼:#includeusing namespace std;int main()int x,y;coutx;if(x0)y=x+10;else if(x=10)y=8*x+5;elsey=4*x-10;coutx=x,y=yendl;return 0;運行結(jié)果:4、 輸入并運行以下程序,分析程序運行結(jié)果程序代碼:#includeusing namespace std;int main()float x,y;x=2.2;y=x/2.0;y=y*2.0;if (y=2.2)coutx=yendl;if (x=y)cout這是一個邏輯錯誤!endl;return 0;運行結(jié)果:5、 求一元二次方程ax*x+bx+c=0 的根。程序代碼:#include#includeusing namespace std;int main()float a,b,c,d,x1,x2,x3,x4,lp,ip;cinabc;if(fabs(a)1e-6)coutis not quadraticendl;elsed=b*b-4*a*c;if (fabs(d)=1e-6)couthas two equal roots:n;x1=x2=-b/(2*a);coutx1=x2=x11e-6)x1=(-b+sqrt(d)/(2*a);x2=(-b-sqrt(d)/(2*a);couthas two real roots:n;coutx1=x1,x2x2endl;else lp=-b/(2*a);ip=sqrt(-d)/(2*a);couthas two complex roots:n;coutx1=lp+ipin;coutx2=lp-ipin;return 0;運行結(jié)果:6、 編寫一個程序,輸出給定的某年某月的天數(shù)。程序代碼:#includeusing namespace std;int main()int year,month,days,leap;coutyearmonth;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&year%100!=0|year%400=0)leap=1;elseleap=0;if(leap)days=29;elsedays=28;coutyear年month月為days天endl;return 0;運行結(jié)果:3、 自測練習(xí)1、 將40000s轉(zhuǎn)換成幾小時幾分幾秒的形式.程序代碼:#includeusing namespace std;int main()int a,b,c,d;a=40000;b=a/3600;c=(a-3600*b)/60;d=a-3600*b-60*c;coutbhcmindsendl;return 0;運行結(jié)果:2、輸入三個邊長,判斷能否構(gòu)成三角形,如不能,輸出提示信息,否則判斷他是不是直角、等腰、全等或普通三角形。程序代碼:#includeusing namespace std;int main()int a,b,c;coutabc;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)成一個等邊三角形endl;else if(a=b|a=c|b=c)cout可以構(gòu)成一個等腰三角形c&a+cb&b+ca)cout可以構(gòu)成一個普通三角形endl;return 0;運行結(jié)果:3、輸入圓錐體的半徑和高,計算其體積。程序代碼:#includeusing namespace std;int main()float r,h,v;cinrh;v=3.14*r*r*h/3;cout圓錐體的體積為:vendl;return 0;運行結(jié)果:4、 輸入一個小于6位的數(shù),判斷他是幾位數(shù),并按照相反順序輸出程序代碼:#includeusing namespace std;int main()int x,a,b,c,d,e,f;coutx;a=x/100000;b=(x-a*100000)/10000;c=(x-a*100000-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)coutx為6位數(shù),且倒序為fedcba;else if (b!=0)coutx為5位數(shù),且倒序為fedcb;else if (c!=0)coutx為4位數(shù),且倒序為fedc;else if (d!=0)coutx為3位數(shù),且倒序為fed;else if (e!=0)coutx為2位數(shù),且倒序為fe;else if (f!=0)coutx為1位數(shù),且倒序為f;return 0;運行結(jié)果:5、設(shè)置整形變量,a,b,c,d分別存儲輸入的四個數(shù),按從大到小排列,是a為最大值,d為最小值,并順序輸出。程序代碼:#includeusing namespace std;int main()int a,b,c,d,max,m;cinabcd;if (ab)max=a;else max=b,b=a;if (cmax)m=max,max=c,c=b,b=m;elseif (cb)m=c,c=b,b=m;if (dmax)m=max,max=d,d=c,c=b,b=m;else if (db)m=b,b=d,d=c,c=m;else if(dc)m=c,c=d,d=m;coutmax b c dendl;return 0;運行結(jié)果:6、 輸入某學(xué)生的考試成績,如果在90分以上,輸出優(yōu)秀;8090分輸

溫馨提示

  • 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

提交評論