1135人加入学习
(5人评价)
C++编程系列 第二季函数和类

制作于2018.4.2

价格 免费
课程还未发布,不允许加入和购买

药品物资管理

要求:

1.创建药品类来完成习题四中对药品相关的管理。

2.利用对象数组来完成对不同种类药品信息的存储。

3.使用this*指针。

4.使用作用域为类的常量。

Medicines.h

#pragma once
#pragma once
#define MEDICINES_H_
#include "string"
using namespace std;
enum kind { hp, mp };

class Medicines
{
private:
	string name_;
	kind kin_;
	int number_;
	float buy_;
	float sell_;
	static const int Price = 100;
public:
	Medicines(string name, kind kin, int number);
	float Buyin(Medicines& m, float res);
	float Sellout(Medicines& m, float res);
	void display(Medicines m);
	void show(kind k);
};

Medicines.cpp

#include "Medicines.h"
#include <iostream>
#include "stdio.h"
#include "string"
Medicines::Medicines(string name, kind kin, int number)
{
	name_ = name;
	kin_ = kin;
	number_ = number;
	buy_ = Price;
	sell_ = Price * (3.0 / 4);
}

float Medicines::Buyin(Medicines& m, float res)
{
	int i;
	int j = res;
	cout << "请输入购买数量:";
	cin >> i;
	if (j > i * this->buy_)
	{
		this->number_ += i;
		j -= i * this->buy_;
		cout << "购买了" << i << "瓶";
		show(this->kin_);
		cout << "药水" << endl;
	}
	else
		cout << "金钱不足" << endl;
	return j;
}

float Medicines::Sellout(Medicines& m, float res)
{
	int i;
	int j = res;
	cout << "请输入卖出数量:";
	cin >> i;
	if (i < this->number_)
	{
		this->number_ -= i;
		j += i * this->sell_;
		cout << "卖出了" << i << "瓶";
		show(this->kin_);
		cout << "药水" << endl;
	}
	else
		cout << "数量不足" << endl;
	return j;
}

void Medicines::display(Medicines m)
{
	cout << "名称:" << m.name_ << "  " << "种类:";
	show(m.kin_);
	cout << "  " << "数量:" << m.number_ << "  " << "购买价格" << m.buy_ << "  " << "出售价格" << m.sell_ << endl;

}

void Medicines::show(kind k)
{
	if (k == 0)
		cout << "hp ";
	else
		cout << "mp ";
}

主程序

#include <iostream>
#include "stdio.h"
#include "string"
#include "Medicines.h"
using namespace std;
int main()
{
	int select;
	float res = 1000;
	Medicines ms[2] = { { "hp药水",kind(0),5} ,{ "mp药水",kind(1),5} };
	do {
		cout << "请输入对应指令" << endl << "1.购买hp药 2.卖出hp药 3.购买mp药 4.卖出mp药 5.显示背包 6.退出" << endl;
		cin >> select;
		switch (select)
		{
		case 1:
			res = ms[0].Buyin(ms[0],res);
			break;
		case 2:
			res = ms[0].Sellout(ms[0], res);
			break;
		case 3:
			res = ms[1].Buyin(ms[1], res);
			break;
		case 4:
			res = ms[1].Sellout(ms[1], res);
			break;
		case 5:
		{
			ms[0].display(ms[0]);
			ms[1].display(ms[1]);
			cout << "剩余金钱为:" << res << endl;
		}
		break;
		case 6:
			break;
		default:
			break;
		}
	} while (select != 6);


}

 

[展开全文]

授课教师

SIKI学院老师

课程特色

下载资料(1)
视频(43)

学员动态