商店销售某一商品,每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),在此基础上,一次购10件以上者,还可以享受9.8折优惠。现已知当天m个销货员销售情况为
销货员号(num) 销货件数(quantity) 销货单价(price)
101 5 23.5
102 12 24.56
103 100 21.5
请编写程序,计算出当日此商品的总销售款sum以及每件商品的平均售价。要求用静态数据成员和静态成员函数。
(提示: 将折扣discount,总销售款sum和商品销售总件数n声明为静态数据成员,再定义静态成员函数average(求平均售价)和display(输出结果)。
#include <iostream>
using namespace std;
class shop {
int num;
int quantity;
double price;
static double discount;
static double sum;
static int n;
static double average;
public:
shop(int num,int quantity,double price):num(num),quantity(quantity),price(price){}
shop(shop& p) { num = p.num; quantity = p.quantity; price = p.price; }
void sum_cout() {
double x=0;
if (quantity < 10)
x = quantity * price;
else
x = quantity * price * discount;
sum +=x;
}
static void average_cout(shop a, shop b, shop c) {
average = sum / (a.quantity + b.quantity + c.quantity);
}
void display() {
cout <<"sum="<< sum <<endl<<"average="<< average<< endl;
}
};
double shop::discount = 0.98;
double shop::sum = 0;
int shop::n = 3;
double shop::average = 0;
int main()
{
shop a(101, 5, 23.5), b(102, 12, 24.56), c(103, 100, 21.5);
a.sum_cout(); b.sum_cout(); c.sum_cout();
shop::average_cout(a,b,c);
a.display();
return 0;
}