面向对象程序设计试卷A答案及评分标准.doc_第1页
面向对象程序设计试卷A答案及评分标准.doc_第2页
面向对象程序设计试卷A答案及评分标准.doc_第3页
面向对象程序设计试卷A答案及评分标准.doc_第4页
面向对象程序设计试卷A答案及评分标准.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

面向对象程序设计试卷A答案及评分标准本试卷共10个题,分别按以下标准评分,最后根据整个答题的情况,从程序设计风格的角度给予0-5分的附加分。1、编写程序,将从键盘读入的所有大小写字母写入名为a.txt的文件中(遇EOF结束)。(本题总分10分,fopen函数使用妥当4分,读入过程正确4分,关闭文件2分。程序结构完整,有不妥之处,酌情扣分。)#include main ( )FILE *fptr;fptr = fopen(a.txt,w);if (fptr=NULL)return 0;char a;a=getchar( );while(a!=EOF )if(a=a & a=A & a=Z) fputc(a,fptr);a = getchar();fclose(fptr);return 0;2、定义一个矩形类Rectangle,并在main函数中用它声明一个矩形对象,然后利用接口设置该矩形对象的长和宽、计算面积并输出。(本题总分10分,类结构2分,设置矩阵对象的长与高各1分,计算面积函数2分,输出函数2分,主函数2分。程序结构完整,有不妥之处,酌情扣分。)#include #include class Rectanglepublic: int getArea(); void setWidth(int w); void setLength(int l); private:int Length;int Width;int Rectangle:getArea() return Length*Width;void Rectangle:setLength(int l) Length=l;void Rectangle:setWidth(int w) Width=w;main() int len,wid; Rectangle r1; coutInput the Rectangles Informationendl; coutLentgh:len; coutWidth:wid; r1.setLength(len); r1.setWidth(wid); coutRectangles Area:r1.getArea()endl; return 0;3、定义一个整数栈类IStack,并用该类声明一个整数栈对象istack,往该对象压入整数6、7、8,然后逐一弹栈输出。(本题总分10分,类结构2分,构造、析构函数各1分,压栈、出栈函数实现2分,主函数2分。程序结构完整,有不妥之处,酌情扣分。)#include #include struct Node int item;struct Node *next;class IStack public:IStack();IStack();void push(int item);int pop();int getItemNum();private:Node *head;int size;IStack:IStack()head = NULL;size = 0;IStack:IStack()Node *temp = head;while (temp != NULL) temp = head-next; delete head;head = temp;void IStack:push(int item)Node *temp = new Node;temp-item = item;temp-next = head;head = temp;size+;int IStack:pop()if (size = 0) coutnext;int i = temp-item;delete temp;return i;int IStack:getItemNum() return size;main() IStack istack; istack .push(6); istack .push(7); istack .push(8); coutistack .pop( )endl; coutistack .pop( )endl; coutistack .pop( )endl; return 0;4、定义分数类Rational,要求在private部分用整数表示分子和分母,分子和分母以简化形式表示,即24/36应该以2/3的形式表示,并实现如下功能:(1) 两个分数对象可以用*相乘,结果表示为简化形式;(2) 按a/b的形式输出分数的值,a、b为整数。最后在main函数中对上述功能进行测试。(本题总分10分,类结构2分,分数相乘实现函数2分,化简函数实现2分,输出格式转化函数2分,主函数2分。程序结构完整,有不妥之处,酌情扣分。)#include #include class Rationalpublic:Rational(int num1=1,int num2=1);Rational operator*(Rational r1); void showNomal();private:int up;int down;int Minmultiple(int a,int b); /最小公倍数int Maxdivisor(int a,int b);/最大公约数;Rational:Rational(int num1,int num2) up=num1; down=(num2=0)?1:num2; int i; i=Maxdivisor(up,down); up=up/i; down=down/i;int Rational:Maxdivisor(int a,int b) int temp; int remainder; if(ab) temp=a;a=b;b=temp; remainder=a%b; while(remainder!=0) a=b;b=remainder;remainder=a%b; return b;int Rational:Minmultiple(int x,int y) return x*y/Maxdivisor(x,y);Rational Rational:operator*(Rational r1) int Ndown,Nup,i; Ndown=down*r1.down; Nup=up*r1.up; i=Maxdivisor(Nup,Ndown); return Rational(Nup/i,Ndown/i);void Rational:showNomal() coutup/downendl;main() Rational r1(4,14); Rational r2(5,8); Rational r; coutThe r1 is:endl; r1.showNomal(); coutThe r2 is:endl; r2.showNomal(); r=r1*r2; coutThe r1*r2 is:endl; r.showNomal(); return 0;5、定义包含时、分、秒信息的时间类Time,并实现时间对象的流输入输出,格式为时:分:秒,如23:35:18。(本题总分10分,类结构4分,输入输出函数实现各2分,主函数2分。程序结构完整,有不妥之处,酌情扣分。)#include time.h#include class Time friend ostream & operator(istream &input, Time &t);public:Time(int h=0, int m=0, int s=0);private:int hour, minute, second;Time:Time(int h, int m, int s)hour=h;minute=m;second=s;ostream & operator(ostream &output, Time &t)output t.hour : t.minute : t.second (istream &input, Time &t)char a,b;input t.hour a t.minute b t.second ;return input;int main()Time t;cout Please input the time hh:mm:ss: t;/调用自己定义的运算符重载函数operator(cin,t)cout your input time is: endl;cout t endl;/调用自己定义的运算符重载函数operator(cout,t)return 0;6、对于下面的类MyString,要求重载一些运算符后可以计算表达式:a = b + c,其中a、b、c都是类MyString的对象,+用以实现两个字符串对象的连接。请重载相应的运算符并编写程序测试。(本题总分10分,类结构4分,重载函数实现4分,主函数2分。程序结构完整,有不妥之处,酌情扣分。)class MyString public:MyString(char *s) str = new charstrlen(s)+1;strcpy(str, s);MyString() delete str;private:char *str;7、下面是一个形状类Shape,请编写类Shape的派生类:圆类Circle、三角形类Triangle和矩形类Rectangle,并重定义基类的成员函数使之返回正确的结果(show函数要输出对象的基本信息),然后编写程序测试它们。(本题总分10分,三个类的实现各3分,主函数1分。程序结构完整,有不妥之处,酌情扣分。)class Shape public:/图形的面积double area() return 0.0;/输出对象的信息void show() coutShape Object: endl;#include #include class Circle:public Shapepublic:Circle(double xn,double yn,double rn) x=xn;y=yn;r=rn;double area()return 3.14*r*r; void show() coutCircle Object:endl; cout圆心坐标:xxnyyn半径r=rendl;private:double x,y,z;class Triangle:public Shapepublic:Triangle(double an,double bn,double cn) a=an;b=bn;c=cn;double area()double t=0.5*(a+b+c);return splow(t*(t-a)*(t-b)*(t-c);void show()ccutTriangle:endl;cout三边分别为abcendl;private:double a,b,c;class Rectangle:public Shapepublic:Rectangle(double an,double bn)a=an;b=bn;double area()return a*b;void show()coutRectangle Objectendl;cout长:a宽:bendl;private:double a,b;int main()Circle circle(1.0,1.0,1.0);Triangle triange(1.0,1.0,1.0);Rectangle rectangle(1.0,1.0,1.0);circle.show();triange.show();rectangle.show();return 0;8、修改上题的形状类Shape,并编写计算不同图形的面积总和的函数,该函数在增加新的图形后不必修改。最后编写程序测试它们。(本题总分10分,类结构6分,其中求面积总和函数声明4分;主函数4分。程序结构完整,有不妥之处,酌情扣分。)#include int n=0;class shapevirtual double area() const=0; 虚函数friend addarea(shape &a) 用addarea(shape *)用shape*指针进行动态绑定 nt=a.area();int main()Circle a(1.0,1.0,1.0);Triagle b(1.0,1.0,1.0);Rectangle c(1.0,1.0,1.0);shape * a1;shape * b1;shape * c1;a1=&a;b1=&b;c1=&c;addarea(a1);addarea(b1);addarea(c1);coutn;return 0;9、编写一个函数模板,实现将任意数组的元素倒置,即将数组的首尾逆转。并编写一个使用该模板的main函数,分别实现一个有10个整数(int)的数组和一个有20个实数(float)的数组的倒置。(本题总分10分,模板声明2分,逆转函数实现4分,主函数实现4分。程序结构完整,有不妥之处,酌情扣分。)#include template void change(Tname a,int n)Tnamen b=new namen+1;for(int i=0;in;i+)bi=an-i-1;a=b;delete b;int main()int a10;for(int i=0;i10;i+)ai=i;float b20;for(float n=0.0,n20;n+)bn=n;change(a,10);change(b,20);for(int i=0;i10;i+)coutait;for(i=0;i20;i+)coutbit;return 0;10、编写一个用于英语单词学习的系统,按照如下步骤进行:(1) 编写类Eword,类Eword的对象表示一个英文单词以及它的含义、应用示例;(2) 为类Eword添加显示对象信息的成员函数;(3) 为类Eword添加保存对象到文件以及从文件恢复对象的成员函数;(4) 编写类WordList,以链表或数组的方式管理类Eword的多个对象;(5) 为类WordList添加查询一个单词的成员函数;(6) 为类WordList添加插入一个单词(类Eword的对象)的成员函数;(7) 为类WordList添加逐个显示英语单词的成员函数;(8) 编写程序利用上述两个类借助文件管理自己要学习的英语单词;(9) 对程序进行进一步的优化,如为已经记住的单词添加标记,下次不再显示等,使系统更加实用。(本题总分10分,第1小题至第7小题各一分,第8题2分,第9题1分。程序结构完整,有不妥之处,酌情扣分。)class Eworld public Eword(char *a,char *hanyi,char *shili)word=new charstrlen(a)+1;strcpy(word,a);h=new charstrlen(hanyi+1);strcpy(h,hanyi); s=new charstrlen(shiyi+1);strcpy(s,shili);void show() /显示单词及含义,示例coutwordendl;couthendl;coutsend

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论