大作业题(四)

本人大一新生,这是根据老师布置的作业自己写的代码,有不足之处请指正。
问题描述
编写商品销售统计程序,商品的信息有:商品的名称,计量单位(重量或件),单价。所有商品的信息应事先已存入计算机,屏幕上显示所有商品的名称,选择商品名,输入商品计量单位(如重量,件数等),根据单价算出总价。客户一次购物可能购买多种商品,程序应计算出客户应付的钱款数。
基本要求
程序分为两个部分:第一部分用于输入商品的信息并允许修改和删除;第二部分实现销售统计。程序运行时由用户选择进入哪一部分功能,并能在运行时在两部分之间切换。第二部分运行时,首先显示所有商品名称及代码(商品数目较多时,应考虑分屏显示),用户输入商品代码及商品重量或件数,用户一次操作可输入若干商品的购买信息,然后输入一个特殊的代码(如-1)表示本次购物结束。此时。程序计算出应付钱款数并显示。
测试数据
程序应输入不少于10种商品的信息,并进行模拟运行。
实现提示
本程序的商品信息管理可采用与课程设计题目二类似的数据结构,既定义一个商品类,每种商品作为商品类的实例(对象)存储在数组或链表节点中。
选做内容
程序在营业结束时统计每种商品的销售量,销售金额及总营业额。因此第二部分应有营业结束的选择,当用户选择此项时屏幕上显示当天营业的每种商品的销售量,销售金额及总营业额。注意,商品类的数据成员应增加有商品的销售量和销售金额。总营业额是所有商品的营业额之和,可用静态数据成员实现。或可由原商品类派生出一个特殊的类,增加上面的数据成员及相应的成员函数。
 

头文件:Goods.h

#ifndef _GOODS_H_
#define _GOODS_H_
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::istream;
using std::setw;
using std::string;
using std::stringstream;
using std::vector;
class Goods {
public:
	Goods();
	Goods(string,bool,double);
	void Print();
	double getValue() const { return Value; }
	bool getMes() const { return Measurement; }
	void PrintName() { cout << Name; }
	int num;
	double sumValue;
	static double sum;
private:
	string Name;
	bool Measurement;//0按件计,1按重量计
	double Value;
};
#endif

类的实现:Goods.cpp

#include "Goods.h"
extern vector<Goods> Gds;
extern vector<vector<vector<int>>> shoppinglist;
double Goods::sum = 0;
Goods::Goods() :Name(), Measurement(0), Value(0), num(0), sumValue(0) {}
Goods::Goods(string _Name, bool _Measurement, double _Value) : Name(_Name), Measurement(_Measurement), Value(_Value), num(0), sumValue(0) {}
void Goods::Print() {
	cout << setw(20) << Name << setw(10) << (Measurement ? "按重量计" : "按件数计") << setw(8) << Value;
}
void Input() {
	cout << "添加商品" << endl;
	string str;
	bool bl;
	double v;
	cout << "请输入商品的名称:" << endl;
	cin >> str;
	cout << "请输入商品的计价方式(0按件计,1按重量计):" << endl;
	cin >> bl;
	cout << "输入商品的价格:" << endl;
	cin >> v;
	Gds.push_back(Goods(str, bl, v));
	cout << "添加完成" << endl;
}
void Print() {
	cout << "商品编号" << setw(20) << "商品名称" << setw(10) << "计价方式"<< setw(8)<<"价格" << endl;
	int i;
	for (i = 0; i < Gds.size(); i++) {
		cout << setw(8) << i;
		Gds[i].Print();
		cout << endl;
	}
}
void fun1() {
	cout << "商店共有" << Gds.size() << "件商品,分别为:" << endl;
	Print();
	char a;
	int b;
	cout << "请选择功能:" << endl;
	cout << "1.修改现有的商品信息" << endl;
	cout << "2.增加新的商品信息" << endl;
cs0:cin >> a;
	switch (a) {
	case '1':cout << "请选择要更改的产品的编号:"; goto cs1;; break;
	case '2':goto cs2; break;
	default:cout << "输入有误,请重新选择:"; goto cs0;
	}
cs1:cin >> b;
	if (b > Gds.size()) {
		cout << "有误,请重新输入编号:";
	}
	cout << "要修改的产品信息如下:";
	Gds[b].Print();
	cout << "\n确认要修改吗?是:y;重新选择:n或r" << endl;
	cin >> a;
	switch (a) {
	case 'y': {
		string str;
		bool bl;
		double v;
		cout << "请输入商品的名称:" << endl;
		cin >> str;
		cout << "请输入商品的计价方式(0按件计,1按重量计):" << endl;
		cin >> bl;
		cout << "输入商品的价格:" << endl;
		cin >> v;
		Gds[b] = Goods(str, bl, v);
		cout << "修改完成的产品信息如下:" << endl;
		Gds[b].Print();
		cout << endl;
	}
	default:cout << "请重新输入编号:"; goto cs1;
	}
	goto cse;
cs2:Input();
cse:return;
}
void fun2() {
	cout << "商店的商品如下:" << endl;
	Print();
	int id, num = 0;
	double s = 0;
	vector<vector<int>> _shoppinglist;
	cout << "请选择您要购买的商品,请依次输入商品代码和件数:" << endl;
	string str;
	getline(cin, str);
	stringstream ss(str);
	while (1) {
		ss >> id;
		if (ss.fail()) break;
		if (id == -1) break;
		ss >> num;
		vector<int> a = { id,num };
		_shoppinglist.push_back(a);
	}
	cout << "您总共购买了" << _shoppinglist.size() << "种产品,分别为:" << endl;
	for (int i = 0; i < _shoppinglist.size(); i++) {
		cout << _shoppinglist.at(i)[1] << "件";
		Gds.at(_shoppinglist.at(i)[0]).PrintName();
		cout << endl;
		Gds.at(_shoppinglist.at(i)[0]).num += _shoppinglist.at(i)[1];
		Gds.at(_shoppinglist.at(i)[0]).sumValue = Gds.at(_shoppinglist.at(i)[0]).getValue()*_shoppinglist.at(i)[1];
		s += Gds.at(_shoppinglist.at(i)[0]).sumValue;
	}
	cout << "总价为" << s << "元" << endl;
	shoppinglist.push_back(_shoppinglist); 
	Goods::sum += s;
	if (id == -1) return;
	else return fun2();
}
void fun3() {
	cout << endl;
	cout << "今天共进行了" << shoppinglist.size() << "单生意,分别如下:" << endl;
	cout << endl;
	int i, j;
	for (i = 0; i < shoppinglist.size(); i++) {
		cout << "第" << i + 1 << "单生意:" << endl;
		for (j = 0; j < shoppinglist.at(i).size(); j++) {
			cout << shoppinglist.at(i).at(j)[1] << ((Gds.at(shoppinglist.at(i).at(j)[0]).getMes() == 0) ? "件" : "千克");
			Gds.at(shoppinglist.at(i).at(j)[0]).PrintName();
			cout << endl;
		}
		if (shoppinglist.at(i).size() == 0) {
			cout << "未卖出商品。" << endl;
		}
		cout << endl;
	}
	cout << "其中,每件产品的营业额如下:" << endl;
	cout << "商品编号" << setw(20) << "商品名称" << setw(10) << "计价方式" << setw(8) << "价格" << setw(8) << "销售量" << setw(8) << "销售额" << endl;
	for (i = 0; i < Gds.size(); i++) {
		cout << setw(8) << i;
		Gds[i].Print();
		cout << setw(8) << Gds[i].num << setw(8) << Gds[i].sumValue << endl;
	}
	cout << endl;
	cout << "总营业额为" << Goods::sum << "元。\n";
	cout << endl;
	system("pause");
}

主程序:main.cpp

#include "Goods.h"
vector<Goods> Gds{
	Goods("营养快线",0,4),
	Goods("脉动",0,4),
	Goods("火鸡面",0,4.5),
	Goods("清风纸抽",0,3),
	Goods("杯子",0,7),
	Goods("三只松鼠松子",0,6),
	Goods("小米MIX2S 6G+128G",0,3299),
	Goods("薯片",1,18),
	Goods("开心果",1,32),
	Goods("洗面奶",0,32),
	Goods("三只松鼠大礼箱",0,88),
	Goods("草稿纸",0,2),
	Goods("笔记本",0,6),
	Goods("耳机",0,89),
	Goods("Type-C耳机",0,89),
	Goods("高数习题册",0,5),
};
vector<vector<vector<int>>> shoppinglist;
void fun1();
void fun2();
void fun3();
int main() {
	char a;
brk:cout << "请选择功能,输入其他内容退出:" << endl;
	cout << "1.修改商品信息" << endl;
	cout << "2.购买商品" << endl;
	cout << "3.营业结束" << endl;
	cin >> a;
	cin.ignore();
	switch (a) {
	case '1':fun1(); break;
	case '2':fun2(); break;
	case '3':fun3(); break;
	default:return 0;
	}
	goto brk;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43773570/article/details/85477840