假设公司员工分为:雇员(Employee)、经理(Manager)、技术员(Technician)、销售员(Salesman)和销售经理(SalesManager),他们分别包括如下属性(其中的姓名长度不定):
Employee类:姓名、年龄、专业、固定工资
Manager类:姓名、年龄、固定工资、专业、级别、补贴
Technician类:姓名、年龄、固定工资、专业、工作时间、计时工资
Salesman类:姓名、年龄、固定工资、专业、销售额、销售提成比率
SalesManager类:姓名、年龄、固定工资、专业、补贴、销售额、销售提成比率
类中包括的成员函数(要完成操作有):
(1)构造函数;
(2)析构函数;
(3)输入函数;
(4)输出函数;
(5)计算工资函数,计算工资方法如表2:
表2 工资计算方法
人员类别 固定工资 补贴 计时工资 销售提成
Employee 1500 0 0 0
Manager 8000 2000 0 0
Technician 1000 0 100(或根据实际情况修改) 0
Salesman 500 0 0 5%(或根据实际情况修改)
SaleManager 2000 1000 0 2%(或根据实际情况修改)
提示:找到类之间的共性,用继承或多继承实现。
#include
#include
using namespace std;
class employee
{
protected:
string name,part;
int age;
double salary;
public:
employee(){
salary=1500;}
virtual void input()
{
cout<<"输入基本信息:"<<endl
<<"姓名:";cin>>name;cout<<"年龄:";cin>>age;
cout<<"专业:";cin>>part;
}
virtual void display()
{
cout<<"您的基本信息:"<<endl
<<"姓名:"<<name<<endl<<"年龄:"<<age<<endl
<<"专业:"<<part<<endl<<"基础工资:"<<salary<<endl;
}
virtual double salary_calculate()
{
return salary;
}
};
class manager:public employee //mamanger
{
protected:
int level;
double subsidy;
public:
manager(){
salary=8000;subsidy=2000;}
virtual void input(){
cout<<"输入基本信息:"<<endl
<<"姓名:";cin>>name;cout<<"年龄:";cin>>age;
cout<<"专业:";cin>>part;cout<<"级别:";cin>>level;
}
virtual void display(){
cout<<"您的基本信息:"<<endl
<<"姓名:"<<name<<endl<<"年龄:"<<age<<endl
<<"专业:"<<part<<endl<<"级别:"<<level<<endl
<<"基础工资:"<<salary<<endl<<"补贴:"<<subsidy<<endl;
}
virtual double salary_calculate()
{
return salary+subsidy;
}
};
class technician:public employee //technician
{
protected:
double worktime;
double timesalary;
public:
technician(){
salary=1000;timesalary=100;}
virtual void input(){
cout<<"输入基本信息:"<<endl
<<"姓名:";cin>>name;cout<<"年龄:";cin>>age;
cout<<"专业:";cin>>part;
}
virtual void display()
{
cout<<"您的基本信息:"<<endl
<<"姓名:"<<name<<endl<<"年龄:"<<age<<endl
<<"专业:"<<part<<endl
<<"基础工资:"<<salary<<endl<<"计时工资:"<<timesalary<<endl;
}
virtual double salary_calculate()
{
cout<<"请输入您的工作时间(以小时为单位):";cin>>worktime;
return salary+worktime*timesalary;
}
};
class salesman:public employee //saler
{
protected:
double value;
double percentage;
public:
salesman(){
salary=500;percentage=0.05;}
virtual void input()
{
cout<<"输入基本信息:"<<endl
<<"姓名:";cin>>name;cout<<"年龄:";cin>>age;
cout<<"专业:";cin>>part;
}
virtual void display()
{
cout<<"您的基本信息:"<<endl
<<"姓名:"<<name<<endl<<"年龄:"<<age<<endl
<<"专业:"<<part<<endl
<<"基础工资:"<<salary<<endl<<"销售提成:"<<percentage*100<<"%"<<endl;
}
virtual double salary_calculate()
{
cout<<"请输入您的销售额:";cin>>value;
return salary+value*percentage;
}
};
class salesmanager:public employee
{
protected:
double subsidy;
double value;
double percentage;
public:
salesmanager()
{
salary=2000;subsidy=1000;percentage=0.02;}
virtual void input()
{
cout<<"输入基本信息:"<<endl
<<"姓名:";cin>>name;cout<<"年龄:";cin>>age;
cout<<"专业:";cin>>part;
}
virtual void display()
{
cout<<"您的基本信息:"<<endl
<<"姓名:"<<name<<endl<<"年龄:"<<age<<endl
<<"专业:"<<part<<endl
<<"补贴:"<<subsidy<<endl<<"基础工资:"<<salary<<endl
<<"销售提成:"<<percentage*100<<"%"<<endl;
}
virtual double salary_calculate()
{
cout<<"请输入您的销售额:";cin>>value;
return salary+subsidy+value*percentage;
}
};
void salary_display(double s)
{
cout<<"您的工资预计:"<<s<<endl;
}
int main()
{
int judge;
double salary;
employee A;manager B;technician C;salesman D;salesmanager E;
cout<<endl;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I 员工系统 I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (1) 员工 "<<endl
<<" (2) 经理 "<<endl
<<" (3) 技术员 "<<endl
<<" (4) 销售员 "<<endl
<<" (5) 销售经理 "<<endl
<<endl
<<" 选择应聘的职位 :" ;
cin>>judge;
cout<<"您应聘的职位是:";
switch(judge)
{
case 1:cout<<"员工"<<endl;break;
case 2:cout<<"经理"<<endl;break;
case 3:cout<<"技术员"<<endl;break;
case 4:cout<<"销售员"<<endl;break;
case 5:cout<<"销售经理"<<endl;break;
}
switch(judge)
{
case 1:
A.input();A.display();salary=A.salary_calculate();break;
case 2:
B.input();B.display();salary=B.salary_calculate();break;
case 3:
C.input();C.display();salary=C.salary_calculate();break;
case 4:
D.input();D.display();salary=D.salary_calculate();break;
case 5:
E.input();E.display();salary=E.salary_calculate();break;
}
salary_display(salary);
cout<<"欢迎加入!"<<endl;
return 0; }
动物园管理程序设计。假设某动物园有20只笼子用于饲养宠物,包括猫和狗。动物园可以收容宠物,也可以被认领出去。猫和狗都有名字属性和“叫”的方法(方法内涵不同)。
要求用一个数据项(如数组等)管理这20只笼子和其中的宠物,另外编制一个函数bark(),用来根据实际对象调用他们“叫”的方法。
#include
#include
#include
#include //详情详见7-1,文件流操作以7-1为模板!
using namespace std;
class animals
{
protected:
string name;
string number;
string type;
bool isfull;
public:
virtual void add_pets(){
}
virtual void del_pets(){
}
void select(string sign)
{
isfull=true;
ifstream inData("dwy.txt",ios::in);
if (!inData)
{
cout <<endl <<"文件找不到!" <<endl;
system("pause");
return;
}
string str1;
string str;
while (inData>>name>>number)
{
getline(inData, str);
if (sign==number)
{
isfull=false;}
}
}
virtual void save_new(){
}
virtual void bark(){
}
};
class cat:public animals
{
public:
void bark()
{
cout<<"喵!"<<endl;
}
void save_new()
{
type="猫";
ofstream outData("dwy.txt", ios::app);
outData <<setiosflags(ios::left)<<setw(17)<<name<<" "<<setw(16)<<number<<" "<<setw(16)<<type<<endl;
outData.close();
}
void add_pets()
{
cout<<"请输入宠物信息:" <<endl;
cout<<"名称:";cin>>name;
cout<<"编号:";cin>>number;
select(number);
if(isfull==false){
cout<<"错误!该位置已经有宠物了!"<<endl;
}
else{
save_new();
bark();
cout<<"宠物信息已经保存!" <<endl;}
system("pause");
}
void del_pets()
{
{
string sign,str1,str; //定义字符串
bool flag = true; //布尔型变量初始为真
cout<<"你要取出的宠物姓名或编号:"<<endl; //输入要查找的相关信息
cin>>sign;
ofstream outData("temp.txt", ios::out); //磁盘文件的输出
ifstream inData("dwy.txt", ios::in); //输入
while (inData >>name >>number)
{
getline(inData, str); //接收一个字符串
if ((sign==name) || (sign==number))
{
cout <<"你想取出的宠物:"<<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl; //左对齐
flag = false;
break;
}
outData <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl;
}
if (flag)
{
cout <<endl <<"不存在此宠物!" <<endl <<endl;
}
else
{
while (getline(inData, str))
{
outData <<str <<endl;
}
outData.close();
inData.close();
ofstream out("dwy.txt", ios::out);
ifstream in("temp.txt", ios::in);
while (getline(in, str))
{
out <<str <<endl;
}
out.close(); //文件流关闭
in.close();
cout <<endl <<"已提出宠物!" <<endl <<endl;
}
system("pause");
}
}
};
class dog:public animals
{
public:
void bark()
{
cout<<"汪!"<<endl;
}
void save_new()
{
type="狗";
ofstream outData("dwy.txt", ios::app);
outData <<setiosflags(ios::left)<<setw(17)<<name<<" "<<setw(16)<<number<<" "<<setw(16)<<type<<endl;
outData.close();
}
void add_pets()
{
cout<<"请输入宠物信息:" <<endl;
cout<<"名称:";cin>>name;
cout<<"编号:";cin>>number;
select(number);
if(isfull==false){
cout<<"错误!该位置已经有宠物了!"<<endl;
}
else{
save_new();
bark();
cout<<"宠物信息已经保存!" <<endl;}
system("pause");
}
void del_pets()
{
{
string sign,str1,str; //定义字符串
bool flag = true; //布尔型变量初始为真
cout<<"你要取出的宠物姓名或编号:"<<endl; //输入要查找的相关信息
cin>>sign;
ofstream outData("temp.txt", ios::out); //磁盘文件的输出
ifstream inData("dwy.txt", ios::in); //输入
while (inData >>name >>number)
{
getline(inData, str); //接收一个字符串
if ((sign==name) || (sign==number))
{
cout <<"你想取出的宠物:"<<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl; //左对齐
flag = false;
break;
}
outData <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl;
}
if (flag)
{
cout <<endl <<"不存在此宠物!" <<endl <<endl;
}
else
{
while (getline(inData, str))
{
outData <<str <<endl;
}
outData.close();
inData.close();
ofstream out("dwy.txt", ios::out);
ifstream in("temp.txt", ios::in);
while (getline(in, str))
{
out <<str <<endl;
}
out.close(); //文件流关闭
in.close();
cout <<endl <<"已提出宠物!" <<endl <<endl;
}
system("pause");
}
}
};
int main()
{
int judge,judge1;
cat x;
dog y;
fstream oo("dwy.txt",ofstream::out);
while(1)
{
cout<<endl;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I 宋明桥 I | "<<endl
<<" | I の I | "<<endl
<<" | I 动物园 I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (1) 存放动物 "<<endl
<<" (2) 取出动物请按 "<<endl
<<" (3) 关闭请按 "<<endl
<<endl
<<" 选择 :" ;
cin>>judge;switch(judge)
{
case 1:
cout<<"存放"<<endl
<<"(1)猫"<<endl
<<"(2)狗"<<endl;
cin>>judge1;
switch(judge1)
{
case 1:
x.add_pets();
system("cls"); //清屏操作
break;
case 2:
y.add_pets();
system("cls"); //清屏操作
break;
}
break;
case 2:
cout<<"提取"<<endl
<<"(1)猫"<<endl
<<"(2)狗"<<endl;
cin>>judge1;
switch(judge1)
{
case 1:x.del_pets();break;
case 2:y.del_pets();break;
}
break;
case 3:return 0;
}}}
设计职工信息表
建立职工信息数据,包括职工编号、姓名、性别和年龄。
要求如下:
(1)根据职工信息表,建立只含有姓名和年龄的职工信息简表;
(2)使用继承的方法构造2个类,使用相应的对象数组放置10个职工信息。
(3)编写同名display()成员函数,用来输出数组的内容。
(4)另外编制一个函数print(),用来根据实际对象输出他们的内容。
#include
#include
#include
#include
using namespace std;
void Display();
void Printer();
void ShowMenu();
void brevity();
int Menu_select();
void Dispvemps();
void Dispvemp(); //构造的两个类
class employee
{
//成员变量
public:
char name[10],id[9];
char *GetName()
{
return name;
}
void SetName(char n[])
{
strcpy(name,n);
}
char * GetID()
{
return id;
}
void SetID(char i[])
{
strcpy(id,i);
}
virtual void Display()
{
cout<<"\t"<<GetID()<<"\t"<<GetName()<<endl;}
};
class employees:public employee
{
class employee;
protected:
char sex[4];
int age;
public:
char * GetSex()
{
return sex;
}
void SetSex(char s[])
{
strcpy(sex,s);
}
int GetAge()
{
return age;
}
void SetAge(int a)
{
age=a;
}
void WriteFile();
void ReadFile();
//virtual的实现
void Display()
{
cout<<GetID()<<"\t"<<GetName()<<"\t"<<GetSex()<<"\t"<<GetAge()<<endl;
}
};
vector<employee>vemp; //容器
vector<employees>vemps;
void Printer(employee& s)
{
s.Display();
}
void AddNew() //添加新数据
{
char ch[5];
int age;
employees emps;
for (int i=0;i<10;i++) //连续输入10次
{
cout<<"(请输入数字)——0为退出输入:"<<endl
<<endl
<<"编号:";
cin.getline(ch,'\n');
if (ch[0]=='0') break;
emps.SetID(ch);
cout<<"姓名:";
cin.getline(ch,'\n');
emps.SetName(ch);
cout<<"性别:";
cin.getline(ch,'\n');
emps.SetSex(ch);
cout<<"年龄:";
cin>>age;
emps.SetAge(age);
vemps.push_back(emps);
getchar();
emps.WriteFile();
system("cls"); //清屏操作
}
}
void employees::WriteFile() //写入
{
ofstream curFile("Workers.txt");
for(int i=0;i<3;i++)
{
curFile<<id<<'\n'<<name<<'\n'<<sex<<'\n'<<age<<'\n';
}
curFile.close();
}
void employees::ReadFile()
{
char line[81];
ifstream inFile("Workers.txt");
inFile.getline (line, 80);
if(inFile.eof())
{
cout<<"没有记录,请先手动打开文件输入信息"<<endl;}
while(!inFile.eof())
{
cout<<line<<endl;
inFile.getline (line, 80);
}
inFile.close ();
}
void brevity()
{
class employee;
employees emps;
int top=vemps.size();
int size=vemp.size();
if (top==0) //检测
{
cout<<"没有建立记录,请先建立记录信息。"<<endl;
return;
}
for(int i=size;i<top;i++)
{
vemp.push_back(emps);
strcpy(vemp[i].id,vemps[i].id);
strcpy(vemp[i].name,vemps[i].name);
}
cout<<"信息简表已生成,请继续"<<endl;
system("cls"); //清屏操作
}
void Dispvemps()
{
void Printer(employee&);
if(vemps.size()==0)
{
cout<<"没有输入信息,请先输入信息。"<<endl;
return;
}
else
{
cout<<"编号_姓名_性别_年龄"<<endl;
for(int i=0;i<vemps.size();i++)
Printer(vemps[i]);
}
}
void Dispvemp()
{
if (vemp.size()==0)
{
cout<<"没有生成新的简表,请先生成新的简表"<<endl;
return ;
}
else
{
cout<<"编号"
<<endl
<<"姓名"<<endl;
for(int i=0;i<vemp.size();i++)
Printer(vemp[i]);
}}
void ShowMenu() //显示菜单并进行处理
{
char Menu_sel();
void Dispvemps();
void Dispvemp();
void brevity();
while(1)
{
switch( Menu_sel())
{
case 'a':
AddNew();
break;
case 'b':
brevity();
break;
case 'c':
Dispvemps();
break;
case 'd':
Dispvemp();
break;
case 'e':
cout<<endl<<"再见!";
return;
default :
return;
}
}
}
char Menu_sel()
{
char s[4],ch;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I 通讯录 I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (a) 输入职工信息 "<<endl
<<" (b) 手动生成信息简表 "<<endl
<<" (c) 显示原始记录 "<<endl
<<" (d) 显示简表 "<<endl
<<" (e) 结束程序运行 "<<endl
<<endl
<<" 选择 :" ;
gets(s);
ch=*s;
if(ch<'a'|| ch>'e')
cout<<"错误!(重选a-e:)";
else
return ch;
}
int main() //主函数
{
ShowMenu();
}
求解有理数分式方程
有理数就是两个整数的比率,通常表示为a/b。其中a称为分子,b成为分母。要求:
(1)分母不能为0;
(2)分子和分母有公约数,应对其进行简化,也就是约分;
(3)对其进行四则运算;
(4)有理数可以比较大小;
(5)附加:重载<<和>>,能够把分数进行整体输入和输出。
#include
using namespace std;
class Rational
{
public:
int x;
int y;
Rational(int x1=0,int y1=1){
x=x1;y=y1;}
Rational operator+(Rational r);//重载加法运算符
Rational operator-(Rational r);//重载减法运算符
Rational operator*(Rational r);//重载乘法运算符
Rational operator/(Rational r);//重载除法运算符
friend istream & operator>>(istream& in,Rational &r);
friend ostream & operator<<(ostream& out,Rational &r);
};
istream & operator>>(istream& in,Rational &r)
{
in>>r.x>>r.y;
if(r.y==0){
cout<<"分母不能为0\n";
exit(1);
}
return in;
}
ostream & operator<<(ostream& out,Rational &r)
{
int i;
float m,n;
if(r.x<r.y)
for(i=r.x;i>1;i--)
{
if(r.x%i==0&&r.y%i==0)
{
m=r.x/i;
r.x=m;
r.y=n;
break;
}
return out
}
if(r.x>r.y)
for(i=r.y;i>1;i--)
{
if(r.x%i==0&&r.y%i==0)
{
m=r.x/i;
n=r.y/i;
r.x=m;
r.y=n;
break;
}}
if(r.y==1)
{
out<<"计算结果为:"<<r.x<<endl;
return out;
}
else
{
out<<"计算结果为:"<<r.x<<"/"<<r.y<<endl;
return out;
}}
Rational Rational::operator+(Rational r) //加
{
return Rational(x*r.y+y*r.x,y*r.y);
}
Rational Rational::operator-(Rational r) //减
{
return Rational(x*r.y-y*r.x,y*r.y);
}
Rational Rational::operator*(Rational r) //乘
{
return Rational(x*r.x,y*r.y);
}
Rational Rational::operator/(Rational r) //除
{
return Rational(x*r.y ,y*r.x);
system("pause");
}
int main(){
Rational A,B,C;
cout<<"请输入A的值"<<endl;
cin>>A;
cout<<"请输入B的值"<<endl;
cin>>B;
cout<<"两数之和为:"<<endl;
C=A+B;
cout<<C;
cout<<"两数之差为:"<<endl;
C=A-B;
cout<<C;
cout<<"两数之积为:"<<endl;
C=A*B;
cout<<C;
cout<<"两数之商为:"<<endl;
C=A/B;
cout<<C;
system("pause");
return 0;
}
设计字符串类
设计一个自己的字符串类(String)。要求:
(1)各种串操作(子串操作、串复制、串连接),对字符串的界限进行检查和处理;
(2)以复制方式实现串赋值,避免不同指针共享字符串的情况,提高数据的独立性;
(3)使用合适的运算符定义字符串的操作(例如,使用==、>=、<等等)
(4)可以定义一些高层次的操作,例如子串操作、模式匹配
#include
#include
using namespace std;
class String {
private:
char * Pstr;
public:
String(const char* p = NULL) {
if (p == NULL) {
Pstr = new char[1];
*Pstr = '\0';
} else {
Pstr = new char[strlen(p) + 1];
strcpy(Pstr, p); } }
String(const String& s): Pstr(new char[strlen(s.Pstr) + 1]) {
strcpy(Pstr, s.Pstr);}
~String() {
if (Pstr)
delete[] Pstr;}
String& operator=(const String& s) {
if (Pstr == s.Pstr)
return *this;
else if (NULL == this) {
delete Pstr;
Pstr = new char[strlen(s.Pstr) + 1];
strcpy(Pstr, s.Pstr);
} else {
strcpy(Pstr, s.Pstr);
}
return *this;}
friend ostream & operator<<(ostream &out, const String &s) {
out << s.Pstr;
return out;}
friend istream & operator >>(istream &in, String &s) {
in >> s.Pstr;
if(in)
return in;}
String operator+(const String &s2) {
char *p=new char[strlen(Pstr) + strlen(s2.Pstr)+1];
p=strcat(Pstr,s2.Pstr);
return String(p);}
void operator+=(const String &s2) {
strcat(Pstr, s2.Pstr); }
char& operator[](int n) {
return Pstr[n]; }
int Length() {
int n=strlen(Pstr) ;
return n;
}
bool operator==(const String &s2) {
return strcmp(Pstr, s2.Pstr) == 0;
}
bool operator<(const String &s2) {
return strcmp(Pstr, s2.Pstr) < 0; }
};
int main() {
String s1("Cprogram"), s2("Cprogram"), s3(s2), s4, s5;
cout<<"可输出字符测试:(例如:hello)"<<endl;
s3 = "Hello!";
cout << "测试前:" << s3 << endl;
cout << "源字符为:" << s1 << endl;
s3 = s2;
cout << "复制前:" << s3 << endl;
s3 += s2;
cout << "复制后:" << s3 << endl;
cout<<endl
<<endl
<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I 字符串类 I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<endl
<<"请输入字符:"<<endl;
cin >> s4;
cout << "已输入:" << s4 << endl;
s5 = s3 + s4;
cout << "输入后:" << s5 << endl;
s5[0] = 'g';
cout << "输入后(首字符更为g):" << s5 << endl;
cout << "长度:" << s5.Length() << endl;
cout<<"源字符是否与输入字符长度相同:"<<endl;//判断字符串长度
cout << boolalpha << (s3 == s1) << endl;
cout<<"源字符是否短于输入字符:" <<endl;
cout << boolalpha << (s3 < s1) << endl;
return 0;
}
修改实验2中定义的集合类,将部分方法修改为重载运算,包括:
(1)Add修改为<<,功能是添加一个元素;
(2)Remove修改为>>,功能是一个元素;
(3)Assign修改为=,功能是集合赋值;
(4)EqualTo修改为==,功能是判别集合是否相等(即是否有相同的元素);
(5)Intersect修改为*,功能是求集合的交集;
(6)Union修改为+,功能是求集合的并集。
此外,为该类添加如下重载运算符方法:
(1)!=:功能是判别两个集合是否不相等;
(2)<:功能是判别一个集合是否为另一个集合的真子集;
(3)<=:功能判别一个集合是否为另一个集合的子集。
(4)-:功能是计算两个集合的差集。
#include
using namespace std;
const int SetCapacity = 100;
class set
{
int elements[SetCapacity]; //数据区
int _size; //元素个数
int k; //计数变量
public:
set(int *a,int size)
{
_size=size;
for(k=0;k<_size;k++)
{
elements[k]=a[k];
}
}; //构造函数
set(const set& src)
{
this->_size=src._size;
for(k=0;k<_size;k++)
{
this->elements[k]=src.elements[k];
}
}
bool Contains(int el) //是否包含元素el
{
for(k=0;k<_size;k++)
{
if(elements[k]==el)
{
cout<<"Found "<<el<<" at: elements["<<k<<"]."<<endl;
return true;
}
}
cout<<"Not found."<<endl;
return false;
}
set operator <<(int el) //添加元素el
{
k=_size;
elements[k]=el;
_size++;
cout<<"Successfully added."<<endl;
}
set operator >>(int el) //删除元素el
{
bool search;
for(k=0;k<_size;k++)
{
if(elements[k]==el)
{
search=true;
break;
}
}
if(search==false)
{
cout<<"Not found."<<endl;
}
else
{
cout<<"Found "<<el<<" at: elements["<<k<<"]."<<endl;
int k0;
for(;k<_size-1;k++)
{
elements[k]=elements[k+1];
}
_size--;
}
}
set operator =(set& st) //将st赋值给当前集合
{
_size=st._size;
for(k=0;k<_size;k++)
{
elements[k]=st.elements[k];
}
}
set operator ==(set& st) //判别集合st与当前集合是否相同(元素相同)
{
if(st._size!=_size)
{
cout<<"The set doesn't equals to the other set."<<endl;
}
else
{
bool judge=true;
for(k=0;k<_size;k++)
{
if(st.elements[k]!=elements[k])
{
judge=false;
}
}
if(judge==true)
{
cout<<"The set equals to the other set."<<endl;
}
else
{
cout<<"The set doesn't equals to the other set."<<endl;
}
}
}
bool Empty() //集合是否为空
{
if(_size==0)
{
cout<<"The set is empty."<<endl;
return true;
}
return false;
}
set operator *(set& st) //求集合st 与当前集合的交集
{
int temp[SetCapacity];
int tempsize=0;
int l;
int tempk=0;
for(k=0;k<_size;k++)
{
for(l=0;l<st._size;l++)
{
if(elements[k]==st.elements[l])
{
temp[tempk]=elements[k];
tempsize++;
tempk++;
}
}
}
return set(temp,tempsize);
}
set operator +(set& st) //求集合st 与当前集合的并集
{
int temp[SetCapacity];
int tempsize=_size;
int l;
int tempk;
bool judge=true;
k=0;
for(tempk=0;tempk<tempsize;tempk++)
{
temp[tempk]=elements[k];
k++;
}
for(l=0;l<st._size;l++)
{
for(k=0;k<_size;k++)
{
if(elements[k]==st.elements[l])
{
judge=false;
break;
}
}
if(judge==true)
{
temp[tempk]=st.elements[l];
tempk++;
tempsize++;
}
judge=true;
}
return set(temp,tempsize);
}
void print()
{
for(k=0;k<_size;k++)
{
cout<<elements[k]<<" ";
}
cout<<endl;
}
};
int main()
{
int a[5]={
1,2,3,4,5};
int b[6]={
3,4,5,6,7,8};
int size1=5;
int size2=6;
set s0(a,size1);
set s2(a,size1);
set s3(b,size2);
s0.print();
s0.Contains(5);
s0.Contains(12);
s0<<6;
s0.print();
s0>>2;
s0.print();
set s1(s0);
s1==s0;
s2==s0;
set s4=s2*s3;
set s5=s2+s3;
s4.print();
s5.print();
s5=s4;
s5.print();
return 0;
}
修改实验2中定义的大整数类,实现大整数的赋值、加、减、取负、大于、小于、大于等于、小于等于、自加、相等、不相等运算的重载,并为其增加输入和输出重载运算符方法。
#include
#include
#include
#include //reverse函数所需添加的头文件
using namespace std;
/*大整数类*/
class BigInt
{
private:
inline int compare(string s1, string s2)
{
if(s1.size() < s2.size())
return -1;
else if(s1.size() > s2.size())
return 1;
else
return s1.compare(s2);
}
public:
bool flag;//true表示正数,false表示负数,0默认为正数
string values;//保存所有位上的数字
BigInt():values("0"),flag(true){
};//构造函数
BigInt(string str)//类型转换构造函数(默认为正整数)
{
values = str;
flag = true;
}
public:
friend ostream& operator << (ostream& os, const BigInt& bigInt);//重载输出操作符
friend istream& operator>>(istream& is, BigInt& bigInt);//输入操作符重载
BigInt operator+(const BigInt& rhs);//加法操作重载
BigInt operator-(const BigInt& rhs);//减法操作重载
BigInt operator*(const BigInt& rhs);//乘法操作重载
BigInt operator/(const BigInt& rhs);//除法操作重载
BigInt operator%(const BigInt& rhs);//求余操作重载
};
/*重载流提取运算符'>>',输出一个整数*/
ostream& operator << (ostream& os, const BigInt& bigInt)
{
if (!bigInt.flag)
{
os << '-';
}
os << bigInt.values;
return os;
}
/*重载流插入运算符'>>',输入一个正整数*/
istream& operator >> (istream& is, BigInt& bigInt)
{
string str;
is >> str;
bigInt.values = str;
bigInt.flag = true;
return is;
}
/*
两个正整数相加
*/
BigInt BigInt::operator+(const BigInt& rhs)
{
BigInt ret;
ret.flag = true;//正整数相加恒为正数
string lvalues(values), rvalues(rhs.values);
//处理特殊情况
if (lvalues == "0")
{
ret.values = rvalues;
return ret;
}
if (rvalues == "0")
{
ret.values = lvalues;
return ret;
}
//调整s1与s2的长度
unsigned int i, lsize, rsize;
lsize = lvalues.size();
rsize = rvalues.size();
if (lsize < rsize)
{
for (i = 0; i < rsize - lsize; i++)//在lvalues左边补零
{
lvalues = "0" + lvalues;
}
}
else
{
for (i = 0; i < lsize - rsize; i++)//在rvalues左边补零
{
rvalues = "0" + rvalues;
}
}
//处理本质情况
int n1, n2;
n2 = 0;
lsize = lvalues.size();
string res = "";
reverse(lvalues.begin(), lvalues.end());//颠倒字符串,以方便从低位算起计算
reverse(rvalues.begin(), rvalues.end());
for (i = 0; i < lsize; i++)
{
n1 = (lvalues[i] - '0' + rvalues[i] - '0' + n2) % 10;//n1代表当前位的值
n2 = (lvalues[i] - '0' + rvalues[i] - '0' + n2) / 10;//n2代表进位
res = res + char(n1 + '0');
}
if (n2 == 1)
{
res = res + "1";
}
reverse(res.begin(), res.end());
ret.values = res;
return ret;
}
/*
两个正整数相减
*/
BigInt BigInt::operator-(const BigInt& rhs)
{
BigInt ret;
string lvalues(values), rvalues(rhs.values);
//负数减负数
if(flag==false&&rhs.flag==false)
{
string tmp = lvalues;
lvalues = rvalues;
rvalues = tmp;
}
//负数减正数
if(flag==false&&rhs.flag==true)
{
BigInt res(lvalues);
ret=res+rhs;
ret.flag = false;
return ret;
}
if(flag==true&&rhs.flag==false)
{
BigInt rel(lvalues),res(rhs.values);
ret=rel+res;
ret.flag = true;
return ret;
}
//处理特殊情况
if (rvalues == "0")
{
ret.values = lvalues;
ret.flag = true;
return ret;
}
if (lvalues == "0")
{
ret.values = rvalues;
ret.flag = false;
return ret;
}
//调整s1与s2的长度
unsigned int i, lsize, rsize;
lsize = lvalues.size();
rsize = rvalues.size();
if (lsize < rsize)
{
for (i = 0; i < rsize - lsize; i++)//在lvalues左边补零
{
lvalues = "0" + lvalues;
}
}
else
{
for (i = 0; i < lsize - rsize; i++)//在rvalues左边补零
{
rvalues = "0" + rvalues;
}
}
//调整使‘-’号前边的数大于后边的数
int t = lvalues.compare(rvalues);//相等返回0,str1str2返回正数
if (t < 0)
{
ret.flag = false;
string tmp = lvalues;
lvalues = rvalues;
rvalues = tmp;
}
else if (t == 0)
{
ret.values = "0";
ret.flag = true;
return ret;
}
else
{
ret.flag = true;
}
//处理本质情况
unsigned int j;
lsize = lvalues.size();
string res = "";
reverse(lvalues.begin(), lvalues.end());//颠倒字符串,以方便从低位算起计算
reverse(rvalues.begin(), rvalues.end());
for (i = 0; i < lsize; i++)
{
if (lvalues[i] < rvalues[i])//不足,向前借一维
{
j = 1;
while(lvalues[i+j] == '0')
{
lvalues[i+j] = '9';
j++;
}
lvalues[i+j] -= 1;
res = res + char(lvalues[i] + ':' - rvalues[i]);
}
else
{
res = res + char(lvalues[i] - rvalues[i] + '0');
}
}
reverse(res.begin(), res.end());
res.erase(0, res.find_first_not_of('0'));//去掉前导零
ret.values = res;
return ret;
}
/*
两个正整数相乘
*/
BigInt BigInt::operator*(const BigInt& rhs)
{
BigInt ret;
string lvalues(values), rvalues(rhs.values);
//处理0或结果正负
if (lvalues == "0" || rvalues == "0")
{
ret.values = "0";
ret.flag = true;
return ret;
}
if(flag==false||rhs.flag==false)
{
ret.flag=false;
}
//处理特殊情况
unsigned int lsize, rsize;
lsize = lvalues.size();
rsize = rvalues.size();
string temp;
BigInt res, itemp;
//让lvalues的长度最长
if (lvalues < rvalues)
{
temp = lvalues;
lvalues = rvalues;
rvalues = temp;
lsize = lvalues.size();
rsize = rvalues.size();
}
//处理本质情况
int i, j, n1, n2, n3, t;
reverse(lvalues.begin(), lvalues.end());//颠倒字符串
reverse(rvalues.begin(), rvalues.end());
for (i = 0; i < rsize; i++)
{
temp = "";
n1 = n2 = n3 = 0;
for (j = 0; j < i; j++)
{
temp = temp + "0";
}
n3 = rvalues[i] - '0';
for (j = 0; j < lsize; j++)
{
t = (n3*(lvalues[j] - '0') + n2);
n1 = t % 10;//n1记录当前位置的值
n2 = t / 10;//n2记录进位的值
temp = temp + char(n1 + '0');
}
if (n2)
{
temp = temp + char(n2 + '0');
}
reverse(temp.begin(), temp.end());
itemp.values = temp;
res = res + itemp;
}
ret.values = res.values;
return ret;
}
/*
两个正整数相除
*/
BigInt BigInt::operator/(const BigInt& rhs)
{
BigInt ret;
string lvalues(values), rvalues(rhs.values);
string quotient;
string temp;
//处理特殊情况
if(rvalues == "0")
{
ret.values = "error";//输出错误
ret.flag = true;
return ret;
}
if(lvalues == "0")
{
ret.values = "0";
ret.flag = true;
return ret;
}
if(compare(lvalues, rvalues) < 0)
{
ret.values = "0";
ret.flag = true;
return ret;
}
else if(compare(lvalues, rvalues) == 0)
{
ret.values = "1";
ret.flag = true;
return ret;
}
else
{
//处理本质情况
unsigned int lsize, rsize;
lsize = lvalues.size();
rsize = rvalues.size();
int i;
if(rsize > 1) temp.append(lvalues, 0, rsize-1);
for(i = rsize - 1; i < lsize; i++)
{
temp = temp + lvalues[i];
//试商
for(char c = '9'; c >= '0'; c--)
{
BigInt t = (BigInt)rvalues * (BigInt)string(1, c);
BigInt s = (BigInt)temp - t;
if(s.flag == true)
{
temp = s.values;
quotient = quotient + c;
break;
}
}
}
}
//去除前导零
quotient.erase(0, quotient.find_first_not_of('0'));
ret.values = quotient;
ret.flag = true;
return ret;
}
/*
两个正整数取余
*/
BigInt BigInt::operator%(const BigInt& rhs)
{
BigInt ret,kj(values),ki(rhs.values);
string lvalues(values), rvalues(rhs.values);
string quotient;
string temp;
//处理特殊情况
if(rvalues == "0")
{
ret.values = "error";//输出错误
ret.flag = true;
return ret;
}
if(lvalues == "0")
{
ret.values = "0";
ret.flag = true;
return ret;
}
if(compare(lvalues, rvalues) < 0)
{
if(flag==false)
{
ret.values=(ki-kj).values;
ret.flag = true;
return ret;
}else{
ret.values = lvalues;
ret.flag = true;
return ret;
}
}
else if(compare(lvalues, rvalues) == 0)
{
ret.values = "0";
ret.flag = true;
return ret;
}
else
{
//处理本质情况
unsigned int lsize, rsize;
lsize = lvalues.size();
rsize = rvalues.size();
int i;
if(rsize > 1) temp.append(lvalues, 0, rsize-1);
for(i = rsize - 1; i < lsize; i++)
{
if(temp=="0"){
temp=lvalues[i];
}else{
temp = temp + lvalues[i];
}
//试商
for(char c = '9'; c >= '0'; c--)
{
BigInt t = (BigInt)rvalues * (BigInt)string(1, c);
BigInt s = (BigInt)temp - t;
if(s.flag == true)
{
//cout<
temp = s.values;
quotient = quotient + c;
break;
}
}
}
}
//去除前导零
quotient.erase(0, quotient.find_first_not_of('0'));
ret.values = temp;
ret.flag = true;
return ret;
}
/*
欧几里德求GCD
*/
BigInt gcd(BigInt a,BigInt b)
{
BigInt stemp;
//cout<
//cout<
if((a-b).flag==false)//判断大小
{
stemp.values=a.values;
a.values=b.values;
b.values=stemp.values;
}
if(b.values=="0") return a;
else return gcd(b,a%b);
}
/*
快速幂
*/
BigInt fast(BigInt a,BigInt b)
{
BigInt aa=a,t("1"),k("2");
// int b2=atoi(b1[lsize-1].c_str());
while(b.values!="0")
{
if((b%k).values!="0")
{
t=t*aa;
}
aa=aa*aa;
b=b/k;
}
return t;
}
/*
快速幂模
*/
BigInt mod_fast(BigInt a,BigInt b,BigInt p)
{
BigInt aa=a,t("1"),k("2");
// int b2=atoi(b1[lsize-1].c_str());
while(b.values!="0")
{
if((b%k).values!="0")
{
t=(t%p)*(aa%p)%p;
}
aa=(aa%p)*(aa%p)%p;
b=b/k;
}
return t%p;
}
/*
扩展欧几里德实现乘法逆
*/
BigInt extgcd(BigInt a, BigInt b, BigInt& x, BigInt& y)
{
BigInt d(a.values);
if(b.values != "0"){
d = extgcd(b, a % b, y, x);
y = y-(a / b) * x;
}else {
x.values = "1";
y.values = "0";
}
return d;
}
BigInt mod_inverse(BigInt a, BigInt m)
{
BigInt x, y;
extgcd(a, m, x, y);
if(x.flag==false)
{
x.flag=true;
x=m-x;
}
return (m + x % m) % m;
}
int main()
{
BigInt a,b,n;
char op;
while(1)
{
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I 大整数计算器I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (1) 计算(a+b)&(a+b)mod n "<<endl
<<" (2) 计算(a-b)&(a-b)mod n "<<endl
<<" (3) 计算(a*b)&(a*b)mod n "<<endl
<<" (4) 计算(a/b)&(a/b)mod n "<<endl
<<" (5) 计算(a%b)&(a%b)mod n "<<endl
<<" (6) 计算(a^b)&(a^b)mod n "<<endl
<<" (7) 计算 GCD(a,b) "<<endl
<<" (8) 计算a和n乘法逆值 "<<endl
<<endl
<<" 选择 :" ;
cin >> op;
switch(op)
{
case '1':
cout<<"请输入a、b和n的值:\n";
cin>>a>>b>>n;
cout<<endl;
cout<<"a+b的值:"<<a+b<<endl;
cout<<"(a+b)mod n的值: "<< (a+b)%n<<endl<<endl<<endl<<endl;
break;
case '2':
cout<<"请输入a、b和n的值:\n";
cin>>a>>b>>n;
cout<<endl;
cout<<"a-b的值:"<<a-b<<endl;
cout<<"(a-b)mod n的值: "<< (a-b)%n<<endl<<endl<<endl<<endl;
break;
case '3':
cout<<"请输入a、b和n的值:\n";
cin>>a>>b>>n;
cout<<endl;
cout<<"a*b的值:"<<a*b<<endl;
cout<<"(a*b)mod n的值: "<< (a*b)%n<<endl<<endl<<endl<<endl;
break;
case '4':
cout<<"请输入a、b和n的值:\n";
cin>>a>>b>>n;
cout<<endl;
cout<<"a/b的值:"<<a/b<<endl;
cout<<"(a/b)mod n的值: "<< (a/b)%n<<endl<<endl<<endl<<endl;
break;
case '5':
cout<<"请输入a、b和n的值:\n";
cin>>a>>b>>n;
cout<<endl;
cout<<"a%b的值:"<<a%b<<endl;
cout<<"(a%b)mod n的值: "<< (a%b)%n<<endl<<endl<<endl<<endl;
break;
case '6':
cout<<"请输入a、b和n的值:\n";
cin>>a>>b>>n;
cout<<endl;
cout<<"a^b的值:"<<fast(a,b)<<endl;
cout<<"(a^b)mod n的值: "<<mod_fast(a,b,n)<<endl<<endl<<endl<<endl;
break;
case '7':
cout<<"请输入a和b的值:\n";
cin>>a>>b;
cout<<endl;
cout<<"GCD(a,b)的值:"<<gcd(a,b)<<endl<<endl<<endl<<endl;
break;
case '8':
cout<<"请输入a和n的值:\n";
cin>>a>>n;
cout<<endl;
cout<<"a和n的乘法逆: "<< mod_inverse(a,n)<<endl<<endl<<endl<<endl;
break;
default:break;
}
}
return 0;}
修改实验 2 中定义的产品类,使该类是可流的。然后,在 main 函数中建立一个由“进货”和“销售”构成的主菜单。进货部分由键盘输入几种商品,用文件流保存到一个产品文件;销售部分读入产品文件中的数据,实现一个二级销售菜单,演示用户任意购买各种商品的过程,并重新更新库存。
题6:修改实验3中定义的电子时钟类,用++,+来修改以有的函数,以实现时间、日期的自加1和加n。
#include
#include
#include
#include
#include //设置流操作符
using namespace std;
class GoodsInfo
{
public:
char * name ; //商品名称
int number; //商品编号
float cost_p; //成本单价
float unit_p; //销售单价
int num_all; //共计商品件数
int num_now; //现存商品件数
int num_sell; //已售出商品件数
float cost_all; //进货总计成本价
float profit; //已销售商品获得的利润
GoodsInfo * next; //指向下一个商
GoodsInfo(); //构造函数
GoodsInfo(char * a,int b,float c,float u,int all,int sell) ; //重载构造函数
void operator= (const GoodsInfo &right); //重载=
void setInfo(char * a,float c,float u,int all,int sell) ; //数据载入
char* getname(){
return name;} //返回姓名用于查找 //显示商品信息
void Amend(GoodsInfo *);
void show(GoodsInfo *);
~GoodsInfo()
{
delete [] name;}
};
class GoodList :public GoodsInfo
{
public:
GoodList(); //构造函数,指针初始化
~GoodList(); //析构函数,释放
GoodsInfo * head; //头指针
GoodsInfo * p; //活动指针1
GoodsInfo * p2; //活动指针2(指向最后一个)
void create(); //创建函数
void showinfo(GoodsInfo *); //显示
GoodsInfo * search(); //查找
void saveInfo(); //信息存档
void getInfo(); //上传信息
void addInfo(GoodsInfo *); //添加数据
void sort(); //按销量显示
void menu();
void delete_(); //删除节点
};
GoodsInfo::GoodsInfo() //构造函数
{
name=new char [21];
number=cost_p=unit_p=num_all=num_now=num_sell=cost_all=profit=0;
next=NULL;
}
GoodsInfo::GoodsInfo(char * a,int b,float c,float u,int all,int sell) //重载构造函数
{
strcpy(name,a);
number=b;
cost_p=c;
unit_p=u;
num_all=all;
num_sell=sell;
num_now=all-sell;
cost_all=c*all;
profit=(u-c)*sell;
next=NULL;
}
void GoodsInfo::operator= (const GoodsInfo &right) //重载=
{
strcpy(name,right.name);
number=right.number ;
cost_p=right.cost_p;
unit_p=right.unit_p ;
num_all=right.num_all ;
num_sell=right.num_sell ;
num_now=right.num_now ;
cost_all=right.cost_all;
profit=right.profit ;
}
void GoodsInfo::setInfo(char * a,float c,float u,int all,int sell) //数据载入
{
strcpy(name,a);
cost_p=c;
unit_p=u;
num_all=all;
num_sell=sell;
num_now=all-sell;
cost_all=c*all;
profit=(u-c)*sell;
next=NULL;
}
void GoodsInfo::show(GoodsInfo * h) //显示
{
if(h==NULL) cout<<"NULL!\n";
else
{
cout<<"-----------------------------------\n"
<<"商品名: "<<h->name<<endl
<<"编号: "<<h->number<<endl
<<"成本: "<<h->cost_p<<endl
<<"销售单价: "<<h->unit_p<<endl
<<"销量:"<<h->num_sell <<endl
<<"库存: "<<h->num_now<<endl
<<"已获得利润:"<<h->profit<<endl;
}
}
void GoodsInfo:: Amend(GoodsInfo * h) //修改信息
{
int F;
char S;
cout<<"~~~~~~~~~~~~~~~~~~\n"
<<"^2.商品成本单价 ^"<<endl
<<"^3.商品销售单价 ^"<<endl
<<"^4.进货数量 ^"<<endl
<<"^5.销售数量 ^"<<endl
<<"~~~~~~~~~~~~~~~~~~\n";
do
{
cout<<"请选择需要修改的项目编号:"<<endl;
cin>>F;
switch(F)
{
case 1 :{
cout<<"~~~~~~~~~~~~~~~~~~~~\n"
<<"|请输入修改后的名称|\n"
<<"~~~~~~~~~~~~~~~~~~~~";
cin.ignore ();
cin.getline(h->name,21);break;}
case 2 :{
cout<<"~~~~~~~~~~~~~~~~~~~~~~~\n"
<<"|请输入修改后的成本价: \n"
<<"~~~~~~~~~~~~~~~~~~~~~~~";
cin>>h->cost_p;break;}
case 3 :{
cout<<"请输入修改后的销售单价: ";
cin>>h->unit_p;break;}
case 4 :{
cout<<"请输入修改后的进货量: ";
cin>>h->num_all;break;}
case 5 :{
cout<<"请输入修改后的销售量: ";
cin>>h->num_sell;break;}
}
h->cost_all=h->cost_p*(h->num_all);
h->num_now=h->num_all -(h->num_sell);
h->profit=(h->unit_p -(h->cost_p ))*h->num_sell;
cout<<"是否需要继续修改?";
cin>>S;
}
while(S=='Y');
}
GoodList::GoodList() //构造函数
{
p=new GoodsInfo [sizeof(GoodsInfo)];
head=NULL;
p2=NULL;
p->next =NULL;
}
GoodList::~GoodList() //析构函数
{
while(head!=NULL)
{
p=head;
head=p->next ;
delete [] p;
}
}
void GoodList::create() //创建
{
int n=0;
char f='Y';
cout<<"请输入商品信息:"<<endl;
while(f=='Y')
{
n+=1;
if(n==1) head=p;
else
{
p=new GoodsInfo [sizeof(GoodsInfo)];
p2->next=p;
}
cout<<"商品名称: ";
if(n==1) cin.ignore ();
cin.getline (p->name,21);
cout<<"商品编号:";
cin>>p->number;
cout<<"成本价: ";
cin>>p->cost_p;
cout<<"销售价: ";
cin>>p->unit_p;
cout<<"共计商品件数: ";
cin>>p->num_all;
cout<<"已售出商品件数: ";
cin>>p->num_sell;
p->num_now=p->num_all-(p->num_sell);
p->cost_all=p->cost_p*(p->num_all);
p->profit=((p->unit_p)-(p->cost_p))*(p->num_sell);
p2=p;
cout<<"是否继续输入?('Y'确认保存,'N'返回主菜单)";
cin>>f;
cin.ignore();
}
if(head!=NULL) p2->next=NULL; //P2指向最后一个
}
GoodsInfo * GoodList::search() //查找
{
if(head==NULL) {
cout<<"数据为空!\n";return NULL;}
else
{
GoodsInfo *h;
h=head;
int f;
cout<<"~~~~~~~~~~~~~~~~\n"
<<"请选择查找方式: \n"
<<"1.按商品编号 \n" <<"2.按商品名称 "<<endl
<<"~~~~~~~~~~~~~~~~\n";
cin>>f;
switch(f)
{
case 1:
{
int x,y;
cout<<"请输入需查找的商品编号:";
cin>>x;
while(h!=NULL&&(x!=(y=h->number)))
h=h->next;
if(x==y) return h;
else
{
cout<<"没有找到该商品!\n";
return NULL;}
}
case 2:
{
char x[21];
int c;
cout<<"请输入需查找的商品名称:";
cin.ignore ();
cin.getline(x,21);
while(h!=NULL&&(c=strcmp(x,h->name))!=0)
h=h->next;
if(c==0) return h;
else {
cout<<"没有找到该商品!\n";return NULL;}
}
}
}
}
void GoodList::showinfo(GoodsInfo * h) //显示信息
{
cout<<setw(50)<<"商品名称"<<setw(10)<<"编号"<<setw(8)<<"进价"<<setw(8)<<"售价"
<<setw(8)<<"销量"<<setw(8)<<"库存"<<setw(12)<<"已得利润"<<endl;
while(h!=NULL)
{
cout<<setw(10)<<h->name<<setw(10)<<h->number<<setw(8)<<h->cost_p<<setw(8)<<h->unit_p
<<setw(8)<<h->num_sell<<setw(8)<<h->num_now<<setw(12)<<h->profit<<endl;
h=h->next;
}
}
void GoodList::saveInfo() //存档
{
fstream File;
File.open("GoodsInformation.txt",ios::out|ios::binary);
if(!File)
{
cout<<"文件打开失败!\n";
exit(0);
}
p=head;
GoodsInfo X;
File<<"商品名称 编号 进价 售价 总量 销量 库存 已得利润 \n";
while(p!=NULL)
{
X=*p;
File<<X.name<<" "<<X.number<<" "<<X.cost_p<<" "<<X.unit_p<<" "
<<X.num_all <<" "<<X.num_sell<<" "<<X.num_now <<" "<<X.profit<<endl;
p=p->next;
}
cout<<"文件保存成功!\n";
File.close();
}
void GoodList::getInfo() //上传
{
fstream File;
File.open("GoodsInformation.txt",ios::in);
if(!File)
{
cout<<"文件打开失败!\n";
exit(0);
}
char s[81];
GoodsInfo h;
File.getline (s,81);
int n=0;
while(!File.eof ())
{
if(File.fail()) break;
n+=1;
if(n==1) head=p;
else
{
p=new GoodsInfo [sizeof(GoodsInfo)];
p2->next=p;
}
File>>h.name>>h.number>>h.cost_p>>h.unit_p
>>h.num_all>>h.num_sell>>h.num_now
>>h.profit;
*p=h;
p2=p;
}
cout<<"上传显示完毕!"<<endl;
File.close();
}
void GoodList::addInfo(GoodsInfo * h) //添加
{
cout<<"请输入商品信息:"<<endl;
p=new GoodsInfo [sizeof(GoodsInfo)];
h->next=p;
cin.ignore();
cout<<"商品名称: ";
cin.getline (p->name,21);
cout<<"商品编号:";
cin>>p->number;
cout<<"成本价: ";
cin>>p->cost_p;
cout<<"销售价: ";
cin>>p->unit_p;
cout<<"共计商品件数: ";
cin>>p->num_all;
cout<<"已售出商品件数: ";
cin>>p->num_sell;
p->num_now=p->num_all-(p->num_sell);
p->cost_all=p->cost_p*(p->num_all);
p->profit=((p->unit_p)-(p->cost_p))*(p->num_sell);
p2=p;
p2->next=NULL;
}
void GoodList::delete_() //删除节点
{
if(head==NULL)
cout<<"NULL!\n";
else
{
cout<<"请输入需要删除的商品编号:";
int n;
GoodsInfo * h;
cin>>n;
h=head;
while(h->next!=NULL&&h->number!=n)
{
p=h;
h=h->next;
}
if(h->number==n)
{
char F;
cout<<"是否确定删除该节点?";
cin>>F;
if(F=='Y')
{
if(h==head) head=h->next;
else p->next=h->next;
delete [] h;
cout<<"删除成功!\n";
}
}
else cout<<"未找到该节点!\n";
}
}
void GoodList::sort () //按销量排序显示
{
GoodsInfo *h1,*h2,t;
int n;
if(head==NULL) cout<<"NULL!\n";
else
{
int F;
cout<<"~~~~~~~~~~~~~~~~\n"
<<"请选择排序方式:\n"
<<"1.按销量降序排列\n"
<<"2.按利润降序排列\n"
<<"~~~~~~~~~~~~~~~~\n";
cin>>F;
switch(F)
{
case 1:
for(h1=head;h1->next!=NULL;h1=h1->next)
{
n=h1->num_sell;
for(h2=h1->next;h2!=NULL;h2=h2->next)
{
if((h2->num_sell)>(h1->num_sell))
n=h2->num_sell;
if(n!=h1->num_sell)
{
t=*h2;*h2=*h1;*h1=t;}
}
}break;
case 2:
for(h1=head;h1->next!=NULL;h1=h1->next)
{
n=h1->profit;
for(h2=h1->next;h2!=NULL;h2=h2->next)
{
if((h2->profit)>(h1->profit))
n=h2->profit;
if(n!=h1->profit)
{
t=*h2;*h2=*h1;*h1=t;}
}
}break;
}
}
showinfo(head);
}
void GoodList::menu() //菜单
{
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I商场管理系统 I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (1) 创建 "<<endl
<<" (2) 添加 "<<endl
<<" (3) 修改 "<<endl
<<" (4) 显示 "<<endl
<<" (5) 存档 "<<endl
<<" (6) 查找 "<<endl
<<" (7) 上传 "<<endl
<<" (8) 排序 "<<endl
<<" (9) 清屏 "<<endl
<<" (10) 删除 "<<endl
<<" (0) 退出 "<<endl
<<endl
<<" 选择 :" ;
}
int main()
{
cout<<endl<<endl<<endl<<endl<<endl<<endl
<<" ****************************************************\n"
<<" *** ***\n"
<<" *** ***\n"
<<" *** 欢迎进入商场销售管理系统! ***\n"
<<" *** ***\n"
<<" *** ***\n"
<<" ****************************************************\n"
<<" ";
system("pause");
system("cls");
GoodList goods;
GoodsInfo * a;
int N;
goods.menu();
cin>>N;
while(N!=0)
{
switch(N)
{
case 1 : {
goods.create();
break;
}
case 2 : {
goods.addInfo(goods.p2);
break;
}
case 3 : {
cout<<"~~~~~~~~~~~~~~~~~~~~\n"
<<"请选择需要修改的单元\n"
<<"~~~~~~~~~~~~~~~~~~~~\n";
a=goods.search();
goods.show(a);
if(a!=NULL)
goods.Amend(a);
else cout<<"*error!*\n";
break;
}
case 4 : {
goods.showinfo(goods.head);
break;
}
case 5 : {
goods.saveInfo();
break;
}
case 6 : {
a=goods.search ();
goods.show (a);
break;
}
case 7 : {
goods.getInfo();
break;
}
case 8 : {
goods.sort();
break;
}
case 9 : {
system("cls");
break;
}
case 10 :{
goods.delete_();
break;
}
}
goods.menu();
cin>>N;
}
char F;
cout<<" ***是否需要保存文件***('Y'确认保存,'N'返回主菜单) ";
cin>>F;
if(F=='Y') goods.saveInfo();
system("cls");
cout<<"谢谢使用!"
<<endl;
return 0;
}
编写一个函数模板Accumulator,用于实现对一个浮点型数组元素的“累加”。要求:累加方式由一个函数 AccumulateMethod 决定,可能代表两个元素的和、积、平方和等,AccumulateMethod 以函数指针方式传递给 Accumulator,缺省方式为累加求和。
#include
#define N 100
using namespace std;
template <typename T>
T AccumulateMethod(T *a,int temp,int m)
{
int k;
T sum,product,squaresum;
switch(temp)
{
case 1:
sum=0;
for(k=0;k<m;k++)
{
sum+=a[k];}
return sum;
break;
case 2:
product=1;
for(k=0;k<m;k++)
{
product*=a[k];}
return product;
break;
case 3:
squaresum=0;
for(k=0;k<m;k++)
{
squaresum+=(a[k]*a[k]);}
return squaresum;
break;
}
}
int main()
{
L1:float a[N];
float result;
int m,k,x;
int temp=1;
float(*f)(float*,int,int);
f=AccumulateMethod;
cout<<endl;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I 浮点数累加 I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<endl
<<endl;
cout<<"请输入数组长度:"<<endl;
cin>>m;
cout<<"请输入数组元素:"<<endl;for(k=0;k<m;k++){
cin>>a[k];}
cout<<"您输入的数组为:"<<endl;for(int k=0;k<m;k++){
cout<<a[k]<<" ";}cout<<endl;
while(1)
{
cout<<"(1)继续执行功能"<<endl
<<endl
<<"(2)重新输入数组"<<endl
<<endl
<<" 选择 :" ;
cin>>x;
switch(x)
{
case 1:
cout<<"请输入您要使用的功能:"<<endl
<<endl
<<"(1)求和"<<endl
<<endl
<<"(2)求积"<<endl
<<endl
<<"(3)平均和"<<endl
<<endl
<<" 选择 :" ;
cin>>temp;
result=(*f)(a,temp,m);
cout<<"计算结果:"<<result<<endl;
break;
case 2:
goto L1;
}}
return 0; }
编写一个计算数组元素平均值的函数模板。
#include
using namespace std; //头文件
template<typename T>
T average(T a[],int m) //定义函数模板
{
int i=0;
double sum=0;
for(i;i<m;i++)
{
sum=sum+a[i];} //循环求和
return sum/i;} // 返回平均数
int main()
{
double a[50];
int x=0;
int y;
cout<<endl
<<"设置个数:"<<endl;
cin>>y;
cout<<"请输入数值"<<endl;
for(x=0;x<y;x++){
cin>>a[x];} //循环输入
cout<<"平均数:"<<average(a,y)<<endl; //调用函数模板并输出平均数
return 0;}
将实验 2 的⑸所定义的集合构造成一个类模板。
#include
using namespace std;
template <typename T>
class Capacity
{
private:
int size; //元素个数
T *el;
public:
Capacity(T *l,int s):el(l),size(s) //构造函数
{
el= new T[size];
}
~Capacity(){
} //析构函数
int getsize()
{
return size; //返回元素个数
}
void display()
{
for(int i=0;i<size;i++)
cout<<el[i]<<endl; //for 循环输出 el[i]的元素;
}};
int main()
{
int *a;
int m;
int i;
float *b;
double *c;
cout<<"输入数组个数"<<endl; cin>>m;
a=new int[m]; //获得数组空间for(int i=0;i
cin>>a[i]; //做准备
Capacity<int> ca(a,m); //前面准备的实现
cout<<ca.getsize()<<endl;
ca.display();
return 0;
}
利用列表设计一个小型电话簿管理程序,要求具有加入联系人、删除联系人、显示电话簿内容和查询联系人电话、存盘等基本功能。
#include
#include //设置流操作符
#include //文件流操作
using namespace std;
class book //类book
{
private:
string name; //姓名
string address; //地址
string number; //电话号码
public:
book(); //构造函数
char inter_face(); //首页
void add_person(); //添加联系人
void del_person(); //删除联系人
void show_all(); //显示所有联系人
void alter(); //修改信息
void select(); //查询联系人
void save_new(); //保存新增加的联系人
};
book::book()
{
name = "\0";
address = "\0";
number = "\0";
}
//首页
char book::inter_face() //实现首界面的函数
{
system("cls"); //清屏操作
cout<<endl;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I 通讯录 I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (1) 添加联系人 "<<endl
<<" (2) 删除联系人 "<<endl
<<" (3) 显示联系人 "<<endl
<<" (4) 修改信息 "<<endl
<<" (5) 查询联系人 "<<endl
<<" (6) 关闭通迅录 "<<endl
<<endl
<<" 选择 :" ;
char choose;
cin>>choose;
return choose;
}
void book::add_person() //类外定义添加联系人函数
{
cout<<"请输入新联系人信息" <<endl;
cout<<"姓名 :";
cin >>name;
cout <<"电话 : ";
cin >>number;
cout <<"地址 : ";
cin >>address;
save_new();
cout<<"新信息已经保存!" <<endl;
system("pause");
}
void book::del_person() //类外定义删除联系人函数
{
string sign,str1,str; //定义字符串
bool flag = true; //布尔型变量初始为真
cout<<"你要删除输入姓名或电话号 :"<<endl; //输入要查找的相关信息
cin>>sign;
ofstream outData("temp.txt", ios::out); //磁盘文件的输出
ifstream inData("pbook.txt", ios::in); //输入
if (!outData || !inData) //判断
{
cout<<"对不起,找不到文件!" <<endl;
system("pause");
}
while (inData>>name>>number) //将数组或字符输入indata
{
getline(inData, str); //接收一个字符串
if ((sign==name) || (sign==number)) //存在此联系人
{
cout <<"你想删除的联系人:"<<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl; //左对齐
flag = false;
break;
}
outData <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl;
}
if (flag)
{
cout <<endl <<"联系人中不存在!" <<endl <<endl;
}
else
{
while (getline(inData, str))
{
outData <<str <<endl;
}
outData.close();
inData.close();
ofstream out("pbook.txt", ios::out);
ifstream in("temp.txt", ios::in);
if (!out || !in)
{
cout <<endl <<"对不起不能打开文件!" <<endl <<endl;
system("pause");
return;
}
while (getline(in, str))
{
out <<str <<endl;
}
out.close(); //文件流关闭
in.close();
cout <<endl <<"信息已中删除!" <<endl <<endl;
}
system("pause");
}
void book::show_all() //显示所有联系人
{
//类外定义显示所有联系人函数
ifstream inData("pbook.txt",ios::in);
if (!inData)
{
cout <<endl <<"对不起,没有找到文件!" <<endl;
system("pause");
return;
}
bool flag = true;
string record;
while (getline(inData, record))
{
if (flag)
{
cout <<endl <<"所有联系人信息如下: "<<endl;
}
cout <<"姓名\t\t"<<"电话\t\t"<<"住址\t\t"<<endl;
cout <<record <<endl;
flag = false;
}
if (flag)
{
cout <<endl <<"你的通迅录中没有联系人!" <<endl <<endl;
}
else
{
cout <<endl <<"所有联系人已经全部显示!" <<endl <<endl;
}
system("pause");
}
//修改信息
void book::alter() //类外定义显示信息函数
{
ofstream outData("temp.txt", ios::out);
ifstream inData("pbook.txt", ios::in);
if (!outData || !inData) //任意为假值运行
{
cout <<endl <<"找不到文件!" <<endl;
system("pause");
return;
}
string sign;
cout <<endl <<"你要修改输入姓名或电话号 :";
cin >>sign;
string str1;
bool flag = true;
string str;
while (inData >>name >>number)
{
getline(inData, str);
if ((sign==name) || (sign==number))
{
cout <<endl <<"你想修改的联系人:" <<endl <<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl;
cout <<endl <<"请修改信息 : " <<endl;
cout <<"姓名 :" ;
cin >>name;
cout <<"电话号:";
cin >>number;
cout <<"地址 :";
cin >>address;
save_new();
flag = false;
break;
}
outData <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl;
}
if (flag)
{
cout <<endl <<"联系人中不存在!"<<endl;
}
else
{
while (getline(inData, str))
{
outData <<str <<endl;
}
outData.close();
inData.close();
ofstream out("pbook.txt", ios::out);
ifstream in("temp.txt", ios::in);
if (!out || !in)
{
cout <<endl <<"对不起不能打开文件!!!"<<endl;
system("pause");
return;
}
while (getline(in, str))
{
out <<str <<endl;
}
out.close();
in.close();
cout<<"已修改!!!"<<endl;
}
system("pause");
}
//查询联系人
void book::select() //类外定义查询联系人函数
{
ifstream inData("pbook.txt",ios::in);
if (!inData)
{
cout <<endl <<"文件找不到!" <<endl;
system("pause");
return;
}
string sign;
cout <<endl <<"输入你想查找的联系人的姓名或电话号码: ";
cin >>sign;
string str1;
bool flag = true;
string str;
while (inData >>name >>number)
{
getline(inData, str);
if ((name==sign) || (number==sign))
{
cout <<endl <<"你要查找的联系人是: " <<endl <<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(17) <<name
<<number <<str <<endl;
flag = false;
system("pause");
break;
}}}
void book::save_new() //类外定义保存联系人函数
{
ofstream outData("pbook.txt", ios::app);
if (!outData)
{
cout <<"对不起,打开文件失败!"<<endl;
system("pause");
return;
}
outData << setiosflags(ios::left) << setw(17) << name<<" " << setw(16) << number <<" "<< setw(20) << address <<endl;
outData.close();
}
enum power{
a1 = '1', a2 = '2', a3 = '3', a4 = '4', a5 = '5', a6 = '6'};
int main()
{
char choose;
book song;
while (choose = song.inter_face())
{
switch (choose)
{
case a1:
song.add_person();//添加联系人
break;
case a2:
song.del_person();//删除联系人
break;
case a3:
song.show_all();//显示所有联系人
break;
case a4:
song.alter();//修改信息
break;
case a5:
song.select();//查询联系人
break;
case a6:
cout <<endl <<"谢谢使用!" <<endl <<endl;
exit(0);
break;
default:
break;}}
return 0;
}
设计一个学生类 student,包含如下基本信息:
学号、姓名、性别、年龄、成绩
假设学号是从 1 开始连续编码的整数。 为该类重载输入、 输出运算符, 并实现如下功能:
(1)从键盘输入若干学生信息并将其存放到文件中。
(2)按学号检索并显示学生的信息。
(3)按学号检索并修改学生的信息。
(4)显示所有成绩优秀的学生的信息。
# include
# include
# include
#include //用getch();
using namespace std;
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌Student类﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
class Student
{
public:
char name[20];
char Id[20];
int Age; //年龄
int Mnum; //数学课程得分
int Enum; //英语课程得分
int sum; //总分
Student * Next;
void Input()
{
cout<<"请输入学生的姓名:"; cin>>name;
cout<<"请输入学生的学号:"; cin>>Id;
cout<<"请输入年龄:"; cin>>Age;
cout<<"请输入数学课程的成绩:"; cin>>Mnum;
cout<<"请输入英语课程的成绩:"; cin>>Enum;
sum=Mnum+Enum;
}
void ReadFile(istream & in)
{
in>>name>>Id>>Age>>Mnum>>Enum>>sum;
}
void Show()
{
cout<<"姓名:"<<name<<endl<<"学号:"<<Id<<endl<<"年龄:"<<Age<<endl
<<"数学:"<<Mnum<<endl<<"外语:"<<Enum<<endl<<"总成绩:"<<sum<<endl<<endl<<endl;
}
};
//﹌﹌﹌﹌﹌﹌﹌﹌﹌Studentmassage类﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
class Studentmassage
{
public:
Studentmassage();
~Studentmassage();
void ShowMenu();
void Find();
void Save();
void ModifyItem();
void RemoveItem();
void Swap(Student *,Student *);
void Sort();
int ListCount();
void Display()
{
for(Student * p=Head->Next;p!=End;p=p->Next)
p->Show();
cout<<"输入任意字符!继续……";
getch();
}
void AddItem()
{
End->Input();
End->Next=new Student;
End=End->Next;
cout<<"添加成功!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
private:
Student * Head,* End;
ifstream in;
ofstream out;
Student *FindItem(char * name)
{
for(Student * p=Head;p->Next!=End;p=p->Next)//匹配成功则返回上一个指针,不成功就返回空
if(!strcmp(p->Next->name,name))return p;
return NULL;
}
Student *FindID(char * Id)
{
for(Student * p=Head;p->Next!=End;p=p->Next)//匹配成功则返回上一个指针,不成功就返回空
if(!strcmp(p->Next->Id,Id))return p;
return NULL;
}
};
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌构造函数﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
Studentmassage::Studentmassage()
{
Head=new Student;
Head->Next=new Student;
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 Student;
End=End->Next;
}
in.close();
cout<<"读取学生信息成功!"<<endl;
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌析构函数﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
Studentmassage::~Studentmassage()
{
Save();
for(Student * temp;Head->Next!=End;)
{
temp=Head->Next;
Head->Next=Head->Next->Next;
delete temp;
}
delete Head,End;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌菜单﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::ShowMenu()
{
cout<<endl;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I 学生管理系统I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (1) 增加学生信息 "<<endl
<<" (2) 显示学生成信息 "<<endl
<<" (3) 排序统计成绩 "<<endl
<<" (4) 查找学生信息 "<<endl
<<" (5) 删除学生成绩 "<<endl
<<" (6) 修改学生信息 "<<endl
<<" (0) 安全退出系统 "<<endl
<<endl
<<" 选择 :" ;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌查找函数﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Find()
{
char name[20] ,Id[10];
int x;
Student * p=NULL;
cout<<"(1)按学生的姓名查找"<<endl
<<" (2)按学生学号查找"<<endl
<<"选择 :";
cin>>x;
switch(x)
{
case 1:{
cout<<"\t\t请输入要查找的学生的姓名:";cin>>name;
if(p=FindItem(name))
{
p->Next->Show();
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"没有找到该姓名的学生!"<<'\n'<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}break;
case 2:
{
cout<<"请输入要查找的学生的学号:";cin>>Id;
if(p=FindID(Id))
{
p->Next->Show();
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"没有找到该学好的学生!"<<'\n'<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}break;
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌修改信息﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::ModifyItem() //修改信息
{
char name[20];
Student * p=NULL;
cout<<"请输入要修改的人的姓名:";cin>>name;
if(p=FindItem(name))
{
cout<<"已找到学生的信息,请输入新的信息!"<<endl;
p->Next->Input();
cout<<"修改成功!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"没有找到!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌删除信息﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::RemoveItem() // 删除信息
{
char name[20];
Student * p=NULL,*temp=NULL;
cout<<"请输入要删除的学生的姓名:"<<endl;cin>>name;
if(p=FindItem(name))
{
temp=p->Next;
p->Next=p->Next->Next;
delete temp;
cout<<"删除成功!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"没有找到!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Swap(Student *p1, Student *p2)//交换两个combox变量的数据域
{
Student *temp=new Student;
strcpy(temp->name,p1->name);
strcpy(temp->Id,p1->Id);
temp->Age=p1->Age;
temp->Mnum=p1->Mnum;
temp->Enum=p1->Enum;
temp->sum=p1->sum;
strcpy(p1->name,p2->name);
strcpy(p1->Id,p2->Id);
p1->Age=p2->Age;
p1->Mnum=p2->Mnum;
p1->Enum=p2->Enum;
p1->sum=p2->sum;
strcpy(p2->name,temp->name);
strcpy(p2->Id,temp->Id);
p2->Age=temp->Age;
p2->Mnum=temp->Mnum;
p2->Enum=temp->Enum;
p2->sum=temp->sum;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
int Studentmassage::ListCount()//统计当前链表的记录总数,返回一个整数
{
if(! Head)
return 0;
int n=0;
for(Student * p=Head->Next;p!=End;p=p->Next)
{
n++;
}
return n;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Sort()//对当前链表进行排序
{
cout <<"Sorting..."<<endl;
Student *p=NULL,*p1=NULL,*k=NULL;
int n=Studentmassage::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->sum>k->sum)
{
Studentmassage::Swap(p,k);
}
}
cout <<"排序完成!"<<endl;
getch();
return;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌保存函数﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Save()
{
out.open("sort.txt");
for(Student *p=Head->Next;p!=End;p=p->Next)
out<<p->name<<"\t"<<p->Id<<"\t"<<p->Age<<"\t"
<<p->Mnum<<"\t"<<p->Enum<<"\t"<<p->sum<<'\n';
out.close();
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌主函数﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
int main()
{
int x,i=0;
bool quit=false;
Studentmassage Grade;
cout<<"按任意键开始……";
getch();
while(!quit)
{
system("cls");
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;
}