来源:学姐在大一下学期的期末大作业,超市系统
运行环境:VScode
运行结果截图:
程序代码:
点击查看代码
#include<iostream>
#include"box.h"
using namespace std;
int main() {
set();
huanying();
huansong();
get();
}
#include<iostream>
#include <stdlib.h>
#include<vector>
#include"DailyGoods.h"
#include"Food.h"
#include"ElectricalAppliance.h"
#include"Goods.h"
#include"Supermarket.h"
using namespace std;
int DailyGoods::count1 = 0; //日用类商品样数
int Food::count2 = 0; //食物类商品样数
int ElectricalAppliance::count3 = 0; //家电类商品样数
extern vector<DailyGoods> goods1; // 超市的日用商品库存
extern vector<Food> goods2;
extern vector<ElectricalAppliance> goods3;
vector<Supermarket> store;
string Supermarket::get_name() {
return name;
}
Supermarket::Supermarket(string s):name(s){
DailyGoods a("模板",0);
Food b("模板",0,0);
ElectricalAppliance c("模板",0,"模板");
goods1.push_back(a);
goods2.emplace_back(b);
goods3.emplace_back(c);
}
void Supermarket::Sale() { // 卖出商品
char m;
xuanze:
cout << "请选择要卖出的商品类别:" << endl;
cout << "1.日用商品类" << endl;
cout << "2.食物类" << endl;
cout << "3.家电类" << endl;
cout << "请输入:" << endl;
int x = 0; cin >> x;
switch (x) {
case(1):
{
string s = "";
int amount = 0, b = 0;
int flag = 1;
cout << "请输入商品名称:" << '\t';
cin >> s; m=getchar();
b = search01(s);
if (b == 0) {
cout << endl << "该商品不存在,请重新确认商品类别!" << endl;
system("pause");
system("cls");
goto xuanze;
}
wojiuyaozhege1:
cout << "请输入卖出的商品数量:" << '\t';
cin >> amount; m=getchar();
if (goods1[b].quantity >= amount) {
goods1[b].quantity -= amount;
cout << "成功卖出商品:" << goods1[b].name << endl;
}
else {
cout << "商品"<< goods1[b].name<<"剩余量为:"<<goods1[b].quantity << ",库存不足,无法卖出!" << endl;
}
cout << "若继续买出该商品,请按“1”;若重新选择商品,请按“2”;若退出商品买出界面,请按此外任意建:" << '\t';
cin >> x; m=getchar();
if (x == 1) {
//system("cls");
goto wojiuyaozhege1;
}
else if (x == 2) {
system("cls");
goto xuanze;
}
else {
system("cls");
break;
}
}
case(2):
{
string s = "";
int amount = 0, b = 0;
int flag = 1;
cout << "请输入商品名称:" << '\t';
cin >> s; m=getchar();
b = search02(s);
if (b == 0) {
cout << endl << "该商品不存在,请重新确认商品类别!" << endl;
system("pause");
system("cls");
goto xuanze;
}
wojiuyaozhege2:
cout << "请输入卖出的商品数量:" << '\t';
cin >> amount; m=getchar();
if (goods2[b].quantity >= amount) {
goods2[b].quantity -= amount;
cout << "成功卖出商品:" << goods2[b].name << endl;
}
else {
cout << "商品" << goods2[b].name << "剩余量为:" <<goods2[b].quantity << ",库存不足,无法卖出!" << endl;
}
cout << "若继续卖出该商品,请按“1”;若重新选择商品,请按“2”;若退出商品卖出界面,请按此外任意建:" << '\t';
cin >> x; m=getchar();
if (x == 1) {
system("cls");
goto wojiuyaozhege2;
}
else if (x == 2) {
// system("cls");
goto xuanze;
}
else {
system("cls");
break;
}
}
case(3):
{
string s = "";
int amount = 0, b = 0;
int flag = 1;
cout << "请输入商品名称:" << '\t';
cin >> s; m=getchar();
b = search03(s);
if (b == 0) {
cout << endl << "该商品不存在,请重新确认商品类别!" << endl;
system("pause");
system("cls");
goto xuanze;
}
wojiuyaozhege3:
cout << "请输入卖出的商品数量:" << '\t';
cin >> amount; m=getchar();
if (goods3[b].quantity >= amount) {
goods3[b].quantity -= amount;
cout << "成功卖出商品:" << goods3[b].name << endl;
}
else {
cout << "商品" << goods3[b].name << "剩余量为:" << goods3[b].quantity << ",库存不足,无法卖出!" << endl;
}
cout << "若继续卖出该商品,请按“1”;若重新选择商品,请按“2”;若退出商品卖出界面,请按此外任意建:" << '\t';
cin >> x; m=getchar();
if (x == 1) {
system("cls");
goto wojiuyaozhege3;
}
else if (x == 2) {
//system("cls");
goto xuanze;
}
else {
system("cls");
break;
}
}
default:
cout << "输入错误,请重新输入!";
}
}
int Supermarket::search01(string s) {
if (goods1.size() == 0) return 0;
int i;
for (i = 0; i < goods1.size(); i++)
if (goods1[i].name == s) break;
if (i < goods1.size()) return i;
else return 0;
}
int Supermarket::search02(string s) {
if (goods2.size() == 0) return 0;
int i;
for (i = 0; i < goods2.size(); i++)
if (goods2[i].name == s) break;
if (i < goods2.size()) return i;
else return 0;
}
int Supermarket::search03(string s) {
if (goods3.size() == 0) return 0;
int i;
for (i = 0; i < goods1.size(); i++)
if (goods3[i].name == s) break;
if (i < goods3.size()) return i;
else return 0;
}
void Supermarket::Search() { // 查询商品情况
char m;
xuanze:
cout << "请选择要查询的商品类别:" << endl;
cout << "1.日用商品类" << endl;
cout << "2.食物类" << endl;
cout << "3.家电类" << endl;
cout << "4.返回上一界面" << endl;
cout << "请输入:" << endl;
int x = 0;
cin >> x; m=getchar();
switch (x) {
case(1):
if (goods1.size() == 1) cout << "----------当前类无商品!----------" << endl;
else {
cout << "--------以下是超市" <<name<<"的所有日日常用品类商品--------"<< endl;
cout << "当前日常用品类商品总数为:" <<goods1.size()-1<< endl;
for (int i = 1; i < goods1.size(); i++)
{
goods1[i].ShowMe(); cout << endl;
}
cout << "----------查询完毕!----------" << endl;
}
cout << "若继续查询,请按“1”,若结束查询,请按此外任意键:" << endl;
cin >> x; m=getchar();
if (x == 1) {
//system("pause");
system("cls");
goto xuanze;
}
else {
system("cls");
break;
}
case(2):
if (goods2.size() == 1) cout << "----------当前类无商品!----------" << endl;
else {
cout << "--------以下是超市" << name << "的所有食物类商品--------" << endl;
cout << "当前食物类商品总数为:" << goods2.size() - 1 << endl;
for (int i = 1; i < goods2.size(); i++)
{
goods2[i].ShowMe(); cout << endl;
}
cout << "----------查询完毕!----------" << endl;
}
cout << "若继续查询,请按“1”,若结束查询,请按此外任意键:" << endl;
cin >> x; m=getchar();
if (x == 1)
{
//system("pause");
system("cls");
goto xuanze;
}
else {
system("cls");
break;
}
case(3):
if (goods3.size() == 1) cout << "----------当前类无商品!----------" << endl;
else {
cout << "--------以下是超市" << name << "的所有电器类商品--------" << endl;
cout << "当前电器类商品总数为:" << goods3.size() - 1 << endl;
for (int i = 1; i < goods3.size(); i++)
{
goods3[i].ShowMe(); cout << endl;
}
cout << "----------查询完毕!----------" << endl;
}
cout << "若继续查询,请按“1”,若结束查询,请按此外任意键:" << endl;
cin >> x; m=getchar();
if (x == 1) {
//system("pause");
system("cls");
goto xuanze;
}
else {
system("cls");
break;
}
default:
cout << "输入错误,请重新输入!" << endl;
goto xuanze;
}
}
void Supermarket::Add() { // 增加商品
char m;
caidan1:
cout << "请按需求选择: " << endl;
cout << "1.增加商品" << endl;
cout << "2.增加商品数量" << endl;
cout << "3.返回上一界面" << endl;
cout << "请输入:" << endl;
int x;
cin >> x; m=getchar();
switch (x) {
case(1):
caidan2:
cout << "请选择要增加的商品类别: " << endl;
cout << "1.日用商品类" << endl;
cout << "2.食物类" << endl;
cout << "3.家电类" << endl;
cout << "4.返回上一界面" << endl;
cin >> x; m=getchar();
switch (x) {
case(1):
{
DailyGoods d;
cin >> d;
int p = search01(d.name);
if (p) {
cout << "该商品已存在,不可重复添加!" << endl << "目前该商品的剩余量为";
cout<< goods1[p].quantity << endl;
cout << "若继续添加其数量,请按“1”,若停止请按此外任意键:";
int q; cin >> q; m = getchar();
if (q != 1) goto caidan2;
else {
system("cls");
goto caidan1;
}
}
else {
goods1.push_back(d);
DailyGoods::count1++;
cout << "添加成功!" << endl;
}
cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
cin >> x; m=getchar();
if (x == 1) {
system("cls");
goto caidan1;
}
else {
system("cls");
break;
}
}
case(2):
{
Food b;
cin >> b;
int p = search02(b.name);
if (p) {
cout << "该商品已存在,不可重复添加!" << endl << "目前该商品的剩余量为";
cout << goods2[p].quantity << endl;
cout << "若继续添加其数量,请按“1”,若停止请按此外任意键:";
int q; cin >> q; m = getchar();
if (q != 1) goto caidan2;
else {
system("cls");
goto caidan1;
}
}
else {
goods2.push_back(b);
Food::count2++;
cout << "添加成功!" << endl;
}
cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
cin >> x; m=getchar();
if (x == 1) {
system("cls");
goto caidan1;
}
else {
system("cls");
break;
}
}
case(3):
{
ElectricalAppliance c;
cin >> c;
int p = search03(c.name);
if (p) {
cout << "该商品已存在,不可重复添加!" << endl << "目前该商品的剩余量为";
cout << goods3[p].quantity << endl;
cout << "若继续添加其数量,请按“1”,若停止请按此外任意键:";
int q; cin >> q; m = getchar();
if (q != 1) goto caidan2;
else {
system("cls");
goto caidan1;
}
}
else {
goods3.emplace_back(c);
ElectricalAppliance::count3++;
cout << "添加成功!" << endl;
}
cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
cin >> x; m=getchar();
if (x == 1) {
system("cls");
goto caidan1;
}
else {
system("cls");
break;
}
}
case(4):
//system("pause");
system("cls");
goto caidan1;
default:
cout << "输入错误,请重新输入!";
goto caidan2;
}
goto caidan1;
default:
cout << "输入错误,请重新输入!";
goto caidan1;
}
}
#include"Goods.h"
#include<iostream>
#include<string>
#include "Food.h"
#include"Supermarket.h"
using namespace std;
istream& operator >>(istream &is, Goods& goods){
char m;
cout << "请输入商品名称: " << endl;
is >> goods.name; m=getchar();
cout << "请输入商品数量: " << endl;
is >> goods.quantity; m=getchar();
return is;
}
#include<iostream>
#include"Food.h"
#include<vector>
using namespace std;
void Food::ShowMe(){
cout << "商品名称:" << name << endl;
cout << "商品现有数量:" << quantity << endl;
cout << "保质期:" << shelfLife << " 天" << endl;
}
istream& operator >>(istream& is, Food& food) {
char m;
cout << "请输入商品名称: " << endl;
is >> food.name; m=getchar();
cout << "请输入商品数量: " << endl;
is >> food.quantity;m=getchar();
cout << "请输入商品保质期(单位/天): " << endl;
is >> food.shelfLife;m=getchar();
return is;
}
ostream& operator<<(ostream& output, Food& food) {
output << "商品名称:" << food.name << endl;
output << "商品现有数量:" << food.quantity << endl;
output << "保质期:" << food.shelfLife << " 天" << endl;
return output;
}
#include"ElectricalAppliance.h"
#include<iostream>
#include<vector>
using namespace std;
void ElectricalAppliance::ShowMe(){
cout << "商品名称:" << name << endl;
cout << "商品现有数量:" << quantity << endl;
cout << "颜色:" << color << endl;
}
istream& operator >>(istream& is, ElectricalAppliance& appliance) {
char m;
cout << "请输入商品名称: " << endl;
is >> appliance.name; m=getchar();
cout << "请输入商品数量: " << endl;
is >> appliance.quantity; m=getchar();
cout << "请输入商品颜色: " << endl;
is >> appliance.color; m=getchar();
return is;
}
ostream& operator<<(ostream& output, ElectricalAppliance& appliance) {
output << "商品名称:" << appliance.name << endl;
output << "商品现有数量:" << appliance.quantity << endl;
output << "颜色:" << appliance.color << endl;
return output;
}
#include<iostream>
#include <stdlib.h >
#include <windows.h>
#include<string>
#include<vector>
#include<fstream>
#include"Supermarket.h"
#include"Food.h"
#include"box.h"
using namespace std;
extern vector<Supermarket> store;
int search_store(string s) {
int i;
for (i = 0; i < store.size(); i++) {
if (s == store[i].name) break;
}
if (i < store.size()) return i;
else return 0;
}
void huansong() {
cout << "----------***感谢您的使用!***----------" << endl;
}
/*void Buy() {
Supermarket a;
cout << "请输入您所买商品的名称: " << endl;
string s;
cin >> s;getchar();
cout << "请输入您要购买的数量: " << endl;
int n = 0;
cin >> n;getchar();
a.Sale(s,n);
}*/
void set() {
Supermarket w("模板");
store.push_back(w);
ifstream ifs;
ifs.open("商品.text", ios::in);
int n=0,a=0,b=0,c=0;
ifs >> n;
for (int i = 1; i < n; i++) {
string y;ifs >> y;
Supermarket w(y);
ifs >> a ;
DailyGoods::count1 = a;
for (int j = 1; j < a; j++)
{
string s; int x;
ifs >> s;
ifs >>x;
DailyGoods a(s, x);
w.goods1.push_back(a);
}
ifs >>b;
Food::count2 = b;
for (int j = 1; j < b; j++)
{
string s; int x, h;
ifs >>s;
ifs >>x;
ifs>>h;
Food a(s, x, h);
w.goods2.push_back(a);
}
ifs >> c;
ElectricalAppliance::count3 = c;
for (int j = 1; j < c; j++)
{
string s, l; int x;
ifs >>s;
ifs >>x;
ifs >>l;
ElectricalAppliance a(s, x, l);
w.goods3.push_back(a);
}
store.push_back(w);
}
ifs.close();
}
void get() {
ofstream ofs;
ofs.open("商品.text", ios::out);
ofs << store.size() << endl;
for (int i = 1; i < store.size(); i++) {
ofs << store[i].name << endl;
ofs << store[i].goods1.size() << endl;
for (int j = 1; j < store[i].goods1.size(); j++)
{
ofs << store[i].goods1[j].name<<endl;
ofs << store[i].goods1[j].quantity << endl;
}
ofs << store[i].goods2.size() << endl;
for (int j = 1; j < store[i].goods2.size(); j++)
{
ofs << store[i].goods2[j].name << endl;
ofs << store[i].goods2[j].quantity << endl;
ofs << store[i].goods2[j].shelfLife << endl;
}
ofs << store[i].goods3.size() << endl;
for (int j = 1; j < store[i].goods3.size(); j++)
{
ofs << store[i].goods3[j].name << endl;
ofs << store[i].goods3[j].quantity << endl;
ofs << store[i].goods3[j].color << endl;
}
}
ofs.close();
}
void add_store() {
char m;
cout << "----------***欢迎创建您的超市***-----------" << endl;
cout << "请输入您的新超市名称:" << '\t';
string s;
cin >> s; m=getchar();
Supermarket a(s);
store.push_back(a);
cout << "恭喜您拥有自己的第" <<store.size()-1<<"家超市!" << endl;
long time;
{
for (time = 0; time < 3; time++)
system("color 0F");
Sleep(50);
for (time = 0; time < 3; time++)
system("color 0A");
Sleep(50);
for (time = 0; time < 3; time++)
system("color 0B");
Sleep(50);
for (time = 0; time < 3; time++)
system("color 0C");
Sleep(50);
for (time = 0; time < 3; time++)
system("color 0D");
Sleep(50);
for (time = 0; time < 3; time++)
system("color 0E");
Sleep(50);
for (time = 0; time < 3; time++)
system("color 0D");
Sleep(50);
for (time = 0; time < 3; time++)
system("color 0B");
Sleep(50);
for (time = 0; time < 3; time++)
system("color 0F");
Sleep(50);
}
}
void delete_store() {
char m;
cout << "请输入您要解散的超市名称:" << endl;
string s;
cin >> s; m = getchar();
for (auto it = store.begin(); it != store.end();)
{
if (s == it->name) {
it = store.erase(it);
}
else {
++it;
}
}
cout << "------***解散成功!***-------" << endl;
}
void hb() {
int flag = 1; char m;
int x, y;
//输入被解散的超市a
while (flag) {
cout << "您想要取消的超市是:";
string a;
cin >> a; m = getchar();
x = search_store(a);
if (x == 0) {
cout << "您输入的超市不存在,请重新输入!" << endl;
flag++;
if (flag == 3) {
cout << "您已输入错误三次!" << endl;
cout << "若您选择返回上一界面,请按“1”,若选择继续输入,请按此外任意键:" << endl;
int x;
cin >> x; m = getchar();
if (x == 1) exit(6);
else flag = 1;
}
}
else flag = 0;
}
flag = 1;
//输入被加入的超市b
while (flag) {
cout << "您想要将前者合并到超市:";
string b;
cin >> b; m = getchar();
y = search_store(b);
if (y == 0) {
cout << "您输入的超市不存在,请重新输入!" << endl;
flag++;
if (flag == 3) {
cout << "您已输入错误三次!" << endl;
cout << "若您选择返回上一界面,请按“1”,若选择继续输入,请按此外任意键:" << endl;
int x;
cin >> x; m = getchar();
if (x == 1) exit(6);
else flag = 1;
}
}
else flag = 0;
}
//a有b也有的商品,只加数量
for (int j = 1; j < store[x].goods1.size(); j++) {
int g = store[y].search01(store[x].goods1[j].name);
if (g != 0) {
store[y].goods1[g].quantity += store[x].goods1[j].quantity;
auto it = store[x].goods1.begin();
int w = 0;
while (w != g) {
it++;
w++;
}
store[x].goods1.erase(it);
}
}
for (int j = 1; j < store[x].goods2.size(); j++) {
int g = store[y].search02(store[x].goods2[j].name);
if (g != 0) {
store[y].goods2[g].quantity += store[x].goods2[j].quantity;
auto it = store[x].goods2.begin();
int w = 0;
while (w != g) {
it++;
w++;
}
store[x].goods2.erase(it);
}
}
for (int j = 1; j < store[x].goods3.size(); j++) {
int g = store[y].search03(store[x].goods3[j].name);
if (g != 0) {
store[y].goods3[g].quantity += store[x].goods3[j].quantity;
auto it = store[x].goods3.begin();
int w = 0;
while (w != g) {
it++;
w++;
}
store[x].goods3.erase(it);
}
}
//a有b没有的整个加入
for (int j = store[x].goods1.size() - 1; j > 0; j--) {
store[y].goods1.push_back(store[x].goods1[j]);
store[x].goods1.pop_back();
}
for (int j = store[x].goods2.size() - 1; j >0; j--) {
store[y].goods2.push_back(store[x].goods2[j]);
store[x].goods2.pop_back();
}
for (int j = store[x].goods3.size() - 1; j >0; j--) {
store[y].goods3.push_back(store[x].goods3[j]);
store[x].goods3.pop_back();
}
auto it = store.begin();
int w = 0;
while (w != x) {
w++;
it++;
}
store.erase(it);
cout << "-----***合并成功!***-----" << endl;
}
void huanying() {
char m;
if (store.size() ==1) {
cout << "----------***欢迎来到超市经营系统***----------" << endl;
cout << "请先创建您的第一个超市!" << endl;
cout << "请输入您的超市名称:" << '\t';
string s;
cin >> s; m=getchar();
Supermarket w(s);
store.push_back(w);
cout << "---------*创建成功*----------!" << endl;
cout << "*******恭喜您拥有自己的第一个超市!*******" << endl;
long time;
{
for(time=0;time<3;time++)
system("color A8");
Sleep(50);
for (time = 0; time < 3; time++)
system("color B8");
Sleep(50);
for (time = 0; time < 3; time++)
system("color C8");
Sleep(50);
for (time = 0; time < 3; time++)
system("color D8");
Sleep(50);
for (time = 0; time < 3; time++)
system("color E8");
Sleep(50);
for (time = 0; time < 3; time++)
system("color D8");
Sleep(50);
for (time = 0; time < 3; time++)
system("color E8");
Sleep(50);
for (time = 0; time < 3; time++)
system("color F8");
Sleep(50);
for (time = 0; time < 3; time++)
system("color 0F");
Sleep(50);
}
system("pause");
system("cls");
goto zhucaidan;
}
else {
zhucaidan:
cout << "----------***欢迎来到超市经营系统***----------" << endl;
cout << "请选择按需求选择您的操作:" << endl;
cout << "1.管理物资" << endl;
cout << "2.增加新店" << endl;
cout << "3.解散店铺" << endl;
cout << "4.合并店铺" << endl;
cout << "5.查看已有店铺" << endl;
cout << "6.退出系统" << endl;
cout << "请输入:" << endl;
int x;
cin >> x; m=getchar();
switch (x) {
case(1):
{
cout << "请输入您要管理的超市名称:" << endl;
string s;
cin >> s; m=getchar();
int i = search_store(s);
if (i == 0) {
cout << "该商店不存在!" << endl;
system("pause");
system("cls");
goto zhucaidan;
}
else {
caidan1:
cout << "请选择按需求选择您的操作:" << endl;
cout << "1.查询商品" << endl;
cout << "2.返回上一界面" << endl;
cout << "请输入:" << endl;
cin >> x;m=getchar();
system("cls");
switch (x) {
case(1):
{
store[i].Add();
cout << "如果您想继续在运行该超市,请按“1”,否则请按此外任意按键:" << '\t';
int y = 0;
cin >> y; m=getchar();
if (y == 1) {
system("cls");
goto caidan1;
}
else goto zhucaidan;
}
case(2):
{
store[i].Search();
cout << "如果您想继续在运行该超市,请按“1”,否则请按此外任意按键:" << '\t';
int y = 0;
cin >> y; m=getchar();
if (y == 1) {
system("cls");
goto caidan1;
}
else goto zhucaidan;
}
case(3):
{
store[i].Sale();
cout << "如果您想继续在运行该超市,请按“1”,否则请按此外任意按键:" << '\t';
int y = 0;
cin >> y; m=getchar();
if (y == 1) {
goto caidan1;
system("cls");
}
else goto zhucaidan;
}
case(4):
goto zhucaidan;
default:
cout << "输入错误,请重新输入!" << endl;
goto caidan1;
}
}
}
case(2):
add_store();
system("pause");
system("cls");
goto zhucaidan;
case(3):
delete_store();
system("pause");
system("cls");
goto zhucaidan;
case(4):
hb();
system("pause");
system("cls");
goto zhucaidan;
case(5):
cout << "----------***欢迎查看您的资产***----------" << endl;
for (int i = 1; i < store.size(); i++) {
cout <<"第"<<i<<"家超市名称为" << store[i].get_name() << endl;
}
system("pause");
system("cls");
goto zhucaidan;
case(6):
break;
default:
cout << "输入错误,请重新输入!" << endl;
goto zhucaidan;
}
}
}
主要问题改善:
增加了二级界面的返回功能,并分成直接返回最开始界面还是该超市界面
新代码截图:
总结:
这一次的体会让我深刻意识到为何有些公司面对多年运行下来的屎山代码无能为力的感觉。在面对其他同学的代码时,想要读懂他所写的代码和他的运行逻辑就不是一件简单的事情,在最开始的更改中,常常会改了一小点之后就导致整个程序报错无法运行,因而只能磕磕绊绊地一点点修改些细枝末节的地方,对于代码的主干要吗进行全部推倒重构,要么只能选择敬而远之。不过我在这次的经历中也收获不少,在读了同学的代码后,我也感觉到了写代码时规范的重要性,规范的写法才能使程序员更好读懂你语句的含义。希望自己以后能在这方面尽可能的做好