1、东南大学09级C+(下)上机试卷B-答案一、改错题(50分)本题共10个错误,每个错误5分#include #include using namespace std;class studentchar *pName;public:student();student(char *pname);student(student &s);student();student & operator=(student &s);void print();student:student()/去掉voidcoutConstructor;pName=NULL;cout缺省endl;student:student (
2、 char *pname )coutConstructor;pName = new charstrlen(pname);if ( pName ) strcpy ( pName, pname);/pName与pname互换位置coutpNameendl;student:student(student &s) /改为student &scoutCopy Constructor;if(s.pName)int len = strlen( s.pName );pName = new charlen+1; /len改为len+1if ( pName ) strcpy (pName, s.pName);co
3、utpNameendl;else pName=NULL;student:student()coutDestructor;if ( pName ) coutpNameendl;delete pName; /改为delete pName;student & student:operator = ( student &s ) coutCopy Assign operator;delete pName; if ( s.pName ) pName = new charstrlen(s.pName)+1;if ( pName ) strcpy ( pName, s.pName );coutpNameend
4、l;else pName=NULL;return *this; /改为return *this;void student:print( ) /改为void student:print( )if (pName = NULL ) cout NULL endl; else cout pName print();/改为s3-print();delete s3; return; /去掉0二、编程题(50分)每段代码10分,共50分。红字为要求设计的部分。#include #include #include using namespace std;class goods;ostream& operator
5、(istream &is, goods &a);class goodsstringName;/名称intAmount;/数量floatPrice;/单价public:goods (); goods ();/析构函数,将数据保存到文件中bool IsEmpty() return Name=; ;friend ostream& operator(istream &is, goods &a);goods: goods () ifstream file(goodinfo.data);if ( file ) /文件打开成功file *this;file.close();elseName = ;Amoun
6、t = 0;Price = 0;goods: goods ()ofstream file(goodinfo.data);file *this;file.close();ostream& operator (ostream &os, goods &a)os a.Name endl;os a.Amount endl;os a.Price (istream &is, goods &a)if ( is = cin )cout 请输入你的以下信息: endl;cout a.Name;cout a.Amount;cout a.Price;elseis a.Name;is a.Amount;is a.Pri
7、ce;return is;void main()goods g;if ( g.IsEmpty() ) cin g;cout endl;cout 显示商品信息: endl;cout g;if(!datafile) coutproductNamematNamematPrice0servicePrice; ProductName=productName; MatName=matName; MatPrice0=matPrice0; ServicePrice=servicePrice;编程题评分标准: 1. 头文件引用是否正确 2分 2. 主函数:22分 (1)创建文件流对象与要求的文件相对应数否正确 (2分) (2)是否关闭文件 (2分) (3)变量初始化 (2分) (4)产生a数组是否正确 (8分) (5)将a数组是否按要求写入文件 (4分) (6)将符合条件的数及其位置是否按要求写入文件 (4分) 3. fun函数: 26分 (1)求各行平均值,是否正确 (8分) (2)求各列最大值,是否正确 (8分) (3)条件满足判断返回是否正确 (6分) (4)不满足条件返回是否正确 (4分)