收藏 分销(赏)

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

上传人:w****g 文档编号:2882056 上传时间:2024-06-08 格式:DOC 页数:28 大小:123.54KB 下载积分:10 金币
下载 相关 举报
C程序设计实训总结报告.doc_第1页
第1页 / 共28页
C程序设计实训总结报告.doc_第2页
第2页 / 共28页


点击查看更多>>
资源描述
实训一:类和对象定义及使用 实训目标: (1)掌握类和对象定义和使用方法,了解面向对象方法中经过对象间传输消息工作机制。 (2)正确掌握类不一样属性组员使用方法。 (3)掌握结构函数和析构函数概念,了解结构函数和析构函数实施过程。 (4)掌握友元函数和友元类定义和使用。 (5)基础掌握指针和引用作为函数参数应用。 实训内容: 定义一个时间类Time,有三个私有组员变量Hour、Minute、Second,定义结构函数、析构函数和用于改变、获取、输出时间信息公有函数,主函数中定义时间对象,并经过调用多种组员函数完成时间设定、改变、获取、输出等功效。 ① 按要求完成类定义和实现。 ② 修改数据组员访问方法,观察编译结果。 ③ 在Time类中定义一个组员函数,用于实现时间增加一秒功效,主函数中经过对象调用该函数,并输出增加一秒后时间信息。 ④ 定义一个一般函数。 void f(Time t) { t. PrintTime( ); } 在Time类中增加拷贝结构函数定义,主函数中调用该函数,利用调试工具跟踪,分析整个程序调用结构函数(包含拷贝结构函数)和析构函数次数;再将f函数形式参数分别修改为引用参数和指针参数(此时函数代码修改为{t-> PrintTime( );},主函数中调用,再分析此时调用结构函数和析构函数次数。 实训代码: #include<iostream> using namespace std; class Time { private: 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; Minute=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<<Hour<<": "<<Minute<<": "<<Second<<endl; } void Time::IncreaseOneSecond() { Second++; } /*void Time::f(Time t) { t.PrintTime(); cout<<"call f\n"; }*/ 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); cout<<a.GetHour()<<":"<<a.GetMinute()<<":"<<a.GetSecond()<<endl; cout<<b.GetHour()<<":"<<b.GetMinute()<<":"<<b.GetSecond()<<endl; cout<<c.GetHour()<<":"<<c.GetMinute()<<":"<<c.GetSecond()<<endl; cout<<d.GetHour()<<":"<<d.GetMinute()<<":"<<d.GetSecond()<<endl; return 0; } 程序运行结果 实训小结: 结构函数和析构函数调用方法及实施次序是:先是结构函数然后是析构函数。 调用方法是自动调用,实施次序是先实施结构函数,待程序结束时再实施析构函数。 实训二:个人银行账户管理程序类设计 实训目标: 掌握面向对象中类、继承、多态性开发思想;掌握流概念; 独立设计个人银行账户管理程序。 实训内容: 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 &desc); 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(const 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: accumulator acc; double credit; double rate; double fee; double getdebt() const { double balance=getBalance(); 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 (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{ private: 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;this->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() 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、 个人银行账户管理程序类关系图 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();cout<<endl; wutingmin.show();cout<<endl; 4、 个人银行账户管理程序关键代码 #include "account.h" #include<iostream> using 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); 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,"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)); cout<<endl; sa1.show();cout<<endl; sa2.show();cout<<endl; wutingming.show();cout<<endl; wutingmin.show();cout<<endl; ca.show();cout<<endl; cout<<"total: "<<account::gettotal()<<endl; return 0; } #include "account.h" #include<iostream> using 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=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: "<<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); accounts[index]->deposit(date,amount,desc); break; case 'w': cin>>index>>amount; getline(cin,desc); accounts[index]->withdraw(date,amount,desc); break; case 's': for (i=0;i<n;i++){ cout<<"["<<i<<"]"; accounts[i]->show(); cout<<endl; } break; case 'c': cin>>day; if (day<date.getday()) cout<<"you cannot specify 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++) accounts[i]->settle(date); break; } }while (cmd!='e'); return 0; } 程序运行结果 实训小结: 经过本实训,应用虚函数和抽象类对程序进行改善: 1) 将show函数申明为虚函数,所以经过指向creditaccount类实例account类型指针来调用show函数时,被实际调用将是creditaccount类定义show函数。这么,假如创建一个account指针类型数组,使各个元素分别指向各个账户对象,就能够经过一个循环来调用它们show函数。 2)在account类中添加deposit,withdraw,settle这3个函数申明,且将它们全部申明为纯虚函数,这使得经过基类指针能够调用派生类对应函数,而且无须给出它们在基类中实现。经过这一改动以后,account类就变成了抽象类。 实训三、图书信息管理系统主界面 实训目标: 能够很好完成程序主体设计,界面友好,功效齐全;程序思绪清楚易懂,能够充足利用所学工具实现各项操作。独立力完成实训汇报,内容充实、见解明确、新奇。 实训内容: 图书信息管理系统,使之能提供以下功效: 1、    系统以菜单方法工作。 2、    借书 3、    还书 4、    图书维护 5、    读者维护 6、    退出:包含返回主界面和退出系统等功效。 实训代码: #include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> struct books_list { char author[20]; /*作者名*/ char bookname[20]; /*书名*/ char publisher[20]; /*出版单位*/ char pbtime[15]; /*出版时间*/ char loginnum[10]; /*登陆号*/ float price; /*价格*/ char classfy[10]; /*分类号*/ 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(struct books_list * head); /*查询*/ void info_change(struct books_list * head);/*修改*/ void save(struct books_list * 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=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->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; } /*开辟新空间,存入数据,添加进链表*/ 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->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(stdin); 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 temp[20]; p=head; if(head==NULL || head->next==NULL) /*判定数据库是否为空*/ { printf(" ━━━━ 图书库为空!━━━━\n"); } else { printf("请输入您要查找书名: "); fflush(stdin); scanf("%s",temp); /*指针从头节点开始移动,遍历至尾结点,查找书目信息*/ while(p->next!= NULL) { p=p->next; if(strcmp(p->bookname,temp)==0) { printf("\n图书已找到!\n"); printf("\n"); printf("登录号: %s\t\n",p->loginnum); printf("书名: %s\t\n",p->bookname); printf("作者名: %s\t\n",p->author); printf("出版单位: %s\t\n",p->publisher); printf("出版时间: %s\t\n",p->pbtime); printf("分类号: %s\t\n",p->classfy); printf("价格: %.2f\t\n",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 ━━━━ 没有图书统计! ━━━━\n\n"); return; } p=head; printf("┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n"); printf("┃登录号┃ 书 名 ┃ 作 者┃ 出版单位 ┃ 出版时间 ┃分类号┃ 价格 ┃\n"); printf("┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\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 * p; int panduan=0; /*此变量用于判定是否找到书目*/ char temp[20]; 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(stdin); 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 ━━━━ 没有图书统计! ━━━━\n\n"); } return; } /*删除操作*/ void DeleteDoc(struct books_list * head) { struct books_list *s,*p; /*s为中间变量,p为遍历时使用指针*/ char temp[20]; 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(" ┏━
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

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

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

关于我们      便捷服务       自信AI       AI导航        抽奖活动

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

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

gongan.png浙公网安备33021202000488号   

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

关注我们 :微信公众号    抖音    微博    LOFTER 

客服