收藏 分销(赏)

C程序设计实训总结报告.doc

上传人:w****g 文档编号:2882056 上传时间:2024-06-08 格式:DOC 页数:28 大小:123.54KB
下载 相关 举报
C程序设计实训总结报告.doc_第1页
第1页 / 共28页
C程序设计实训总结报告.doc_第2页
第2页 / 共28页
C程序设计实训总结报告.doc_第3页
第3页 / 共28页
C程序设计实训总结报告.doc_第4页
第4页 / 共28页
C程序设计实训总结报告.doc_第5页
第5页 / 共28页
点击查看更多>>
资源描述

1、实训一:类和对象定义及使用实训目标:(1)掌握类和对象定义和使用方法,了解面向对象方法中经过对象间传输消息工作机制。(2)正确掌握类不一样属性组员使用方法。(3)掌握结构函数和析构函数概念,了解结构函数和析构函数实施过程。(4)掌握友元函数和友元类定义和使用。(5)基础掌握指针和引用作为函数参数应用。实训内容:定义一个时间类Time,有三个私有组员变量Hour、Minute、Second,定义结构函数、析构函数和用于改变、获取、输出时间信息公有函数,主函数中定义时间对象,并经过调用多种组员函数完成时间设定、改变、获取、输出等功效。 按要求完成类定义和实现。 修改数据组员访问方法,观察编译结果。

2、 在Time类中定义一个组员函数,用于实现时间增加一秒功效,主函数中经过对象调用该函数,并输出增加一秒后时间信息。 定义一个一般函数。void f(Time t) t. PrintTime( );在Time类中增加拷贝结构函数定义,主函数中调用该函数,利用调试工具跟踪,分析整个程序调用结构函数(包含拷贝结构函数)和析构函数次数;再将f函数形式参数分别修改为引用参数和指针参数(此时函数代码修改为t- PrintTime( );,主函数中调用,再分析此时调用结构函数和析构函数次数。实训代码:#includeusing namespace std;class Timeprivate:int Hour

3、,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; Minute=m; Second=s;Time:Time(const Time &ob) Hour=ob.Hour; M

4、inute=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() coutHour: Minute: Secondendl;void Time:IncreaseOneSecond() Second

5、+;/*void Time:f(Time t) t.PrintTime(); coutcall fn;*/int main() Time a; Time b(13); Time c(13,15); Time d(13,15,45); a.PrintTime(); b.PrintTime(); c.PrintTime(); d.PrintTime(); a.ChangeTime(12,15,45); b.ChangeTime(12,15,45); c.ChangeTime(12,15,45); d.ChangeTime(12,15,45); couta.GetHour():a.GetMinute

6、():a.GetSecond()endl; coutb.GetHour():b.GetMinute():b.GetSecond()endl; coutc.GetHour():c.GetMinute():c.GetSecond()endl; coutd.GetHour():d.GetMinute():d.GetSecond()endl; return 0;程序运行结果实训小结:结构函数和析构函数调用方法及实施次序是:先是结构函数然后是析构函数。调用方法是自动调用,实施次序是先实施结构函数,待程序结束时再实施析构函数。 实训二:个人银行账户管理程序类设计实训目标:掌握面向对象中类、继承、多态性开发

7、思想;掌握流概念; 独立设计个人银行账户管理程序。实训内容:1、 个人银行账户管理程序类关系图2、 个人银行账户管理程序个人账户设置和应用3、 个人银行账户管理程序实训代码:1、 个人银行账户管理程序类设计class account private: std:string id; double balance; static double total; protected: account(const Date &date,const std:string &id); void record(const Date &date,double amount,const std:string &de

8、sc); void error(const std:string &msg) const; public: const std:string &getId() const return id; double getBalance() const return balance; static double gettotal() return total; void show() const; ; class SavingsAccount:public account private: accumulator acc; double rate; public: SavingsAccount(con

9、st Date &date,const std:string &id,double rate); double getrate() const return rate; 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); ; class creditaccount:public account private:

10、 accumulator acc; double credit; double rate; double fee; double getdebt() const double balance=getBalance(); return (balance0 ? balance : 0); public: creditaccount(const Date &date,const std:string &id,double credit,double rate,double fee); double getcredit() const return credit; double getrate() c

11、onst return rate; double getfee() const return fee; double getavailablecredit() const if (getBalance()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

12、); int getyear() const return year; double getmonth() const return month; int getday() const 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、 个人银行账户管理程序

13、类关系图3、 个人银行账户管理程序个人账户设置和应用SavingsAccount wutingming(date,0.015); creditaccount wutingmin(date,0.0005,50);wutingming.deposit(Date(,11,5),1000,buy book);wutingmin.withdraw(Date(,11,5),buy MP3);wutingming.settle(Date(,12,5);wutingmin.settle(Date(,12,5);wutingming.show();coutendl;wutingmin.show();couten

14、dl;4、 个人银行账户管理程序关键代码#include account.h#includeusing namespace std;int main()Date date(,11,1); SavingsAccount sa1(date,03755217,0.015);SavingsAccount sa2(date,02342342,0.015);SavingsAccount wutingming(date,0.015);creditaccount ca(date,C5392394,10000,0.0005,50);creditaccount wutingmin(date,0.0005,50);

15、sa1.deposit(Date(,11,5),5000,salary);sa2.deposit(Date(,11,25),10000,sell stock 0323);wutingming.deposit(Date(,11,5),1000,buy book);wutingmin.withdraw(Date(,11,5),buy MP3);ca.withdraw(Date(,11,15),buy a cell);ca.settle(Date(,12,1);ca.deposit(Date(,12,1),repay the credit);sa1.deposit(Date(,12,5),5500,

16、salary);sa1.settle(Date(,1,1);sa2.settle(Date(,1,1); wutingming.settle(Date(,12,5);wutingmin.settle(Date(,12,5);ca.settle(Date(,12,1); coutendl;sa1.show();coutendl;sa2.show();coutendl;wutingming.show();coutendl;wutingmin.show();coutendl;ca.show();coutendl;couttotal: account:gettotal()endl;return 0;

17、#include account.h#includeusing namespace std;int main()Date date(,11,1); SavingsAccount sa1(date,s3755217,0.015);SavingsAccount sa2(date,02342342,0.015);SavingsAccount wutingming(date,0.015);creditaccount ca(date,C5392394,10000,0.0005,50);account*accounts=&sa1,&sa2,&wutingming,&ca;const int n=sizeo

18、f(accounts)/sizeof(account *);cout(d)deposit (w)withdraw (s)show (c)chang day (n)next month (e)exitendl;char cmd;do date.show();coutttotal: account:gettotal()endl;cout;int index,day,i;double amount;string desc;cincmd;switch (cmd)case d:cinindexamount;getline(cin,desc);accountsindex-deposit(date,amou

19、nt,desc); break;case w:cinindexamount;getline(cin,desc);accountsindex-withdraw(date,amount,desc); break;case s:for (i=0;in;i+)coutishow();coutday;if (daydate.getday()coutdate.getmaxday()coutinvalid day;else date=Date(date.getyear(),date.getmonth(),day); break;case n:if(date.getmonth()=12)date=Date(d

20、ate.getyear()+1,1,1);else date=Date(date.getyear(),date.getmonth()+1,1);for(i=0;isettle(date); break; while (cmd!=e); return 0;程序运行结果实训小结:经过本实训,应用虚函数和抽象类对程序进行改善:1) 将show函数申明为虚函数,所以经过指向creditaccount类实例account类型指针来调用show函数时,被实际调用将是creditaccount类定义show函数。这么,假如创建一个account指针类型数组,使各个元素分别指向各个账户对象,就能够经过一个循环

21、来调用它们show函数。2)在account类中添加deposit,withdraw,settle这3个函数申明,且将它们全部申明为纯虚函数,这使得经过基类指针能够调用派生类对应函数,而且无须给出它们在基类中实现。经过这一改动以后,account类就变成了抽象类。实训三、图书信息管理系统主界面实训目标:能够很好完成程序主体设计,界面友好,功效齐全;程序思绪清楚易懂,能够充足利用所学工具实现各项操作。独立力完成实训汇报,内容充实、见解明确、新奇。实训内容:图书信息管理系统,使之能提供以下功效:1、 系统以菜单方法工作。2、 借书3、 还书4、 图书维护5、 读者维护6、 退出:包含返回主界面和退

22、出系统等功效。实训代码:#include#include#include#includestruct books_list char author20; /*作者名*/ char bookname20; /*书名*/ char publisher20; /*出版单位*/ char pbtime15; /*出版时间*/ char loginnum10; /*登陆号*/ float price; /*价格*/ char classfy10; /*分类号*/ struct books_list * next; /*链表指针域*/; struct books_list * Create_Books_D

23、oc(); /*新建链表*/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(struct books_list * head); /*查询*/void info_change(struct books_list * head);/*修改*/void save(struct books_list *

24、head);/*保留数据至文件*/*新建链表头节点*/struct books_list * Create_Books_Doc() struct books_list * head; head=(struct books_list *)malloc(sizeof(struct books_list); /*分配头节点空间*/ head-next=NULL; /*头节点指针域初始化,定为空*/ return head; /*保留数据至文件*/void save(struct books_list * head) struct books_list *p; FILE *fp; p=head; fp

25、=fopen(data.txt,w+); /*以写方法新建并打开 data.txt文件*/ fprintf(fp,n); /*向文件输出表格*/ fprintf(fp,登录号 书 名 作 者 出版单位 出版时间 分类号 价格 n); fprintf(fp,n); /*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/ 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

26、-pbtime,p-classfy,p-price); fprintf(fp,n); fclose(fp); printf( 已将图书数据保留到 data.txt 文件n);/*插入*/void InsertDoc(struct books_list *head) /*定义结构体指针变量 s指向开辟新结点首地址 p为中间变量*/ struct books_list *s, *p; char flag=Y; /*定义flag,方便用户选择反复输入*/ p=head; /*遍历到尾结点,p指向尾结点*/ while(p-next!= NULL) p=p-next; /*开辟新空间,存入数据,添加进

27、链表*/ while(flag=Y|flag=y) s=(struct books_list *)malloc(sizeof(struct books_list); printf(n 请输入图书登陆号:); fflush(stdin); scanf(%s,s-loginnum); printf(n 请输入图书书名:); fflush(stdin); scanf(%s,s-bookname); printf(n 请输入图书作者名:); fflush(stdin); scanf(%s,s-author); printf(n 请输入图书出版社:); fflush(stdin); scanf(%s,s

28、-publisher); printf(n 请输入图书出版时间:); fflush(stdin); scanf(%s,s-pbtime); printf(n 请输入图书分类号:); fflush(stdin); scanf(%s,s-classfy); printf(n 请输入图书价格:); fflush(stdin); scanf(%f,&s-price); printf(n); p-next=s; /*将新增加节点添加进链表*/ p=s; /*p指向尾节点,向后移*/ s-next=NULL; printf( 添加成功!); printf(n 继续添加?(Y/N):); fflush(st

29、din); scanf(%c,&flag); printf(n); if(flag=N|flag=n) if(flag=Y|flag=y); save(head); /*保留数据至文件*/ return;/*查询操作*/void search_book(struct books_list *head) struct books_list * p; char temp20; p=head; if(head=NULL | head-next=NULL) /*判定数据库是否为空*/ printf( 图书库为空!n); else printf(请输入您要查找书名: ); fflush(stdin);

30、scanf(%s,temp); /*指针从头节点开始移动,遍历至尾结点,查找书目信息*/ while(p-next!= NULL) p=p-next; if(strcmp(p-bookname,temp)=0) printf(n图书已找到!n); printf(n); printf(登录号: %stn,p-loginnum); printf(书名: %stn,p-bookname); printf(作者名: %stn,p-author); printf(出版单位: %stn,p-publisher); printf(出版时间: %stn,p-pbtime); printf(分类号: %stn,

31、p-classfy); printf(价格: %.2ftn,p-price); if(p-next=NULL) printf(n查询完成!n); return; /*浏览操作*/ void Print_Book_Doc(struct books_list * head) struct books_list * p; if(head=NULL | head-next=NULL) /*判定数据库是否为空*/ printf(n 没有图书统计! nn); return; p=head; printf(n); printf(登录号 书 名 作 者 出版单位 出版时间 分类号 价格 n); printf(

32、n); /*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/ while(p-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); /*循环输出表格*/ printf(n); printf(n); /*修改操作*/void info_change(struct books_list * head) struct books_list *

33、p; int panduan=0; /*此变量用于判定是否找到书目*/ char temp20; p=head; printf(请输入要修改书名:); scanf(%s,temp); while(p-next!= NULL) p=p-next; if(strcmp(p-bookname,temp)=0) printf(n 请输入图书登陆卡号:); fflush(stdin); scanf(%s,p-loginnum); printf(n 请输入图书书名:); fflush(stdin); scanf(%s,p-bookname); printf(n 请输入图书作者名:); fflush(std

34、in); scanf(%s,p-author); printf(n 请输入图书出版社:); fflush(stdin); scanf(%s,p-publisher); printf(n 请输入图书出版时间:); fflush(stdin); scanf(%s,p-pbtime); printf(n 请输入图书分类号:); fflush(stdin); scanf(%s,p-classfy); printf(n 请输入图书价格:); fflush(stdin); scanf(%f,&p-price); printf(n); panduan=1; if(panduan=0) printf(n 没有

35、图书统计! nn); return;/*删除操作*/void DeleteDoc(struct books_list * head) struct books_list *s,*p; /*s为中间变量,p为遍历时使用指针*/ char temp20; int panduan; /*此变量用于判定是否找到了书目*/ panduan=0; p=s=head; printf( 请输入您要删除书名:); scanf(%s,temp); /*遍历到尾结点*/ while(p!= NULL) if(strcmp(p-bookname,temp)=0) panduan+; break; p=p-next; if(panduan=1) for(;s-next!=p;) /*找到所需删除卡号结点上一个结点*/ s=s-next; s-next=p-next; /*将后一节点地址赋值给前一节点指针域*/ free(p); printf(n 删除成功! n); else /*未找到对应书目*/ printf( 您输入书目不存在,请确定后输入!n); return;int main(void) struct books_list * head; char choice; head=NULL; for(;) /*实现反复输入选择*/ printf(

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 考试专区 > 中考

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服