江科大c++期末實驗報告_第1頁
江科大c++期末實驗報告_第2頁
江科大c++期末實驗報告_第3頁
江科大c++期末實驗報告_第4頁
江科大c++期末實驗報告_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質文檔-傾情為你奉上實 驗 報 告 制作者: 徐珩 第一章: 1、從鍵盤輸入10個整數(shù),求它們的平均值。#include <iostream.h>void main()cout<<"輸入10個整數(shù):"<<endl;int j=1,k;float s,n;for(s=0;j<=10;j+)cin>>k;s=s+k;n=s/10;cout<<s/10<<endl;運行結果:2、計算s=1+2+3+I,累加到s大于1000,并輸出s和i的值。#include <iostream.h>v

2、oid main()int s,i=0;for(s=0;s<=1000;i+)s=s+i;cout<<s<<'t'<<i<<endl;運行結果:3、找到2到200間的素數(shù),按5個一行輸出。#include <iostream.h>void main()int s,i,k=0;cout<<"2到200間的素數(shù):"<<endl;for(i=2;i<=200;i+)for (s=2;s<=i/2;s+)if(i%s=0) break;if(s<=i/2) c

3、ontinue;elsecout<<i<<'t'k+; if(k%5=0) cout<<endl;運行結果:第四章:1、設計一個程序,通過指針變量求鍵盤輸入的一串字符中單詞的個數(shù),如輸入“I am a boy.”則輸出其中包含的單詞個數(shù)4。#include<iostream.h>void main() char p100; cin.getline(p,100); int num=0,n=0, flag=0; for(;pn;n+) if(pn!=' ') flag=1; if(flag)if(pn+1='

4、'|pn+1='0') num+; cout<<p<<"包含的單詞個數(shù)是:"<<num<<endl;程序運行結果是:2、找出一維數(shù)組中值最大的元素及其下標,注意最大元素可能不止一個。例如,3,5,2,7,6,1,7,4,7,5中的最大元素為7,其下標分別為3,6,8。#include<iostream.h>void main()int a10=3,5,2,7,6,1,7,4,7,5;for(int n=0,k=a0;n<10;n+)if(an>k) k=an;cout<&l

5、t;"最大的元素是:"<<k<<'n'<<"下標是:"<<'t'for( int b=0;b<10;b+)if(ab=k)cout<<b<<'t'cout<<endl;程序運行結果為:3、將楊輝三角的前N行保存到二維數(shù)組的下半三角中。楊輝三角由正整數(shù)構成,每行除最左側與最右側的數(shù)為1外,其他數(shù)等于其左上方與正上方兩個數(shù)的和,楊輝三角的前5行如下:11 11 2 11 3 3 11 4 6 4 1#include<

6、iostream.h>void main() int n44; for(int k=0;k<5;k+) nk0=1; nkk=1; for(int i=2;i<5;i+) for(int j=1;j<i;j+)nij=ni-1j+ni-1j-1; for(i=0;i<5;i+) for(int j=0;j<i+1;j+) cout<<nij<<'t' cout<<endl; 程序運行結果:第五章:1、設計函數(shù)int int_to_string(int num,char a,int &n),對一個不為

7、0的任意位數(shù)的十進制整數(shù)num,統(tǒng)計出num的位數(shù)n及各位數(shù)字之和s,并將每位數(shù)字以字符的形式存儲到數(shù)組a 中。在主函數(shù)中調用int_to_string函數(shù),對從鍵盤讀入的整數(shù)進行測試。調用該程序的運行結果如下(帶下劃線部分為鍵盤輸入內(nèi)容): 請輸入一個整數(shù):12345 12345是5位數(shù),其各位數(shù)字為1、2、3、4、5,各位數(shù)字之和為15。#include<iostream.h> int int_to_string (int num,char a,int &n) int sum=0,c=num,i=0,k; while(num) k=num%10; num/=10; su

8、m+=k; ai=k+48; i+; cout<<c<<"是"<<i<<"位數(shù),其各位數(shù)字為" for(i-;i;i-) cout<<ai<<"、" cout<<a0<<"," return sum; void main() int sum,num,n;char a100; cin>>num; sum=int_to_string(num,a,n); cout<<"各位數(shù)字之和為"

9、;<<sum<<endl; 運行結果:2、設計處理二維數(shù)組的程序,要求如下。(1)定義函數(shù)void input(int a4,int n),用于對二維數(shù)組進行初始化。(2)定義函數(shù)void output(int (*a)4,int n),用于輸出二維數(shù)組中的元素。(3)定義函數(shù)int search_max(int a4,int n,int &cool,int &vol),用于查找二維數(shù)組a中的最大值元素,并記錄其行下標和列下標,其中最大值元素的值通過函數(shù)返回值帶回主函數(shù),對應的下標分別存儲到col和vol中。(4)在主函數(shù)中進行測試。#include&l

10、t;iostream.h>void input(int a4,int n) int k4=1,2,3,4,6,7,8,9,10,11,12,13; for(int i=0;i<n;i+) for(int j=0;j<4;j+) aij=kij;void output(int(*a)4,int n)for(int i=0;i<n;i+) for(int j=0;j<4;j+) cout<<aij<<'t' cout<<endl;int search_max(int a4,int n,int &col,int

11、 &vol) int max=a00;for(int i=0;i<n;i+) for(int j=0;j<4;j+)if(aij>max) max=aij; col=i; vol=j; return max;void main()int a104;int col,vol;input(a,3);output(a,3);cout<<search_max(a,3,col,vol)<<endl;cout<<col<<'t'<<vol<<endl;運行結果:3、設計程序,求兩個整數(shù)的最小公倍

12、數(shù)。#include<iostream.h>void fun(int x,int y) int k=x; if(x>y) k=y; if(k<=0) cout<<"請輸入有效數(shù)據(jù)!"<<endl; else for(k;k<=x*y;k+) if(k%x=0&&k%y=0) cout<<"最小公倍數(shù)"<<k<<endl; break; void main() int a,b; cin>>a>>b; fun(a,b);運行結果:

13、第七章:1、定義一個Point類表示平面上的一個點,再定義一個Rectangle類表示平面上的矩形,用Point類的對象作為Rectanlge類的成員描述平面上的矩形的頂點坐標。要求類Point中有相應的成員函數(shù)可以讀取點的坐標值,類Rectangle中含有一個函數(shù),用以計算并輸出矩形的面積及頂點坐標。在主函數(shù)中對類Rectangle進行測試。#include<iostream.h>#include<math.h>class Point float a,b;public:Point(float x,float y)a=x;b=y;Point();float geta()

14、return a;float getb()return b;class Rectangle float area,x1,y1,x2,y2;public:Rectangle(Point p1,Point p2)x1=p1.geta();y1=p1.getb();x2=p2.geta();y2=p2.getb();void print()area=fabs(x1-x2)*fabs(y1-y2); cout<<"面積是:"<<area<<endl; cout<<"頂點坐標是("<<x1<<

15、","<<y1<<")("<<x1<<","<<y2<<")(" <<x2<<","<<y1<<")("<<x2<<","<<y2<<")"<<endl; ;void main() Point p1(2,3); Point p2(6,7); Rectangle

16、 r1(p1,p2); r1.print();運行結果:2、定義一個類,用于刪除字符串中多余的字符,使其中的字符互不相同,具體要求如下:(1)類的數(shù)據(jù)成員(字符串)用指針表示,并在構造函數(shù)中根據(jù)參數(shù)的實際情況為該成員指針申請內(nèi)存空間。(2)刪除多余字符串時,只能在原字符串空間中進行,不得借助其他輔助空間。#include<iostream.h>#include<string.h>class D char *s;public:D(char *p) int n=strlen(p); s=new charn+1; strcpy(s,p);D()delete s;void de

17、al();void print();void D:deal() int i,j,k,flag;for(i=0;si;i+)for(j=i+1;sj;j+)if(si=sj)k=j;flag=1;while(sk) sk=sk+1;k+;if(flag=1) flag=0; j-;void D:print() cout<<s<<endl;void main() char a100;cin.getline(a,100); D d1(a);d1.deal();d1.print();運行結果:第八章:1、教師月工資的計算公式為:基本工資+課時補貼。教授的基本工資為5000元,補

18、貼為50元/課時;講師的基本工資為3000元,補貼為20元/課時。設計一個程序求教授和講師的月工資,具體要求如下。(1)定義教師類Teacher作為基類,包含數(shù)據(jù)成員姓名、月工資和授課時數(shù),以及構造函數(shù)(初始化姓名和月授課時數(shù))、輸出數(shù)據(jù)成員的函數(shù)。(2)定義教師類Teacher的公有派生類Professor 表示教授,公有派生類Lecturer表示講師,并分別計算其月工資。(3)在主函數(shù)中對定義的類進行測試。#include<iostream.h>#include<string.h>class Teacher char *name;float wage;float h

19、our;public:Teacher(char *p,float w,float t) int n=strlen(p);name=new charn+1;strcpy(name,p);wage=w;hour=t;Teacher()delete name;void print()cout<<"姓名"<<name<<'n'<<"月工資"<<wage<<'n'<<"授課時數(shù)"<<hour<<endl;c

20、lass Professor:public Teacherpublic:Professor(char *p,float t):Teacher(p,5000+50*t,t);class Lecturer:public Teacherpublic:Lecturer(char *p,float t):Teacher(p,3000+20*t,t);void main() char p110="professor",p210="lecturer"float t=10;Professor k1(p1,t);k1.print();Lecturer k2(p2,t);k

21、2.print();運行結果:2、設計一個程序輸出汽車信息,具體要求如下。(1)定義汽車類Auto作為抽象類,包含車牌號、車輪數(shù)等數(shù)據(jù)成員,以及構造函數(shù)、輸出車輛信息的純虛函數(shù)。(2)定義類Auto 的公有派生類Car表示小客車,新增荷載人數(shù),重新定義輸出函數(shù)。(3)定義類Auto 的公有派生類Truck表示貨車,新增荷載噸位,重新定義輸出函數(shù)。(4)在主函數(shù)中對定義的類進行測試,用基類對象的引用實現(xiàn)動態(tài)聯(lián)編。#include<iostream.h>#include<string.h>class Autoprotected:char *s;int wheel;public: Auto(char *p,int t) int n=strlen(p); s=new charn+1; strcpy(s,p); wheel=t;Auto()delete s;virtual void print()=0;

溫馨提示

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

評論

0/150

提交評論