西安交大C 程序設計第十一章作業(yè)_第1頁
西安交大C 程序設計第十一章作業(yè)_第2頁
西安交大C 程序設計第十一章作業(yè)_第3頁
西安交大C 程序設計第十一章作業(yè)_第4頁
西安交大C 程序設計第十一章作業(yè)_第5頁
已閱讀5頁,還剩19頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

西安交通大學實驗報告

課程計算機程序設計實驗名稱標準庫和輸入輸出流第1頁共25頁

系別實驗日期2014年6月7日

專業(yè)班級組別實驗報告日期2014年6月7日

姓名學號報告退發(fā)(訂正、重做)

同組人教師審批簽字

一、實驗目的

掌握C++語言輸入和輸出操作的方法、流和流類庫的使用方法。

二、實驗內容

(一)第一題:編寫一個程序,分別用不同的域寬(0~10)打印

出整數(shù)12345和浮點數(shù)1.2345。觀察當域寬小于數(shù)值的實際需要

域寬時會發(fā)生什么狀況。

1.源程序代碼:

#include<iostream>

usingnamespacestd;

voidmain()

intWIDTH;

for(WIDTH=10;WIDTH>0;WIDTH—)

cout<〈〃域寬、精度為〃<<Wn)TH<〈〃:\n〃

cout.precision(WIDTH);〃全局作用

cout.width(WIDTH);〃只起一次作用

cout?12345?,,\nz,?l.2345?endl;

2.實驗結果:

SBC:\windows\system32\...|0||

城寬、精度為10:7

12345

1.2345

域寬、精度為9:=

12345

1.2345

域寬、精度為8:

12345

1.2345

域寬、精度為7:

12345

1.2345

域寬、精度為6:

12345

1.2345

域寬、精度為5:

12345

1.2345

域寬、精度為4:

12345

1.234

域寬、精度為3:

12345

1.23

域寬、精度為2:

12345

1.2

域寬、精度為1:

12345

1

請按任意鍵繼續(xù).?.

4IfHI?

3.結論:

⑴域寬不夠時會自動補足。

⑵精度只需一次定義則一直有效。

(3)域寬需要每次輸出時均進行定義

(二)第二題:編寫一個程序,將華氏溫度0度~212度轉化為浮點

型攝氏溫度,浮點精度為3.轉換公式為如下:

Celsius=5.0/9.0*(Fahrenheit-32);

輸出用兩個右對齊序列,攝氏溫度前面加上正負號。

1.源程序代碼:

#include<iostream>

usingnamespacestd;

voidmain()

(

doubleCelsius,Fahrenheit;

cout.precision(3);

cout?〃轉換結果為:\n〃;

for(Fahrenheit=O;Fahrenheit<=212;Fahrenheit++)

(

cout.unsetf(ios::showpos);

Celsius=5.0/9.0*(Fahrenheit-32);

cout.setf(ios::showpos);

cout〈<Celsius<〈”度\n〃;

)

2.實驗結果:

SBC:\windows\system32\cmd.exe|CD||Bl|rfaw|

轉換結果為:

華氏0度=攝氏T7.8度

華氏1度=攝氏T7.2度

華氏2度=攝氏-16.7度

華氏3度=攝氏-16.1度

華氏4度=攝氏T5.6度

華氏5度=攝氏-15度

華氏6度=攝氏-14.4度

華氏7度=攝氏T3.9度

華氏8度=攝氏-13.3度

華氏9度=攝氏-12.8度

華氏10度=攝氏T2.2度

華氏11度=攝氏TL7度

華氏12度=攝氏-11.1度

華氏13度=攝氏-10.6度

華氏14度=攝氏T0度

圖C:\windows\system32\cmd.exe|UI||回Hw-I

華氏195度=攝氏-90.6度

華氏196度=謖氏-91.1度

華氏197度=宗氏-91.7度

華氏198度=攝氏-92.2度

華氏199度=費氏-92.8度

華氏200度=攝氏,93.3度

華氏201度=景氏-93.9度

華氏202度=索氏+94.4度

華氏203度=攝氏-95度

華氏204度=景氏-95.6度

華氏205度=攝氏+96.1度

華氏206度=費氏-96.7度

華氏207度=攝氏-97.2度

華氏208度=索氏-97.8度

華氏209度=攝氏,98.3度

華氏210度;景氏,98.9度

華氏211度二強氏-99.4度

華氏212度=攝氏+100度

請按任意鍵繼續(xù)...

(三)第三題:編寫一個程序,打印出ASCH字符集中碼值為

33~126的字符的ASCH碼表。要求輸出十進制值、八進制值、十六進

制值以及碼值所表示的字符。

1.源程序代碼:

#include<iostream>

usingnamespacestd;

voidmain()

(

inta;

charw;

cout?〃字符八進制十進制十六進制\n〃;

for(a=33;a<=126;a++)

(

w=a;

cout<<w<<,,\t/z<<oct<<a<</,\t/z<<dec<<a<<,,\t,,<<hex?a<<endl;

2.實驗結果:

QBC:\windows\system32\cmd.exe|

字符八進制十進制十六進制

I

,,4133

4234

433523

$443624

I_J

%453725

&463826

473927

(504028

)014129

*52422a

一53432b

?54442c

-55452d

56462e

57472f

0604830

1614931

?625032

3635133

4645234

5655335

6665436

670037

8705638

9715739

*72583a

73593b

/

\74603c▼

4nr□?

OSC:\windows\system32\cmd.exe-IlBIlrf3?l

d14410064>

e14510165

f14610266

g14710367

h15010468

i15110569

j1521066a

k1531076b

11541086c

m1551096d

n1561106e

o1571116f

p16011270

q16111371三

r16211472

s16311573u

t16411674

u16511775

v16611876

w16711977

x17012078

y17112179

z1721227a

{1731237b

1741247c

1751257d

1761267e

請按任意鍵繼續(xù)...

*Lmi?

四、第四題:修改例11-2中的程序,重載〉運算符,使其能夠直接

使用cin語句輸入Date類對象。

1.源程序代碼:

//日期類定義date.h

#ifndefDATE_H

#defineDATEH

Sinclude<iostream>

usingnamespacestd;

classDate

friendostream&operator<<(ostream&,constDate&);

friendistream&operator?(istream&,Date&);

intday,month,year;

voidIneDay();〃日期增加一天

intDayCalcOconst;//距基準日期的天數(shù)

staticconstintdays]];〃每月的天數(shù)

public:

Date(inty,intm,intd);〃構造函數(shù)

Date(intm,intd);〃構造函數(shù),年默認為系統(tǒng)當前年份

DateO;〃構造函數(shù),默認為系統(tǒng)日期

voidSystemDate();

voidSetDate(intyy,intmm,intdd);〃日期設置

voidSetDate(intmm,intdd);〃日期設置,年默認為系統(tǒng)年份

boolIsLeapYear(intyy)const;//是否閏年?

boolIsEndofMonthOconst;//是否月末?

voidprint_ymd()const;〃輸出日期yymmdd

voidprint_mdy()const;〃輸出日期mm_dd_yy

constDate&operator+(intdays);//日期增加任意天

constDate&operator+=(intdays);//日期增加任意天

intoperator-(constDate&ymd)const;//兩個日期之間的天數(shù)

voidShow(Date&da);

);

#endif

//Date類成員函數(shù)定義date,cpp

Sinclude<iostream>

Sinclude<time.h>

Sinclude"date.h〃

usingnamespacestd;

constintDate::days[]={0,31,28,31,30,31,30,

31,31,30,31,30,31);

〃構造函數(shù)

Date::Date(inty,intm,intd){SetDate(y,m,d);)

Date::Date(intm,intd){SetDate(m,d);}

Date::Date(){SystemDate();}

voidDate::SystemDate()

{〃取得系統(tǒng)日期

tm*gm;

time_tt=time(NULL);

gm=gmtime(&t);

year=1900+gm->tm_year;

month=gm->tm_mon+1;

day=gm->tm_mday;

voidDate::SetDate(intyy,intmm,intdd)

(

month=(mm>=1&&mm<=12)?mm:1;

year=(yy>=1900&&yy<=2100)?yy:1900;

if(month==2&&IsLeapYear(year))

day=(dd>=1&&dd<=29)?dd:1;

else

day=(dd>=1&&dd<二days[month])?dd:1;

)

voidDate::SetDate(intmm,intdd)

(

tm*gm;

time_tt=time(NULL);

gm=gmtime(&t);

month=(mm>=1&&mm<=12)?mm:1;

year=1900+gm->tm_year;

if(month==2&&IsLeapYear(year))

day=(dd>=1&&dd<=29)?dd:1;

else

day=(dd>=1&&dd<=days[month])?dd:1;

)

constDate&Date::operator+(intdays)

{〃重載+

for(inti=0;i<days;i++)

IneDay();

return*this;

)

constDate&Date::operator^(intdays)

{〃重載+=

for(inti=0;i<days;i++)

IneDay();

return*this;

}

intDate::operator-(constDate&ymd)const

{〃重載-

intdays;

days=DayCalc()-ymd.DayCalc();

returndays;

)

boolDate::IsLeapYear(inty)const

if(y%400==0i|(y%100!=0&&y%4==0)

returntrue;

returnfalse;

)

boolDate::IsEndofMonth()const

{

if(month==2&&IsLeapYear(year))

returnday==29;〃二月需要判斷是否閏年

else

returnday==days[month];

)

voidDate::IneDay()

(〃日期遞增一天

if(IsEndofMonth())

if(month==12){//年末

day=1;

month=1;

year++;

}

else{//月末

day=1;

month++;

}

elseday++;

)

intDate::DayCalc()const

(

intdd;

intyy=year-1900;

dd=yy*365;

if(yy)dd+=(yy-l)/4;

for(inti=l;i<month;i++)

dd+=days[i];

if(IsLeapYear(year)&&(month>2))

dd++;

dd+=day;

returndd;

)

voidDate::print_ymd()const

(

cout<<year<<"-"?month<<"-"?day<<endl;

)

voidDate::print_mdy()const

char*monthName[12]={"January”,

rebruary,March,Apri1,May,June,

“July","August","September","October”,

“November","December");

cout<<monthName[month-1]<X''

?day<<"、"<<year<<endl;

)

voidDate::Show(Date&da)

(

if(day==da.day&&month==da.month)

cout<</zHappyBirthday!,z;

else

(

if(da-Date::Date()<0)

da.year++;

cout<<,zItwillbeyourbirthdayafter,z<<da-Date::Date()?z,

days!;

)

)

ostream&operator?(ostream&output,constDate&d)

(

staticchar*monthName[12]=

(

〃丁〃

January,

“February”,

“March”,

"April",

"May",

〃T〃

June,

〃July〃,

“August”,

“September”,

“October”,

“November”,

“December”

};

output?monthName[d.month-1]'<<d.day<<〃,〃<<d?year;

returnoutput;

)

istream&operator?(istream&in,Date&d)//

{

inta,b,c;

in?a>>b>>c;

d.year=a;

d.month=b;

d.day=c;

returnin;

)

〃main.cpp修改例-9中的程序,重載〉運算符,使其能夠直接使用cin語句輸入

Date類對象。

Sinclude<iostream>

Sinclude"date.h〃

usingnamespacestd;

intmain()

(

Datetoday,Olympicday(2004,8,13);

cout<<"Today(thecomputer'sday)is:/z<<today<<endl;//

today+=365;//

cout<<"After365days,thedateis:z,<<today?endl;

Datetestday(2,28);

cout<<"thetestdateis:,,?testday<<endl;//

Datenextday=testday+1;

cout<<"thenextdateis:,z?testday<<endl;//

today.SystemDateO;

cout?”theAthensOlympicGamesopendayis:/,<<01ympicday<<endl;

cout<<"Andafter〃<<Olympicday-today

?〃days,theAthensOlympicGameswillopen.z,?endl;

Datebirthday;

cout。〃輸入出生年月日:〃;

cin>>birthday;

cout<<〃輸入的是:/z?birthday<<endl;

return0;

)

2.實驗結果:

SBC:\windows\system32\cmd.exe|o

Today(thecomputer,sday)is:April11,2014-

After365days,thedateis:April11,2015□

thetestdateis:February28,2014

thenextdateis:March1,2014

theAthensOlympicGamesopendayis:August13,2004

Andafter_3528days,theAthensOlympicGaineswillopen.

輸入出生年月日:200011

輸入的是:January1,2000

請按任意鍵繼續(xù)?...

五、第五題:編寫一個程序,可以讀入一個C++語言的源文件,每一

行加上行號后保存到另一個后綴為.pm的同名文件中。

1.源程序代碼:

Sinclude<fstream>

Sinclude<string>

#include<iostream>

usingnamespacestd;

intmain()

(

strings,name,namel;

cout?”請輸入源C++文件的名稱(不含有后綴名):〃;

cin>>name;

namel=name+,/.prn〃;

name+=z,.cpp〃;

ifstreamread(name.c_str());〃讀取的文件,用name,cstr()是將string

的char*類型轉換成const的類型,不然會出錯

fstreamwrite;

write,open(namel.c_str(),ios::trunc/*創(chuàng)建文件*/1ios::out/*寫文件

*/);〃輸出的文件,創(chuàng)建文件并寫文件

inti=0;

if(!read)

cout<<z,Cannotopeninput

return0;

}〃如果打開輸入文件失敗

if(!write)

cout<</zCannotopenoutputfile\n”;

return0;

}〃如果打開輸出文件失敗

while(getline(read,s))〃逐行讀取文件中數(shù)據(jù)到字符串s中

write〈〈++i<Xs〈〈endl;〃輸出讀取文件的信息到目標文件

read,close();

write.close0;〃關閉文件

cout?/z目標文件生成成功!(和源文件同目錄)“<<endl;

return0;

2.實驗結果:

實驗前:

實驗后:

hao.pm-記事本

文件(B編輯但格式9)查看凹幫助(H)

2

3#include<fstream/

4usingnainespacestd;

5intmain。

6{

7charch;

8inti=l;““

9ifstreamin(//hao.cpp,y,ios::binary);

10ofstreamout(〃hao.prn");

11if(1out)

12(〃,

13cout?y/Cannotopenthefile//?endl;

14return1;

15)

16if(lin)

17{“.

18cout?”Cannotopenthethefilez/?e

19return1;

20)

21out?i?,\t";

22while(in)

23{

24in.get(ch);

25if(in)out<<ch;

26if(ch-3\n)

27{out?i+l?,\t";

28i++;

29}

30)

31out.close();

32return0;

}

六、第六題:將一個文本文件內容用凱撒爾方法加密,密鑰是4。即

文本文件中字母用其后第4個字母代替,若是數(shù)字則用其后第4個數(shù)

字代替。例如文本文件原內容為“RedAndBlack2008",加密后

文本文件內容為“VihErhFpego6442”。(提示:先用記事本創(chuàng)

建一個文本文件,其內容應該是有意義的英文句子,然后以只讀方式

打開,加密后的內容寫入另一個文本文件中。)

1.源程序代碼:

#include<iostream>

#include<fstream>

usingnamespacestd;

charjiami(charch);

intmain()

(

ifstreamin(〃test.txt〃);

ofstreamout("test1.txt,z);

if(!in)

(

cout?z,Cannotopenthefile.,,?endl;

return1;

)

if(!out)

{

cout<<z,Cannotopenthefile.,z?endl;

return1;

}

charch;

while(in)

(

in.get(ch);

ch=jiami(ch);

if(in)out<<ch;

)

in.closeO;

out.close();

return0;

)

charjiami(charch)

(

if(ch>='0'&&ch<='9')

(

if(ch>,5,)ch=ch-6;

elsech+=4;

}

elseif(ch>=,a"&&ch<=,z,)

(

=,

if(ch>w')ch二ch+4,z'+'a,-1;

elsech+=4;

)

elseif(ch>='A'&&ch〈二'Z')

(

if(ch>=,W,)ch=ch+4-'Z'+'A'-1;

elsech+=4;

returnch;

)

2.實驗結果:

加密前:

Change[heUnchangeable.

GoodEvening,everyone.Itj-smypleasuretogivethisspeechtoday.

Thetopicofmyspeechis,changetheunchangeable.P

Asisknowntousall,wealwaysmeetwithalIkindsofdifficulties

andotherthingsthatcannotbechangedinourIife.TheyarejustIike

stumblingblocksonourroad,whichpreventusfromabetterIife.

Butdoesitmeanthatwecandonothingbutacceptthefactand

reaIizethatalIthesethingsareunchangeable?Here,Iwanttoshare

astorywithyou,whichmightgiveyoutheanswer.

HaveyoueverheardofthestoryofNickVujicis,afamousAustralia

speakerwhowasbornwithoutIimbs?WhenhewasaIittIeboy,heoften

feltverydepressedandevenwantedtosuicideforthechiIdrenaround

himoftensaidthathewasamonsterandalwaysIaughathim.Itseemed

thathiswassounluckyandnearlyeveryonethoughtthathisunfortunate

Iifewouldbeunchangeable.Butinfact,withtheencouragementofhis

parents,hesuddenlyrealizedthathecouldalsoIiveabetterIife

thanordinarypeoplealthoughhewasnotthesameasthem.Sofromthen

on,hebegantotrytodoanythingthathewantedtodo,including

writing,playingfootbalIFswimmingandmanyotherthings,whichseemed

tobeimpossibleforhim.Andintheend,hegottv/obachelorj-sdegrees

andbecameafamousspeaker.|

Thankyou!?

加密后:

GlerkiXIiYrglerkiefpi^

KsshIzirmrk,izivcsri.Mx*wqctpiev/yvixskmzixlmv/wtiigIxshec.XIixstmg

sjqcwtiigImw,glerkixliyrglerkiefpi.

Ewmworsarxsywepp,aiepaecwqiixamxleppomrhwsjhmjjmgypxmiwerhsxliv

xlmrkv/xlexgerrsxfiglerkihmrsyvpmji.XIicevinywxpmoiwxyqfpmrkfpsgowsr

syvvseh,aImgItvizirxywjvsqefixxivpmji.P

Fyxhsiwmxqierxlexaigerhsrsxlmrkfyxeggitxxlijegxerhviepmdixlex

eppxliwixlmrkweviyrglerkiefpi?Livi,Maerxxswleviewxsvcamxlcsy.aImgI

qmklxkmzicsyxlierwaiv.

LezicsyizivIievhsjxliwxsvcsjRmgoZynmgmw.ejeqsywEywxvepmewtieoiv

aIsaewfsvramxIsyxpmqfw?AlirIiaewepmxxpifsc.Iisjxirjipxzivchitviwwih

erhiziraerxihxswymgmhijsvxligImphvirevsyrhImqsjxirwemhxlexIiaewe

qsrwxiverhepaecv/peyklexImq.MxwiiqihxlexImwaewwsyrpygocerhrievpc

izivcsrixlsyklxxlexImwyrjsvxyrexipmjiasyphfiyrglerkiefpi.Fyxmrjegx.

amxlxliirgsyvekiqirxsjImwtevirxw,IiwyhhirpcviepmdihxlexIigsyphepv/s

pmziefixxivpmjixlersvhmrevctistpiepxlsyklIiaewrsxxliweqiewxliq.Ws

jvsqxlirsr.IifikerxsxvcxshsercxlmrkxlexIiaerxihxshs,mrgpyhmrk

avmxmrk,tpecmrkjssxfepp,wamqqmrkerhqercsxlivxlmrkw.aImgIwiiqihxsfi

mqtswwmfpijsvImq.Erhmrxliirh,Iiksxxasfeglipsv,v/hikviiwerhfigeqie

jeqsywwtieoiv.~

Xlerocsy!^

(七)第七題:(必作題)找出100以內的勾股數(shù),輸出到文件gouku

中。所謂勾股數(shù)指找出三個數(shù)滿足A2+B2X2,并且A〈B<C。要求將三

個勾股數(shù)的計算公式A'2+B"2=C^2輸出到文件中的每一行,例如

3c2+412=5”9+16=25。

1.源程序代碼:

#include<iostream>

#include<fstream>

#include<iomanip>

usingnamespacestd;

intmain()

(

ofstreamout(,zgougu.txt");

if(!out)

cout?/zCannotopenthefile.\n〃;

return1;

for(inti=0;i<101;i++)

for(intj=i+l;j<101;j++)

for(intk=j+l;k<101;k++)

{

if==k*k)

(

out.setf(ios::left);

out?setw(3)?i<<setw(3)?j<<setw(3)?k;

out?,\tJ?i?,?2???2??=?2?endl;

)

)

out.close();

return0;

)

2.實驗結果:

P453"+4A2=5”一

512135A2+123132

68106*2+8^2=10*2^

724257^2+24^2=25*2^

815178*2+15^2=17*2^

912159^2+12^2=15*2^

940419-2+40-2=412〃

10242610^2+24*2=26^2^

1160611廠2+60-2=6廠2"

12162012A2+16320,22

12353712^2+35*2=37*2^

13848513^2+84*2=85*2^

14485014c2+483502

15202515^2+20^2=25^2^

15

溫馨提示

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

最新文檔

評論

0/150

提交評論