东南大学08级C++(下)上机试卷B
(考试时间80分钟,卷面成绩100分)
学号姓名机位号
说明:首先在Z盘建立一个以自己的学号命名的文件夹,用于存放上交的*.CPP文件,考试结束前根据机房要求,将这个文件夹传送到网络服务器上,注意:提交时只保留文件夹中的CPP文件。
一、改错题(50分)
?
【要求】调试程序,修改其中的语法错误及少量逻辑错误。只能修改、不能增加或删除整条语句,但可增加少量说明语句和编译预处理指令。
【注意】源程序以“学号”命名,存入自己学号文件夹。【题目】以下程序实现了冒泡排序算法。
【含错误的源程序】
/*冒泡排序*/
//错误1
using namespace std //错误2
void BubbleSort(int slist,int n){ //错误3
!
bool noswap;
int i,j;
int temp;
for (i=0;i noswap=true; //未交换标志为真 for(j=n-1;j>i;j--){ //从下往上冒泡if(slist[j] temp=slist[j]; ^ slist[j]=slist[j-1]; slist[j-1]=temp; noswap=false; } } if(!noswap) break; //本趟无交换,则终止算法。//错误5 } } 、 int main(){ int h=10; //错误6 int i; int list[h]={5,8,7,9,6,12,11,15,3,10}; cout>>"未排序数组:"< for(i=0;i BubbleSort(list,h); cout<<"已排序数组:"< 、 for(i==0;i } 二、编程题(50分) 【注意】源程序以“学号”命名,存入自己学号文件夹。【题目】创建类inventory,该类的定义如下,在创建inventory对象时,会调用默认的构造函数inventory()从“”文件中读取数据来初始化类的成员;在撤消inventory 对象时,会调用默认的析构函数~inventory()将类的成员保存至工程目录下“”文件中。请完成该类的构造函数、析构函数以及SetParam()函数的定义。并用main主函数来验证该类的上述功能。 【注意】将源程序以文件名“学号”存入Z盘自己的文件夹中。 ? class inventory { string Description; string No; int Quantity; double Cost; double Retail; fstream datafile; > Public: inventory(); //从文件中读取数据进行初始化 ~inventory(); //数据保存到文件中 void SetParam(string ,string,int,double,double);//为类的成员数据赋值 }; inventory::inventory() { //此处添加代码 ~ } inventory::~inventory() { //此处添加代码 } void inventory::SetParam(string description ,string number,int quantity, double cost,double retail) { / //完成成员函数SetParam()定义,实现为类的成员数据赋值 } 用于测试的main函数如下: int main() { inventory goods; ("家电","10",100,2000,3500); return 0; ( } 【提醒】上传的学号文件夹中只需包含、及三个文件即可,其余文件上传前尽可删除。 答案解析: 一.改错题 #include — using namespace std ; void BubbleSort(int slist[],int n) { bool noswap; int i,j; int temp; for (i=0;i { # noswap=true; //未交换标志为真 for(j=n-1;j>i;j--) { //从下往上冒泡 if(slist[j] { temp=slist[j]; slist[j]=slist[j-1]; slist[j-1]=temp; % noswap=false; } } if(noswap) break; //本趟无交换,则终止算法。//错误5 } } int main() { $ const int h=10; int i; int list[h]={5,8,7,9,6,12,11,15,3,10}; cout<<"未排序数组:"< for(i=0;i BubbleSort(list,h); cout<<"已排序数组:"< for(i=0;i ] return 0; } 二.编程题 #include #include #include using namespace std; class inventory 《 { string Description; string No; int Quantity; double Cost; double Retail; fstream datafile; public: < inventory(); //从文件中读取数据进行初始化 ~inventory(); //数据保存到文件中 void SetParam(string ,string,int,double,double);//为类的成员数据赋值 }; inventory::inventory() { fstream datafile("d:\\",ios::in); if(!datafile==0) $ { string description; string no; int quantity; double cost; double retail; while(!()) { … datafile>>description; datafile>>no; datafile>>quantity; datafile>>cost; datafile>>retail; } Description=description; No=no; $ Quantity=quantity; Cost=cost; Retail=retail; } } inventory::~inventory() { fstream datafile("d:\\",ios::out); > datafile< datafile< datafile< datafile< datafile< (); } void inventory::SetParam(string description ,string number,int quantity, double cost,double retail) { Description=description; //完成成员函数SetParam()定义,实现为类的成员数据赋值 No=number; Quantity=quantity; Cost=cost; Retail=retail; } int main() { inventory goods; ("家电","10",100,2000,3500); return 0; } //元算符重载存在问题