资源描述
东南大学08级C++(下)上机试卷A
(考试时间80分钟,卷面成绩100分)
学号 姓名 机位号
说明:首先在Z盘建立一个以自己的学号命名的文件夹,用于存放上交的*.CPP文件,考试结束前根据机房要求,将这个文件夹传送到网络服务器上,注意:提交时只保留文件夹中的CPP文件。
一、改错题 (50分)
【要求】调试程序,修改其中的语法错误及少量逻辑错误。只能修改、不能增加或删除整条语句,但可增加少量说明语句和编译预处理指令。
【注意】源程序以“学号f1.cpp”命名,存入自己学号文件夹。
【题目】以下程序用于构造一个空的数组对象,通过调用插入函数建立按升序排列的数组,并输出数组内容。
【含错误的源程序】
#include<iostream>
#include<iomanip>
using 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 i=0;i<=last;i++) cout<<setw(4)<<p[i];
cout<<endl;
void Array::insertOrder(){ //将关键字插入到数组中某个位置 //错误行
if(last=-1){last++; p[last]=key;} //错误行
int i=last;
while(i>=0&&key<p[i]){
p[i+1]=p[i]; //错误行
i--;
p[i]=key; //错误行
last++;
void main(){
Array a(); //创建数组 //错误行
int t;
for(int i=0;i<10;i++){ //读入10个数据创建一个升序数组
cin>>t;
a.insertOrder(int t); //错误行
print(); //输出数组 //错误行
二、编程题(50分)
【注意】源程序以“学号f2.cpp”命名,存入自己学号文件夹。
【题目】以下程序定义了一个整型数组类Array,数组长度为30个元素。数组的初始化数据来源以及程序结束后数组的数据保存都指向工程文件夹下的文本文件”vdata.txt”。即,创建数组对象时,在构造函数中读该取文件获得数据(首次创建对象时文件是打不开的,数组就没有初始化值),当程序结束时,析构函数将数据写入上述文件。
【说明】本程序的执行流程是,创建数组对象并初始化,向数组中添加一些数据。请按以上说明和要求将下面程序补充完整,并调试运行。
#include<iostream>
#include<fstream>
using namespace std;
class Array;
ostream& operator<<(ostream &os,Array &a);
class Array{
int v[30];
int last; //最后一个元素下标
public:
Array(); //创建表,从文件中读取数据进行初始化
~Array(); //数据保存到文件中
void insertAfter(int); //在当前表的最后添加一个元素
friend ostream& operator<<(ostream &, Array &); //用于直接输出数组对象
Array::Array(){
//此处添加代码
Array::~Array(){
//此处添加代码
void Array::insertAfter(int t){
last++;
v[last]=t;
ostream& operator<<(ostream &os,Array &a){
//此处添加代码
void main(){
Array vector; //创建数组
int t;
cout<<vector;
cout<<"向数组添加3个数:";
for(int i=0;i<3;i++){ //向数组中添加3个数据
cin>>t;
vector.insertAfter(t);
cout<<"当前数组内容:"<<endl;
cout<<vector;
【提醒】上传的学号文件夹中只需包含f1.cpp、f2.cpp及vdata.txt三个文件即可,其余文件上传前尽可删除。
东南大学08级C++(下)上机试卷D
(考试时间80分钟卷面成绩100分)
学号 姓名 机位号
说明:首先在Z盘建立一个以自己的学号命名的文件夹,用于存放上交的*.CPP文件,考试结束前根据机房要求,将这个文件夹传送到网络服务器上,注意:提交时只保留文件夹中的CPP文件。
一、改错题 (50分)
【要求】调试程序,修改其中的语法错误及少量逻辑错误。只能修改、不能增加或删除整条语句,但可增加少量说明语句和编译预处理指令。
【注意】源程序以“学号f1.cpp”命名,存入自己学号文件夹。
【题目】以下程序实现动态生成数据成员,析构函数用来释放动态分配的内存,复制构造函数和复制赋值操作操作符实现深复制。
【含错误的源程序】
#include <iostream>
#include <cstring>
using namespace std;
class student
char *pName;
public:
student( );
student( char *pname, int len ); //错误1
student( student &s );
~student( );
student & operator = ( student &s );
} //错误2
student::student( )
cout >> "Constructor"; //错误3
pName = NULL;
cout << "默认" << endl;
student::student( char *pname )
cout << "Constructor";
pName = new char[strlen(pname)+1];
if ( pName ) strcpy( pName, pname );
cout << pName << endl;
student::student( student s ) //错误4
cout<<"Copy Constructor";
if( s.pName )
{ int len = strlen(s.pName);
pName = new char(len+1); //错误5
if ( pName ) strcpy( pName, s.pName );
cout << pName << endl;
else pName = NULL;
student::~student()
cout << "Destructor";
if ( pName ) cout << pName << endl;
delete PName; //错误6
student & Student::operator = ( student &s ) //错误7
cout << "Copy Assign operator";
delete[] pName;
if(s.pName)
len = strlen(s.pName); //错误8
pName = new char[len]; //错误9
if( pName ) strcpy( pName, s.pName );
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的数据成员包括ProductName(表示产品名称,为字符串型)、MatName(表示原材料名,为字符串型)、MatPrice0(表示原材料进价,为整型)、ServicePrice(表示加工费,为整型)、SalePrice(表示商品销售价,为整型)。
类Product的构造函数实现从文本文件Product.txt中读取产品名称、原材料名、原材料进价和加工费。
类Product的成员函数CalSalePrice()计算产品的销售价格。
类Product的析构函数将完整的产品信息写入文本文件Output.txt。写入的信息包括产品名称、原材料名称、原材料价格、加工费、产品销售价。
【注意】 将源程序以文件名“学号f2.cpp”存入Z盘自己的文件夹中。
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
class Product
string ProductName; //产品名称
string MatName; // 原材料名称
int MatPrice0; // 原材料进价
int ServicePrice; //加工费
int SalePrice; //最终定价
public:
Product();
~Product();
void CalSalePrice();
Product::Product()
//类Product的构造函数实现从文本文件Product.txt中读取产品名称、原材料名称、原材料进价、加工费。
Product::~Product()
{ //此处添加代码
//类Product的析构函数将完整的产品信息写入文本文件Output.txt
void Product::CalSalePrice()
//类Product的成员函数CalSalePrice()计算产品的销售价格。
用于测试的main函数如下:
int main()
Product pro;
pro.CalSalePrice();
return 0;
【提醒】上传的学号文件夹中只需包含f1.cpp、f2.cpp及Output.txt三个文件即可,其余文件上传前尽可删除。
第 12 页
展开阅读全文