C++经典习题
2010-11-23 11:53
190 查看
1、 设计一个立方体类BOX,它能计算并输出立方体的体积和表面积。
提示:定义一个BOX类,含有一个私有数据成员(立方体边长length),有两个公有数据函数(构造
函数Box和计算输出函数show)
2、 有5个学生,每个学生的数据包括学号、姓名、三门课成绩,从键盘输入5个学生的数据,要求计算并输出。
1) 每个学生三门课的总成绩
2) 三门课每门课程的平均成绩
3、 假定居民的基本数据包括身份证号、姓名、性别和出生日期,而居民中的成年人又多项数据:最高学历和职业,成年人中的党员又多一项数据:党员类别。现要求建立三个类,让成年人类继承居民类,而党员类继承成年人类,并要求在每个类中都提供有数据输入和输出的功能。
4、 设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算,加时1秒,但要使计时过程能够自动进位。
5、 简化的职工档案管理程序。其中把职工的档案数据和对这些数据的设置、修改、删除、添加等操作组成一个程序模块。程序通过这个模块一类的公有部分对档案数据进行处理,实现了面向对象程序设计的“封装”功能。
说明:需要在同目录下新建一个文件sort.txt
提示:定义一个BOX类,含有一个私有数据成员(立方体边长length),有两个公有数据函数(构造
函数Box和计算输出函数show)
/** * 立方体类 * 作者:hellostory * 日期:2010-11-23 */ #include <iostream> using namespace std; class Box { private: double length; //立方体边长 double volume; //体积 double area; //表面积 public: /* * 构造函数 */ Box(double l) { length = l; volume = 0.0; area = 0.0; } /* * 计算体积 */ double getVolume() { return length * length * length; } /* * 计算表面积 */ double getArea() { return length * length * 6; } /* * 输出立方体体积和表面积 */ void show() { volume = getVolume(); area = getArea(); cout<<"立方体的体积:"<<volume<<",表面积:"<<area<<endl; } }; /* * 主函数 */ int main() { int length = 0; cout<<"请输入立方体的边长:"; cin>>length; Box box(length); box.show(); getchar(); return 0; }
2、 有5个学生,每个学生的数据包括学号、姓名、三门课成绩,从键盘输入5个学生的数据,要求计算并输出。
1) 每个学生三门课的总成绩
2) 三门课每门课程的平均成绩
/** * 学生成绩管理 * 作者:hellostory * 日期:2010-11-23 */ #include<string> #include<iostream> using namespace std; #define N 5 //定义常量N=5 // 定义学生结构体 typedef struct { string no; //学号 string name; //姓名 float chinese; //语文 float math; //数学 float english; //英语 float total; //总成绩 }Student; // 定义5个学生 Student stu ; /* * 录入每个学生的成绩等 */ void input(int n) { string no; //学号 string name; //姓名 float chinese; //语文 float math; //数学 float english; //英语 for(int i=0; i!=n; i++) { cout<<"请输入第"<<i+1<<"个学生的学号:"; cin>>no; stu[i].no = no; cout<<"请输入该学生的姓名:"; cin>>name; stu[i].name = name; cout<<"请输入该学生语文成绩:"; cin>>chinese; stu[i].chinese = chinese; cout<<"请输入该学生数学成绩:"; cin>>math; stu[i].math = math; cout<<"请输入该学生英语成绩:"; cin>>english; stu[i].english = english; // 计算该学生三门课的总成绩 stu[i].total = stu[i].chinese + stu[i].math + stu[i].english; } } /* * 打印三门课每门课程的平均成绩 */ void printAvg() { int i = 0; float sumChinese=0, sumMath=0, sumEnglish=0; float avgChinese=0, avgMath=0, avgEnglish=0; for(i=0; i<N; i++) { sumChinese += stu[i].chinese; sumMath += stu[i].math; sumEnglish += stu[i].english; } avgChinese = sumChinese / N; avgMath = sumMath / N; avgEnglish = sumEnglish / N; cout<<"语文课:"<<avgChinese<<"/r/n"; cout<<"数学课:"<<avgMath<<"/r/n"; cout<<"英语课:"<<avgEnglish<<"/r/n"; } /* * 主函数 */ void main() { // 录入每个学生的成绩等 input(N); cout<<"/r/n---------- 统计结果 ----------/r/n"; cout<<"以下是每个学生三门课的总成绩:"<<"/r/n"; for (int i=0; i<N; i++) { cout<<stu[i].name<<":"<<"/t"<<stu[i].total<<"/r/n"; } cout<<"/r/n以下是每门课的平均成绩:"<<"/r/n"; printAvg(); // 接收多余的"/r/n" getchar(); getchar(); }
3、 假定居民的基本数据包括身份证号、姓名、性别和出生日期,而居民中的成年人又多项数据:最高学历和职业,成年人中的党员又多一项数据:党员类别。现要求建立三个类,让成年人类继承居民类,而党员类继承成年人类,并要求在每个类中都提供有数据输入和输出的功能。
/** * C++类继承 * 作者:hellostory * 日期:2010-11-23 */ #include<string> #include<iostream> using namespace std; /* * 居民类 */ class People { private: char id[19]; //身份证号 char name[11]; //姓名 char sex[4]; //性别 char birth[11]; //出生日期 public: void input() { cout<<"请按顺序(身份证号、姓名、性别、出生日期--用回车隔开)输入居民信息:"<<endl; cin>>id>>name>>sex>>birth; } void output() { cout<<id<<' '<<name<<' '<<sex<<' '<<birth<<endl; } }; /* * 成人类 */ class Adult: public People { private: char education[11]; //最高学历 char job[11]; //职业 public: void input() { People::input(); cout<<"请输入最高学历和职业(用回车隔开):"<<endl; cin>>education>>job; } void output() { People::output(); cout<<education<<' '<<job<<endl; } }; /* * 党员类 */ class Party: public Adult { private: char parties[15]; //党员类别 public: void input() { Adult::input(); cout<<"请输入党员类别:"<<endl; cin>>parties; } void output() { cout<<"/n/r输出党员信息:"<<endl; Adult::output(); cout<<parties<<endl; } }; // 程序入口 void main() { // 测试党员类(按继承关系可以一起测试居民类、成人类) Party party; party.input(); party.output(); }
4、 设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算,加时1秒,但要使计时过程能够自动进位。
/** * C++时钟 * 作者:hellostory * 日期:2010-11-23 */ #include<iostream> #include<cmath> using namespace std; /* * 时钟类 */ class Clock { private: int Hour, Minute, Second; public: Clock(int h=0, int m=0, int s=0); void ShowTime(); Clock& operator ++(); Clock operator ++(int); }; /* * 时钟类构造函数 */ Clock::Clock(int h,int m, int s) { if(h>=0 && h<=24 && m>=0 && m<=60 && s>=0 && s<=60) { Hour = h; Minute =m; Second= s; } else cout<<"输入的时间格式错误!"<<endl; } /* * 显示时间 */ void Clock::ShowTime() { cout<<Hour<<":"<<Minute<<":"<<Second<<endl; } /* * 时间递增一秒(重载前缀++运算符) */ Clock& Clock::operator ++() { Second++; if (Second >= 60) { Second = Second - 60; Minute++; if (Minute >= 60) { Minute = Minute - 60; Hour++; Hour = Hour % 24; } } return *this; } /* * 时间递增一秒(重载后缀++运算符) */ Clock Clock::operator ++(int) { Clock old = *this; ++(*this); return old; } /* * 主函数 */ void main() { Clock myClock(23,59,59); cout<<"初始化显示时间为:/t/t"; myClock.ShowTime(); cout<<"执行myClock++后的时间为:/t"; //先执行ShowTime(),输出myClock=23:59:59, //再执行myClock++,此时myClock=00:00:00 (myClock++).ShowTime(); cout<<"执行++myClock后的时间为:/t"; //先执行++myClock,此时myClock=00:00:01 //再执行ShowTime(),输出myClock=00:00:01 (++myClock).ShowTime(); }
5、 简化的职工档案管理程序。其中把职工的档案数据和对这些数据的设置、修改、删除、添加等操作组成一个程序模块。程序通过这个模块一类的公有部分对档案数据进行处理,实现了面向对象程序设计的“封装”功能。
说明:需要在同目录下新建一个文件sort.txt
/** * 职工档案数据管理系统 * 作者:hellostory * 日期:2010-11-23 */ #include <iostream> #include <fstream> #include <string.h> #include <conio.h> using namespace std; // 职工类Staff class Staff { public: char Id[20]; //工号 char name[20]; //姓名 char sex[10]; //性别 char dept[20]; //部门 Staff * Next; //指向下一个职工节点 // 录入一个职工的档案数据 void Input() { cout<<"/t请输入新职工的档案数据:"<<endl; cout<<"/t工号:"; cin>>Id; cout<<"/t姓名:"; cin>>name; cout<<"/t性别:"; cin>>sex; cout<<"/t部门:"; cin>>dept; } // 从文件(sort.txt)输入流中读取一行数据赋值给Staff对象 void ReadFile(istream & in) { in>>Id>>name>>sex>>dept; } // 显示该职工的档案数据 void Show() { cout<<"/t工号/t姓名/t性别/t部门"<<endl; cout<<"/t"<<Id<<"/t"<<name<<"/t"<<sex<<"/t"<<dept<<endl; } }; // 职工档案数据管理类Staffmassage class Staffmassage { private: Staff * Head,* End; ifstream in; ofstream out; // 根据职工姓名查找并返回职工档案数据(返回前一个节点) Staff *FindItem(char * name) { for(Staff *p=Head; p->Next != End; p=p->Next) { if(!strcmp(p->Next->name,name)) return p; } return NULL; } // 根据职工工号查找并返回职工档案数据(返回前一个节点) Staff *FindID(char * Id) { for(Staff * p=Head; p->Next!=End; p=p->Next) { if(!strcmp(p->Next->Id,Id)) return p; } return NULL; } public: Staffmassage(); ~Staffmassage(); void ShowMenu(); void Find(); void Save(); void ModifyItem(); void RemoveItem(); void Swap(Staff *,Staff *); void Sort(); int ListCount(); // 显示全部职工的档案数据 void Display() { for(Staff *p=Head->Next; p!=End; p=p->Next){ p->Show(); } cout<<"/t输入任意字符继续..."; getch(); } // 增加职工档案数据 void AddItem() { End->Input(); End->Next = new Staff; End = End->Next; cout<<"/t添加成功!"<<endl; cout<<"/t输入任意字符继续..."; getch(); } }; // 构造函数 Staffmassage::Staffmassage() { Head = new Staff; Head->Next = new Staff; End = Head->Next; in.open("sort.txt"); if(!in) cout<<"系统无任何职工档案数据!请先录入职工档案数据!"<<endl; else { while(!in.eof()) { End->ReadFile(in); if(End->name[0]=='/0') break; End->Next = new Staff; End = End->Next; } in.close(); cout<<"读取职工档案数据成功!"<<endl; } } // 析构函数 Staffmassage::~Staffmassage() { // 将职工档案数据保存在文本文件中 Save(); // 删除存放职工档案数据的链表 for(Staff * temp;Head->Next!=End;) { temp=Head->Next; Head->Next=temp->Next; delete temp; } delete Head,End; } // 显示系统菜单 void Staffmassage::ShowMenu() { cout<<"/t ┏━━━━━━━━━━━━━━┓ "<<endl; cout<<"/t┏━━━━━━┫ 职 工 管 理 系 统 ┣━━━━━━┓"<<endl; cout<<"/t┃ ┗━━━━━━━━━━━━━━┛ ┃"<<endl; cout<<"/t┃ 1.增加职工档案数据 4.查找职工档案数据 ┃"<<endl; cout<<"/t┃ 2.显示职工档案数据 5.删除职工档案数据 ┃"<<endl; cout<<"/t┃ 3.排序统计档案数据 6.修改职工档案数据 ┃"<<endl; cout<<"/t┃ 0.安全退出系统 ┃"<<endl; cout<<"/t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl; cout<<"/n/t请选择:"; } // 查找并显示职工档案数据 void Staffmassage::Find() { char name[20] ,Id[10]; int x; Staff * p=NULL; cout<<"/n/t━━━━━━━━━━━━━━━━━/n"; cout<<"/t 1.按职工的姓名查找 2.按职工学号查找"; cout<<"/n/t━━━━━━━━━━━━━━━━━/n/t请选择:"; cin>>x; switch(x) { case 1: { cout<<"/t请输入要查找的职工的姓名:"; cin>>name; if(p=FindItem(name)) { p->Next->Show(); cout<<"/t输入任意字符继续..."; getch(); } else { cout<<"/t没有找到该姓名的职工!/n"<<endl; cout<<"/t输入任意字符继续..."; getch(); } } break; case 2: { cout<<"/t请输入要查找的职工的学号:"; cin>>Id; if(p=FindID(Id)) { p->Next->Show(); cout<<"/t输入任意字符继续..."; getch(); } else { cout<<"/t没有找到该工号的职工!/n"<<endl; cout<<"/t输入任意字符继续..."; getch(); } } break; } } // 修改职工档案数据 void Staffmassage::ModifyItem() { char name[20]; Staff * p=NULL; cout<<"/t请输入要修改的职工姓名:"; cin>>name; if(p=FindItem(name)) { cout<<"/t已找到职工的档案数据,请输入新的档案数据!"<<endl; p->Next->Input(); cout<<"/t修改成功!"<<endl; cout<<"/t输入任意字符继续..."; getch(); } else { cout<<"/t对不起,没有找到该职工档案数据!"<<endl; cout<<"/t输入任意字符继续..."; getch(); } } // 根据职工姓名删除对应的档案数据 void Staffmassage::RemoveItem() { char name[20]; Staff * p=NULL,*temp=NULL; cout<<"/t请输入要删除的职工的姓名:"<<endl;cin>>name; if(p=FindItem(name)) { // 先使被删除职工的前一个节点Next指向其下一个节点 temp=p->Next; p->Next=temp->Next; // 再删除该职工节点数据 delete temp; cout<<"/t删除成功!"<<endl; cout<<"/t输入任意字符继续..."; getch(); } else { cout<<"/t对不起,没有找到该职工档案数据!"<<endl; cout<<"/t输入任意字符继续..."; getch(); } } // 交换两个职工的档案数据 void Staffmassage::Swap(Staff *p1, Staff *p2) { Staff *temp=new Staff; strcpy(temp->Id,p1->Id); strcpy(temp->name,p1->name); strcpy(temp->sex,p1->sex); strcpy(temp->dept,p1->dept); strcpy(p1->Id,p2->Id); strcpy(p1->name,p2->name); strcpy(p1->sex,p2->sex); strcpy(p1->dept,p2->dept); strcpy(p2->name,temp->name); strcpy(p2->Id,temp->Id); strcpy(p2->sex,temp->sex); strcpy(p2->dept,temp->dept); } // 统计当前链表的记录总数,返回一个整数 int Staffmassage::ListCount() { if(!Head) { return 0; } int n=0; for(Staff * p=Head->Next;p!=End;p=p->Next) { n++; } return n; } // 对当前链表进行排序(采用选择排序算法) void Staffmassage::Sort() { cout <<"/t正在排序(按工号从小到大)..."<<endl; Staff *p=NULL,*p1=NULL,*k=NULL; int n=Staffmassage::ListCount(); if(n<2) return; for(p=Head->Next; p!=End; p=p->Next) { for(k=p->Next;k!=End;k=k->Next) { if(p->Id < k->Id) { Staffmassage::Swap(p,k); } } } cout <<"/t排序完成!"<<endl; getch(); return; } // 保存职工档案数据到文本文件"sort.txt" void Staffmassage::Save() { out.open("sort.txt"); for(Staff *p=Head->Next;p!=End;p=p->Next) { out<<p->Id<<"/t"<<p->name<<"/t"<<p->sex<<"/t"<<p->dept<<'/n'; } out.close(); } // 主函数(程序入口) int main() { cout<<"欢迎进入【职工档案数据管理系统】!"<<endl; Staffmassage Grade; //创建职工档案管理对象 cout<<"按任意键开始……"; getch(); int x; bool quit = false; while(!quit) { system("cls"); //清除DOS屏幕 Grade.ShowMenu(); //显示系统菜单 cin>>x; switch(x) { case 0:quit=true;break; case 1:Grade.AddItem();break; case 2:Grade.Display();break; case 3:Grade.Sort();break; case 4:Grade.Find();break; case 5:Grade.RemoveItem();break; case 6:Grade.ModifyItem();break; } } return 0; }
相关文章推荐
- 算法竞赛入门经典课后习题答案:第一章(C++解法)
- C++经典程序100例(附答案)【1】(希望一些像我一样的新手能够从这些习题中取得进步)
- c++的经典题型,课后习题
- c++ 经典习题源码
- C++经典题目:有n个人围成一圈,顺序排号,然后数数进行淘汰的解法和一些思考
- 常用算法经典代码(C++版)
- 经典问题6 c/c++ 程序设计 ---与或非问题
- C/C++经典笔试题
- C++经典题目:有n个整数,使前面各数顺序向后移动m个位置
- C/C++经典程序训练3---模拟计算器
- C语言之经典习题
- c++学习经典书籍
- C++中垃圾回收机制中几种经典的垃圾回收算法
- 【经典】C++&RPG对战游戏
- 【C++ 快速入门】习题1.3 1.4 1.5 1.6 1.7 1.8
- C++习题 对象转换
- C/C++经典程序训练3---模拟计算器
- c++ primer plus第六章习题答案
- 算法竞赛入门经典 习题2-4 倒三角形(triangle)
- C++经典书籍