C++程序設(shè)計(jì)課后習(xí)題:第11章 輸入_第1頁
C++程序設(shè)計(jì)課后習(xí)題:第11章 輸入_第2頁
C++程序設(shè)計(jì)課后習(xí)題:第11章 輸入_第3頁
C++程序設(shè)計(jì)課后習(xí)題:第11章 輸入_第4頁
C++程序設(shè)計(jì)課后習(xí)題:第11章 輸入_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、第11章 輸入/輸出流11.1 選擇題1在下列流類中,可以用于處理文件的是( D )。(A)ios(B)iostream(C)strstream (D)fstream 2在下列選項(xiàng)中,( B )是istream類的對(duì)象。(A)cerr(B)cin(C)clog(D)cout3用于處理字符串流的是( A )。(A)strstream(B)ios(C)fstream(D)iostream4能夠從輸入流中提取指定長度的字節(jié)序列的函數(shù)是( C )。(A)get(B)getline(C)read (D)cin5能夠把指定長度的字節(jié)序列插入到輸出流中的函數(shù)是( B )。(A)put(B)write(C)c

2、out (D)print6getline函數(shù)的功能是從輸入流中讀?。?C )。(A)一個(gè)字符(B)當(dāng)前字符(C)一行字符(D)指定若干個(gè)字節(jié)7在下列選項(xiàng)中,用于清除基數(shù)格式位設(shè)置以十六進(jìn)制數(shù)輸出的語句是( B )。(A)cout<<setf( ios:dec, ios:basefield );(B)cout<<setf( ios:hex, ios:basefield );(C)cout<<setf( ios:oct, ios:basefield );(D)cin>>setf( ios:hex, ios:basefield );8下列格式控制符,既

3、可以用于輸入,又可以用于輸出的是( A )。(A)setbase(B)setfill(C)setprecision(D)setw9要求打開文件 D:file.dat,并能夠?qū)懭霐?shù)據(jù),正確的語句是( D )。(A)ifstream infile("D:file.dat", ios:in );(B)ifstream infile("D:file.dat", ios:out ); (C)ofstream outfile("D:file.dat", ios:in );(D)fstream infile("D:file.dat&quo

4、t;, ios:in|ios:out );10能實(shí)現(xiàn)刪除文件功能的語句是( A )。(A)ofstream fs("date.dat", ios:trunc );(B)ifstream fs("date.dat", ios:trunc );(C)ofstream fs("date.dat", ios:out );(D)ifstream fs("date.dat", ios:in );11設(shè)已定義浮點(diǎn)型變量data,以二進(jìn)制代碼方式把data的值寫入輸出文件流對(duì)象outfile中,正確的語句是( C )。(A)out

5、file.write(double *) &data, sizeof(double);(B)outfile.write(double *) &data, data);(C)outfile.write(char *) &data, sizeof(double);(D)outfile.write(char *) &data, data);12二進(jìn)制數(shù)據(jù)文件流fdat讀指針移到文件頭的語句是( A )。(A)fdat.seekg( 0, ios:beg);(B)fdat.tellg( 0, ios:beg ); (C)fdat.seekp( 0, ios:beg);(

6、D)fdat.tellp( 0, ios:beg );11.2 閱讀下列程序,寫出運(yùn)行結(jié)果1 #include<iostream>using namespace std;int main() double x = 123.456;cout.width( 10 );cout.setf( ios:dec, ios:basefield );cout<<x<<endl;cout.setf( ios:left );cout<<x<<endl;cout.width( 15 );cout.setf( ios:right , ios:left );c

7、out<<x<<endl;cout.setf( ios:showpos );cout<<x<<endl;cout<<-x<<endl;cout.setf( ios:scientific );cout<<x<<endl; 【解答】 123.456 123.456 123.456 +123.456 -123.456 +1.234560e+0022 #include<iostream>using namespace std;int main() double x = 123.45678; co

8、ut.width( 10 ); cout<<( "#" ); cout<<x<<endl; cout.precision( 5 ); cout<<x<<endl; cout.setf( ios:showpos ); cout<<x<<endl; cout.setf( ios:scientific ); cout<<x<<endl; 【解答】#123.457123.46+123.46+1.23457e+0023 #include<iostream>#incl

9、ude <iomanip>using namespace std;int main() double x = 123.456789; cout<<setiosflags( ios:fixed | ios:showpos )<<x<<endl; cout<<setw( 12 )<<setiosflags( ios:right ); cout<<setprecision( 3 )<<-x<<endl; cout<<resetiosflags( ios:fixed | ios:sh

10、owpos )<<setiosflags( ios:scientific ); cout<<setprecision( 5 )<<x<<endl;【解答】+123.456789 -123.457 1.23457e+0024寫出文件D:f1.txt中的內(nèi)容和屏幕顯示的結(jié)果。#include<iostream>#include<fstream>using namespace std;int main() int i; ofstream ftxt1; ftxt1.open( "D:f1.txt", ios:o

11、ut ); for( i=1; i<10; i+ ) ftxt1<<i<<' ' ftxt1.close(); ifstream ftxt2; ftxt2.open( "D:f1.txt", ios:in ); while( !ftxt2.eof() ) ftxt2>>i>>i; cout<<i<<endl; 【解答】D:f1.txt:1 2 3 4 5 6 7 8 9屏幕顯示: 2 4 6 8 95以下程序使用了習(xí)題11.2第4小題中生成的文件D:f1.txt。寫出程序運(yùn)行后屏幕

12、顯示的結(jié)果。#include<iostream>#include<fstream>using namespace std;int main() int i; ifstream f1( "d:f1.txt", ios:in ); fstream f2; f2.open( "d:f2.dat", ios:out|ios:binary ); while(!f1.eof() f1>>i; i = i*5; f2.write( ( char* ) &i, sizeof( int ) ); f1.close(); f2.c

13、lose(); f2.open( "d:f2.dat", ios:in|ios:binary ); do f2.read( ( char* ) &i, sizeof( int ) ); cout<<i<<" " while( i<30 ); cout<<endl; f2.close();【解答】 5 10 15 20 25 3011.3 思考題1在Visual C+中,流類庫的作用是什么?有人說,cin是鍵盤,cout是顯示器,這種說法正確嗎?為什么?【解答】在Visual C+中,流類庫是一個(gè)程序包,作

14、用是實(shí)現(xiàn)對(duì)象之間的數(shù)據(jù)交互?!癱in是鍵盤,cout是顯示器”的說法不正確。cin和cout分別是istream和ostream的預(yù)定義對(duì)象,默認(rèn)連接標(biāo)準(zhǔn)設(shè)備鍵盤、顯示器,解釋從鍵盤接受的信息,傳送到內(nèi)存;把內(nèi)存的信息解釋傳送到顯示器。所以稱為標(biāo)準(zhǔn)流對(duì)象。程序可以對(duì)cin、cout重定向,連接到用戶指定的設(shè)備,例如指定的磁盤文件。2什么叫文件?C+讀/寫文件需要通過什么對(duì)象?有些什么基本操作步驟?【解答】任何一個(gè)應(yīng)用程序運(yùn)行,都要利用內(nèi)存儲(chǔ)器存放數(shù)據(jù)。這些數(shù)據(jù)在程序運(yùn)行結(jié)束之后就會(huì)消失。為了永久的保存大量數(shù)據(jù),計(jì)算機(jī)用外存儲(chǔ)器(如磁盤和磁帶)保存數(shù)據(jù)。各種計(jì)算機(jī)應(yīng)用系統(tǒng)通常把一些相關(guān)信息組織起

15、來保存在外存儲(chǔ)器中,并用一個(gè)名字(稱為文件名)加以標(biāo)識(shí),稱為文件。C+讀/寫文件需要用到文件流對(duì)象。文件操作的三個(gè)主要步驟是:打開文件、讀/寫文件、關(guān)閉文件流。打開文件包括建立文件流對(duì)象,與外部文件關(guān)聯(lián),指定文件的打開方式。讀/寫文件是按文件信息規(guī)格、數(shù)據(jù)形式與內(nèi)存交互數(shù)據(jù)的過程。關(guān)閉文件包括把緩沖區(qū)數(shù)據(jù)完整地寫入文件,添加文件結(jié)束表示符,切斷流對(duì)象和外部文件的連接。3一個(gè)已經(jīng)建立的文本文件可以用二進(jìn)制代碼方式打開操作嗎?一個(gè)二進(jìn)制數(shù)據(jù)文件可以用文本方式打開嗎?為什么?寫一個(gè)程序試一試?!窘獯稹恳粋€(gè)已經(jīng)建立的文本文件可以用二進(jìn)制方式打開操作。但必須以字符類型數(shù)據(jù)讀取數(shù)據(jù)然后轉(zhuǎn)換成需要的類型數(shù)據(jù)

16、才有意義。通常一個(gè)二進(jìn)制文件用文本方式打開是沒有意義的,除非這個(gè)二進(jìn)制文件全部是用字符類型數(shù)據(jù)建立的。因?yàn)槲谋疚募且钥勺x形式ASC碼存放數(shù)據(jù)的,二進(jìn)制文件直接用計(jì)算機(jī)表示數(shù)據(jù)的二進(jìn)制形式存放數(shù)據(jù),它們之間解釋方式不同。程序略。11.4 編程題1以表格形式輸出當(dāng)x = 1°,2°,10°時(shí)sinx、cosx和tanx的值。要求:輸出時(shí),數(shù)據(jù)的寬度為10,左對(duì)齊,保留小數(shù)點(diǎn)后5位?!窘獯稹?include <iostream>#include <cmath>#include <iomanip>using namespace std

17、;int main() int x; double a; cout << "x sin(x) cos(x) tg(x)" << endl; /輸出表頭 for( x=1; x<=10; x+ ) a = x * 3.14159265 / 180; /角度轉(zhuǎn)換為弧度 cout << setw(3) << setiosflags( ios:left ); cout << setiosflags( ios:fixed ); cout << setprecision(5); cout << x

18、; cout << setw(10) << sin(a); cout << setw(10) << cos(a); cout<<setw(10)<<sin(a)/cos(a)<<endl; 2. 建立一個(gè)文本文件,從鍵盤輸入一篇短文存放在文件中。短文由若干行構(gòu)成,每行不超過80個(gè)字符?!窘獯稹?include <iostream>#include <fstream>using namespace std;int main() char filename20; fstream outfil

19、e; cout << "Please input the name of file :n" cin >> filename ; outfile.open( filename, ios:out ); if ( !outfile ) cerr << "File could not be open." << endl; abort(); outfile << "This is a file of studentsn" outfile << "Input the

20、 number, name, and score.n" outfile << "Enter Ctrl-Z to end input? " outfile.close();3讀出由習(xí)題11.4第2小題建立的文本文件,顯示在屏幕上并統(tǒng)計(jì)該文件的行數(shù)?!窘獯稹?include <iostream>#include <fstream>using namespace std;int main() char filename20; fstream infile; cout << "Please input the na

21、me of file :n" cin >> filename ; infile.open( filename, ios:in ); if ( !infile ) cerr << "File could not be open." << endl; abort(); char textline80; int i = 0; while ( !infile.eof() ) infile.getline( textline,sizeof( textline ); cout << textline << endl;

22、 +i;infile.close();cout << "i=" << i << endl;4讀出一個(gè)作業(yè)cpp文件,刪除全部注釋內(nèi)容,即以“/*/”相括的文本和以“/”開始到行末的文本,生成一個(gè)新的cpp文件?!窘獯稹柯?。5建立某單位職工通訊錄的二進(jìn)制數(shù)據(jù)文件,文件中的每個(gè)記錄包括:職工編號(hào)、姓名、電話號(hào)碼、郵政編碼和住址。【解答】#include <iostream>#include<fstream>using namespace std;struct txrec char no6; char name20; c

23、har tel9; char postc7; char addr30;int main() int n,i; txrec gzrec; char filename20; fstream outfile; cout << "請(qǐng)輸入通訊錄文件名:" ; cin >> filename ; outfile.open( filename, ios:out|ios:binary ); if ( !outfile ) cerr << "文件不能打開!" << endl ; abort(); cout <<

24、"請(qǐng)輸入職工人數(shù):" ; cin >> n; for( i=1; i<=n; i+ )cout << "請(qǐng)輸入第"<< i <<"個(gè)職工的編號(hào):" ;cin >> gzrec.no ;cout << "請(qǐng)輸入第"<< i <<"個(gè)職工的姓名:" ;cin >> ;cout << "請(qǐng)輸入第"<< i <<&

25、quot;個(gè)職工的電話號(hào)碼:" ;cin >> gzrec.tel ;cout << "請(qǐng)輸入第"<< i <<"個(gè)職工的郵政編碼:" ;cin >> gzrec.postc ;cout << "請(qǐng)輸入第"<< i <<"個(gè)職工的通信地址:" ;cin >> gzrec.addr ;outfile.write( ( char* )&gzrec,sizeof( txrec ) ; outfil

26、e.close() ;6從鍵盤輸入職工的編號(hào),在由習(xí)題11.4第5小題所建立的通訊錄文件中查找該職工資料。查找成功后,顯示職工的姓名、電話號(hào)碼、郵政編碼和住址?!窘獯稹?include <iostream>#include<fstream>using namespace std;struct txrecchar no6;char name20;char tel9;char postc7;char addr30;int main() struct txrec gzrec; int i;char filename20, num6; fstream infile; cout

27、<< "請(qǐng)輸入通訊錄文件名:" cin >> filename ; infile.open( filename, ios:in|ios:binary ); if ( !infile ) cerr << "文件不能打開!" << endl; abort(); infile.seekg( 0,ios:end );long posend = infile.tellp();infile.seekg( 0,ios:beg );cout << "請(qǐng)輸入職工編號(hào):" ;cin >&g

28、t; num;do infile.read( char * )&gzrec,sizeof( txrec ); while ( strcmp( gzrec.no,num ) != 0 && long(infile.tellp() != posend );if ( strcmp( gzrec.no,num ) = 0 )cout << "該職工的記錄找到了!" << endl;cout << "編號(hào):" << gzrec.no << endl;cout << &q

29、uot;姓名:"<< << endl;cout << "電話號(hào)碼:"<< gzrec.tel << endl;cout << "郵政編碼:" << gzrec.postc << endl;cout << "通信地址:" << gzrec.addr << endl;else cout << "該職工的記錄找不到!" << endl;

30、infile.close();7設(shè)有兩個(gè)按升序排列的二進(jìn)制數(shù)據(jù)文件f和g,將它們合并生成一個(gè)新的升序二進(jìn)制數(shù)據(jù)文件h?!窘獯稹?include <iostream>#include<fstream>using namespace std;int main()int data1,data2;fstream infile1,infile2,outfile;infile1.open( "d:vcf.dat", ios:in|ios:binary);if ( !infile1 ) cerr << "文件不能打開!" << endl ; abort();infile1.seekg(0,ios:end);long posend1 = infile1.tellp();infile2.open( "d:vcg.dat", ios:in|ios:binary);if ( !infile2 ) cerr << "文件不能打開!" << endl ; abort();infile2.seekg

溫馨提示

  • 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)論