您的位置:首页 > 其它

面向对象复习题

2012-02-13 22:50 218 查看
1、 设计一个立方体类BOX,它能计算并输出立方体的体积和表面积。

提示:定义一个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;
}
/**
* 立方体类
* 作者: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();
}
/**
* 学生成绩管理
* 作者: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();
}
/**
* 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();
}
/**
* 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;
}



立方体类Box,计算并输出体积和表面积.

提示:定义一个Box类,含有一个私有数据成员(立方体边长length )有两个公有数据函数(构造函数Box和计算输出函数show).

#include <iostream>
using namespace std;

class Box{
private:
float length;
public:
Box(){}
Box(float r){length=r;}
void setLength(float r){length=r;}
float getVolume(){return length*length*length;}
float getArea(){return length*length*6;}
void show(){
cout<<"volume:"<<getVolume();
cout<<"area:"<<getArea();
};
};

void main(){
Box box1(5),box2;
box2.setLength(6);

cout<<"box1 is ";
box1.show();
cout<<endl;
cout<<"box2 is ";
box2.show();
cout<<endl;cout<<endl;
////////////////////
float box3l,box4l;
cout<<"please input box3'length:";
cin>>box3l;
cout<<"please input box4' length:";
cin>>box4l;

Box box3(box3l),box4;
box4.setLength(box4l);

cout<<"box3 is ";
box3.show();
cout<<endl;
cout<<"box4 is ";
box4.show();
cout<<endl;cout<<endl;
}



有5个学生,每个学生的数据包括学号,姓名,三门课成绩,从键盘输入5个学生的数据,要求计算输出.

1.每个学生3门课总成绩.(学生类里有个函数)

2.三门课每门课程的平均成绩.(单独写一个函数来算,写成静态成员)

#include <iostream>
#define N 5
using namespace std;

class Stud{
private:
int no;
char name[10];
double score[3];
public:
void setInfo(int n,char na[],double sc[]){
no=n;strcpy(name,na);
for(int k=0;k<3;k++){
score[k]=sc[k];
}
}
int getNo(){
return no;
}
double getCourse1(){return score[0];}
double getCourse2(){return score[1];}
double getCourse3(){return score[2];}

double average(){
double sum=0;
for(int k=0;k<3;k++){
sum=sum+score[k];
}
return (sum/3);

}
static void get3CourseAvg(Stud st[]){
cout<<"there is "<<sizeof(st)<<"student";
cout<<"first course'average:";
cout<<"second course'average:";
cout<<"third course'average:";

}
};

void main(){

Stud st
;
int i,n;
double s[3];
char na[10];
for(i=0;i<N;i++){
cout<<"plese input "<<i+1<<" student's no,name and 3 course'score:";
cin>>n>>na;
for(int k=0;k<3;k++)
cin>>s[k];
st[i].setInfo(n,na,s);
}
//////////////////
for(i=0;i<N;i++){
cout<<"the no:"<<st[i].getNo()<<"'s average:"<<st[i].average()<<endl;
}
}



居民基本数据:身份证,姓名,性别,出生日期,居民中的成年人又多两项数据:最高学历和职业,成人中的党员又多一项数据:党派类别.现要求建立三个类,让成人类继承居民类,而党员类继承成人类,并要求在每个类中都提供有数据输入和输出的功能.



时钟类,记录时\分\秒.重载他++运算符,每执行一次++运算,加时1秒,但要使计时过程能够自动进位.

#include<iostream>
#include<cmath>
#include<windows.h>
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()
{
if(Hour<10)
cout<<"0";
cout<<Hour<<":";
if(Minute<10)
cout<<"0";
cout<<Minute<<":";
if(Second<10)
cout<<"0";
cout<<Second<<endl;

//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();
while(true){
Sleep(1000);

system("cls");
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();
}



职工档案管理程序.把职工的档案数据和对数据的设置\修改\删除\添加等操作组成一个程序模块.程序通过这个模块-类的共有部门对档案数据进行处理,实现了面向对象程序设计的"封装"功能.

数据自己设计,越完整越好.

#include <iostream>
#include <fstream>
#include <string.h>
#include <conio.h>
#include <algorithm>

#include <vector>

using namespace std;

// 职工类Staff
class Staff
{

private:

char Id[20]; //工号
char name[20]; //姓名
char sex[10]; //性别
char dept[20]; //部门

public:

void setId(char newId[20])
{
strcpy(Id, newId);
}
char * getId()
{
return Id;
}

void setName(char newName[20])
{
strcpy(name, newName);
}
char * getName()
{
return name;
}

void setSex(char newSex[10])
{
strcpy(sex, newSex);
}
char * getSex()
{
return sex;
}

void setDept(char newDept[20])
{
strcpy(dept, newDept);
}
char * getDept()
{
return dept;
}

// 录入一个职工的档案数据
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;
}
};

bool pr(Staff s1, Staff s2)
{
if (strcmp(s1.getId(),s2.getId())==1)
{
return false;
}
else
{
return true;
}
}

// 职工档案数据管理类Staffmassage
class Staffmassage
{
private:
vector<Staff> staffs;
ifstream in;
ofstream out;

public:
Staffmassage();
~Staffmassage();
void ShowMenu();
void Find();
void Save();
void ModifyItem();
void RemoveItem();
void Sort();

// 根据职工姓名查找并返回职工档案数据
Staff *FindByName(char * name)
{
for(vector<Staff>::iterator it=staffs.begin(); it<staffs.end(); it++)
{

if (strcmp(name, it->getName()) == 0)
{
Staff s=*it;
return it;
}
}
return NULL;
}

// 根据职工工号查找并返回职工档案数据
Staff *FindByID(char * Id)
{
for(vector<Staff>::iterator it=staffs.begin(); it<staffs.end(); it++)
{

if (strcmp(Id, it->getId()) == 0)
{
return it;
}
}
return NULL;
}

// 显示全部职工的档案数据
void Display()
{

for(vector<Staff>::iterator it=staffs.begin(); it<staffs.end(); it++)
{

it->Show();
}
cout<<"\t输入任意字符继续...";
getch();
}

// 手工增加职工档案数据
void AddItem()
{
Staff s;
s.Input();

staffs.push_back(s);

cout<<"\t添加成功!"<<endl;
cout<<"\t输入任意字符继续...";
getch();
}

// 文件增加职工档案数据
void AddItem(Staff s)
{

staffs.push_back(s);
}
};

// 构造函数
Staffmassage::Staffmassage()
{
in.open("sort.txt");
if(!in)
cout<<"系统无任何职工档案数据!请先录入职工档案数据!"<<endl;
else
{
while(!in.eof())
{
Staff staff;
staff.ReadFile(in);
if(staff.getName()[0]=='\0') break;
AddItem(staff);
}
in.close();
cout<<"读取职工档案数据成功!"<<endl;
}
}

// 析构函数
Staffmassage::~Staffmassage()
{
// 将职工档案数据保存在文本文件中
Save();
// 删除存放职工档案数据的链表
staffs.clear();
}

// 显示系统菜单
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.安全退出系统 write by 郭毅棋 ┃"<<endl;
cout<<"\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;

cout<<"\n\t请选择:";
}

// 查找并显示职工档案数据
void Staffmassage::Find()
{
char name[20] ,Id[10];
int x;
Staff *s;
cout<<"\n\t━━━━━━━━━━━━━━━━━\n";
cout<<"\t 1.按职工的姓名查找 2.按职工学号查找";
cout<<"\n\t━━━━━━━━━━━━━━━━━\n\t请选择:";
cin>>x;
switch(x)
{
case 1:
{
cout<<"\t请输入要查找的职工的姓名:";
cin>>name;
if(s=(Staff*)(FindByName(name)))
{
s->Show();
cout<<"\t输入任意字符继续...";
getch();
}
else
{
cout<<"\t没有找到该姓名的职工!\n"<<endl;
cout<<"\t输入任意字符继续...";
getch();
}
}
break;
case 2:
{
cout<<"\t请输入要查找的职工的学号:";
cin>>Id;
if(s=FindByID(Id))
{
s->Show();
cout<<"\t输入任意字符继续...";
getch();
}
else
{
cout<<"\t没有找到该工号的职工!\n"<<endl;
cout<<"\t输入任意字符继续...";
getch();
}
}
break;
}
}

// 修改职工档案数据
void Staffmassage::ModifyItem()
{
char name[20];
Staff *s;
cout<<"\t请输入要修改的职工姓名:";
cin>>name;
if(s=(Staff *)FindByName(name))
{
cout<<"\t已找到职工的档案数据,请输入新的档案数据!"<<endl;
s->Input();
cout<<"\t修改成功!"<<endl;
cout<<"\t输入任意字符继续...";
getch();
}
else
{
cout<<"\t对不起,没有找到该职工档案数据!"<<endl;
cout<<"\t输入任意字符继续...";
getch();
}
}

// 根据职工姓名删除对应的档案数据
void Staffmassage::RemoveItem()
{
char name[20];
Staff *s;
cout<<"\t请输入要删除的职工的姓名:"<<endl;cin>>name;
if(s=(Staff *)FindByName(name))
{

staffs.erase(s);
cout<<"\t删除成功!"<<endl;
cout<<"\t输入任意字符继续...";
getch();
}
else
{
cout<<"\t对不起,没有找到该职工档案数据!"<<endl;
cout<<"\t输入任意字符继续...";
getch();
}
}

//bool greatermark(const Staff& s1,const Staff& s2)
//{
//return s1.getId() > s2.getId();
//}

// 对职工排序
void Staffmassage::Sort()
{
cout <<"\t正在排序(按工号从小到大)..."<<endl;
sort(staffs.begin(),staffs.end(),pr);
cout <<"\t对"<<staffs.size()<<"职工排序完成!"<<endl;
getch();
return;
}

// 保存职工档案数据到文本文件"sort.txt"
void Staffmassage::Save()
{
out.open("sort.txt");
for(vector<Staff>::iterator it=staffs.begin(); it<staffs.end(); it++)
{

out<<it->getId()<<"\t"<<it->getName()<<"\t"<<it->getSex()<<"\t"<<it->getDept()<<'\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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: