1、东南大学08级C+(下)上机试卷A(考试时间80分钟,卷面成绩100分)学号 姓名 机位号 说明:首先在Z盘建立一个以自己的学号命名的文件夹,用于存放上交的*.CPP文件,考试结束前根据机房要求,将这个文件夹传送到网络服务器上,注意:提交时只保留文件夹中的CPP文件。一、改错题 (50分)【要求】调试程序,修改其中的语法错误及少量逻辑错误。只能修改、不能增加或删除整条语句,但可增加少量说明语句和编译预处理指令。【注意】源程序以“学号f1.cpp”命名,存入自己学号文件夹。【题目】以下程序用于构造一个空的数组对象,通过调用插入函数建立按升序排列的数组,并输出数组内容。 【含错误的源程序】#inc
2、lude #includeusing namespace std;class Array int *p; int last; /最后一个元素下标public: Array(int=20); /创建一定长度的空表,给定长度缺省值为20 Array(); void insertOrder(int); /在升序表中插入一个元素,使之仍然升序 void print();void Array:Array(int max) /错误行 last=-1; p=new int; /初始化为给定长度 /错误行Array:Array()delete p; /错误行void print() /错误行 for(int
3、i=0;i=last;i+) coutsetw(4)pi; cout=0&keypi) pi+1=pi; /错误行 i-; pi=key; /错误行 last+;void main() Array a(); /创建数组 /错误行 int t; for(int i=0;it; a.insertOrder(int t); /错误行 print(); /输出数组 /错误行二、编程题(50分) 【注意】源程序以“学号f2.cpp”命名,存入自己学号文件夹。【题目】以下程序定义了一个整型数组类Array,数组长度为30个元素。数组的初始化数据来源以及程序结束后数组的数据保存都指向工程文件夹下的文本文件”
4、vdata.txt”。即,创建数组对象时,在构造函数中读该取文件获得数据(首次创建对象时文件是打不开的,数组就没有初始化值),当程序结束时,析构函数将数据写入上述文件。【说明】本程序的执行流程是,创建数组对象并初始化,向数组中添加一些数据。请按以上说明和要求将下面程序补充完整,并调试运行。#include #includeusing namespace std;class Array;ostream& operator(ostream &os,Array &a);class Array int v30; int last; /最后一个元素下标public: Array(); /创建表,从文件中
5、读取数据进行初始化 Array(); /数据保存到文件中 void insertAfter(int); /在当前表的最后添加一个元素 friend ostream& operator(ostream &, Array &); /用于直接输出数组对象;Array:Array() /此处添加代码Array:Array() /此处添加代码void Array:insertAfter(int t) last+; vlast=t;ostream& operator(ostream &os,Array &a) /此处添加代码void main() Array vector; /创建数组 int t; co
6、utvector; cout向数组添加3个数:; for(int i=0;it; vector.insertAfter(t); cout当前数组内容:endl; coutvector; 【提醒】上传的学号文件夹中只需包含f1.cpp、f2.cpp及vdata.txt三个文件即可,其余文件上传前尽可删除。答案#include #includeusing namespace std;class Array;ostream& operator(ostream &os,Array &a);class Array int v30; int last; /最后一个元素下标public: Array();
7、/创建表,从文件中读取数据进行初始化 Array(); /数据保存到文件中 void insertAfter(int); /在当前表的最后添加一个元素 friend ostream& operator(ostream &, Array &); /用于直接输出数组对象; Array:Array() /此处添加代码/*for(last=0;last30;last+) vlast=0; 不需要,因为”数组的初始化数据来源以及程序结束后数组的数据保存都指向工程文件夹下的文本文件”ifstream infile(vdata.txt);if(!infile)coutvlast) last+; /不可写成w
8、hile(infilevlast+);原因不明?last-;infile.close();/*(1) infilevlast;while(vlast!=0)last+;infilevlast;last-;错误,可能vdata.txt包含数字0*/*(2)char a;while(infile.get(a)&last30) vlast=a;last+;错误,整数过长时,char a无法一次读出一个数*/Array:Array() /此处添加代码/*if(!outfile)cout写入数据时文件vdata.txt打开失败n;exit(1);不需要*/ ofstream outfile(vdata.
9、txt);for(int i=0;i=last;i+) outfilevi ;outfile.close();void Array:insertAfter(int t) last+; vlast=t;ostream& operator(ostream &os,Array &a) /此处添加代码for(int i=0;i=a.last;i+) osa.vi ;return os;void main() Array vector; /创建数组 int t; coutvector; cout向数组添加3个数:; for(int i=0;it; vector.insertAfter(t); cout当
10、前数组内容:endl; coutvector; 东南大学08级C+(下)上机试卷D(考试时间80分钟卷面成绩100分)学号 姓名 机位号 说明:首先在Z盘建立一个以自己的学号命名的文件夹,用于存放上交的*.CPP文件,考试结束前根据机房要求,将这个文件夹传送到网络服务器上,注意:提交时只保留文件夹中的CPP文件。一、改错题 (50分)【要求】调试程序,修改其中的语法错误及少量逻辑错误。只能修改、不能增加或删除整条语句,但可增加少量说明语句和编译预处理指令。【注意】源程序以“学号f1.cpp”命名,存入自己学号文件夹。【题目】以下程序实现动态生成数据成员,析构函数用来释放动态分配的内存,复制构造
11、函数和复制赋值操作操作符实现深复制。 【含错误的源程序】#include #include using namespace std;class student char *pName;public: student( ); student( char *pname, int len ); /错误1 student( student &s ); student( ); student & operator = ( student &s ); /错误2student:student( ) cout Constructor; /错误3 pName = NULL; cout 默认 endl;stude
12、nt:student( char *pname ) cout Constructor; pName = new charstrlen(pname)+1; if ( pName ) strcpy( pName, pname ); cout pName endl;student:student( student s ) /错误4 coutCopy Constructor; if( s.pName ) int len = strlen(s.pName); pName = new char(len+1); /错误5 if ( pName ) strcpy( pName, s.pName ); cout
13、 pName endl; else pName = NULL;student:student() cout Destructor; if ( pName ) cout pName endl; delete PName; /错误6student & Student:operator = ( student &s ) /错误7 cout Copy Assign operator; delete pName; if(s.pName) len = strlen(s.pName); /错误8 pName = new charlen; /错误9 if( pName ) strcpy( pName, s.p
14、Name ); cout pName endl; else pName=NULL; return *this;int main(void) student s1(范英明), s2(沈俊); student s3(s1); student *s4 = new student(s2); delete s3; /错误10 return 0;二、编程题(50分) 【注意】源程序以“学号f2.cpp”命名,存入自己学号文件夹。【题目】给产品销售价定价,请编写产品类Product。确定产品的销售价的公式为:产品销售价 = 原材料价格*1.5 + 加工费*2.0要求:类Product的数据成员包括Produ
15、ctName(表示产品名称,为字符串型)、MatName(表示原材料名,为字符串型)、MatPrice0(表示原材料进价,为整型)、ServicePrice(表示加工费,为整型)、SalePrice(表示商品销售价,为整型)。类Product的构造函数实现从文本文件Product.txt中读取产品名称、原材料名、原材料进价和加工费。类Product的成员函数CalSalePrice()计算产品的销售价格。类Product的析构函数将完整的产品信息写入文本文件Output.txt。写入的信息包括产品名称、原材料名称、原材料价格、加工费、产品销售价。【注意】 将源程序以文件名“学号f2.cpp”存
16、入Z盘自己的文件夹中。#include#include#includeusing namespace std;class Product string ProductName; /产品名称 string MatName; / 原材料名称 int MatPrice0; / 原材料进价 int ServicePrice; /加工费 int SalePrice; /最终定价public: Product(); Product(); void CalSalePrice();Product:Product() /类Product的构造函数实现从文本文件Product.txt中读取产品名称、原材料名称、原
17、材料进价、加工费。Product:Product() /此处添加代码 /类Product的析构函数将完整的产品信息写入文本文件Output.txtvoid Product:CalSalePrice() /类Product的成员函数CalSalePrice()计算产品的销售价格。用于测试的main函数如下:int main() Product pro; pro.CalSalePrice(); return 0;【提醒】上传的学号文件夹中只需包含f1.cpp、f2.cpp及Output.txt三个文件即可,其余文件上传前尽可删除。答案:#include#include#includeusing n
18、amespace std;class Product string ProductName; /产品名称 string MatName; / 原材料名称 int MatPrice0; / 原材料进价 int ServicePrice; /加工费 int SalePrice; /最终定价public: Product(); Product(); void CalSalePrice();Product:Product() /类Product的构造函数实现从文本文件Product.txt中读取产品名称、原材料名称、原材料进价、加工费。ifstream infile(Product.txt);if(!
19、infile)coutProductNameMatNameMatPrice0ServicePrice;infile.close();Product:Product() /此处添加代码 /类Product的析构函数将完整的产品信息写入文本文件Output.txtofstream outfile;outfile.open(Output.txt);outfileProductNameMatNameMatPrice0ServicePriceSalePrice;outfile.close();void Product:CalSalePrice() /类Product的成员函数CalSalePrice()计算产品的销售价格。SalePrice=MatPrice0*1.5+ServicePrice*2.0;/用于测试的main函数如下:int main() Product pro; pro.CalSalePrice(); return 0;