资源介绍
//--------------------Add_info.h-----------------------//
#ifndef Add_info_h
#define Add_info_h
#include"Goodnode.h"
//------------------------添加节点(货物信息)的函数-------------------------//
int Add_Goodnode(int Goodnum,char *Goodname,char *exp,char *pubname,int innum,float buyp,float sellp,Good* last);
//-----------------------添加货物信息模块-----------------------//
void Add_info(Good *p);
#endif
//--------------------Attached_fun.h----------------//
#ifndef Attached_fun_h
#define Attached_fun_h
//-----------------------------------计算盈利函数----------------------------------//
template
//函数模板
T Cal_income(Good *p,T m) //???
{
T total_income; //定义总盈利变量total_income
total_income=((p->Sellp)-(p->Buyp))*(p->Innum);
return total_income;
}
//-----------------------------------显示盈利函数----------------------------------//
void Show_income(Good *p);
//---------------------------显示盈利最多的货物的信息函数---------------------------//
void Show_max_income(Good *p);
//----------------------------显示盈利最少的货物的信息函数----------------------------//
void Show_min_income(Good *p);
//--------------------------------显示全部盈利函数------------------------------------//
void Show_all_income(Good *p);
//----------------------------------附加功能模块--------------------------------------//
void Attached_fun(Good *p);
#endif
//-------------------Delete_info.h------------------//
#ifndef Delete_info_h
#define Delete_info_h
#include"Goodnode.h"
//--------------------显示所有货物信息的函数-----------------//
void Show_info(Good *p);
//--------------------删除节点函数-----------------------//
void Delete_node(Good *pr,Good *p);
//------------------------删除货物信息模块-----------------//
void Delete_info(Good *p);
#endif
//--------------------Goodclass.h--------------------//
//---------------------------------------------货物类头文件-----------------------------------------------//
#ifndef Goodclass_h //条件编译
#define Goodclass_h
#include
using namespace std;
class Data //定义数据类
{
protected://与私有成员性质相似,唯一的差异表现在派生类中
int Goodnum; //定义货物编号
int Innum; //定义库存
float Buyp; //定义进价
float Sellp; //定义卖价
public:
virtual void Set_Goodnum(int num)=0; //设置货物编号,纯虚函数
virtual void Set_Innum(int num)=0; //设置货物库存,纯虚函数
virtual void Set_Buyp(float price)=0; //设置货物进价,纯虚函数
virtual void Set_Sellp(float price)=0; //设置货物卖价,纯虚函数
int* Get_Goodnum(); //得到各个数据成员的地址,方便储存在文件中
int* Get_Innum();
float* Get_Buyp();
float* Get_Sellp();
};
class Gooddata: public Data //公有继承父类Data
{
protected:
char Goodname[30]; //定义货物名
char Exp[30]; //定义保质期
char Pubname[30]; //定义生产厂商名
public:
void Set_Goodnum(int num); //父类的虚函数在子类中实现
void Set_Innum(int num); //父类的虚函数在子类中实现
void Set_Buyp(float price); //父类的虚函数在子类中实现
void Set_Sellp(float price); //父类的虚函数在子类中实现
void Set_Goodname(char *s);
void Set_Exp(char *s);
void Set_Pubname(char *s);
char* Get_Goodname(); //得到各个数据成员的地址,方便储存在文件中
char* Get_Exp();
char* Get_Pubname();
};
#endif
//---------------------Goodnode.h---------------------//
//-----------------------------定义货物链表Good-------------------------------//
#ifndef Goodnode_h
#define Goodnode_h
#include
#include
#include
#include"Goodclass.h"
using namespace std;
//---------------给已有的类型起别名,有利于提高程序的可读性---------------//
typedef struct node
{
int Goodnum;
char Goodname[30];
char Exp[30];
char Pubname[30];
int Innum;
float Buyp;
float Sellp;
node *next;
} Good;
//------------------------Good链表的删除函数----------------------//
void Delete_Good(Good *p);
//--------------使用文件数据来建立Good链表的建立函数,形参为ifstream类的引用----------------------//
Good* Create_Good(ifstream& infile);
//------------------------通过链表中的数据来建立文件的函数----------------------//
void Create_file(ofstream& outfile,Good *p);
#endif
//------------------------Load_file.h-------------------//
#ifndef Load_file_h
#define Load_file_h
//----------------------增加链表模块-----------------------//
void Add_Good(ifstream& infile,Good* last);
//----------------------载入文件模块-----------------------//
void Load_file(Good *last);
#endif
//-----------------------Save_info.h---------------//
#ifndef Save_info_h
#define Save_info_h
//----------------------信息保存模块-------------------//
void Save_info(ofstream& outfile,Good *p);
#endif
//----------------------Scan_info.h-----------------//
#ifndef Scan_info_h
#define Scan_info_h
//----------------------浏览货物全部信息的函数-------------------//
void Scan(Good *p);
//------------------------按价格浏览货物信息------------------------------------//
void Scan(Good *p,int);
//----------------------按库存浏览货物信息-----------------//
void Scan(Good *p,int,int);
//---------------------浏览货物信息模块--------------------//
void Scan_info(Good *p);
#endif
//-------------------Search_info.h-----------------//
#ifndef Search_info_h
#define Search_info_h
#include"Goodnode.h"
//---------------------按编号查询货物信息-------------------//
Good* Search_Goodnum(Good *p,int n);
//--------------------------按货物名查找------------------------------------------//
Good* Search_Goodname(Good *p,char *s);
//-----------------按保质期查找货物------------------//
Good* Search_Exp(Good *p,char *s);
//-----------------------------------------------查找与修改模块-----------------------------------------//
void Search_info(Good *p);
#endif
//--------------------------main.cpp--------------------//
#include
#include
#include
#include//基类
#include
#include
#include"Add_info.h"
#include"Attached_fun.h"
#include"Goodclass.h"
#include"Goodnode.h"
#include"Delete_info.h"
#include"Load_file.h"
#include"Save_info.h"
#include"Scan_info.h"
#include"Search_info.h"
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
char ch;
char count='0'; //标记变量,判断链表是否已经发生改变
Good *head;
cout<<" 正在从磁盘打开文件...";
cout<
#include
#include
#include
#include"Goodnode.h"
#include"Add_info.h"
using namespace std;
//------------------------添加节点(货物信息)的函数-------------------------//
int Add_Goodnode(int goodnum,char *goodname,char *exp,char *pubname,int innum,float buyp,float sellp,Good* last)
{
Good *p;
p=(Good *)(new Good); //动态分配内存,强制转化为Good型指针
if(p==NULL)
{
cout<<"创建失败!";
cout<Goodnum=goodnum;
strcpy(p->Goodname,goodname);
strcpy(p->Exp,exp);
strcpy(p->Pubname,pubname);
p->Innum=innum;
p->Buyp=buyp;
p->Sellp=sellp;
last->next=p;
last=p;
last->next=NULL;
return 1;
}
//-----------------------添加货物信息模块-----------------------//
void Add_info(Good *p) //将链表的首地址作为实参
{
char count='0'; //添加标记变量
int goodnum;
char goodname[30];
char exp[30];
char pubname[30];
int innum;
float buyp;
float sellp;
Good *last;
Good *Search;
last=p;
system("cls"); //清屏
cout<<" ";
cout<next!=NULL) last=last->next; //把结点指针推向最后一个
while(1)
{
cout<<"请输入编号: ";
cout<>goodnum;
for(Search=p;Search;Search=Search->next) //用来判断编号是否重复
if(Search->Goodnum==goodnum)
break;
if(Search)
{
cout<<"有重复的编号,请重新输入!";
cout<>goodname;
cout<<"请输入保质期: ";
cout<>exp;
cout<<"请输入生产厂商: ";
cout<>pubname;
cout<<"请输入库存: ";
cout<>innum;
cout<<"请输入进价: ";
cout<>buyp;
cout<<"请输入卖价: ";
cout<>sellp;
cout<
#include
#include
#include
#include"Goodnode.h"
#include"Attached_fun.h"
using namespace std;
//-----------------------------------显示盈利函数----------------------------------//
void Show_income(Good *p)
{
Good *q;
q=p;
cout<Goodname<Buyp<Sellp<Innum<next;
q2=q1->next;
max_income=Cal_income(q1,(float)1.0);
bsearched=q1;
while(q2!=NULL)
{
if(max_incomenext;
}
Show_income(bsearched);
}
//----------------------------显示盈利最少的货物的信息函数----------------------------//
void Show_min_income(Good *p)
{
Good *bsearched; //指向盈利最少的货物
Good *q1;
Good *q2;
float min_income;
q1=p->next;
q2=q1->next;
min_income=Cal_income(q1,(float)1.0);
bsearched=q1;
while(q2!=NULL)
{
if(min_income>Cal_income(q2,(float)1.0))
{
bsearched=q2; //将最小盈利保存在bsearched指针中
min_income=Cal_income(q2,(float)1.0);
}
q2=q2->next;
}
Show_income(bsearched);
}
//--------------------------------显示全部盈利函数------------------------------------//
void Show_all_income(Good *p)
{
Good *q;
q=p->next;
while(q!=NULL)
{
Show_income(q);
q=q->next;
}
}
//----------------------------------附加功能模块--------------------------------------//
void Attached_fun(Good *p)
{
Good *q;
char count;
q=p;
while(1)
{
system("cls");
cout<<" ";
cout<
#include
#include
#include"Delete_info.h"
#include"Goodnode.h"
using namespace std;
//--------------------显示所有货物信息的函数-----------------//
void Show_info(Good *head)
{
Good *q;
q=head->next;
cout<<"货物编号 货物名 保质期 生产厂商 库存 进价 卖价\n";
while(q)
{
cout<Goodnum<<" "<Goodname<<" "<Exp<<" "<Pubname<<" ";
cout<Innum<<" "<Buyp<<" "<Sellp;
cout<next;
}
}
//--------------------删除节点函数-----------------------//
void Delete_node(Good *pr,Good *p)
{
pr->next=p->next;
delete p;
cout<<"货物信息已经成功删除 !";
cout<next;
qr=head;
system("cls");
cout<<" ";
cout<next; //防止在出现一次输入错误后不能再次输入
cin>>num;
while(q)
{
if(q->Goodnum==num)
break; //寻找直到输入编号与货物编号相同,跳出循环删除信息
{
qr=q;
q=q->next;
}
}
if(q==NULL)
{
cout<<"输入错误,请重新输入: ";
cout<
using namespace std;
//--------------------------------成员函数的实现-------------------------------//
//__________________________________________________________________________________________________
int* Data::Get_Goodnum() //得到各个数据成员的地址,方便储存在文件中
{
return &Goodnum;
}
int* Data::Get_Innum()
{
return &Innum;
}
float* Data::Get_Buyp()
{
return &Buyp;
}
float* Data::Get_Sellp()
{
return &Sellp;
}
//_____________________________________________________________________________________________________
void Gooddata::Set_Goodnum(int num)
{
Goodnum=num;
}
void Gooddata::Set_Innum(int num)
{
Innum=num;
}
void Gooddata::Set_Buyp(float price)
{
Buyp=price;
}
void Gooddata::Set_Sellp(float price)
{
Sellp=price;
}
void Gooddata::Set_Goodname(char *s)
{
strcpy(Goodname,s);
}
void Gooddata::Set_Exp(char *s)
{
strcpy(Exp,s);
}
void Gooddata::Set_Pubname(char *s)
{
strcpy(Pubname,s);
}
char* Gooddata::Get_Goodname() //得到各个数据成员的地址,方便储存在文件中
{
return Goodname;
}
char* Gooddata::Get_Exp()
{
return Exp;
}
char* Gooddata::Get_Pubname()
{
return Pubname;
}
//----------------------------Goodnode.cpp-------------//
#include
#include"Goodnode.h"
#include"Goodclass.h"
#include
using namespace std;
//-----------------------各种共用函数的实现-------------------//
//------------------------Good链表的删除函数----------------------//
void Delete_Good(Good *p)
{ Good *pr;//前驱结点
while(p!=NULL)
{
pr=p;
p=p->next;
delete pr;
}
}
//--------------使用文件数据来建立Good链表的建立函数,形参为ifstream输入流类对象的引用----------------------//
Good* Create_Good(ifstream& infile)
{
Good *p;//结构体指针
Good *last; //指向p->next
Good *pr;
Good *head;//链表的首地址
head=(Good *)(new Good);//动态分配内存,强制转化为Good型指针
p=(Good *)(new Good); //新建立一个节点,强制转化为Good型指针
if(p==NULL||head==NULL) //判断节点是否建立
{
cout<<"内存不足,创建失败 !";
cout<next=p;
while(1)
{
infile.read((char *)&(p->Goodnum), sizeof(int)); //为p指针所指节点的数据域赋值
infile.read(p->Goodname,sizeof(char)*30);
infile.read(p->Exp,sizeof(char)*30);
infile.read(p->Pubname,sizeof(char)*30);
infile.read((char *)&(p->Innum), sizeof(int));//把数字换成字符串,强制转换
infile.read((char *)&(p->Buyp),sizeof(float));
infile.read((char *)&(p->Sellp),sizeof(float));
(int *)&(p->Goodnum); //把字符串换成数字,强制转换
(int *)&(p->Innum);
(float *)&(p->Buyp);
(float *)&(p->Sellp);
if(!infile.eof( ))//文件结束标记
{
last=(Good *)(new Good);//新建立一个节点,强制转化为Good型指针
if(last==NULL)
{
cout<<"内存不足,创建失败 !";
cout<next=last; //将现有节点与新建立的节点连接起来
pr=p;
p=last;
}
else break;
}
delete last;
p=pr;//让p成为链表的尾部
p->next=NULL;
return head; //返回首地址给函数
}
//------------------------通过链表中的数据来建立文件的函数----------------------//
void Create_file(ofstream& outfile,Good *head)
{
Good *pn;//后驱结点
Good *p;
p=head->next;
Gooddata obj;
while(pn!=NULL)
{
pn=p->next;
obj.Set_Goodnum(p->Goodnum); //将链表中的信息赋值给类的对象
obj.Set_Goodname(p->Goodname);
obj.Set_Exp(p->Exp);
obj.Set_Pubname(p->Pubname);
obj.Set_Innum(p->Innum);
obj.Set_Buyp(p->Buyp);
obj.Set_Sellp(p->Sellp);
outfile.write((char *)(obj.Get_Goodnum()),sizeof(int)); //将对象中的数据写到文件
outfile.write(obj.Get_Goodname(),sizeof(char)*30);
outfile.write(obj.Get_Exp(),sizeof(char)*30);
outfile.write(obj.Get_Pubname(),sizeof(char)*30);
outfile.write((char *)(obj.Get_Innum()),sizeof(int));
outfile.write((char *)(obj.Get_Buyp()),sizeof(float));
outfile.write((char *)(obj.Get_Sellp()),sizeof(float));
p=pn;
(int *)(obj.Get_Goodnum()); //将被强制转换为字符串类型的数据转换为原数据类型
(int *)(obj.Get_Innum());
(float *)(obj.Get_Buyp());
(float *)(obj.Get_Sellp());
}
}
//-------------------Load_file.cpp----------------//
#include
#include
#include
#include
#include
#include"Goodnode.h"
#include"Load_file.h"
using namespace std;
//----------------------增加链表模块-----------------------//
void Add_Good(ifstream& infile,Good* last) //?
{
Good * p;
p=Create_Good(infile);
last->next=p->next;
delete p; //删除无用的头结点
}
//----------------------载入文件模块-----------------------//
void Load_file(Good *head)
{
while(1)
{
ifstream infile;
char count; //操作符
Good *last;
char Filename[100];
last=head;
while(last->next!=NULL)
last=last->next;
system("cls");
cout<<"---------------------载入文件------------------";
cout<>Filename;
try
{
infile.open(Filename,ios_base::in); //ios::nocreate|ios::in只打开已有文件,不创建新文件
if(!infile)
throw 1;
}
catch(int)
{
cout<<"文件打开失败,请重新输入路径 !";
cout<
#include
#include
#include
#include"Goodnode.h"
#include"Goodclass.h"
#include"Save_info.h"
using namespace std;
//----------------------信息保存模块-------------------//
void Save_info(ofstream& outfile,Good *p)
{
char count; //操作符
system("cls");
cout<<" ";
cout<
#include
#include
#include"Goodnode.h"
#include"Scan_info.h"
using namespace std;
//----------------------浏览货物全部信息的函数-------------------//
void Scan(Good *p)
{
Good *q;
q=p;
cout<<"货物编号 货物名 保质期 生产厂商 库存 进价 卖价";
cout<Goodnum<<" "<Goodname<<" "<Exp<<" "<Pubname;
cout<<" "<Innum<<" "<Buyp<<" "<Sellp;
cout<next;
}
}
//------------------------按价格浏览货物信息------------------------------------//
void Scan(Good *p,int) //函数的重载
{
Good *q;
q=p;
cout<<"货物编号 货物名 进价 卖价";
cout<Goodnum<<" "<Goodname<<" "<Buyp<<" "<Sellp;
cout<next;
}
}
//----------------------按库存浏览货物信息-----------------//
void Scan(Good *p,int,int) //函数的重载
{
Good *q;
q=p;
cout<<"货物编号 货物名 库存";
cout<Goodnum<<" "<Goodname<<" "<Innum;
cout<next;
}
p=q;
}
//---------------------浏览货物信息模块--------------------//
void Scan_info(Good *head)
{ char count;
Good *q;
q=head->next;
system("cls");
cout<<" ";
cout<
#include
#include
#include
#include"Goodnode.h"
#include"Goodclass.h"
#include"Search_info.h"
using namespace std;
//---------------------按编号查询货物信息-------------------//
Good* Search_Goodnum(Good *p,int n)
{
Good *q;
int count=0; //操作符
q=p; //对q进行操作,不改变p的地址
while(q!=NULL)
{
if(q->Goodnum==n) //若查找成功,则输出商品信息
{
cout<<"查找成功 !";
cout<Goodnum<<" "<Goodname<<" "<Exp<<" "<Pubname<<" ";
cout<Innum<<" "<Buyp<<" "<Sellp;
cout<next;
}
if(count==0) cout<<"查找失败 !";
cout<Goodname,s)) //若查找成功,则输出商品信息
{
cout<<"货物编号 货物名 保质期 生产厂商 库存 进价 卖价";
cout<Goodnum<<" "<Goodname<<" "<Exp<<" "<Pubname<<" ";
cout<Innum<<" "<Buyp<<" "<Sellp;
cout<next;
}
if(count==0) cout<<" 查找失败!";
cout<Exp,s)) //若查找成功,则输出商品信息
{
cout<<"查找成功 !";
cout<Goodnum<<" "<Goodname<<" "<Exp<<" "<Pubname<<" ";
cout<Innum<<" "<Buyp<<" "<Sellp;
cout<next;
}
if(count==0) cout<<"查找失败 !";
cout<next;
system("cls");
cout<<" ";
cout<>goodnum;
bsearched=Search_Goodnum(q,goodnum); //调用函数查找
}
break;
case '2': //按货物名查找
{
char goodname[30];
cout<<"请输入要查找的货物:";
cout<>goodname;
bsearched=Search_Goodname(q,goodname); //调用函数查找
}
break;
case '3': //按保质期查找
{
char exp[30];
cout<<"请输入要查找的货物保质期:";
cout<>exp;
bsearched=Search_Exp(q,exp); //调用函数查找
}
break;
default:
cout<<" 系统故障,请稍后再试...";
cout<>goodnum;
bsearched->Goodnum=goodnum;
}
cout<<" 是否修改货物名? ";
cout<>goodname;
strcpy(bsearched->Goodname,goodname);
}
cout<<" 是否修改保质期? ";
cout<>exp;
strcpy(bsearched->Exp,exp);
}
cout<<" 是否修改生产厂商? ";
cout<>pubname;
strcpy(bsearched->Pubname,pubname);
}
cout<<" 是否修改库存?";
cout<>innum;
bsearched->Innum=innum;
}
cout<<" 是否修改进价?";
cout<>buyp;
bsearched->Buyp=buyp;
}
cout<<" 是否修改卖价?";
cout<>sellp;
bsearched->Sellp=sellp;
}
cout<<" 修改已完成!";
cout<
- 上一篇: Linux 初级操作文档
- 下一篇: C语言精典版本C程序设计语言