C++實驗報告冊_第1頁
C++實驗報告冊_第2頁
C++實驗報告冊_第3頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、.實驗名稱:分支結構程序設計1、編寫一個程序,輸入某雇員的每周工作時間(以小時計)和每小時的工資數(shù),計算并輸出他的工資。若雇員每周工作小時超過40 小時,則超過部分按原工資的1.5 倍計算。若雇員每周工作小時超過60 小時,則超過60 的部分按原工資的3 倍計算。要求:( 1)要求有輸入提示和輸出提示,如輸入雇員的工作時間和每小時的工資值時,可以提示:“Please input employee s work time and wage_per_hour: ” 輸出時,提示:“ The employee s wage : ”。( 2)寫出雇員在各種周工作時間情況下的輸入數(shù)據(jù)和輸出數(shù)據(jù)。2、編程

2、計算圖形的面積。程序可計算圓形、長方形、正方形的面積,運行時先提示用戶選擇圖形的類型,然后,要求用戶對圓形輸入半徑值,要求用戶對長方形輸入長和寬的值,要求用戶對正方形輸入邊長的值,計算出面積的值后將其顯示出來。1、打開 VC 環(huán)境。2、輸入以下程序代碼:#include <iostream.h>void main()int salary,hour;/(1)定義每小時工資數(shù)及每周工作時數(shù)的變量double week;/(2) 定義每周的工資數(shù)變量cout<<"Please inputemployee's work time and wage_per_ho

3、ur:"/(3) 輸入提示信息cin>>hour>>salary;/(4)輸入每小時工資數(shù)及每周工作時數(shù)if (hour<=40)/(5) 判斷工作時數(shù)是否小于40week=salary*hour;/(6) 計算小于 40 情況的周工資數(shù)if (hour<=60&&hour>40)/(7) 判斷工作時數(shù)是否在40 至 60 之間week=salary*(hour-40)*1.5+salary*40;/(8) 計算周工資數(shù)if (hour>60) /(9)判斷工作時數(shù)是否大于60 之間week=salary*40+sala

4、ry*20*1.5+salary*(hour-60)*3;/(10) 計算周工資數(shù)cout<<"The employee's wage :"<<week<<endl;/(11) 輸出周工資數(shù)實驗 2 的參考程序:#include <iostream>using namespace std;const float PI = 3.1416;int main()int iType;float radius, a, b, area;cout << " 圖形的類型為 ?(1-圓形 2-長方形 3-正方形 )

5、:" cin >> iType;switch(iType)1/12.case 1:cout << " 圓的半徑為: "cin >> radius;area = PI * radius * radius;break;case 2:cout << " 矩形的長為: "cin >> a;cout << " 矩形的寬為: "cin >> b;area = a * b;break;case 3:cout << " 正方形的邊長為:

6、 "cin >> a;area = a * a;break;default:cout << " 不是合法的輸入值 !"<<endl;cout<<" 面積為: "<<area<<endl;實驗名稱:循環(huán)結構程序設計1、 編寫一個程序,循環(huán)提示從鍵盤輸入數(shù)值,找出這些數(shù)值中的最大值和最小值。2、編寫程序實現(xiàn)階乘之和1! 2!+3!+ +m!。要求如下:( 1)求階乘之和小于100000 的最大 m值并輸出m及和值。( 2)根據(jù)求解結果,輸出完整的階乘表達式,如:1!+2!+3!

7、=9。3、編寫一個程序,實現(xiàn)計算一個整數(shù)的各位數(shù)字之和,如輸入2568, 該程序能實現(xiàn)計算8+6+5+2 的值并輸出。1、#include<iostream.h>void main()int num,data,max,min,i;cout<<" 輸入數(shù)據(jù)的個數(shù): "cin>>num;cout<<" 輸入初始值 :"cin>>data;max=min=data;for(i=1;i<num;i+)cout<<" 輸入數(shù)據(jù) :"cin>>data;if

8、(max<data)max=data;2/12.else if(min>data)min=data;cout<<"Max="<<max<<" Min="<<min<<endl;2、#include <iostream.h>void main()int m,sum,i,fac;sum=0;fac=1;m=1;while(1) / 求階乘 ,找出階乘的最小m 值fac=fac*m;sum+=fac;if(sum>100000)m-;sum=sum-fac;cout<

9、;<" 滿足不等式的最小值 ="<<m<<"和值 ="<<sum<<endl;break;m+;for(i=1;i<m;i+)/此循環(huán)完成輸出格式,根據(jù)m 值求階乘cout<<i<<"!+"cout<<i<<"!="<<sum<<endl;3、#include <iostream.h>void main()int num,residue,sum=0;cout<<

10、"Enter an integer:"cin>>num;while(1)residue=num%10;sum+=residue;num/=10;if(num!=0)/控制輸出格式cout<<residue<<"+"3/12.elsecout<<residue<<"="<<sum<<endl;break;實驗名稱:函數(shù)1、由鍵盤輸入兩個整數(shù),求兩個數(shù)的最大公約數(shù)和最小公倍數(shù)。要求:( 1)最大公約數(shù)和最小公倍數(shù)分別由函數(shù)實現(xiàn)。( 2)在主函數(shù)中調用所寫函

11、數(shù),給出在運行程序時的輸入數(shù)據(jù)和輸出數(shù)據(jù)。2、輸入三個數(shù),驗證是否構成一個三角形,如果能,通過海倫公式s=(其中 p=( a+b+c)/2) ,計算三角形面積,要求:( 1)實現(xiàn)判斷三個數(shù)是否構成三角形和計算面積由函數(shù)實現(xiàn)。( 2)編寫主函數(shù)對編寫的函數(shù)進行測試。1、#include<iostream.h>int gCD(int data1,int data2) /求最大公約數(shù)int residue;if(data1<data2)/data1 大于 data2int temp;temp=data1;data1=data2;data2=temp;doresidue=data1%

12、data2;data1=data2;data2=residue;while(residue!=0);return (data1);int lCM(int data1,int data2)/求最小公倍數(shù)return (data1*data2)/gCD(data1,data2);int main()int d1,d2;cout<<" 輸入兩個整數(shù) "4/12.cin>>d1>>d2;cout<<d1<<","<<d2<<" 的最大公約數(shù)是: "<&l

13、t;gCD(d1,d2)<<endl; cout<<d1<<","<<d2<<" 的最小公倍數(shù)是: "<<lCM(d1,d2)<<endl; return 0;2、bool isTriangle(float a,float b,float c)實現(xiàn)判斷 a,b,c 能否構成三角形的三條邊,并編寫主調函數(shù)。/設計一個函數(shù),對傳遞給它的double 型數(shù)值進行四舍五入作為int 型值返回。#include<iostream.h>bool isTriangle(fl

14、oat a,float b,float c)return (a+b>c&&a+c>b&&b+c>a);double TriangleArea(float a,float b,float c)double p=(a+b+c)/2;double s=sort(p*(p+a)*(p+b)*(p+c);return p;int main()float a,b,c;cout<<" 輸入三條邊 :"cin>>a>>b>>c;if(!isTriangle(a,b,c)cout<<

15、" 不能構成三角形:"<<endl;elsecout<<" 能構成三角形:"<<endl;cout<< ”三角形面積 =”<< TriangleArea(a,b,c);return 0;實驗名稱:類和對象創(chuàng)建一個學生類 Student ,該類中數(shù)據(jù)成員有: 學號 (sno) 、姓名 (sname) 、年齡 (sage) 、數(shù)學 (math) 、計算機 (computer) ;實現(xiàn)相關功能的成員函數(shù)有:構造函數(shù)、析構函數(shù)、求每個學生的平均成績【平均成績 =(數(shù)學 +計算機) /2 】、輸出學生所有

16、信息。參考程序:#include<iostream.h>#include<string.h>#include<iomanip.h>5/12.class Studentint no;char name;char sex;int age;float math;float computer;public:Student(int no1,char na,char se,int ag,float math,float computer);Student();void set();float average();void display();Student: Stude

17、nt(int no1,char na,char se,int ag,float ma,float com)no=no1;name=na;sex=se;age=ag;computer=com;math=ma;Student:Student()no=0;computer=0;math=0;void Student:set()cout<<" 輸入學號 "cin>>no;cout<<" 輸入姓名 :"cin>>name;cout<<" 輸入性別 :"cin>>sex;co

18、ut<<" 輸入年齡 :"cin>>age;cout<<" 輸入數(shù)學成績 :"cin>>math;cout<<" 輸入計算機成績 :"cin>>computer;6/12.float Student:average()return (math+computer)/2;void Student:display()cout<<setw(10)<<no<<setw(20)<<name<<setw(10)<

19、<sex; cout<<setw(4)<<age<<setw(5)<<math<<setw(5)<<endl;void main()Student s1,s2(2,'l','m',20,84,70);s1.set ();cout<<" 學生 1 的平均成績 "<<s1.average() <<endl; cout<<" 學生 2 的平均成績 "<<s2.average() <<

20、;endl;試驗 6-1#include <iostream>using namespace std;const double PI=3.14;class Shapepublic:virtual void Show()=0;virtual void GetArea()=0;virtual void Set(float x,float y)=0;class Rectangle:public Shapeprivate:float Length,Width;float Area;public:Rectangle()Length=0;Width=0;Rectangle(float x,fl

21、oat y)Length=x;7/12.Width=y;void Show();void GetArea();void Set(float x,float y);void Rectangle:Show ()cout<<" 長為: "<<Length<<endl;cout<<" 寬為: "<<Width<<endl;cout<<" 面積為: "<<Area <<endl;void Rectangle:GetArea ()Area=

22、Length*Width;void Rectangle:Set (float x,float y)Length=x;Width=y;class Circle:public Shapeprivate:float x,y;float r;float area;public:Circle()x=0;y=0;r=0;Circle(float a,float b,float c)x=a;y=b;r=c;void Show();void GetArea();void Set(float x1,float y1);8/12.void Circle:Show ()cout<<" 圓心為:

23、 ("<<x<<","<<y<<")"<<endl;cout<<" 半徑為: "<<r<<endl;cout<<" 面積為: "<<area<<endl;void Circle:GetArea()area=PI*r*r;void Circle:Set (float x1,float y1)x=x1;y=y1;int main()Shape *p;Rectangle r1;Ci

24、rcle c1;p=&r1;p->GetArea ();p->Show ();p=&c1;p->GetArea ();p->Show ();return 0;試驗 6-2#include <iostream.h>/using namespace std;class Pointprivate:int x;int y;public:Point()x=0;y=0;Point(int a,int b)9/12.x=a;y=b;Point operator+(Point &p1);Point operator-(Point &p1);P

25、oint operator+();Point operator+(int);Point operator-();Point operator-(int);friend istream &operator>>(istream &in,Point &p1);friend ostream &operator<<(ostream &out,Point &p1);Point Point:operator + (Point &p1)Point temp;temp.x =x+p1.x ;temp.y =y+p1.y;return

26、 temp;Point Point:operator - (Point &p1)Point temp;temp.x =x-p1.x ;temp.y =y-p1.y;return temp;Point Point:operator + ()x+;y+;return *this;Point Point:operator + (int)Point temp(*this);x+;y+;return temp;istream &operator>>(istream &in,Point &p1)in>>p1.x;in>>p1.y;retu

27、rn in;10/12.ostream &operator<<(ostream &out,Point &p1)out<<"("<<p1.x<<","<<p1.y<<")"<<endl;return out;int main()Point p1,p2; cin>>p1>>p2; cout<<p1+<<endl; cout<<+p2<<endl; cout<

28、;<p1+p2<<endl; cout<<p1-p2<<endl;return 0 ; /8_4.cpp#include <iostream>using namespace std;class B0 /基類 B0 聲明public:/外部接口virtual void display()/虛成員函數(shù) cout<<"B0:display()"<<endl; void display(int x)/ 同名重載函數(shù) cout<<"B0:display(int x)"<<endl; virtual void show(int x=0) cout<<"B0:show()"<<x<<e

溫馨提示

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

評論

0/150

提交評論