Generates a list of goods bought and sold

A store sells one kind of goods. The goods purchased and sold for container basis, the weight of each box is not the same, therefore, need to record the total weight of the current store inventory (use static member variables). Now the purchase and sale of the situation in C ++ simulation goods in the shops. Tip: Use the list structure and static member variables
need to improve the content includes:
Goods class, including the class constructor, destructor, member functions, static member function as well as private member variables.
Two separate functions: the purchase (Purchase) and sold (Sale)
procedural requirements:
1.main.cpp contains only the main function, and can not be changed.
2. program a clear structure, placing notices in the code .h file, place custom code .cpp file
3. Ensure program can run and correct results.
FIG main.cpp code below
Here Insert Picture Description
so he established at a header file to the main codes Goos.h; according to the above information and that w is a number input box his goods, TotalWeight total weight of the goods, it should be TotalWeight a static member.
The writing Goos class constructor of the public, destructor, and a received value, and a static member of the public.
code show as below:

class Goods {
	public: 
		Goods(int w) {//购入
			weight = w;
			total_weight += w;
		}
		~Goods() {//售出
			total_weight -= weight;
		}
		int  Weight(){return  weight;};
		static int TotalWeight() {return total_weight;}/*公有静态成员*/
	Goods *next;
	private: 
		int weight; 
		static int total_weight;
};
int Goods::total_weight = 0;//静态成员初始化
//题目说明要有两个单独的函数:purchase货物的购进以及sale卖出表头的第一箱货物,所以再构造函数中进行带参来传进重量值来进行计算以及在purchase函数里进行节点的生成
void purchase(Goods *&front, Goods *&rear, int w) {
	if (w <= 0)
	{
cout << "不能买入非正整数\n" ;
	 	return;
}
	Goods *p = new Goods(w);
	p->next = NULL;
	if (front == NULL) { front = rear = p; }
	else { rear->next = p;    rear = rear->next; }};
//析构函数进行sale,货物卖出并且减去头结点的值。
void sale(Goods *&front, Goods *&rear) {
	//卖出货物,实际是删除链表的头结点
		if (front == NULL)
		{
			cout << "没有货物了\n";
			return;
		}
		Goods *q = front;
		front = front->next;
		delete q;
		cout << "已售出\n";
};

Results are as follows:
Here Insert Picture DescriptionHere Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_39686486/article/details/90292864