2008年级上机考试试卷bc下答案_第1页
2008年级上机考试试卷bc下答案_第2页
2008年级上机考试试卷bc下答案_第3页
2008年级上机考试试卷bc下答案_第4页
2008年级上机考试试卷bc下答案_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、参考信息学院 08 年级计算机科学基础 II 上机试卷 B时间 70 分钟卷面成绩 100 分)(学号 机位号说明:首先在 Z 盘建立一个以自己的学号命名的文件夹,用于存放上交的*.CPP 文件,结束前根据机房要求,将这个文件夹传送到网络服务器上,注意:提交时只保留文件夹中的 CPP 文件。一、改错题 (50 分)【要求】调试程序,修改其中的语法错误及少量逻辑错误。只能修改、不能增加或删除整条语句,但可增加少量说明语句和编译预处理指令。【注意】源程序以“学号 f1.cpp”命名,存入自己学【题目】以下程序实现动态生成数据成员,析构函数用来夹。动态分配的内存,构造函数和赋值操作操作符实现深。【含

2、错误的源程序】#include #include using namespatd; class studentchar *pName; public:student( );student( char *pname, student( student &s );student( );len ); /错 1,该句改为:student( char *pname );student & operator = ( student &s );/错 2,该句改为: ;student:student( )cout Constructor; pName = NULL;cout 默认 endl;/错 3,该句改为

3、:cout Constructor;student:student( char *pname )cout Constructor;pName = new charstrlen(pname)+1; if ( pName ) strcpy( pName, pname ); cout pName endl;student:student( student s )/错 4,该句改为:student:student( student &s )coutCopy Constructor; if( s.pName )len = strlen(s.pName);pName = new char(len+1);/

4、错 5,该句改为:pName = new charlen+1;if ( pName ) strcpy( pName, s.pName ); cout pName endl;else pName = NULL;student:student()cout Destructor;if ( pName ) cout pName endl;delete PName;/错 6,该句改为: delete PName;student & Student:operator = ( student &s )/错 7,上一行改为:student & student:operator = ( student &s )

5、cout Copy Assign operator; delete pName;if(s.pName)len = strlen(s.pName); pName = new charlen;/错 8,该句改为:len = strlen(s.pName);/错 9,该句改为:pName = new charlen+1;if( pName ) strcpy( pName, s.pName ); cout pName endl;else pNa return *this;LL;main(void)student s1(范英明), s2( student s3(s1););student *s4 = n

6、ew student(s2);delete s3; return 0;/错 10,该句改为:改为 delete s4;二、编程题(50 分)【注意】源程序以“学号 f2.cpp”命名,存入自己学【题目】夹。给产品销售价定价,请编写产品类 Product。确定产品的销售价的公式为:产品销售价 = 原材料价格*1.5 + 加工费*2.0要求:类 Product 的数据成员包括ProductName(表示产品名称,为字符串型)、MatName(表示原材料名,为字符串型)、MatPrice0(表示原材料进价,为整型)、ServicePrice(表示加工费,为整型)、SalePrice(表示商品销售价,

7、为整型)。类 Product 的构造函数实现从文本文件 Product.txt 中类 Product 的成员函数 CalSalePrice()计算产品的产品名称、原材料名、原材料进价和加工费。类 Product 的析构函数将完整的产品信息写入文本文件 Output.txt。写入的信息包括产品名称、原材料名称、原材料价格、加工费、产品销售价。【注意】 将源程序以文件名“学号 f2.cpp”存入 Z 盘自己的文件夹中。class Productstring ProductName; string MatName;/产品名称/ 原材料名称原材料进价/加工费/最终定价MatPrice0; Servic

8、ePrice; SalePrice;/public:Product();Product();void CalSalePrice();Product:Product()/类Product的构造函数实现从文本文件Product.txt中/以下需要代码:Product:Product()产品名称、原材料名称、原材料进价、加工费。/类Product的析构函数将完整的产品信息写入文本文件Output.txt/以下需要代码:void Product:CalSalePrice()/类Product的成员函数CalSalePrice()计算产品的/以下需要代码:/用于测试的 main 函数如下:main()P

9、roduct pro; pro.CalSalePrice(); return 0;/*调试程序时可先建立数据文件 Product.txt, 内容为:椅子木头。2050这样程序运行后产生数据文件Output.txt,内容将为:产品名称:椅子原材料名称:木头原材料进价:20加工费:50最终定价:130*/夹中只需包含 f1.cpp、f2.cpp 及 Output.txt 三个文件即可,其余文件上传前尽可删除。【提醒】上传的学【参考】#include #include #includeusing namespa class Producttd;string ProductName; string M

10、atName;/产品名称/ 原材料名称原材料进价/加工费/最终定价MatPrice0; ServicePrice; SalePrice;/public:Product();Product();void CalSalePrice();Product:Product()/类Product的构造函数实现从文本文件Product.txt中/以下红颜色的为添加的代码 ifstream infile(Product.txt);if(!infile)cout打开失败!ProductName;infileMatName; infileMatPrice0; infileServicePrice; infile.

11、close();Product:Product()产品名称、原材料名称、原材料进价、加工费。/类Product的析构函数将完整的产品信息写入文本文件Output.txt/以下红颜色的为添加的代码 ofstream outf(Output.txt);if(!outf)cout打开失败!endl;return; outf产品名称:ProductNameendl; outf原材料名称:MatNameendl; outf原材料进价:MatPrice0endl; outf加工费:ServicePriceendl; outf最终定价:SalePriceendl; outf.close();void Prod

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论