C++学习第2天 简单的商品管理系统

//代码来自 书本
#include <iostream>
#include <limits>   	// 为了使用numeric_limits
#include <sstream> 	//为了使用字符串输入输出流
#include <fstream>  	//为了执行文件输入输出操作
using namespace std;
struct goods 
{
	int type;
	int num;
	float price;	
};
const char*file="goods.dat";
class Data 
{
public:
	Data(int number,float Price):num(number),price(Price){}
	virtual ~Data(){};
	int GetNumber()const{return num;}
	float GetPrice()const{return price;}
	virtual void Print()const =0;
private:
	int num;
	float price;
};
class Book:public Data
{
public:
	Book(int number,float Price);
	virtual void Print()const
	{
		cout<<"图书编号为:"<<GetNumber()<<endl;
		cout<<"图书价格为:"<<GetPrice()<<endl;
	}
};
Book::Book(int number,float Price):Data(number,Price){}
class Drug:public Data
{
public :
	Drug(int number,float Price);
	virtual void Print()const
	{
		cout<<"药品编号为:"<<GetNumber()<<endl;
		cout<<"药品价格为:"<<GetPrice()<<endl;
	}
};
Drug::Drug(int number,float Price):Data(number,Price){} 
class Node
{
public:
	Node(Data*);
	~Node();
	void SetNext(Node*node){itsnext=node;}
	Node*GetNext()const;
	Data*GetData()const;
private:
	Data*itsData;
	Node*itsnext;
};
Node::Node(Data*pData):itsData(pData),itsnext(0){}
Node::~Node()
{
	delete itsData;
	itsData=0;
}
Node*Node::GetNext()const
{
	return itsnext;
}
Data*Node::GetData()const
{
	if(itsData)
		return itsData;
	else
		return NULL;
}
class List
{
public:
	List();
	~List();
	Data*Find(int number)const;
	//重载一个带两个参数的Find()函数
	Data*Find(int &increase,int number)const;
	int GetCount()const{return count;}
	Data*GetFirst()const;
	void Insert(Data*);
	void Show()const;
	Data*operator[](int)const;
	void Delete(int num);
private:
	Node*head;
	int count;
};
List::List():head(0),count(0){}
List::~List()
{
	Node*l=head;
	Node*m=0;
	int n=0;
	while(l)
	{
		m=l;
		l=l->GetNext();
		n++;
		cout<<"程序结束,删除第"<<n<<"个节点\n";
		delete m;
	}

}
Data*List::GetFirst()const
{
	if (head)
		return head->GetData();
	else
		return NULL;
}
Data*List::operator[](int offset)const
{
	Node*pn=head;
	if(!head)
		return NULL;
	if(offset>=count)
		return NULL;
	for(int i=0;i<offset;i++)
		pn=pn->GetNext();
	return pn->GetData();
}
Data*List::Find(int number)const
{
	Node*pn=0;
	for(pn=head;pn!=NULL;pn=pn->GetNext())
	{
		if(pn->GetData()->GetNumber()==number)
			break;
	}
	if(pn==NULL)
		return NULL;
	else
		return pn->GetData();
}
//重载一个带两个参数的Find()函数
Data*List::Find(int &increase,int number)const
{
	Node*pn=0;
	for(pn=head,increase=0;pn!=NULL;pn=pn->GetNext(),increase++)
	{
		if(pn->GetData()->GetNumber()==number)
			break;
	}
	if(pn==NULL)
		return NULL;
	else
		return pn->GetData();
} 

void List::Show()const
{
	if(!head)
		return;
	Node*pn=head;
	do 
	pn->GetData()->Print();
	while(pn=pn->GetNext());
}
void List::Delete(int num)
{

	Node*pBack=head;
	Node*pNow=head;	
	if(!head)
	{
		cout<<"没有数据可删除!\n";
		return;
	}
	if(head->GetData()->GetNumber()==num)
	{

		if (!head->GetNext())
		{

			delete head;
			cout<<"数据被清空!\n";
			head=0;
			count--;
			return;
		}
		else
		{
			head=head->GetNext();
			delete pNow;
			pNow=0;
			cout<<"删除成功!\n";
			count--;
			return;
		}
	}
	while(pBack)
	{
		if (pBack->GetNext()==NULL)
		{
			cout<<"找不到要删除的编号。";
			return;
		}
		if(pBack->GetNext()->GetData()->GetNumber()==num)
		{

			pNow=pBack->GetNext();
			pBack->SetNext(pNow->GetNext());
			delete pNow;
			cout<<"删除数据成功!\n";
			count--;
			return;
		}
		pBack=pBack->GetNext();
	}
	cout<<"不存在此编号!\n";
}
void List::Insert(Data*pData)
{
	Node*pn=new Node(pData);
	Node*pNow=head;
	Node*pNext=0;
	int New=pData->GetNumber();
	int next=0;
	count++;
	if(!head)
	{
		head=pn;
		return;
	}
	if(head->GetData()->GetNumber()>New)
	{
		pn->SetNext(head);
		head=pn;
		return;
	}
	for(;;)
	{
		if(!pNow->GetNext())
		{
			pNow->SetNext(pn);
			return;
		}
		pNext=pNow->GetNext();
		next=pNext->GetData()->GetNumber();
		if(next>New)
		{
			pNow->SetNext(pn);
			pn->SetNext(pNext);
			return;
		}
		pNow=pNext;
	}
}
class Repair:private List
{
public:
	void RInsert(Data*);
	void Run();
private:
};
void Repair::RInsert(Data*newData)
{
	int num=newData->GetNumber();
	int place=0;
	if(!Find(place,num))
		Insert(newData);
	else
	{
		cout<<"您输入的编号"<<num<<"与链表中";
		cout<<"第"<<place+1<<"个编号重复\n";
	}
}  
void Repair::Run()
{
	//List pl;
	Data*pData=0;
	int number;
	float price;
	int choice;
	bool quit=false;
	bool load=false;
	bool save=false;
	while (1)
	{
		system("cls");
		cout<<"(1)增加商品(2)列出所有商品(3)删除商品(4)查找商品(5)商品数目(6)退出(7)读取(8)保存:";
		cin>>choice;
		switch (choice)
		{
		case 1:
			while (1)
			{
				cout<<"(0)返回(1)图书(2)药品:";
				cin>>choice;
				if (!choice)
				{
					break;
				}
				else if (choice==1||choice==2)
				{

					cout<<"请输入编号:";
					cin>>number;
					if (choice==1)
					{
						cout<<"请输入图书价格:";
						cin>>price;
						pData=new Book(number,price);
						RInsert(pData);
					}
					else if (choice==2)
					{
						cout<<"请输入药品价格:";
						cin>>price;
						pData=new Drug(number,price);
						RInsert(pData);
					}

				}
				else
					cout<<"请输入0-2之间的数字\n";
			}
			break;
		case 2:
			if(GetFirst()==0)
			{
				cout<<"您的商品为空,请增加商品,\n"<<"按回车键返回主窗口"<<endl;
				cin.get();
				cin.get();
			}
			else
			{
				Show();
				cout<<"请按回车键返回主窗口"<<endl;
				cin.get();
				cin.get();
			}
			break;
		case 3:
			cout<<"请输入您要删除的商品编号:"<<endl;
			cin>>number;
			Delete(number);
			cin.get();
			cin.get();
			break;
		case 4:
			while (1)
			{
				cout<<"(0)返回(1)按编号进行查询(2)按序号进行查询:";
				cin>>choice;
				if(!choice)
				{
					break;
				}
				if(choice==1)
				{
					cout<<"请输入所要查找的商品的编号:"<<endl;
					cin>>number;
					Data*result=Find(number);
					if(result==0)
						cout<<"找不到该编号。\n";
					else
						result->Print();
				}
				else if(choice==2)
				{
					cout<<"请输入所要查找的数据的序号:"<<endl;
					cin>>number;
					if ((*this)[number-1])
					{
						(*this)[number-1]->Print();
					}
					else
						cout<<"找不到您要查询的数据。\n";
				}
				else
					cout<<"请输入0-2之间的数字\n";
			}
			break;
		case 5:
			cout<<"该链表共有"<<GetCount()<<"节点\n";
			cin.get();
			cin.get();
			break;
		case 6:
			quit=true;
			break;
		case 7:
			{
				fstream In; 
				int num=0;
				In.open(file,ios::in|ios::out|ios::binary);
				if (In.is_open())
				{
					In.seekp(0);
					string ch;
					In>>ch;
					stringstream ssout;
					ssout<<ch;
					ssout>>num;
					cout<<"当前内容为:"<<file<<"\n";
					if (num==0)
					{
						cout<<"文件读取成功。\n";
						cout<<"您的商品为空,请增加商品,\n"<<"按回车键返回主窗口"<<endl;
						cin.get();
						cin.get();
						break;
					}
					cout<<"读入"<<num<<"个商品记录。"<<endl;
					In.ignore();
					goods pp={0};

					while (In.read((char*)&pp,sizeof pp))
					{
						if (pp.type==1)
						{
							pData=new Book(pp.num,pp.price);
							RInsert(pData);
						}
						else if (pp.type==2)
						{
							pData=new Drug(pp.num,pp.price);
							RInsert(pData);
						}
						else
							continue;
					}
					if (In.eof())
					{
						In.clear();
					}
					else
					{
						cerr<<"读取错误"<<file<<".\n";
						exit(EXIT_FAILURE);
					}
				}
				else
				{
					cerr<<file<<"不能打开\n";
					exit(EXIT_FAILURE);
				}
				Show();
				cout<<"读取成功\n";
				cin.get();
				cin.get();
				In.close();
			}

			break;
		case 8:
			{
				int num=GetCount();

				goods *pp=new goods[num];

				ofstream fout(file,ios::out|ios::binary);
				fout<<num<<" ";
				for (int i=0;i<num;i++)
				{
					Data*p=(*this)[i];
					if (p)
					{
						if ((typeid(*p).name())==(typeid(Book).name()))
						{
							pp->type=1;
							cout<<"存入商品类型:图书"<<"\t";
						}
						else if((typeid(*p).name())==(typeid(Drug)).name())
						{
							pp->type=2;
							cout<<"存入商品类型:药品"<<"\t";
						}
						else
						{
							pp->type=0;
							cout<<"存入商品类型:无"<<"\t";
						}
						pp->num=p->GetNumber();
						cout<<"编号:"<<pp->num<<"\t";
						pp->price=p->GetPrice();
						cout<<"价格:"<<pp->price<<endl;

						fout.write((char*)&(*pp),sizeof (*pp));
					}
					else
					{
						break;
					}
					pp++;

				}
				cout<<"保存成功\n";
				cin.get();
				cin.get();
				fout.close();

			}
			break;
		default:
			cin.clear();//清除cin流的错误状态 
			cin.ignore( numeric_limits<streamsize>::max(), '\n' );
			cout<<"您只可以输入1-6之间的数字,请重新输入"<<endl<<"请按回车键返回并重新录入"<<endl; 
			cin.get();
			break;
		}	
		if (quit)
		{
			cout<<"程序结束\n";
			break;
		}
	}
}
int main()
{
	Repair rp;
	rp.Run();
	return 0;
}


  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++商品管理系统50页报告+源码。代码详细并且功能完善。 本系统要求实现商品管理、用户管理商品购买/退货管理等日常管理工作,以提高工作效率和管理水平。 系统合法普通用户分为用户(买家)和系统管理员,其中,系统管理员负责用户管理商品管理,用户可以购买/退货管理。本设计要求实现的功能较多,所以将它们分为三个部分来叙述。 商品库存管理系统分为三个主要的功能模块: (1)商品管理模块:包括6个子模块,分别是商品入库模块、商品出库模块、删除商品模块、修改商品模块、查询商品模块和显示商品模块; I商品入库模块:首先自动显示系统中已有的商品信息,如果还没有商品,显示没有记录。提示用户是否需要入库,用户输入需要入库的商品编号,系统自动判断该商品是否已经存在,若存在则无法入库;若不存在,则提示用户输入商品的相关信息,一条商品的所有信息均输入完成之后,系统还会询问是否继续进行其他商品的入库操作。 II商品出库模块:首先自动显示系统中已有的商品信息,并提示用户输入需要出库的商品编号,系统自动判断该商品是否已经存在,若存在则提示用户输入出库的数量;若不存在,则提示用户找不到该商品,无法进行出库操作。I III删除商品模块:首先自动显示系统中已有的商品信息,并提示用户输入需要删除的商品编号,系统自动判断该商品是否已经存在,若存在则提示用户是否删除该商品;若不存在则提示无法找到该商品。 IV修改商品模块:首先自动显示系统中已有的商品信息,并提示用户输入需要修改的商品编号,系统自动判断该商品是否已经存在,若存在则提示用户输入新的商品信息;若不存在则提示无法找到该商品。 V查询商品模块:该模块通过用户输入的商品编号来查找商品,若存在则提示用户是否显示商品所有信息,若不存在则提示无法找到该商品。 VI显示商品模块:该模块负责将所有商品的信息列表显示出来。 (2)用户管理模块:包括5个子模块,分别是新增用户信息模块、删除用户信息模块、修改用户信息模块、查找用户信息模块和显示用户信息模块; I增加用户模块:首先自动显示系统中已有的用户信息,如果还没有用户,显示没有记录。提示管理员是否需要增加,管理员输入需要增加的用户编号,系统自动判断该用户是否已经存在,若存在则无法增加;若不存在,则提示管理员输入要增加用户的相关信息,一条要增加用户的所有信息均输入完成之后,系统还会询问是否继续进行增加其他用户的操作。 II删除用户模块:首先自动显示系统中已有的用户信息,并提示管理员输入需要删除的用户编号,系统自动判断该用户是否已经存在,若存在则提示管理员是否删除该用户;若不存在则提示无法找到该用户。 III修改用户模块:首先自动显示系统中已有的用户信息,并提示管理员输入需要修改的用户编号,系统自动判断该用户是否已经存在,若存在则提示管理员输入新的用户信息;若不存在则提示无法找到该用户。 IV查询用户模块:该模块通过管理员输入的用户编号来查找用户,若存在则提示管理员是否显示用户所有信息,若不存在则提示无法找到该用户。 V显示用户模块:该模块负责将所有用户的信息列表显示出来。 (3)用户购买/退货管理模块:包括2个子模块,分别是用户购买管理模块和用户退货管理模块

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值