# 熟悉题型——改错题（商品销售）

101 5 23.5

102 12 24.56

103 100 21.5

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

#include <iostream>
#include <iomanip>
using namespace std;
class Product

public:
Product(int n,int q,float p):num(n),quantity(q),price(p) {};
Product() {};
void total();
static float ave();
static void display();

private:
int num;
int quantity;
float price;
static float discount;
static float sum;
static int n;
};
/*****************************

int rate=1;
if(quantity>10) rate=0.98*rate;
sum=quantity*price*rate*(1-discount);
n=n+quantity;

********************************/
void Product::display()

cout<<sum<<endl;
cout<<ave()<<endl;

float Product::ave()

return(sum/n);

float Product::discount=0.05;
float Product::sum=0;
int Product::n=0;

int main()
{
const int NUM =10;
Product Prod[10];
int m,i;
cin>>m;
int num;
int quantity;
float price;
for(i=0; i<m; i++)
{
cin>>num>>quantity>>price;
Product temp(num,quantity,price);
Prod[i]=temp;
}
for(i=0; i<m; i++)
Prod[i].total();
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
Product::display();
return 0;
}

m和m个销货员销售情况

``````3
101 5 23.5
102 12 24.56
103 100 21.5``````

``````2387.66
20.41``````

``````
#include <stdio.h>
#include <string.h>
int main()
{
int m,i,quantity,num,n=0;
double price;
double s=0;
scanf("%d",&m);
for(i=0; i<m; i++)
{
scanf("%d%d%lf",&num,&quantity,&price);
if(quantity>10)price*=0.98;
n+=quantity;
s+=quantity*price;
}
printf("%.2lf\n%.2lf\n",s*0.95,s*0.95/n);
return 0;
}
``````

``````
#include <iostream>
#include <iomanip>
using namespace std;
class Product
{
public:
Product(int n,int q,float p):num(n),quantity(q),price(p) {};
Product() {};
void total();
static float ave();
static void display();

private:
int num;
int quantity;
float price;
static float discount;
static float sum;
static int n;
};  void Product::total()
{
float rate=1;
if(quantity>10) rate=0.98*rate;
sum=sum+quantity*price*rate*(1-discount);
n=n+quantity;
}
void Product::display()
{
cout<<sum<<endl;
cout<<ave()<<endl;
}

float Product::ave()
{
return(sum/n);
}
float Product::discount=0.05;
float Product::sum=0;
int Product::n=0;

int main()
{
const int NUM =10;
Product Prod[10];
int m,i;
cin>>m;
int num;
int quantity;
float price;
for(i=0; i<m; i++)
{
cin>>num>>quantity>>price;
Product temp(num,quantity,price);
Prod[i]=temp;
}
for(i=0; i<m; i++)
Prod[i].total();
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
Product::display();
return 0;
}
``````