静态成员—商品销售统计


#define _CRT_SECURE_NO_WARINGS
#include  <iostream>
#include  <iomanip>
using  namespace  std;
//【问题描述】

//商店销售某一商品,每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),在此基础上,一次购10件以上者,还可以享受9.8折优惠。例如,已知当天m个销货员销售情况为

//销货员号(num)            销货件数(quantity)       销货单价(price)

//101               5                            23.5

//102               12                            24.56

//103              100                           21.5

//请编写程序,计算出当日此商品的总销售款sum以及每件商品的平均售价。要求用静态数据成员和静态成员函数。

//(提示: 将销货员设计为Product类,将折扣discount,总销售款sum和商品销售总件数n声明为静态数据成员,再定义静态成员函数average(求平均售价)和display(输出结果)。

//	【输入形式】

//	m和m个销货员销售情况

//	【输出形式】

//	总销售款sum以及每件商品的平均售价
//	【样例输入】
//
//	3 101 5 23.5 102 12 24.56 103 100 21.5
//
//	【样例输出】
//
//	2387.66
//
//	20.41
//补充类体的设计
class Product
{
public:
	int n1;
	double s1;
	static int n;//销售总件数
	static float sum;//销售总额
	static float discount;//折扣
	Product(int num,int quannity,float price)
	{
		n1 = quannity;
		if (quannity > 10)
		{
			s1 = quannity*price*0.98;
		}
		else
			s1 = quannity*price;
		s1 *= 0.95;
	}
	void total()
	{
		n += n1;
		sum += s1;
	}
	static void display()
	{
		cout << sum << endl<<sum/n;
		
	}
};

/*  C++主函数代码  */
float  Product::discount = 0.05;
float  Product::sum = 0;
int  Product::n = 0;
int  main()
{
	const  int  NUM = 10;
	Product*  Prod[NUM];
	int  m, i;
	cin >> m;
	int  num;
	int  quantity;
	float  price;
	for (i = 0; i<m; i++)
	{
		//输入销货员号(num)  、销货件数(quantity)  、销货单价(price)
		cin >> num >> quantity >> price;
		Prod[i] = new  Product(num, quantity, price);
	}
	for (i = 0; i<m; i++)
		Prod[i]->total();
	//输出总销售款以及每件商品的平均售价
	cout << setiosflags(ios::fixed);//控制输出精度
	cout << setprecision(2);//ios::fixed格式化输出流,默认有效位数6位,可通过setprecision()修改有效位数
	Product::display();
	return  0;
	system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值