《C++程序設(shè)計(jì)》實(shí)訓(xùn)報(bào)告(共19頁(yè))_第1頁(yè)
《C++程序設(shè)計(jì)》實(shí)訓(xùn)報(bào)告(共19頁(yè))_第2頁(yè)
《C++程序設(shè)計(jì)》實(shí)訓(xùn)報(bào)告(共19頁(yè))_第3頁(yè)
《C++程序設(shè)計(jì)》實(shí)訓(xùn)報(bào)告(共19頁(yè))_第4頁(yè)
《C++程序設(shè)計(jì)》實(shí)訓(xùn)報(bào)告(共19頁(yè))_第5頁(yè)
已閱讀5頁(yè),還剩15頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上實(shí)訓(xùn)一:類(lèi)和對(duì)象的定義及使用實(shí)訓(xùn)目的:(1)掌握類(lèi)與對(duì)象的定義與使用方法,理解面向?qū)ο蠓椒ㄖ型ㄟ^(guò)對(duì)象間傳遞消息的工作機(jī)制。(2)正確掌握類(lèi)的不同屬性成員的使用方法。(3)掌握構(gòu)造函數(shù)與析構(gòu)函數(shù)的概念,理解構(gòu)造函數(shù)與析構(gòu)函數(shù)的執(zhí)行過(guò)程。(4)掌握友元函數(shù)和友元類(lèi)的定義和使用。(5)基本掌握指針和引用作為函數(shù)參數(shù)的應(yīng)用。實(shí)訓(xùn)內(nèi)容:定義一個(gè)時(shí)間類(lèi)Time,有三個(gè)私有成員變量Hour、Minute、Second,定義構(gòu)造函數(shù)、析構(gòu)函數(shù)以及用于改變、獲取、輸出時(shí)間信息的公有函數(shù),主函數(shù)中定義時(shí)間對(duì)象,并通過(guò)調(diào)用各種成員函數(shù)完成時(shí)間的設(shè)定、改變、獲取、輸出等功能。 按要求完成類(lèi)的

2、定義與實(shí)現(xiàn)。 修改數(shù)據(jù)成員的訪問(wèn)方式,觀察編譯結(jié)果。 在Time類(lèi)中定義一個(gè)成員函數(shù),用于實(shí)現(xiàn)時(shí)間增加一秒的功能,主函數(shù)中通過(guò)對(duì)象調(diào)用該函數(shù),并輸出增加一秒后的時(shí)間信息。 定義一個(gè)普通函數(shù)。void f(Time t) t. PrintTime( );在Time類(lèi)中增加拷貝構(gòu)造函數(shù)的定義,主函數(shù)中調(diào)用該函數(shù),運(yùn)用調(diào)試工具跟蹤,分析整個(gè)程序調(diào)用構(gòu)造函數(shù)(包括拷貝構(gòu)造函數(shù))和析構(gòu)函數(shù)的次數(shù);再將f函數(shù)的形式參數(shù)分別修改為引用參數(shù)和指針參數(shù)(此時(shí)函數(shù)代碼修改為t-> PrintTime( );,主函數(shù)中調(diào)用,再分析此時(shí)調(diào)用構(gòu)造函數(shù)和析構(gòu)函數(shù)的次數(shù)。實(shí)訓(xùn)代碼:#include<iostre

3、am>using namespace std;class Timeprivate:int Hour,Minute,Second;public:Time(int h=0,int m=0,int s=0);Time(const Time &ob);Time();void ChangeTime(int h,int m,int s);int GetHour();int GetMinute();int GetSecond();void PrintTime();void IncreaseOneSecond();Time:Time(int h,int m,int s) Hour=h; Minu

4、te=m; Second=s;Time:Time(const Time &ob) Hour=ob.Hour; Minute=ob.Minute; Second=ob.Second;Time:Time()void Time:ChangeTime(int h,int m,int s) Hour=h; Minute=m; Second=s;int Time:GetHour() return Hour;int Time:GetMinute() return Minute;int Time:GetSecond() return Second;void Time:PrintTime() cout&

5、lt;<Hour<<": "<<Minute<<": "<<Second<<endl;void Time:IncreaseOneSecond() Second+;/*void Time:f(Time t) t.PrintTime(); cout<<"call fn"*/int main() Time a; Time b(13); Time c(13,15); Time d(13,15,45); a.PrintTime(); b.PrintTime(); c.

6、PrintTime(); d.PrintTime(); a.ChangeTime(12,15,45); b.ChangeTime(12,15,45); c.ChangeTime(12,15,45); d.ChangeTime(12,15,45); cout<<a.GetHour()<<":"<<a.GetMinute()<<":"<<a.GetSecond()<<endl; cout<<b.GetHour()<<":"<<b.G

7、etMinute()<<":"<<b.GetSecond()<<endl; cout<<c.GetHour()<<":"<<c.GetMinute()<<":"<<c.GetSecond()<<endl; cout<<d.GetHour()<<":"<<d.GetMinute()<<":"<<d.GetSecond()<&l

8、t;endl; return 0;程序運(yùn)行結(jié)果實(shí)訓(xùn)小結(jié):構(gòu)造函數(shù)與析構(gòu)函數(shù)的調(diào)用方式及執(zhí)行順序是:先是構(gòu)造函數(shù)然后是析構(gòu)函數(shù)。調(diào)用方式是自動(dòng)調(diào)用,執(zhí)行順序是先執(zhí)行構(gòu)造函數(shù),待程序結(jié)束時(shí)再執(zhí)行析構(gòu)函數(shù)。 實(shí)訓(xùn)二:個(gè)人銀行賬戶管理程序的類(lèi)設(shè)計(jì)實(shí)訓(xùn)目的:掌握面向?qū)ο笾蓄?lèi)、繼承、多態(tài)性的開(kāi)發(fā)思想;掌握流的概念; 獨(dú)立設(shè)計(jì)個(gè)人銀行賬戶管理程序。實(shí)訓(xùn)內(nèi)容:1、 個(gè)人銀行賬戶管理程序的類(lèi)關(guān)系圖2、 個(gè)人銀行賬戶管理程序的個(gè)人賬戶設(shè)置和應(yīng)用3、 個(gè)人銀行賬戶管理程序?qū)嵱?xùn)代碼:1、 個(gè)人銀行賬戶管理程序的類(lèi)設(shè)計(jì)class account private: std:string id; double balanc

9、e; static double total; protected: account(const Date &date,const std:string &id); void record(const Date &date,double amount,const std:string &desc); void error(const std:string &msg) const; public: const std:string &getId() const return id; double getBalance() const return

10、balance; static double gettotal() return total; void show() const; ; class SavingsAccount:public account private: accumulator acc; double rate; public: SavingsAccount(const Date &date,const std:string &id,double rate); double getrate() const return rate; void deposit(const Date &date,dou

11、ble amount,const std:string &desc); void withdraw(const Date &date,double amount,const std:string &desc); void settle(const Date &date); ; class creditaccount:public account private: accumulator acc; double credit; double rate; double fee; double getdebt() const double balance=getBal

12、ance(); return (balance<0 ? balance : 0); public: creditaccount(const Date &date,const std:string &id,double credit,double rate,double fee); double getcredit() const return credit; double getrate() const return rate; double getfee() const return fee; double getavailablecredit() const if (

13、getBalance()<0) return credit+getBalance(); Else return credit; void deposit(const Date &date,double amount,const std:string &desc); void withdraw(const Date &date,double amount,const std:string &desc); void settle(const Date &date); void show() const; ;class accumulator priva

14、te: Date lastdate; double value; double sum; public: accumulator(const Date &date,double value) :lastdate(date),value(value),sum(0) double getsum(const Date &date)const return sum+value*date.distance(lastdate);void change(const Date &date,double value) sum=getsum(date); lastdate=date;thi

15、s->value=value; void reset(const Date &date,double value) lastdate=date;this->value=value;sum=0; ;class Date private: int year; int month; int day; int totaldays; public: Date(int year,int month,int day); int getyear() const return year; double getmonth() const return month; int getday() c

16、onst return day; int getmaxday() const; bool isleapyear() const return year%4=0 && year%100!=0 | year%400=0; void show() const; int distance (const Date& date) const return totaldays-date.totaldays; ;2、 個(gè)人銀行賬戶管理程序的類(lèi)關(guān)系圖3、 個(gè)人銀行賬戶管理程序的個(gè)人賬戶設(shè)置和應(yīng)用SavingsAccount wutingming(date,"1",0.

17、015); creditaccount wutingmin(date,"1",2000,0.0005,50);wutingming.deposit(Date(2008,11,5),1000,"buy book");wutingmin.withdraw(Date(2008,11,5),2000,"buy MP3");wutingming.settle(Date(2008,12,5);wutingmin.settle(Date(2008,12,5);wutingming.show();cout<<endl;wutingmin.

18、show();cout<<endl;4、 個(gè)人銀行賬戶管理程序的重點(diǎn)代碼#include "account.h"#include<iostream>using namespace std;int main()Date date(2008,11,1); SavingsAccount sa1(date,"",0.015);SavingsAccount sa2(date,"",0.015);SavingsAccount wutingming(date,"1",0.015);creditaccoun

19、t ca(date,"C",10000,0.0005,50);creditaccount wutingmin(date,"1",2000,0.0005,50);sa1.deposit(Date(2008,11,5),5000,"salary");sa2.deposit(Date(2008,11,25),10000,"sell stock 0323");wutingming.deposit(Date(2008,11,5),1000,"buy book");wutingmin.withdraw(Da

20、te(2008,11,5),2000,"buy MP3");ca.withdraw(Date(2008,11,15),2000,"buy a cell");ca.settle(Date(2008,12,1);ca.deposit(Date(2008,12,1),2016,"repay the credit");sa1.deposit(Date(2008,12,5),5500,"salary");sa1.settle(Date(2009,1,1);sa2.settle(Date(2009,1,1); wutingmi

21、ng.settle(Date(2008,12,5);wutingmin.settle(Date(2008,12,5);ca.settle(Date(2008,12,1); cout<<endl;sa1.show();cout<<endl;sa2.show();cout<<endl;wutingming.show();cout<<endl;wutingmin.show();cout<<endl;ca.show();cout<<endl;cout<<"total: "<<accoun

22、t:gettotal()<<endl;return 0; #include "account.h"#include<iostream>using namespace std;int main()Date date(2008,11,1); SavingsAccount sa1(date,"s",0.015);SavingsAccount sa2(date,"",0.015);SavingsAccount wutingming(date,"1",0.015);creditaccount ca(d

23、ate,"C",10000,0.0005,50);account*accounts=&sa1,&sa2,&wutingming,&ca;const int n=sizeof(accounts)/sizeof(account *);cout<<"(d)deposit (w)withdraw (s)show (c)chang day (n)next month (e)exit"<<endl;char cmd;do date.show();cout<<"ttotal: "&

24、lt;<account:gettotal()<<endl;cout<<"command>"int index,day,i;double amount;string desc;cin>>cmd;switch (cmd)case 'd':cin>>index>>amount;getline(cin,desc);accountsindex->deposit(date,amount,desc); break;case 'w':cin>>index>>

25、amount;getline(cin,desc);accountsindex->withdraw(date,amount,desc); break;case 's':for (i=0;i<n;i+)cout<<""<<i<<""accountsi->show();cout<<endl; break;case 'c':cin>>day;if (day<date.getday()cout<<"you cannot spec

26、ify a previous day"else if (day>date.getmaxday()cout<<"invalid day"else date=Date(date.getyear(),date.getmonth(),day); break;case 'n':if(date.getmonth()=12)date=Date(date.getyear()+1,1,1);else date=Date(date.getyear(),date.getmonth()+1,1);for(i=0;i<n;i+)accountsi-&

27、gt;settle(date); break; while (cmd!='e'); return 0;程序運(yùn)行結(jié)果實(shí)訓(xùn)小結(jié):通過(guò)本實(shí)訓(xùn),應(yīng)用虛函數(shù)和抽象類(lèi)對(duì)程序進(jìn)行改進(jìn):1) 將show函數(shù)聲明為虛函數(shù),因此通過(guò)指向creditaccount類(lèi)實(shí)例的account類(lèi)型的指針來(lái)調(diào)用show函數(shù)時(shí),被實(shí)際調(diào)用的將是creditaccount類(lèi)定義的show函數(shù)。這樣,如果創(chuàng)建一個(gè)account指針類(lèi)型的數(shù)組,使各個(gè)元素分別指向各個(gè)賬戶對(duì)象,就可以通過(guò)一個(gè)循環(huán)來(lái)調(diào)用它們的show函數(shù)。2)在account類(lèi)中添加deposit,withdraw,settle這3個(gè)函數(shù)的聲明,且將它們

28、都聲明為純虛函數(shù),這使得通過(guò)基類(lèi)指針可以調(diào)用派生類(lèi)的相應(yīng)函數(shù),而且無(wú)須給出它們?cè)诨?lèi)中的實(shí)現(xiàn)。經(jīng)過(guò)這一改動(dòng)之后,account類(lèi)就變成了抽象類(lèi)。實(shí)訓(xùn)三、圖書(shū)信息管理系統(tǒng)主界面實(shí)訓(xùn)目的:能夠較好的完成程序的主體設(shè)計(jì),界面友好,功能齊全;程序思路清晰易懂,能夠充分利用所學(xué)工具實(shí)現(xiàn)各項(xiàng)操作。獨(dú)立力完成實(shí)訓(xùn)報(bào)告,內(nèi)容充實(shí)、觀點(diǎn)明確、新穎。實(shí)訓(xùn)內(nèi)容:圖書(shū)信息管理系統(tǒng),使之能提供以下功能:1、    系統(tǒng)以菜單方式工作。2、    借書(shū)3、    還書(shū)4、    圖書(shū)維護(hù)5、 

29、   讀者維護(hù)6、    退出:包括返回主界面和退出系統(tǒng)等功能。實(shí)訓(xùn)代碼:#include<stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>struct books_list char author20; /*作者名*/ char bookname20; /*書(shū)名*/ char publisher20; /*出版單位*/ char pbtime15; /*出版時(shí)間*/ char loginnum10; /*登陸號(hào)*/ floa

30、t price; /*價(jià)格*/ char classfy10; /*分類(lèi)號(hào)*/ struct books_list * next; /*鏈表的指針域*/; struct books_list * Create_Books_Doc(); /*新建鏈表*/void InsertDoc(struct books_list * head); /*插入*/void DeleteDoc(struct books_list * head , int num);/*刪除*/void Print_Book_Doc(struct books_list * head);/*瀏覽*/void search_book(

31、struct books_list * head); /*查詢*/void info_change(struct books_list * head);/*修改*/void save(struct books_list * head);/*保存數(shù)據(jù)至文件*/*新建鏈表頭節(jié)點(diǎn)*/struct books_list * Create_Books_Doc() struct books_list * head; head=(struct books_list *)malloc(sizeof(struct books_list); /*分配頭節(jié)點(diǎn)空間*/ head->next=NULL; /*頭節(jié)

32、點(diǎn)指針域初始化,定為空*/ return head; /*保存數(shù)據(jù)至文件*/void save(struct books_list * head) struct books_list *p; FILE *fp; p=head; fp=fopen("data.txt","w+"); /*以寫(xiě)方式新建并打開(kāi) data.txt文件*/ fprintf(fp,"n"); /*向文件輸出表格*/ fprintf(fp,"登錄號(hào) 書(shū) 名 作 者 出版單位 出版時(shí)間 分類(lèi)號(hào) 價(jià)格 n"); fprintf(fp,"n&

33、quot;); /*指針從頭節(jié)點(diǎn)開(kāi)始移動(dòng),遍歷至尾結(jié)點(diǎn),依次輸出圖書(shū)信息*/ while(p->next!= NULL) p=p->next; fprintf(fp,"%-6.6s%-10.10s%-10.10s%-10.10s%-12.12s%-6.6s%.2f n",p->loginnum,p->bookname,p->author,p->publisher,p->pbtime,p->classfy,p->price); fprintf(fp,"n"); fclose(fp); printf(&q

34、uot; 已將圖書(shū)數(shù)據(jù)保存到 data.txt 文件n");/*插入*/void InsertDoc(struct books_list *head) /*定義結(jié)構(gòu)體指針變量 s指向開(kāi)辟的新結(jié)點(diǎn)首地址 p為中間變量*/ struct books_list *s, *p; char flag='Y' /*定義flag,方便用戶選擇重復(fù)輸入*/ p=head; /*遍歷到尾結(jié)點(diǎn),p指向尾結(jié)點(diǎn)*/ while(p->next!= NULL) p=p->next; /*開(kāi)辟新空間,存入數(shù)據(jù),添加進(jìn)鏈表*/ while(flag='Y'|flag=&

35、#39;y') s=(struct books_list *)malloc(sizeof(struct books_list); printf("n 請(qǐng)輸入圖書(shū)登陸號(hào):"); fflush(stdin); scanf("%s",s->loginnum); printf("n 請(qǐng)輸入圖書(shū)書(shū)名:"); fflush(stdin); scanf("%s",s->bookname); printf("n 請(qǐng)輸入圖書(shū)作者名:"); fflush(stdin); scanf("

36、%s",s->author); printf("n 請(qǐng)輸入圖書(shū)出版社:"); fflush(stdin); scanf("%s",s->publisher); printf("n 請(qǐng)輸入圖書(shū)出版時(shí)間:"); fflush(stdin); scanf("%s",s->pbtime); printf("n 請(qǐng)輸入圖書(shū)分類(lèi)號(hào):"); fflush(stdin); scanf("%s",s->classfy); printf("n 請(qǐng)輸入圖

37、書(shū)價(jià)格:"); fflush(stdin); scanf("%f",&s->price); printf("n"); p->next=s; /*將新增加的節(jié)點(diǎn)添加進(jìn)鏈表*/ p=s; /*p指向尾節(jié)點(diǎn),向后移*/ s->next=NULL; printf(" 添加成功!"); printf("n 繼續(xù)添加?(Y/N):"); fflush(stdin); scanf("%c",&flag); printf("n"); if(flag

38、='N'|flag='n') if(flag='Y'|flag='y'); save(head); /*保存數(shù)據(jù)至文件*/ return;/*查詢操作*/void search_book(struct books_list *head) struct books_list * p; char temp20; p=head; if(head=NULL | head->next=NULL) /*判斷數(shù)據(jù)庫(kù)是否為空*/ printf(" 圖書(shū)庫(kù)為空!n"); else printf("請(qǐng)輸入您要查找的

39、書(shū)名: "); fflush(stdin); scanf("%s",temp); /*指針從頭節(jié)點(diǎn)開(kāi)始移動(dòng),遍歷至尾結(jié)點(diǎn),查找書(shū)目信息*/ while(p->next!= NULL) p=p->next; if(strcmp(p->bookname,temp)=0) printf("n圖書(shū)已找到!n"); printf("n"); printf("登錄號(hào): %stn",p->loginnum); printf("書(shū)名: %stn",p->bookname)

40、; printf("作者名: %stn",p->author); printf("出版單位: %stn",p->publisher); printf("出版時(shí)間: %stn",p->pbtime); printf("分類(lèi)號(hào): %stn",p->classfy); printf("價(jià)格: %.2ftn",p->price); if(p->next=NULL) printf("n查詢完畢!n"); return; /*瀏覽操作*/ void P

41、rint_Book_Doc(struct books_list * head) struct books_list * p; if(head=NULL | head->next=NULL) /*判斷數(shù)據(jù)庫(kù)是否為空*/ printf("n 沒(méi)有圖書(shū)記錄! nn"); return; p=head; printf("n"); printf("登錄號(hào) 書(shū) 名 作 者 出版單位 出版時(shí)間 分類(lèi)號(hào) 價(jià)格 n"); printf("n"); /*指針從頭節(jié)點(diǎn)開(kāi)始移動(dòng),遍歷至尾結(jié)點(diǎn),依次輸出圖書(shū)信息*/ while(p-&

42、gt;next!= NULL) p=p->next; printf("%-6.6s%-10.10s%-10.10s%-10.10s%-12.12s%-6.6s%.2f n",p->loginnum,p->bookname,p->author,p->publisher,p->pbtime,p->classfy,p->price); /*循環(huán)輸出表格*/ printf("n"); printf("n"); /*修改操作*/void info_change(struct books_list

43、* head) struct books_list * p; int panduan=0; /*此變量用于判斷是否找到書(shū)目*/ char temp20; p=head; printf("請(qǐng)輸入要修改的書(shū)名:"); scanf("%s",temp); while(p->next!= NULL) p=p->next; if(strcmp(p->bookname,temp)=0) printf("n 請(qǐng)輸入圖書(shū)登陸卡號(hào):"); fflush(stdin); scanf("%s",p->loginn

44、um); printf("n 請(qǐng)輸入圖書(shū)書(shū)名:"); fflush(stdin); scanf("%s",p->bookname); printf("n 請(qǐng)輸入圖書(shū)作者名:"); fflush(stdin); scanf("%s",p->author); printf("n 請(qǐng)輸入圖書(shū)出版社:"); fflush(stdin); scanf("%s",p->publisher); printf("n 請(qǐng)輸入圖書(shū)出版時(shí)間:"); fflus

45、h(stdin); scanf("%s",p->pbtime); printf("n 請(qǐng)輸入圖書(shū)分類(lèi)號(hào):"); fflush(stdin); scanf("%s",p->classfy); printf("n 請(qǐng)輸入圖書(shū)價(jià)格:"); fflush(stdin); scanf("%f",&p->price); printf("n"); panduan=1; if(panduan=0) printf("n 沒(méi)有圖書(shū)記錄! nn"); return;/*刪除操作*/void DeleteDoc(struct books_list * he

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論