




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、 計算機(jī)及應(yīng)用專業(yè) (b080702) 8347 計算機(jī)及應(yīng)用課程實驗(二) 1901032019464. 面向?qū)ο蟪绦蛟O(shè)計 (上機(jī)考試)樣題. 下列shape類是一個表示形狀的抽象類,area()為求圖形面積的函數(shù),total()則是一個通用的用以求不同形狀的圖形面積總和函數(shù)。請從shape類派生三角形類(triangle) 、矩形類(rectangle),并給出具體的求面積函數(shù)。編寫程序驗證求面積函數(shù)的正確性。shape、 total的定義如下所示。class shapepubilc:virtual float area()=0;float total (shape *s , int n)
2、float sum=0.0;for(int i=0; iarea ();return sum;解答:#include class shape public: virtual float area()=0; ;float total(shape *s, int n) float sum=0; for(int i=0; i area(); return sum;class triangle : public shape protected: float h, w; public: triangle(float h, float w) h=h; w=w; float area() return h*
3、w*0.5;class rectangle : public triangle public: rectangle(float h, float w) : triangle(h, w) float area() return h*w;void main() shape *s4; s0 = new triangle( 3.0, 4.0 ); s1 = new rectangle( 2.0, 4.0 ); s2 = new triangle( 5.0, 8.0 ); s3 = new rectangle( 6.0, 8.0 ); float sum = total(s,4); cout the t
4、otal area is: sum endl;樣題. 以面向?qū)ο蟮母拍钤O(shè)計一個類,此類包括個私有數(shù)據(jù),unlead(無鉛汽油), lead有鉛汽油, total (當(dāng)天總收入)。其中,無鉛汽油價格是17/升,有鉛汽油價格是16/升,請以構(gòu)造函數(shù)的方式建立此值,并編寫程序,該程序能夠根據(jù)加油量,自動計算出油站當(dāng)天的總收入。解答:#include class income private: float unlead, lead, total; public: income(float ul, float l)unlead=ul; lead=l; total=0.0; float calculate
5、(float, float);float income : calculate(float unleadcontent, float leadcontent) total = unlead*unleadcontent + lead*leadcontent; return total;void main() float unleadcontent, leadcontent, total; income account(17, 16); cout please input unlead content: unleadcontent; cout please input lead content:
6、leadcontent; total = account.calculate(unleadcontent, leadcontent); cout the total income is: total endl;樣題. 編寫一個計算兩個給定長方形的面積的程序,要求長方形用一個類(rectangle)來表示,在該類中增加定義一個成員函數(shù) add_area(), 該成員函數(shù)使用對象作為參數(shù),用來計算兩個給定長方形的面積。解答:#include class rectangle private: float h, w; public: rectangle(float h, float w)h=h; w=
7、w; float area() return h*w; float add_area(rectangle &);float rectangle : add_area(rectangle &rec) return area() + rec.area();void main() float h1, w1, h2, w2, totalarea; cout please input the 1st rectangle h & w: h1 w1; rectangle rec1(h1,w1); cout please input the 2st rectangle h & w: h2 w2; rectan
8、gle rec2(h2,w2); totalarea = rec1.add_area(rec2); cout the total area of the two rectangle is: totalarea endl;樣題4. 定義一個復(fù)數(shù)類complex,該類至少提供加、減、賦值、輸出等操作,所有操作均以友元形式實現(xiàn)。編寫程序驗證其功能。解答:#include class complex private: float real; float imag; public: complex(); complex(int r, int i) real=r; imag=i; friend compl
9、ex operator + (complex &, complex &); friend complex operator - (complex &, complex &); void show();complex operator + (complex & a, complex & b) float r, i; r=a.real+b.real; i=a.imag+b.imag; return complex(r,i);complex operator - (complex & a, complex & b) float r, i; r=a.real-b.real; i=a.imag-b.im
10、ag; return complex(r,i);void complex : show() if (imag0) if (imag = 1)cout real +i; else cout real + imag i; else if (imag0) if (imag = -1)cout real -i; else cout real imag i; else cout real;void main() complex a(4,5), b(2,3), x, y; x=a+b; y=a-b; a.show(); cout +; b.show(); cout =; x.show(); cout en
11、dl; a.show(); cout -; b.show(); cout =; y.show(); cout endl;樣題5.定義一個平面幾何中點的位置類position, 它應(yīng)該包含有移動、計算兩點間的距離(包括到原點的距離),求x坐標(biāo)、y坐標(biāo)等操作,其中計算兩點間的距離以友元函數(shù)形式實現(xiàn)。編寫程序驗證其功能。解答:#include #include class position private: float x, y; public: position(float xi, float yi) x=xi, y=yi; void move(float xo, float yo) x+=xo,
12、 y+=yo; float getx()return x; float gety()return y; float distancetoorigin(); friend float distance(position &, position &);float position : distancetoorigin() return sqrt( x*x + y*y );float distance(position & a, position & b) float dx = a.x - b.x; float dy = a.y - b.y; return sqrt( dx*dx + dy*dy);
13、void main() position p1(1.5, 3.5), p2(4.5, 6.5); p1.move(3.5, 5.5); float dis0 = p1.distancetoorigin(); float dis = distance( p1, p2 ); cout the distance p1( p1.getx() , p1.gety() ) to origin is: dis0 endl; cout the distance between p1( p1.getx() , p1.gety() ) and p2( p2.getx() , p2.gety() ) is dis
14、endl;樣題6.利用類和對象,編制出一個賣瓜的程序。每賣一個瓜要計出該瓜的重量,還要計算所賣出瓜的總重量及總個數(shù),同時賣瓜時還允許退瓜。(提示:將每個瓜設(shè)為對象; 用靜態(tài)成員變量分別統(tǒng)計賣出瓜的總重量和總個數(shù); 賣瓜行為用構(gòu)造函數(shù)模擬,退瓜行為用析構(gòu)函數(shù)模擬。)解答:#include class watermelon private: static int n; static float totalweight; float weight; public: watermelon(float w) n+; weight=w; totalweight += w; watermelon(water
15、melon & wa) n+; weight=wa.weight; totalweight += weight; watermelon() n-; totalweight -= weight; int getweight() return weight; static int getnum() return n; static int gettotal() return totalweight; ;int watermelon : n = 0;float watermelon : totalweight = 0;void main() float w; cout the initial wei
16、ght of watermelon: watermelon : gettotal() endl; cout please input weight of watermelon: w; watermelon wa1(w); cout please input weight of watermelon: w; watermelon wa2(w); cout please input weight of watermelon: w; watermelon wa3(w); cout watermelon : getnum() nos. watermelons were sold. endl; cout
17、 the total weight is: watermelon : gettotal() endl; wa3.watermelon:watermelon(); cout now one watermelon was withdrawed! endl; cout watermelon : getnum() nos. watermelons were sold. endl; cout the total weight is: watermelon : gettotal() endl;樣題6.編寫一個程序,用于計算三角形、矩形和圓的總面積。(提示:由于尚不能確定該程序計算的具體形狀,可以先定義一個
18、抽象的類shape, 對于具體種類的形狀,通過從shape派生一個類來對其進(jìn)行描述。)解答:#include class shape public: virtual float area()=0; ;float total(shape *s, int n) float sum=0; for(int i=0; i area(); return sum;class triangle : public shape protected: float h, w; public: triangle(float h, float w) h=h; w=w; float area() return h*w*0.
19、5;class rectangle : public triangle public: rectangle(float h, float w) : triangle(h, w) float area() return h*w;class circle : public shape protected: float radius; public: circle(float r) radius=r; float area() return radius*radius*3.14; ;void main() shape *s4; s0 = new triangle( 3.0, 4.0 ); s1 =
20、new rectangle( 2.0, 4.0 ); s2 = new circle( 5.0 ); s3 = new circle( 8.0 ); float sum = total(s,4); cout the total area is: sum endl;題樣7.編寫一個程序,可以輸入個學(xué)生的總分,并按總分從高到低排序,要求設(shè)計一個學(xué)生類 student, 并編寫其所有成員函數(shù)的代碼,類student的定義如下:class student int total; / 總分成績 pubic: void get_score(); /獲取一個學(xué)生的成績 void display(); /顯示一
21、個學(xué)生的成績 void sort (student *); /將若干學(xué)生成績按總分從高到低排序解答:#include #include class student int total; public: student(int m)total=m; int get_score(); void display(); void sort(student *s3);int student : get_score() return total;void student : display() cout setw(6) total endl;void student : sort(student * s3
22、) student * t; for (int i=0; ii; j-) if (sj-get_score() sj-1-get_score() t=sj;sj=sj-1;sj-1=t; cout the sorted score is: ; for (int k=0; k3; k+) cout setw(6) get_score(); cout endl;void main() int s0, s1, s2; cout please input 3 score: s0s1s2; student *s3; s0=new student(s0); s1=new student(s1); s2=n
23、ew student(s2); s0-sort(s);樣題8.設(shè)計一個棧操作類,該類包含入和出棧成員函數(shù),編寫程序,入棧一組數(shù)據(jù):(5, 2, 6, 7, 3),然后屏幕顯示出棧結(jié)果。解答:#include #include const int size=20;class stack private: int datasize; int top; public: stack() top=-1; void push(int); int pop();void stack : push( int c) if (top=19) cout stack overflow! endl; else data+
24、top=c;int stack : pop() if (top=-1) cout stack underflow! endl; return null; else return datatop-;void main() stack s; s.push(5); s.push(2); s.push(6); s.push(7); s.push(3); for (int i=0; i5; i+) cout setw(6) s.pop(); cout endl;樣題9.編寫一個輸入、顯示學(xué)生(用類student表示)和教師(用類teacher表示)數(shù)據(jù)的程序,學(xué)生的數(shù)據(jù)包括:編號(no)、 姓名(nam
25、e)和班號 (class), 教師的數(shù)據(jù)包括:編號(no)、 姓名(name)和部門(dept)。要求將編號、姓名的輸入和顯示兩項操作設(shè)計成一個類person,并將它作為student類和teacher類的基類。解答:#include #include class person protected: char no5; char name10; public: virtual void input(); virtual void show();class student : public person private: char class3; public: virtual void inp
26、ut(); virtual void show();class teacher : public person private: char dept3; public: virtual void input(); virtual void show();void person : input() cout please input no.: no; cout please input name: name;void person : show() cout.setf(ios:left); cout.width(8); cout no.: no endl; cout.width(8); cout
27、 name: name endl;void student : input() person : input(); cout please input class: class;void student : show() person : show(); cout.setf(ios:left); cout.width(8); cout class: class endl;void teacher : input() person : input(); cout please input dept: dept;void teacher : show() person : show(); cout
28、.setf(ios:left); cout.width(8); cout dept: dept endl;void main() student stu1, stu2; teacher tea1; cout please input 1st students information: endl; stu1.input(); cout please input 2st students information: endl; stu2.input(); cout please input teachers information: endl; tea1.input(); cout endl stu
29、dent: endl; stu1.show(); stu2.show(); cout endl teacher: endl; tea1.show();樣題10.編寫一個可以刪除文本文件中所有以“/”開頭字符串的c+程序。要求必須使用c+的i/o流成員函數(shù)來完成。解答:#include #include void main(int argc, char * argv) char ch; if (argc!=3) cout error, you shall use this programs as: nt purgefile filename1 filename2 endl; return; if
30、stream myin(argv1); if (!myin) cout cant open file argv1 endl; return; ofstream myout(argv2); if(!myout) cout cant create file argv2 endl; return; while (myout & myin.get(ch) if ( ch = / ) myin.get(ch); if ( ch = /)do while (myin.get(ch) & (ch!= )&(ch!=n); else myout.put(/);myout.put(ch);myin.get(ch
31、);domyout.put(ch); while (myin.get(ch) & (ch != )&(ch!=n);if (ch = )|(ch = n) myout.put(ch); else if (ch= )|(ch=n) myout.put(ch); else myout.put(ch); myin.get(ch); do myout.put(ch); while (myin.get(ch) & (ch!= )&(ch!=n); if (ch = )|(ch = n) myout.put(ch); myin.close(); myout.close(); cout purge comp
32、leted. endl;樣題11.己知類test含有整型私有數(shù)據(jù)成員num,編寫主程序,要求:1) 寫出test的完整定義,并含有必要的函數(shù)成員;2) 建立長度為9、元素類型為test的動態(tài)數(shù)組且初值為0;3) 將各元素的值均設(shè)置為0x7ff;4) 顯示各元素的值;5) 刪除動態(tài)數(shù)組;解答:#include #include class test private: int num; public: test() num=0; test(int n) num=n; void setnum(int n) num=n; int getnum() return num; ;void main() te
33、st *ptr=new test9; int i; for (i=0; i9; i+) ptri.setnum(0x7ff); for (i=0; i9; i+) cout setw(8) hex ptri.getnum(); cout 0 ) return add( succ(a), pred(b) ); if ( b0 ) return sub( pred(a), pred(b) ); if ( b0 ) return sub( succ(a), succ(b) );void main() int a, b, x, y; printf(please input a,b:n); scanf(
34、%d,%d, &a, &b); x=add(a,b); y=sub(a,b); printf(%d+%d=%dn, a, b, x); printf(%d-%d=%dn, a, b, y);樣題2. 試編寫一個求解josephus問題的函數(shù)。用整數(shù)序列1, 2, 3, , n 表示順序圍坐在圓桌周圍的人,并采用數(shù)組表示作為求解過程中使用的數(shù)據(jù)結(jié)構(gòu)。然后使用n=9, s=1, m=5,以及n=9, s=1, m=0,或者n=9, s=1, m=10作為輸入數(shù)據(jù),驗證程序的正確性。解答:#include “stdio.h”void josephus( int a, int n, int s, in
35、t m) int i, j, k, tmp; if ( m = 0 ) printf(error! m cant set 0!n); return; for ( i=0; i1; k-) if ( i = k ) i=0; i = ( i+m-1 ) % k; if ( i != k-1) tmp = ai; for ( j=i; jk-1; j+ ) aj = aj+1; ak-1 = tmp; for ( k=0; kn/2; k+ ) tmp=ak; ak=an-k-1; an-k-1=tmp; void main() int a20, n, s, m, i; printf(please
36、 input n, s, m:n); scanf(%d,%d,%d, &n, &s, &m); josephus(a, n, s, m); if (m!=0) printf(the score is:); for (i=0; in; i+)printf(%4d, ai); printf(n); 樣題3. 依次輸入10個整數(shù),分別用順序表與單鏈表存儲,并實現(xiàn)其就地逆置。解答1 (順序表):#include “stdio.h”typedef struct int data20; int length; seqlist;void main() seqlist *l; int tmp; int i,
37、j; printf(please input 10 integer:n); for (i=0; idatai); l-length=10; for (i=0, j=l-length-1; idatai; l-datai=l-dataj; l-dataj=tmp; printf(the reversed order:n); for (i=0; ilength; i+) printf(%6d, l-datai); printf(n);解答2 (單鏈表):#include stdio.h#include malloc.htypedef struct node int data; struct nod
38、e *next; listnode;typedef listnode * linklist;void main() int i, n; linklist l; listnode *s, *r; l=null; r=null; printf(please input 10 integer:n); for(i=0; idata = n; if (l=null) l=s; else r-next = s; r=s; if(r!=null) r-next = null; s=null; while(l) r = l; l = l-next; r-next = s; s = r; l = s; prin
39、tf(the reversed order:n); r=l; while(r) printf(%6d, r-data); r=r-next; printf(n);樣題4. 設(shè)循環(huán)隊列類型如下 typedef struct datatype datamaxsize; int front, rear; cycqueue;cyqueue sq;編寫入隊操作int enqueue(cycqueue sq, datatype x)的代碼,并調(diào)試通過。(提示:將x入隊列sq,成功返回1,否則,返回0)解答:#include stdio.h#define maxsize 100typedef char da
40、tatype;typedef struct datatype datamaxsize; int front, rear; cycqueue;int enqueue(cycqueue *sq, datatype x) if ( (sq-rear+1)%maxsize = sq-front ) printf(queue overflow!n); return 0; sq-rear = (sq-rear+1) % maxsize; sq-datasq-rear = x; return 1;datatype dequeue(cycqueue *sq) if ( sq-rear = sq-front ) printf(queue underflow!n); return null; sq-front = (sq-front+1) % maxsize; return ( sq-datasq-front );void main() int i, n; char c; cycqueue sq; sq.rear=0; sq.front=0; printf(please input cha
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 豫章師范學(xué)院《油畫靜物技法與表現(xiàn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 珠海格力職業(yè)學(xué)院《藏文文法上》2023-2024學(xué)年第二學(xué)期期末試卷
- 遼寧石化職業(yè)技術(shù)學(xué)院《語文學(xué)科教育論》2023-2024學(xué)年第二學(xué)期期末試卷
- 西安歐亞學(xué)院《數(shù)據(jù)分析與可視化》2023-2024學(xué)年第二學(xué)期期末試卷
- 南京工業(yè)大學(xué)《建筑防火設(shè)計》2023-2024學(xué)年第二學(xué)期期末試卷
- 西安科技大學(xué)高新學(xué)院《汽車發(fā)展史》2023-2024學(xué)年第二學(xué)期期末試卷
- 遼寧工程技術(shù)大學(xué)《資產(chǎn)評估學(xué)》2023-2024學(xué)年第二學(xué)期期末試卷
- 四川航天職業(yè)技術(shù)學(xué)院《嵌入式系統(tǒng)設(shè)計與開發(fā)》2023-2024學(xué)年第二學(xué)期期末試卷
- 合肥信息技術(shù)職業(yè)學(xué)院《建筑類專業(yè)導(dǎo)論》2023-2024學(xué)年第二學(xué)期期末試卷
- 南華大學(xué)船山學(xué)院《素描半身帶手及全身像實踐教學(xué)》2023-2024學(xué)年第二學(xué)期期末試卷
- DL-T5704-2014火力發(fā)電廠熱力設(shè)備及管道保溫防腐施工質(zhì)量驗收規(guī)程
- JT-T-610-2004公路隧道火災(zāi)報警系統(tǒng)技術(shù)條件
- 初中英語比較級和最高級專項練習(xí)題含答案
- 鑒賞詩歌人物形象市公開課一等獎省賽課微課金獎?wù)n件
- 大壩安全監(jiān)測系統(tǒng)驗收規(guī)范
- 2024年南京鐵道職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫及答案解析
- 校園超市經(jīng)營投標(biāo)方案(技術(shù)方案)
- 康復(fù)醫(yī)院建筑設(shè)計標(biāo)準(zhǔn)
- 社會穩(wěn)定風(fēng)險評估 投標(biāo)方案(技術(shù)方案)
- 高層建筑火災(zāi)撲救面臨問題及對策
- JC-T 738-2004水泥強度快速檢驗方法
評論
0/150
提交評論