面向对象程序设计试卷A答案和评分标准.docVIP

  • 13
  • 0
  • 约 14页
  • 2017-04-21 发布于北京
  • 举报

面向对象程序设计试卷A答案和评分标准.doc

《面向对象程序设计》试卷A答案及评分标准 本试卷共10个题,分别按以下标准评分,最后根据整个答题的情况,从程序设计风格的角度给予0-5分的附加分。 1、编写程序,将从键盘读入的所有大小写字母写入名为a.txt的文件中(遇EOF结束)。(本题总分10分,fopen函数使用妥当4分,读入过程正确4分,关闭文件2分。程序结构完整,有不妥之处,酌情扣分。) #include stdio.h main ( ) { FILE *fptr; fptr = fopen(a.txt,w); if (fptr==NULL) return 0; char a; a=getchar( ); while(a!=EOF ) { if(a=’a’ a=’z’ || a=’A’ a=’Z’) fputc(a,fptr); a = getchar(); } fclose(fptr); return 0; } 2、定义一个矩形类Rectangle,并在main函数中用它声明一个矩形对象,然后利用接口 设置该矩形对象的长和宽、计算面积并输出。(本题总分10分,类结构2分,设置矩阵对象的长与高各1分,计算面积函数2分,输出函数2分,主函数2分。程序结构完整,有不妥之处,酌情扣分。) #include stdio.h #include iostream.h class Rectangle { public: 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:endl; cinlen; coutWidth:endl; cinwid; 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 stdio.h #include iostream.h 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) { coutNo Item in the stack!\n; return 0; } Node *temp = head; head = head-next; int i = temp-item; delete temp; return i; } int IStack::getItemNum() { return size; } main() { IStack istack; istack .push(6); istack .

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档