逆向软件开发与设计--超市管理系统

一.代码来源

CSDN上面一位人员所编写的超市管理系统。相关网站
点击查看代码

#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
using namespace std;
class Goods {											//货物类(基类)
protected:
	string name;
	int number;
public:
	Goods() {}
	Goods(string Name, int Num) {
		name = Name;
		number = Num;
	}
	virtual void ShowMe() = 0;
	string getname() {								//取商品名称函数
		return name;
	}
	int getnumber() {								//取数量函数
		return number;
	}
	void add(Goods &a) {							//添加货物
		number += a.getnumber();
	}
	void min(Goods &a) {							//售出货物
		number -= a.getnumber();
	}
};
class DailyGoods :public Goods {							//日常用品类
public:
	DailyGoods() {};
	DailyGoods(string Name, int Num) :Goods(Name, Num) {
		name = Name;
		number = Num;
	}
	virtual void ShowMe();
	friend istream& operator >>(istream &cin, DailyGoods &a);
	friend ostream& operator <<(ostream &cout, DailyGoods &a);
	friend ostream& operator <<(ofstream &fout, DailyGoods &a);
};
class Food :public Goods {								//食物类
protected:
	string deadline;
public:
	Food() {};
	Food(string Name, int Num, string Deadline) :Goods(Name, Num) {
		name = Name;
		number = Num;
		deadline = Deadline;
	}
	virtual void ShowMe();
	string getdeadline() {
		return deadline;
	}
	friend istream& operator >>(istream &cin, Food &a);
	friend ostream& operator <<(ostream &cout, Food &a);
	friend ostream& operator <<(ofstream &fout, Food &a);
};
class ElectricalAppliance :public Goods {				//电器类
protected:
	string color;
public:
	ElectricalAppliance() {};
	ElectricalAppliance(string Name, int Num, string Color) :Goods(Name, Num) {
		name = Name;
		number = Num;
		color = Color;
	}
	virtual void ShowMe();
	string getcolor() {
		return color;
	}
	friend istream& operator >>(istream &cin, ElectricalAppliance &a);
	friend ostream& operator <<(ostream &cout, ElectricalAppliance &a);
	friend ostream& operator <<(ofstream &fout, ElectricalAppliance &a);
};
vector<DailyGoods> dailygoods;
vector<Food> food;
vector<ElectricalAppliance> electricalappliance;
istream& operator >>(istream &cin, DailyGoods &a) {				//输入输出符重载
	string Name;												//日用品重载
	int Num;
	cin >> Name >> Num;
	a.name = Name;
	a.number = Num;
	return cin;
}
ostream& operator <<(ostream &cout, DailyGoods &a) {
	cout.fill(' ');
	cout.setf(ios::left);
	cout.width(40);
	cout << a.name;
	cout.width(40);
	cout << a.number << endl;
	return cout;
}
ostream& operator <<(ofstream &fout, DailyGoods &a) {
	fout << a.name << " " << a.number << endl;
	return fout;
}
istream& operator >>(istream &cin, Food &a) {
	string Name, Deadline;										//食品类重载
	int Num;
	cin >> Name >> Deadline >> Num;
	a.name = Name;
	a.number = Num;
	a.deadline = Deadline;
	return cin;
}
ostream& operator <<(ostream &cout, Food &a) {
	cout.fill(' ');
	cout.setf(ios::left);
	cout.width(40);
	cout << a.name;
	cout.width(40);
	cout << a.deadline;
	cout.width(40);
	cout << a.number;
	return cout;
}
ostream& operator <<(ofstream &fout, Food &a) {
	fout << a.name << " " << a.number << "	" << a.deadline << endl;
	return fout;
}
istream& operator >>(istream &cin, ElectricalAppliance &a) {
	string Name, Color;											//电器类重载
	int Num;
	cin >> Name >> Color >> Num;
	a.name = Name;
	a.number = Num;
	a.color = Color;
	return cin;
}
ostream& operator <<(ostream &cout, ElectricalAppliance &a) {
	cout.fill(' ');
	cout.setf(ios::left);
	cout.width(40);
	cout << a.name;
	cout.width(40);
	cout << a.color;
	cout.width(40);
	cout << a.number;
	return cout;
}
ostream& operator <<(ofstream &fout, ElectricalAppliance &a) {
	fout << a.name << " " << a.number << "	" << a.color << endl;
	return fout;
}
//保存到文件
void saveFood() {												//保存食物
	ofstream fout;
	fout.open("Food.txt", ios::out);
	if (!fout) {
		cout << "食物类保存失败!" << endl;
		system("pause");
	}
	else {
		for (vector<Food>::iterator it = food.begin(); it != food.end(); it++) {
			fout << *it;
		}
		cout << "食物类已保存" << endl;
		system("pause");
		fout.close();
	}
}
void saveDailygoods() {											//保存日用品
	ofstream fout;
	fout.open("Dailygoods.txt", ios::out);
	if (!fout) {
		cout << "日用品保存失败!" << endl;
		system("pause");
	}
	else {
		for (vector<DailyGoods>::iterator it = dailygoods.begin(); it != dailygoods.end(); it++) {
			fout << *it;
		}
		cout << "日用品已保存" << endl;
		system("pause");
		fout.close();
	}
}
void saveElectrical() {											//保存电器类
	ofstream fout;
	fout.open("Electricalappliance.txt", ios::out);
	if (!fout) {
		cout << "电器类保存失败!" << endl;
		system("pause");
	}
	else {
		for (vector<ElectricalAppliance>::iterator it = electricalappliance.begin(); it != electricalappliance.end(); it++) {
			fout << *it;
		}
		cout << "电器类已保存" << endl;
		system("pause");
		fout.close();
	}
}
//读取文件
void readDailygoods() {										//读取并展示日用品函数
	ifstream fin;
	string NAME;
	int NUM;
	fin.open("Dailygoods.txt", ios::in);
	if (!fin) {
		cout << "日用品类打开失败" << endl;
		system("pause");
	}
	else if (fin.peek() == EOF) {
		cout << "内容如下:" << endl;
		cout << "空" << endl;
		cout << endl << endl;
		system("pause");
		fin.close();
		return;
	}
	else {
		while (fin >> NAME >> NUM)
		{
			DailyGoods s(NAME, NUM);
			dailygoods.push_back(s);
		}
		cout << "内容如下:" << endl;
		for (vector<DailyGoods>::iterator it = dailygoods.begin(); it != dailygoods.end(); it++) {
			cout << *it;
		}
		cout << endl << endl;
		fin.close();
	}
}
void readFood() {										//读取并展示食品函数
	ifstream fin;
	string NAME, DEADLINE;
	int NUM;
	fin.open("Food.txt", ios::in);
	if (!fin) {
		cout << "食品类打开失败" << endl;
		system("pause");
	}
	else if (fin.peek() == EOF) {
		cout << "内容如下:" << endl;
		cout << "空" << endl;
		cout << endl << endl;
		system("pause");
		fin.close();
		return;
	}
	else {
		while (fin >> NAME >> NUM >> DEADLINE)
		{
			Food s(NAME, NUM, DEADLINE);
			food.push_back(s);
		}
		cout << "内容如下:" << endl;
		for (vector<Food>::iterator it = food.begin(); it != food.end(); it++) {
			cout << *it;
		}
		cout << endl << endl;
		fin.close();
	}
}
void readElectrical() {										//读取并展示电器类函数
	ifstream fin;
	string NAME, COLOR;
	int NUM;
	fin.open("Electricalappliance.txt", ios::in);
	if (!fin) {
		cout << "电器类打开失败" << endl;
		system("pause");
	}
	else if (fin.peek() == EOF) {
		cout << "内容如下:" << endl;
		cout << "空" << endl;
		cout << endl << endl;
		system("pause");
		fin.close();
		return;
	}
	else {
		while (fin >> NAME >> NUM >> COLOR)
		{
			ElectricalAppliance s(NAME, NUM, COLOR);
			electricalappliance.push_back(s);
		}
		fin.close();
		cout << "内容如下:" << endl;
		for (vector<ElectricalAppliance>::iterator it = electricalappliance.begin(); it != electricalappliance.end(); it++) {
			cout << *it;
		}
		cout << endl << endl;
	}
}
//展示
void DailyGoods::ShowMe() {											//展示日用品
	ifstream fin;//input 读 //output 写    //cin   istream//file
	string NAME;
	int NUM;
	fin.open("Dailygoods.txt", ios::in);//方式    只读
	if (!fin) {
		cout << "日用品类列表还未建立" << endl;
		cout << "日用品类建立中..." << endl;
		ofstream fint;
		fint.open("Dailygoods.txt", ios::out);
		fint.close();
		cout << "日用品类已建立文件" << endl;
		system("pause");
	}
	else {
		while (fin >> NAME >> NUM)
		{
			DailyGoods s(NAME, NUM);
			dailygoods.push_back(s);
		}
		cout << "商品名称" << "\t\t\t\t" << "现有数量" << endl << endl;
		for (vector<DailyGoods>::iterator it = dailygoods.begin(); it != dailygoods.end(); it++) {
			cout << *it;
		}
		system("pause");
		dailygoods.clear();
	}
	fin.close();
}
void Food::ShowMe() {											//展示食品类
	ifstream fin;
	string NAME, DEADLINE;
	int NUM;
	fin.open("Food.txt", ios::in);
	if (!fin) {
		cout << "食品类列表还未建立" << endl;
		cout << "食品类建立中..." << endl;
		ofstream fint;
		fint.open("Food.txt", ios::out);
		fint.close();
		cout << "食品类已建立文件" << endl;
		system("pause");
	}
	else if (fin.peek() == EOF) {
		cout << "空" << endl;
		system("pause");
		fin.close();
		return;
	}
	else {
		while (fin >> NAME >> NUM >> DEADLINE)
		{
			Food s(NAME, NUM, DEADLINE);
			food.push_back(s);
		}
		cout << "商品名称" << "\t\t\t\t" << "保质期" << "\t\t\t\t\t" << "现有数量" << endl << endl;
		for (vector<Food>::iterator it = food.begin(); it != food.end(); it++) {
			cout << *it;
		}
		system("pause");
		food.clear();
	}
	fin.close();
}
void ElectricalAppliance::ShowMe() {											//展示电器类
	ifstream fin;
	string NAME, COLOR;
	int NUM;
	fin.open("Electricalappliance.txt", ios::in);
	if (!fin) {
		cout << "电器类列表还未建立" << endl;
		cout << "电器类建立中..." << endl;
		ofstream fint;
		fint.open("Electricalappliance.txt", ios::out);
		fint.close();
		cout << "电器类已建立文件" << endl;
		system("pause");
	}
	else if (fin.peek() == EOF) {
		cout << "空" << endl;
		system("pause");
		fin.close();
		return;
	}
	else {
		while (fin >> NAME >> NUM >> COLOR)
		{
			ElectricalAppliance s(NAME, NUM, COLOR);
			electricalappliance.push_back(s);
		}
		cout << "电器名称" << "\t\t\t\t" << "颜色" << "\t\t\t\t\t" << "现有数量" << endl << endl;
		for (vector<ElectricalAppliance>::iterator it = electricalappliance.begin(); it != electricalappliance.end(); it++) {
			cout << *it;
		}
		system("pause");
		electricalappliance.clear();
	}
	fin.close();
}
//添加商品类别
void addDaily() {												//添加日常用品类
	readDailygoods();
	int open = 0;
	cout << "请输入货物名称、要添加的数量:" << endl;
	DailyGoods newdailygoods;
	cin >> newdailygoods;
	for (vector<DailyGoods>::iterator it = dailygoods.begin(); it != dailygoods.end(); it++) {
		if (it->getname() == newdailygoods.getname()) {
			it->add(newdailygoods);
			cout << "仓库已有此商品,已更新数量" << endl;
			system("pause");
			open = 1;
			break;
		}
	}
	if (open == 0) {
		dailygoods.push_back(newdailygoods);
	}
	saveDailygoods();
	dailygoods.clear();
}
void addFood() {												//添加食品类
	readFood();
	int open = 0;
	cout << "请输入货物名称、食品保质期、要添加的数量" << endl;
	Food newfood;
	cin >> newfood;
	for (vector<Food>::iterator it = food.begin(); it != food.end(); it++) {
		if (it->getname() == newfood.getname() && it->getdeadline() == newfood.getdeadline()) {
			it->add(newfood);
			cout << "仓库已有此商品,已更新数量" << endl;
			system("pause");
			open = 1;
			break;
		}
	}
	if (open == 0) {
		food.push_back(newfood);
	}
	saveFood();
	food.clear();
}
void addElectrical() {												//添加电器
	readElectrical();
	int open = 0;
	cout << "请输入商品名称、电器的颜色、要添加的数量:" << endl;
	ElectricalAppliance newelectricalappliance;
	cin >> newelectricalappliance;
	for (vector<ElectricalAppliance>::iterator it = electricalappliance.begin(); it != electricalappliance.end(); it++) {
		if (it->getname() == newelectricalappliance.getname() && it->getcolor() == newelectricalappliance.getcolor()) {
			it->add(newelectricalappliance);
			cout << "仓库已有此商品,已更新数量" << endl;
			system("pause");
			open = 1;
			break;
		}
	}
	if (open == 0) {
		electricalappliance.push_back(newelectricalappliance);
	}
	saveElectrical();
	electricalappliance.clear();
}
//上货
void loadDaily() {													//日用品类上货
	DailyGoods appointeddailygoods;
	int result = 0;
	system("cls");
	cout << "请输入货物名称、要添加的数量:" << endl;
	readDailygoods();
	cin >> appointeddailygoods;
	for (vector<DailyGoods>::iterator it = dailygoods.begin(); it != dailygoods.end(); it++) {
		if (it->getname() == appointeddailygoods.getname()) {
			it->add(appointeddailygoods);
			result = 1;
		}
	}
	if (result == 0) {
		cout << "未查询到商品" << endl;
		system("pause");
		system("cls");
	}
	saveDailygoods();
	dailygoods.clear();
}
void loadFood() {													//食品类上货
	string NAME, DEADLINE;
	int result = 0, NUMBER;
	system("pause");
	cout << "请输入食品名称、保质期、要添加的数量" << endl;
	readFood();
	cin >> NAME >> DEADLINE >> NUMBER;
	Food appointedfood(NAME, NUMBER, DEADLINE);
	for (vector<Food>::iterator it = food.begin(); it != food.end(); it++) {
		if (it->getname() == appointedfood.getname() && it->getdeadline() == appointedfood.getdeadline()) {
			it->add(appointedfood);
			result = 1;
		}
	}
	if (result == 0) {
		cout << "未查询到商品" << endl;
		system("pause");
		system("cls");
	}
	saveFood();
	food.clear();
}
void loadElectrical() {													//电器类上货
	string NAME, COLOR;
	int NUMBER;
	int result = 0;
	system("pause");
	cout << "请输入电器名称、电器颜色、要添加的数量" << endl;
	readElectrical();
	cin >> NAME >> COLOR >> NUMBER;
	ElectricalAppliance appointedelectrical(NAME, NUMBER, COLOR);
	for (vector<ElectricalAppliance>::iterator it = electricalappliance.begin(); it != electricalappliance.end(); it++) {
		if (it->getname() == appointedelectrical.getname() && it->getcolor() == appointedelectrical.getcolor()) {
			it->add(appointedelectrical);
			result = 1;
		}
	}
	if (result == 0) {
		cout << "未查询到商品" << endl;
		system("pause");
		system("cls");
	}
	saveElectrical();
	electricalappliance.clear();
}
//出售
void saleDaily() {									//售出日用品
	DailyGoods appointeddailygoods;
	int result = 0;
	cout << "请输入货物名称、要卖出的数量:" << endl;
	readDailygoods();
	cin >> appointeddailygoods;
	for (vector<DailyGoods> ::iterator it = dailygoods.begin(); it != dailygoods.end(); it++) {
		if (it->getname() == appointeddailygoods.getname()) {
			if (appointeddailygoods.getnumber() > it->getnumber()) {
				cout << "没有充足数量的商品!" << endl;
				result = 1;
				system("pause");
			}
			else {
				it->min(appointeddailygoods);
				result = 1;
			}
		}
	}
	if (result == 0) {
		cout << "未查询到商品" << endl;
		system("pause");
		system("cls");
	}
	saveDailygoods();
	dailygoods.clear();
}
void saleFood() {									//售出食品
	Food appointedfood;
	int result = 0;
	cout << "请输入食品名称、要卖出的数量、食物保质期:" << endl;
	readFood();
	cin >> appointedfood;
	for (vector <Food>::iterator it = food.begin(); it != food.end(); it++) {
		if (it->getname() == appointedfood.getname()) {
			if (appointedfood.getnumber() > it->getnumber()) {
				cout << "没有充足数量的商品" << endl;
				result = 1;
				system("pause");
			}
			else {
				it->min(appointedfood);
				result = 1;
			}
		}
	}
	if (result == 0) {
		cout << "未查询到商品" << endl;
		system("pause");
		system("cls");
	}
	saveFood();
	food.clear();
}
void saleElectrical() {									//售出食品
	ElectricalAppliance appointedelectrical;
	int result = 0;
	cout << "请输入电器名称、要卖出的数量、电器颜色:" << endl;
	readElectrical();
	cin >> appointedelectrical;
	for (vector<ElectricalAppliance>::iterator it = electricalappliance.begin(); it != electricalappliance.end(); it++) {
		if (it->getname() == appointedelectrical.getname()) {
			if (appointedelectrical.getnumber() > it->getnumber()) {
				cout << "没有充足数量的商品" << endl;
				result = 1;
				system("pause");
			}
			else {
				it->min(appointedelectrical);
				result = 1;
			}
		}
	}
	if (result == 0) {
		cout << "未查询到商品" << endl;
		system("pause");
		system("cls");
	}
	saveElectrical();
	electricalappliance.clear();
}
//查询
void searchDaily() {								//搜索日用品
	string appointeddailygoods;
	int result = 0;
	system("cls");
	readDailygoods();
	cout << "请输入货物名称" << endl;
	cin >> appointeddailygoods;
	for (vector<DailyGoods>::iterator it = dailygoods.begin(); it != dailygoods.end(); it++) {
		if (it->getname() == appointeddailygoods) {
			cout << it->getname() << "\t" << it->getnumber() << " 件\t" << endl;
			result = 1;
		}
	}
	if (result == 0) {
		cout << "未搜索到商品" << endl;
		system("pause");
	}
	saveDailygoods();
	dailygoods.clear();
}
void searchFood() {									//搜索食品
	string appointedfood;
	int result = 0;
	system("cls");
	readFood();
	cout << "请输入食物名称" << endl;
	cin >> appointedfood;
	for (vector<Food>::iterator it = food.begin(); it != food.end(); it++) {
		if (it->getname() == appointedfood) {
			cout << it->getname() << "\t" << it->getnumber() << " 件\t" << it->getdeadline() << "前食用" << endl;
			result = 1;
		}
	}
	if (result == 0) {
		cout << "未搜索到商品" << endl;
		system("pause");
	}
	saveFood();
	food.clear();
}
void searchElectrical() {									//搜索电器
	string appointedelectrical;
	int result = 0;
	system("cls");
	readElectrical();
	cout << "请输入电器名称" << endl;
	cin >> appointedelectrical;
	for (vector<ElectricalAppliance>::iterator it = electricalappliance.begin(); it != electricalappliance.end(); it++) {
		if (it->getname() == appointedelectrical) {
			cout << it->getname() << "\t" << it->getnumber() << " 件\t" << it->getcolor() << " 色" << endl;
			result = 1;
		}
	}
	if (result == 0) {
		cout << "未搜索到商品" << endl;
		system("pause");
	}
	saveElectrical();
	electricalappliance.clear();
}
//主菜单界面
void Menu() {
	cout.unsetf(ios::left);
	cout << endl;
	cout << setw(65) << "超市货物管理系统" << endl;
	cout << endl << endl << endl;
	cout << setw(50) << "请选择你要进行的操作:" << endl;
	cout << setw(64) << "1.查看商品情况" << endl;
	cout << setw(64) << "2.更新商品信息" << endl;
	cout << setw(64) << "3.查找商品信息" << endl;
	cout << setw(60) << "0.退出系统" << endl << endl;
}
//更新商品界面
void Menu2() {
	cout.unsetf(ios::left);
	cout << endl;
	cout << setw(63) << "更新商品信息" << endl;
	cout << endl << endl << endl;
	cout << setw(50) << "请选择你要进行的操作:" << endl;
	cout << setw(63) << "1.添加商品类" << endl;
	cout << setw(65) << "2.添加商品数量" << endl;
	cout << setw(61) << "3.出售商品" << endl;
	cout << setw(57) << "0.返回" << endl;
}
//展示商品界面
void Menu3() {
	cout.unsetf(ios::left);
	cout << endl;
	cout << setw(67) << "请输入你要查看的商品类型:" << endl;
	cout << endl << endl;
	cout << setw(60) << "1.日用品类" << endl;
	cout << setw(58) << "2.食品类" << endl;
	cout << setw(58) << "3.电器类" << endl;
	cout << setw(56) << "0.返回" << endl;
}
//查找商品界面
void Menu4() {
	cout.unsetf(ios::left);
	cout << endl;
	cout << setw(67) << "请输入你要查找商品的所属类型:" << endl;
	cout << endl << endl;
	cout << setw(60) << "1.日用品类" << endl;
	cout << setw(58) << "2.食品类" << endl;
	cout << setw(58) << "3.电器类" << endl;
	cout << setw(56) << "0.返回" << endl;
}
//功能类
class Supermarket {									//调用功能函数
public:
	//添加新商品
	void Add() {									//添加商品类函数
		int choice4;
		while (1) {
			cout << "请输入要添加商品的类别:\n\n1 ->日常用品\n2 ->食品\n3 ->电器类\n0->返回" << endl;
			cin >> choice4;
			switch (choice4)
			{
			case 1:
				addDaily();
				cout << "添加完成" << endl;
				return;
			case 2:
				addFood();
				cout << "添加完成" << endl;
				return;
			case 3:
				addElectrical();
				cout << "添加完成" << endl;
				return;
			case 0:
				return;
			default:
				cout << "输入有误,请选择1、2、3、0输入" << endl;
				system("pause");
				system("cls");
				break;
			}
		}
	}
	//商品上货
	void Load() {									//商品上货
		int choice5;
		while (1) {
			cout << "请输入要补充商品的类别: 1 ->日常用品\n2 ->食品\n3 ->电器类\n0->返回" << endl;
			cin >> choice5;
			switch (choice5)
			{
			case 1:
				loadDaily();
				cout << "上货完成" << endl;
				system("pause");
				return;
			case 2:
				loadFood();
				cout << "上货完成" << endl;
				system("pause");
				return;
			case 3:
				loadElectrical();
				cout << "上货完成" << endl;
				system("pause");
				return;
			case 0:
				return;
			default:
				cout << "请选择已有选项!!" << endl;
				system("pause");
				system("cls");
				break;
			}
		}
	};
	//出售商品
	void Sale() {									//售卖商品函数
		int choice6;
		while (1) {
			cout << "请输入要售卖商品的类别: \n\n1->日常用品\n2 ->食品\n3 ->电器类\n0->返回" << endl;
			cin >> choice6;
			switch (choice6)
			{
			case 1:
				saleDaily();
				cout << "出售完成" << endl;
				return;
			case 2:
				saleFood();
				cout << "出售完成" << endl;
				return;
			case 3:
				saleElectrical();
				cout << "出售完成" << endl;
				return;
			case 0:
				return;
			default:
				cout << "请选择已有选项!!" << endl;
				system("pause");
				system("cls");
				break;
			}
		}
	}
	//查询商品
	void Search() {									//查询商品函数
		while (1) {
			system("cls");
			Menu4();
			string choice6;
			cin >> choice6;
			if (choice6 == "1") {											//搜索日用品
				searchDaily();
			}
			else if (choice6 == "2") {									//搜索食物
				searchFood();
			}
			else if (choice6 == "3") {									//搜索电器
				searchElectrical();
			}
			else if (choice6 == "0") {									//返回主界面
				return;
			}
			else {														//重新选择展示
				cout << "请输入已有选项!!" << endl;
				system("pause");
			}
		}
	}
};
int main() {				/*主函数*/
	int first = 0;
	string choice;
	DailyGoods good1;
	Food good2;
	ElectricalAppliance good3;
	Menu();												//主界面
	cin >> choice;
	do
	{
		if (first != 0) {
			system("cls");
			Menu();
			cin >> choice;
		}
		Supermarket supermarket;
		while (choice == "1") {						//展示商品情况
			system("cls");
			Menu3();
			string choice3;
			cin >> choice3;
			if (choice3 == "1") {											//展示日用品
				good1.ShowMe();
			}
			else if (choice3 == "2") {									//展示食物
				good2.ShowMe();
			}
			else if (choice3 == "3") {									//展示电器
				good3.ShowMe();
			}
			else if (choice3 == "0") {									//返回主界面
				break;
			}
			else {														//重新选择展示
				cout << "请输入已有选项!!" << endl;
			}
		}
		while (choice == "2") {						//更新商品信息
			system("cls");
			Menu2();
			string choice2;
			cin >> choice2;
			if (choice2 == "1") {											//添加商品类
				system("cls");
				supermarket.Add();
			}
			else if (choice2 == "2") {									//商品上货
				system("cls");
				supermarket.Load();
			}
			else if (choice2 == "3") {									//售出商品
				system("cls");
				supermarket.Sale();
			}
			else if (choice2 == "0") {									//返回主界面
				break;
			}
			else {														//重新选择展示
				cout << "请输入已有选项!!" << endl;
				system("pause");
			}
		}
		while (choice == "3") {			//查找商品
			supermarket.Search();
			break;
		}
		first++;
	} while (choice != "0");
	cout << "退出系统" << endl;
	return 0;
}

二.概述

此次逆向设计目标在于理解其设计结构、功能实现,以及指出此次代码中存在不足的地方并提出改正、后续代码运行情况、相关逆向设计实验总结。针对于此次代码,该代码中实现了一个简单的超时货物管理系统,包含商品管理、库存管理、销售管理等诸多功能。

三.设备运行环境

设备名称 小李吃冰淇淋

处理器 AMD Ryzen 9 7945HX with Radeon Graphics 2.50 GHz

机带 RAM 16.0 GB (15.7 GB 可用)

设备 ID 44D49C89-7AF4-4D7B-8227-A9EE94DC32B9

产品 ID 00342-31476-28561-AAOEM

系统类型 64 位操作系统, 基于 x64 的处理器

笔和触控 笔支持

四.项目代码运行和介绍

1.系统主界面显示(查看商品情况、更新商品信息、查找商品信息、退出系统)

2.查找商品功能显示(用户可以查看不同类型的商品库存情况:日用品类、食品类、电器类)


3.更新商品信息(用户通过选择2按钮进行商品信息的更新,包括添加商品类、添加商品数量、出售商品、返回等功能)


4.查找商品信息(用户根据商品名称查找特定商品的信息:日用品类、食品类、电器类)


5.代码介绍

(1)类:Goods:基类,表示货物,包含名称和数量属性等。

DailyGoods:继承自Goods,表示日常用品。

Food:继承自Goods,表示食品,包含保质期功能。

ElectricalAppliance:继承自Goods,表示电器,包含颜色。

Supermarket:管理类,负责调用商品管理、库存管理、销售管理等功能。

(2)功能模块:商品管理:包括商品的添加、展示、更新和保存。

库存管理:包括商品的入库、出库和库存查询。

销售管理:包括商品的销售和销售记录。

(3)数据存储:日常用品信息存储在Dailygoods.txt文件。

食品信息存储在Food.txt文件。

电器信息存储在Electricalappliance.txt文件。

五.代码缺陷和不足

  1. 代码重复:
    (1)在DailyGoods、Food、ElectricalAppliance类的构造函数中,name和number的赋值存在重复代码。
    (2)在saveFood、saveDailygoods、saveElectrical函数中,文件写入的逻辑存在重复代码。
  2. 缺乏异常处理:
    (1)文件打开失败时,仅输出错误信息,未进行异常处理或程序终止。
    (2) 用户输入时,未对输入的有效性进行检查(如负数、非数字输入等)。
  3. 内存管理问题:
    在readDailygoods、readFood、readElectrical函数中,动态分配的内存未在程序结束时释放,可能导致内存泄漏。
  4. 功能不完善:
    (1)订单管理功能缺失,无法记录销售历史和生成销售报表。
    (2)商品信息的展示和查询功能较为简单,缺乏排序和筛选功能。
  5. 用户界面不友好:
    用户界面为简单的命令行界面,缺乏交互性。
  6. 文件格式不明确:
    Dailygoods.txt、Food.txt、Electricalappliance.txt文件的格式未在代码中明确说明,可能导致文件读取错误。

六.代码修改

1.代码重复问题修改
点击查看代码

Goods::Goods(string Name, int Num) : name(Name), number(Num) {}

void saveToFile(const string& filename, const vector<Goods*>& goods) {
    ofstream fout(filename, ios::out);
    if (!fout) {
        cerr << filename << " 文件保存失败!" << endl;
        return;
    }
    for (const auto& item : goods) {
        fout << *item;
    }
    fout.close();
    cout << filename << " 已保存" << endl;
}

2.增加文件打开失败时的异常处理以及提高用户输入的有效性检查,避免非法输入。 点击查看代码

void readFromFile(const string& filename, vector<Goods*>& goods) {
    ifstream fin(filename, ios::in);
    if (!fin) {
        cerr << filename << " 文件打开失败,程序终止!" << endl;
        exit(1);
    }
    // 其他代码...
}

3.内存管理改进 点击查看代码

int main() {
    // 其他代码...
    for (auto& item : dailygoods) {
        delete item;
    }
    for (auto& item : food) {
        delete item;
    }
    for (auto& item : electricalappliance) {
        delete item;
    }
    return 0;
}

4.增加订单管理功能,记录销售历史和生成销售报表功能,以及商品信息的排序和筛选功能 点击查看代码

struct Order {
    string goodsName;
    int quantity;
    double price;
    string date;
};

vector<Order> orders;

void saveOrder(const Order& order) {
    ofstream fout("orders.txt", ios::app);
    if (!fout) {
        cerr << "订单文件保存失败!" << endl;
        return;
    }
    fout << order.goodsName << " " << order.quantity << " " << order.price << " " << order.date << endl;
    fout.close();
}

5.增加商品删除功能。 点击查看代码

void removeDaily() { // 删除日用品
    string name;
    int num;
    cout << "请输入要删除的日用品名称和数量: ";
    cin >> name >> num;

    for (auto it = dailygoods.begin(); it != dailygoods.end(); ++it) {
        if (it->getname() == name) {
            it->remove(num);
            if (it->getnumber() == 0) {
                dailygoods.erase(it); // 如果数量为0,删除该商品
            }
            cout << "日用品删除成功!" << endl;
            return;
        }
    }
    cout << "未找到该日用品!" << endl;
}

void removeFood() { // 删除食品
    string name, deadline;
    int num;
    cout << "请输入要删除的食品名称、保质期和数量: ";
    cin >> name >> deadline >> num;

    for (auto it = food.begin(); it != food.end(); ++it) {
        if (it->getname() == name && it->getdeadline() == deadline) {
            it->remove(num);
            if (it->getnumber() == 0) {
                food.erase(it); // 如果数量为0,删除该商品
            }
            cout << "食品删除成功!" << endl;
            return;
        }
    }
    cout << "未找到该食品!" << endl;
}

void removeElectrical() { // 删除电器
    string name, color;
    int num;
    cout << "请输入要删除的电器名称、颜色和数量: ";
    cin >> name >> color >> num;

    for (auto it = electricalappliance.begin(); it != electricalappliance.end(); ++it) {
        if (it->getname() == name && it->getcolor() == color) {
            it->remove(num);
            if (it->getnumber() == 0) {
                electricalappliance.erase(it); // 如果数量为0,删除该商品
            }
            cout << "电器删除成功!" << endl;
            return;
        }
    }
    cout << "未找到该电器!" << endl;
}

点击查看代码

class Supermarket {
public:
    // 其他成员函数...

    void Remove() { // 删除商品
        int choice;
        while (1) {
            cout << "请输入要删除商品的类别: 1 -> 日用品, 2 -> 食品, 3 -> 电器, 0 -> 返回" << endl;
            cin >> choice;
            switch (choice) {
                case 1:
                    removeDaily();
                    return;
                case 2:
                    removeFood();
                    return;
                case 3:
                    removeElectrical();
                    return;
                case 0:
                    return;
                default:
                    cout << "输入有误,请重新输入!" << endl;
                    break;
            }
        }
    }
};

七.代码测试结果

删除以及其他功能加入

八.逆向软件设计总结

1.难点分析

(1)代码重复:在不破坏现有功能的前提下,消除重复代码,提升代码的可维护性。

(2)异常处理不足:保证程序健壮性的同时,避免因异常输入或文件操作失败导致程序崩溃。

(3)内存管理问题:复杂的对象生命周期中,确保内存的正确释放,避免内存泄漏。

(4)订单管理功能不完善:如何在保持系统简洁性的同时,扩展订单管理的功能,使其更加灵活和实用。

(5)用户界面不友好:如何在保持系统功能完整性的同时,提升用户界面的友好性和易用性。

(6)文件格式不明确:如何在代码中明确文件格式,确保文件读写的正确性和一致性。

2.优化代码后优点

(1)代码更加简洁,易于维护和扩展。

(2)程序更加健壮,能够更好地处理异常情况。

(3)程序的内存管理更加规范,避免了潜在的内存泄漏问题。

(4)订单管理功能更加灵活和实用,能够更好地满足用户需求。

(5)用户界面更加友好和美观,提升了用户的操作体验。

(6)文件读写的正确性和一致性得到了保障,减少了文件读取错误的可能性。

3.心得总结

通过对代码的逆向分析和优化,我发现系统在模块化设计和功能实现上具有一定的优点,但也存在代码重复、异常处理不足、内存管理问题等不足之处。通过优化,提升了代码的可维护性、健壮性和用户体验,但仍有一些不足之处需要进一步改进。之后进一步扩展系统的功能,增加销售报表生成、库存预警等高级功能。优化用户界面,增加更多的交互元素和美观的设计。增加对多种文件格式的支持,提升系统的灵活性。通过本次逆向分析和优化,不仅提升了系统的性能和用户体验,还为未来的功能扩展和改进奠定了基础。