08級C(下)上機試卷D_第1頁
08級C(下)上機試卷D_第2頁
08級C(下)上機試卷D_第3頁
08級C(下)上機試卷D_第4頁
08級C(下)上機試卷D_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、東南大學08級C+(下)上機試卷A(考試時間80分鐘,卷面成績100分)學號 姓名 機位號 說明:首先在Z盤建立一個以自己的學號命名的文件夾,用于存放上交的*.CPP文件,考試結(jié)束前根據(jù)機房要求,將這個文件夾傳送到網(wǎng)絡服務器上,注意:提交時只保留文件夾中的CPP文件。一、改錯題 (50分)【要求】調(diào)試程序,修改其中的語法錯誤及少量邏輯錯誤。只能修改、不能增加或刪除整條語句,但可增加少量說明語句和編譯預處理指令。【注意】源程序以“學號f1.cpp”命名,存入自己學號文件夾?!绢}目】以下程序用于構(gòu)造一個空的數(shù)組對象,通過調(diào)用插入函數(shù)建立按升序排列的數(shù)組,并輸出數(shù)組內(nèi)容。 【含錯誤的源程序】#inc

2、lude #includeusing namespace std;class Array int *p; int last; /最后一個元素下標public: Array(int=20); /創(chuàng)建一定長度的空表,給定長度缺省值為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(); /創(chuàng)建數(shù)組 /錯誤行 int t; for(int i=0;it; a.insertOrder(int t); /錯誤行 print(); /輸出數(shù)組 /錯誤行二、編程題(50分) 【注意】源程序以“學號f2.cpp”命名,存入自己學號文件夾?!绢}目】以下程序定義了一個整型數(shù)組類Array,數(shù)組長度為30個元素。數(shù)組的初始化數(shù)據(jù)來源以及程序結(jié)束后數(shù)組的數(shù)據(jù)保存都指向工程文件夾下的文本文件”

4、vdata.txt”。即,創(chuàng)建數(shù)組對象時,在構(gòu)造函數(shù)中讀該取文件獲得數(shù)據(jù)(首次創(chuàng)建對象時文件是打不開的,數(shù)組就沒有初始化值),當程序結(jié)束時,析構(gòu)函數(shù)將數(shù)據(jù)寫入上述文件。【說明】本程序的執(zhí)行流程是,創(chuàng)建數(shù)組對象并初始化,向數(shù)組中添加一些數(shù)據(jù)。請按以上說明和要求將下面程序補充完整,并調(diào)試運行。#include #includeusing namespace std;class Array;ostream& operator(ostream &os,Array &a);class Array int v30; int last; /最后一個元素下標public: Array(); /創(chuàng)建表,從文件中

5、讀取數(shù)據(jù)進行初始化 Array(); /數(shù)據(jù)保存到文件中 void insertAfter(int); /在當前表的最后添加一個元素 friend ostream& operator(ostream &, Array &); /用于直接輸出數(shù)組對象;Array:Array() /此處添加代碼Array:Array() /此處添加代碼void Array:insertAfter(int t) last+; vlast=t;ostream& operator(ostream &os,Array &a) /此處添加代碼void main() Array vector; /創(chuàng)建數(shù)組 int t; co

6、utvector; cout向數(shù)組添加3個數(shù):; for(int i=0;it; vector.insertAfter(t); cout當前數(shù)組內(nèi)容: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、/創(chuàng)建表,從文件中讀取數(shù)據(jù)進行初始化 Array(); /數(shù)據(jù)保存到文件中 void insertAfter(int); /在當前表的最后添加一個元素 friend ostream& operator(ostream &, Array &); /用于直接輸出數(shù)組對象; Array:Array() /此處添加代碼/*for(last=0;last30;last+) vlast=0; 不需要,因為”數(shù)組的初始化數(shù)據(jù)來源以及程序結(jié)束后數(shù)組的數(shù)據(jù)保存都指向工程文件夾下的文本文件”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包含數(shù)字0*/*(2)char a;while(infile.get(a)&last30) vlast=a;last+;錯誤,整數(shù)過長時,char a無法一次讀出一個數(shù)*/Array:Array() /此處添加代碼/*if(!outfile)cout寫入數(shù)據(jù)時文件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; /創(chuàng)建數(shù)組 int t; coutvector; cout向數(shù)組添加3個數(shù):; for(int i=0;it; vector.insertAfter(t); cout當

10、前數(shù)組內(nèi)容:endl; coutvector; 東南大學08級C+(下)上機試卷D(考試時間80分鐘卷面成績100分)學號 姓名 機位號 說明:首先在Z盤建立一個以自己的學號命名的文件夾,用于存放上交的*.CPP文件,考試結(jié)束前根據(jù)機房要求,將這個文件夾傳送到網(wǎng)絡服務器上,注意:提交時只保留文件夾中的CPP文件。一、改錯題 (50分)【要求】調(diào)試程序,修改其中的語法錯誤及少量邏輯錯誤。只能修改、不能增加或刪除整條語句,但可增加少量說明語句和編譯預處理指令?!咀⒁狻吭闯绦蛞浴皩W號f1.cpp”命名,存入自己學號文件夾?!绢}目】以下程序?qū)崿F(xiàn)動態(tài)生成數(shù)據(jù)成員,析構(gòu)函數(shù)用來釋放動態(tài)分配的內(nèi)存,復制構(gòu)造

11、函數(shù)和復制賦值操作操作符實現(xiàn)深復制。 【含錯誤的源程序】#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”命名,存入自己學號文件夾?!绢}目】給產(chǎn)品銷售價定價,請編寫產(chǎn)品類Product。確定產(chǎn)品的銷售價的公式為:產(chǎn)品銷售價 = 原材料價格*1.5 + 加工費*2.0要求:類Product的數(shù)據(jù)成員包括Produ

15、ctName(表示產(chǎn)品名稱,為字符串型)、MatName(表示原材料名,為字符串型)、MatPrice0(表示原材料進價,為整型)、ServicePrice(表示加工費,為整型)、SalePrice(表示商品銷售價,為整型)。類Product的構(gòu)造函數(shù)實現(xiàn)從文本文件Product.txt中讀取產(chǎn)品名稱、原材料名、原材料進價和加工費。類Product的成員函數(shù)CalSalePrice()計算產(chǎn)品的銷售價格。類Product的析構(gòu)函數(shù)將完整的產(chǎn)品信息寫入文本文件Output.txt。寫入的信息包括產(chǎn)品名稱、原材料名稱、原材料價格、加工費、產(chǎn)品銷售價。【注意】 將源程序以文件名“學號f2.cpp”存

16、入Z盤自己的文件夾中。#include#include#includeusing namespace std;class Product string ProductName; /產(chǎn)品名稱 string MatName; / 原材料名稱 int MatPrice0; / 原材料進價 int ServicePrice; /加工費 int SalePrice; /最終定價public: Product(); Product(); void CalSalePrice();Product:Product() /類Product的構(gòu)造函數(shù)實現(xiàn)從文本文件Product.txt中讀取產(chǎn)品名稱、原材料名稱、原

17、材料進價、加工費。Product:Product() /此處添加代碼 /類Product的析構(gòu)函數(shù)將完整的產(chǎn)品信息寫入文本文件Output.txtvoid Product:CalSalePrice() /類Product的成員函數(shù)CalSalePrice()計算產(chǎn)品的銷售價格。用于測試的main函數(shù)如下: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; /產(chǎn)品名稱 string MatName; / 原材料名稱 int MatPrice0; / 原材料進價 int ServicePrice; /加工費 int SalePrice; /最終定價public: Product(); Product(); void CalSalePrice();Product:Product() /類Product的構(gòu)造函數(shù)實現(xiàn)從文本文件Product.txt中讀取產(chǎn)品名稱、原材料名稱、原材料進價、加工費。ifstream infile(Product.txt);if(!infile)coutProductNameMatNameMatPrice0ServicePrice;infile.close();Product:Product() /此處添加代碼 /類Product的析構(gòu)函數(shù)將完整的產(chǎn)品信息寫入文本文件Output.tx

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論