東南大學(xué)08級(jí)C++試卷B(電類第一場)機(jī)試試卷-和答案解析_第1頁
東南大學(xué)08級(jí)C++試卷B(電類第一場)機(jī)試試卷-和答案解析_第2頁
東南大學(xué)08級(jí)C++試卷B(電類第一場)機(jī)試試卷-和答案解析_第3頁
東南大學(xué)08級(jí)C++試卷B(電類第一場)機(jī)試試卷-和答案解析_第4頁
東南大學(xué)08級(jí)C++試卷B(電類第一場)機(jī)試試卷-和答案解析_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上東南大學(xué)08級(jí)C+(下)上機(jī)試卷B(考試時(shí)間80分鐘,卷面成績100分)學(xué)號(hào) 姓名 機(jī)位號(hào) 說明:首先在Z盤建立一個(gè)以自己的學(xué)號(hào)命名的文件夾,用于存放上交的*.CPP文件,考試結(jié)束前根據(jù)機(jī)房要求,將這個(gè)文件夾傳送到網(wǎng)絡(luò)服務(wù)器上,注意:提交時(shí)只保留文件夾中的CPP文件。一、改錯(cuò)題 (50分)【要求】調(diào)試程序,修改其中的語法錯(cuò)誤及少量邏輯錯(cuò)誤。只能修改、不能增加或刪除整條語句,但可增加少量說明語句和編譯預(yù)處理指令。【注意】源程序以“學(xué)號(hào)f1.cpp”命名,存入自己學(xué)號(hào)文件夾。【題目】以下程序?qū)崿F(xiàn)了冒泡排序算法。 【含錯(cuò)誤的源程序】/*冒泡排序*/錯(cuò)誤1using name

2、space std /錯(cuò)誤2void BubbleSort(int slist,int n) /錯(cuò)誤3bool noswap;int i,j;int temp;for (i=0;i<n;i+) /錯(cuò)誤4noswap=true; /未交換標(biāo)志為真for(j=n-1;j>i;j-) /從下往上冒泡if(slistj<slistj-1)temp=slistj;slistj=slistj-1;slistj-1=temp;noswap=false;if(!noswap) break; /本趟無交換,則終止算法。/錯(cuò)誤5int main()int h=10; /錯(cuò)誤6int i;int

3、listh=5,8,7,9,6,12,11,15,3,10;cout>>"未排序數(shù)組:"<<endl; /錯(cuò)誤7for(i=0;i<h;i+) cout<<listi<<'t' /錯(cuò)誤8BubbleSort(list,h);cout<<"已排序數(shù)組:"<<endl();/錯(cuò)誤9for(i=0;i<h;i+) cout<<listi<<'t' /錯(cuò)誤10return 0;二、編程題(50分) 【注意】源程序以“學(xué)號(hào)f2

4、.cpp”命名,存入自己學(xué)號(hào)文件夾?!绢}目】創(chuàng)建類inventory,該類的定義如下,在創(chuàng)建inventory對(duì)象時(shí),會(huì)調(diào)用默認(rèn)的構(gòu)造函數(shù)inventory()從“mydatafile.txt”文件中讀取數(shù)據(jù)來初始化類的成員;在撤消inventory對(duì)象時(shí),會(huì)調(diào)用默認(rèn)的析構(gòu)函數(shù)inventory()將類的成員保存至工程目錄下“mydatafile.txt”文件中。 請(qǐng)完成該類的構(gòu)造函數(shù)、析構(gòu)函數(shù)以及SetParam()函數(shù)的定義。并用main主函數(shù)來驗(yàn)證該類的上述功能?!咀⒁狻?將源程序以文件名“學(xué)號(hào)f2.cpp”存入Z盤自己的文件夾中。class inventorystring Descri

5、ption;string No;int Quantity;double Cost;double Retail;fstream datafile;Public:inventory(); /從文件中讀取數(shù)據(jù)進(jìn)行初始化inventory();/數(shù)據(jù)保存到文件中void SetParam(string ,string,int,double,double);/為類的成員數(shù)據(jù)賦值;inventory:inventory()/此處添加代碼inventory:inventory()/此處添加代碼void inventory:SetParam(string description ,string number,

6、int quantity, double cost,double retail)/完成成員函數(shù)SetParam()定義,實(shí)現(xiàn)為類的成員數(shù)據(jù)賦值 用于測試的main函數(shù)如下:int main()inventory goods;goods.SetParam("家電","10",100,2000,3500);return 0;【提醒】上傳的學(xué)號(hào)文件夾中只需包含f1.cpp、f2.cpp及mydatafile.txt三個(gè)文件即可,其余文件上傳前盡可刪除。答案解析:一 改錯(cuò)題#include<iostream>using namespace std ;

7、void BubbleSort(int slist,int n) bool noswap;int i,j;int temp;for (i=0;i<n;i+) noswap=true; /未交換標(biāo)志為真for(j=n-1;j>i;j-) /從下往上冒泡if(slistj<slistj-1)temp=slistj;slistj=slistj-1;slistj-1=temp;noswap=false;if(noswap) break; /本趟無交換,則終止算法。/錯(cuò)誤5int main()const int h=10; int i;int listh=5,8,7,9,6,12,11

8、,15,3,10;cout<<"未排序數(shù)組:"<<endl; for(i=0;i<h;i+) cout<<listi<<'t' BubbleSort(list,h);cout<<"已排序數(shù)組:"<<endl;for(i=0;i<h;i+) cout<<listi<<'t' return 0;二 編程題#include<iostream>#include<fstream>#include<

9、iomanip>using namespace std;class inventorystring Description;string No;int Quantity;double Cost;double Retail;fstream datafile;public:inventory(); /從文件中讀取數(shù)據(jù)進(jìn)行初始化inventory();/數(shù)據(jù)保存到文件中void SetParam(string ,string,int,double,double);/為類的成員數(shù)據(jù)賦值;inventory:inventory()fstream datafile("d:mydatafil

10、e.txt",ios:in);if(!datafile=0)string description;string no;int quantity;double cost;double retail;while(!datafile.eof()datafile>>description;datafile>>no;datafile>>quantity;datafile>>cost;datafile>>retail;Description=description;No=no;Quantity=quantity;Cost=cost;Re

11、tail=retail;inventory:inventory()fstream datafile("d:mydatafile.txt",ios:out);datafile<<Description<<setw(3);datafile<<No<<setw(3);datafile<<Quantity<<setw(3);datafile<<Cost<<setw(3);datafile<<Retail<<endl;datafile.close();void inventory:SetParam(string description ,string number,int quantity, double cost,double retail)Description=description;/完成成員函數(shù)SetParam()

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論