中山大学上机实践试题及题目答案_第1页
中山大学上机实践试题及题目答案_第2页
中山大学上机实践试题及题目答案_第3页
中山大学上机实践试题及题目答案_第4页
中山大学上机实践试题及题目答案_第5页
已阅读5页,还剩80页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 计算机及应用专业 (b080702) 8347 计算机及应用课程实验(二) 1901032019464. 面向对象程序设计 (上机考试)样题. 下列shape类是一个表示形状的抽象类,area()为求图形面积的函数,total()则是一个通用的用以求不同形状的图形面积总和函数。请从shape类派生三角形类(triangle) 、矩形类(rectangle),并给出具体的求面积函数。编写程序验证求面积函数的正确性。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;样题. 以面向对象的概念设计一个类,此类包括个私有数据,unlead(无铅汽油), lead有铅汽油, total (当天总收入)。其中,无铅汽油价格是17/升,有铅汽油价格是16/升,请以构造函数的方式建立此值,并编写程序,该程序能够根据加油量,自动计算出油站当天的总收入。解答:#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)来表示,在该类中增加定义一个成员函数 add_area(), 该成员函数使用对象作为参数,用来计算两个给定长方形的面积。解答:#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. 定义一个复数类complex,该类至少提供加、减、赋值、输出等操作,所有操作均以友元形式实现。编写程序验证其功能。解答:#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, 它应该包含有移动、计算两点间的距离(包括到原点的距离),求x坐标、y坐标等操作,其中计算两点间的距离以友元函数形式实现。编写程序验证其功能。解答:#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.利用类和对象,编制出一个卖瓜的程序。每卖一个瓜要计出该瓜的重量,还要计算所卖出瓜的总重量及总个数,同时卖瓜时还允许退瓜。(提示:将每个瓜设为对象; 用静态成员变量分别统计卖出瓜的总重量和总个数; 卖瓜行为用构造函数模拟,退瓜行为用析构函数模拟。)解答:#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派生一个类来对其进行描述。)解答:#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.编写一个程序,可以输入个学生的总分,并按总分从高到低排序,要求设计一个学生类 student, 并编写其所有成员函数的代码,类student的定义如下:class student int total; / 总分成绩 pubic: void get_score(); /获取一个学生的成绩 void display(); /显示一

21、个学生的成绩 void sort (student *); /将若干学生成绩按总分从高到低排序解答:#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.设计一个栈操作类,该类包含入和出栈成员函数,编写程序,入栈一组数据:(5, 2, 6, 7, 3),然后屏幕显示出栈结果。解答:#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.编写一个输入、显示学生(用类student表示)和教师(用类teacher表示)数据的程序,学生的数据包括:编号(no)、 姓名(nam

25、e)和班号 (class), 教师的数据包括:编号(no)、 姓名(name)和部门(dept)。要求将编号、姓名的输入和显示两项操作设计成一个类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流成员函数来完成。解答:#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含有整型私有数据成员num,编写主程序,要求:1) 写出test的完整定义,并含有必要的函数成员;2) 建立长度为9、元素类型为test的动态数组且初值为0;3) 将各元素的值均设置为0x7ff;4) 显示各元素的值;5) 删除动态数组;解答:#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问题的函数。用整数序列1, 2, 3, , n 表示顺序围坐在圆桌周围的人,并采用数组表示作为求解过程中使用的数据结构。然后使用n=9, s=1, m=5,以及n=9, s=1, m=0,或者n=9, s=1, m=10作为输入数据,验证程序的正确性。解答:#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个整数,分别用顺序表与单链表存储,并实现其就地逆置。解答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. 设循环队列类型如下 typedef struct datatype datamaxsize; int front, rear; cycqueue;cyqueue sq;编写入队操作int enqueue(cycqueue sq, datatype x)的代码,并调试通过。(提示:将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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论