想问一下下述代码如何修改成购买商品后商品-1,并且商品删除也有问题,当我输入商品序号时它不按照我输入的来,而是自动删除最后一项商品,求大神指点!
#include
#include
#include
#include
#include
#include
int n; //商品种类
using namespace std;
class Stock;
class Sell;
SYSTEMTIME sys;//声明系统时间类,获取系统时间
class Ware//商品信息类
{
protected:
int number;
string name;
double price;
bool exc;//利用该变量判断当前对象是否存在,如果该对象被删除,则exc=0;
public:
friend void Turn(Ware &a,Stock &b);//友元函数,将Ware对象的基本信息,拷贝给Stock类
friend void Turn(Ware &a,Sell &b);//作用同上,拷贝给Sell类。
Ware()
{
number=0;
price=0;
name="none";
exc=0;
}
void inputnumber()//输入商品编号
{
cout<<"输入序号:";cin>>number;
}
void inputname()//输入商品名称
{
cout<<"输入名称:";cin>>name;
}
void inputprice()//输入商品价格
{
cout<<"输入价格:";
cin>>price;
}
int getnumber()//获取商品编号
{
return number;
}
string getname()//获取商品名称
{
return name;
}
double getprice()//获取商品价格
{
return price;
}
bool Is_exc()//判断是否存在,不存在则返回0
{
return exc;
}
void exced()//将不存在的对象变为存在(即exc=1),用于添加新的商品信息
{
exc=1;
}
void cancer()//将存在的对象变为不存在(即exc=0),用于删除对象
{
exc=0;
}
bool seekn(int n)//查找对象的序号,用于作用于商品序号的显示,删除等功能
{
if(n==number)
return true;
else
return false;
}
}base[1000];
class Stock:public Ware //库存类
{
int number_of_stock;//库存数量
public:
friend void Turn(Ware &a,Stock &b);
Stock()
{
number=0;
price=0;
name="none";exc=0;
number_of_stock=0;
}
void input_number_of_stock()//输入库存数量
{
cout<<"输入库存:";
cin>>number_of_stock;
}
int get_number_of_stock()//获取库存数量
{
return number_of_stock;
}
}stock[1000];
struct Date//日期结构体,做Sell类中的成员
{
int year;
int month;
};
class Sell:public Ware //销售类
{
Date sell_day;//销售日期
int amount;//数量
public:
friend void Turn(Ware &a,Sell &b);
Sell()
{
number=0;
price=0;
name="none";exc=0;
sell_day.year=sell_day.month=0;
amount=0;
}
void inputamount();//输入销售数量
bool seek_day(int y,int m)//按日期查询对象,日期符合,则返回真,否则返回假
{
if(y==sell_day.year&&m==sell_day.month)
return true;
else
return false;
}
int getamount()//获取销售数量
{
return amount;
}
}sell[1000],open;
void Sell::inputamount()
{
cout<<"输入销量:";cin>>amount;
sell_day.year=sys.wYear;//利用SYSTEMTIME类,获取计算机当前时间,并存入对象中
sell_day.month=sys.wMonth;//同上
}
void Turn(Ware &a,Stock &b)
{
b.number=a.number;
b.price=a.price;
b.exc=a.exc;
}
void Turn(Ware &a,Sell &b)
{
b.number=a.number;
b.price=a.price;
b.exc=a.exc;
}
//-----------------------------------------------------------------------------------------
void getinformation()
{
fstream open_set("set.txt",ios::in|ios::binary);//在程序开始之前,把所有需要用到的数据录入程序中。包括基础信息,库存信息等。
open_set>>n;
open_set.close();
fstream open_base("base.txt",ios::in|ios::binary);
for(int i=0;i<n;i++)
{
open_base.seekg(i*sizeof(base[i]),ios::beg);
open_base.read((char *)&base[i],sizeof(base[i]));
}
open_base.close();
fstream open_stock("stock.txt",ios::in|ios::binary);
for(int i=0;i<n;i++)
{
open_stock.seekg(i*sizeof(stock[i]),ios::beg);
open_stock.read((char *)&stock[i],sizeof(stock[i]));
}
open_stock.close();
}
//------------------------------------------------------------------------------
void firstinput()//初次输入商品的基本信息和库存信息
{
fstream open("base.txt",ios::in|ios::binary); //base.txt存放原商品信息
if(open)
{
cout<<"已有初次存入的信息,请勿重复操作!"
<<endl;
return ;
}
cout<<"输入商品种类有多少:";
cin>>n;
fstream save_set("set.txt",ios::out|ios::binary);//将当前已录入的商品数量保存在set.txt文件中
save_set<<n;
save_set.close();
for(int i=0;i<n;i++)
{
cout<<"请输入"<<endl;
base[i].inputnumber();
base[i].inputname();
base[i].inputprice();
base[i].exced();
Turn(base[i],stock[i]);
stock[i].input_number_of_stock();
}
cout<<"输入结束"<<endl;
Sleep(2000);
fstream save("base.txt",ios::out|ios::binary);
for(int i=0;i<n;i++)if(base[i].Is_exc()) //当前对象如果exc=0则代表它不存在,程序会跳过该对象,对其它对象进行操作
save.write((char *)&base[i],sizeof(base[i]));
save.close();
fstream save_stock("stock.txt",ios::out|ios::binary);//将库存信息类的对象保存在stock.txt文件中
for(int i=0;i<n;i++)if(stock[i].Is_exc())
save_stock.write((char *)&stock[i],sizeof(stock[i]));
save_stock.close();
}
//------------------------------------------------------------------------------
void addinfo()//增加商品的基础信息和库存信息
{
cout<<"请输入"<<endl;
base[n].inputnumber();
base[n].inputname();
base[n].inputprice();
base[n].exced();
Turn(base[n],stock[n]);
stock[n].input_number_of_stock();
n++;
fstream save("base.txt",ios::out|ios::binary);
for(int i=0;i<n;i++)
if(base[i].Is_exc())
save.write((char *)&base[i],sizeof(base[i]));
save.close();
fstream save_stock("stock.txt",ios::out|ios::binary);
for(int i=0;i<n;i++)if(stock[i].Is_exc())
save_stock.write((char *)&stock[i],sizeof(stock[i]));
save_stock.close();
fstream save_set("set.txt",ios::out|ios::binary);
save_set<<n;
save_set.close();
}
//-------------------------------------------
void cancerinfo()//删除商品信息
{
cout<<"输入序号:";
int nn;
cin>>nn;
int p=-1;//利用类中定义的seekn函数,查找相应的商品序号,并把对象的数组下标传递给p;
for(int i=0;i<n;i++)
{
if(base[i].seekn(nn))
{
p=i;
break;
}
}
if(p==-1)
{
cout<<"无此序号!"<<endl;
Sleep(1000);
return ;
}
base[p].cancer();
stock[p].cancer();
fstream save("base.txt",ios::out|ios::binary);
for(int i=0;i<n;i++)
if(base[i].Is_exc())
save.write((char *)&base[i],sizeof(base[i]));
save.close();
fstream save_stock("stock.txt",ios::out|ios::binary);
for(int i=0;i<n;i++)
if(stock[i].Is_exc())
save_stock.write((char *)&stock[i],sizeof(stock[i]));
save_stock.close();
n--;
fstream save_set("set.txt",ios::out|ios::binary);
save_set<<n;
save_set.close();
}
//------------------------------------------------------------------------------
void change()//更改商品信息
{
cout<<"输入序号:";int nn;cin>>nn;
int p=-1;
for(int i=0;i<n;i++)
{
if(base[i].seekn(nn))
{p=i;break;}
}
if(p==-1){cout<<"无此序号!"<<endl;Sleep(1000);return ;}
cout<<"请输入"<<endl;
base[p].inputnumber();
base[p].inputname();
base[p].inputprice();
base[p].exced();
Turn(base[p],stock[p]);
stock[p].input_number_of_stock();
fstream save("base.txt",ios::out|ios::binary);
for(int i=0;i<n;i++)
if(base[i].Is_exc())
save.write((char *)&base[i],sizeof(base[i]));
save.close();
fstream save_stock("stock.txt",ios::out|ios::binary);
for(int i=0;i<n;i++)
if(stock[i].Is_exc())
save_stock.write((char *)&stock[i],sizeof(stock[i]));
save_stock.close();
}
//------------------------------------------------------------------------------
void showinfo()//显示商品信息
{
cout<<"输入1,按序号查找"< cout int inp;
cin>>inp;
if(inp==1)
{
int nn;
cout<<"输入序号:";cin>>nn;
int p=-1;
for(int i=0;i<n;i++)
{
if(base[i].seekn(nn))
{p=i;break;}
}
if(p==-1){cout<<"无此序号!"<<endl;Sleep(1000);return ;}
cout<<"商品编号"<<base[p].getnumber()<<endl;
cout<<"商品名称"<<base[p].getname()<<endl;
cout<<"商品价格"<<base[p].getprice()<<endl;
cout<<"库存数量"<<stock[p].get_number_of_stock()<<endl;
}
else if(inp==2)
{
for(int i=0;i<n;i++)
{
cout<<"商品编号"<<base[i].getnumber()<<endl;
cout<<"商品名称"<<base[i].getname()<<endl;
cout<<"商品价格"<<base[i].getprice()<<endl;
cout<<"库存数量"<<stock[i].get_number_of_stock()<<endl;
cout<<endl;
}
}
else
{cout<<"input error!"<<endl;Sleep(1000);}
cout<<"输入任意数字,继续"<<endl;
int nnn;
scanf("%*d");
}
//------------------------------------------------------------------------------
void sellinput()//输入销售情况
{
fstream save_sell("sell.txt",ios::out|ios::app|ios::binary);;//将输入的销售情况以不覆盖的形式(ios::app),保存在sell.txt中。
if(!save_sell)
{
cout<<"销量储存错误"<<endl;
}
for(int i=0;i<n;i++)if(base[i].Is_exc())//与其它数据的保存形式不同,这里是在输入的同时进行保存。
{
cout<<"商品编号:"<<base[i].getnumber()<<endl;
cout<<"商品名称:"<<base[i].getname()<<endl;
cout<<"商品价格:"<<base[i].getprice()<<endl;
sell[i].inputamount();
Turn(base[i],sell[i]);
save_sell.write((char *)&sell[i],sizeof(sell[i]));
}
save_sell.close();
}
//------------------------------------------------------------------------------
void sellshow()
{
fstream open_sell("sell.txt",ios::in|ios::binary);
if(!open_sell)
{cerr<<"读取销量错误"< cout>y>>m;
double total=0;
for(int i=0;;i++)//不断查找sell.txt文件里的数据,并比对日期,如果日期相符,则显示出来,并且统计所有符合该如期的数据的总销售金额
{
open_sell.seekg(i*sizeof(open),ios::beg);
if(!open_sell.read((char )&open,sizeof(open)))break;//从文件起始位置,一直读取到文件终结
open_sell.seekg(i*sizeof(sell[i]),ios::beg);
open_sell.read((char *)&open,sizeof(open));
if(open.seek_day(y,m))
{
cout<<open.getnumber()<<endl;
cout<<open.getname()<<endl;
cout<<open.getprice()<<endl;
cout<<open.getprice()*open.getamount()<<"元"<<endl;
total+=open.getprice()*open.getamount();
}
// cout<<"**"<<endl;
// open.show_day();
}
cout<<endl;
cout<<"该月份总销售额为:"<<total<<"元"<<endl;
cout<<"输入任意数字,继续"<<endl;
scanf("%*d");
open_sell.close();
}
void menu()
{
cout<<"###########################小型商品销售管理系统#####################"< cout cout cout cout cout cout cout cout cout cout }
int main()
{
GetLocalTime( &sys ); //将系统时间传递给sys对象,并在上面函数中对结构体内数据进行操作
getinformation();
for(;;)
{
system("cls");
menu();
int input;
cout cin>>input;
switch(input)//通过switch函数对输入的指令进行选择,并挑选出命令代表的函数,进行调用处理
{
case 1:firstinput();break;
case 2:addinfo();break;
case 3:cancerinfo();break;
case 4:change();break;
case 5:showinfo();break;
case 6:sellinput();break;
case 7:sellshow();break;
case 8:return 0;
default: cout<<"输入错误!"<<endl;Sleep(1000);
}
}
}
https://wenku.baidu.com/view/b1f984317ed184254b35eefdc8d376eeafaa177f.html