//超市每天卖出的货物假如有egg,apple,pear三种,销售账目存储在了字典里,如下:
//account = [{"egg":[5.00, 3, "s"],
//"apple":[2.50, 5, "s"],
//"pear":[4.50, 2, "s"]},
//{"egg":[6.00, 3, "s"],
//"apple":[3.00, 15, "q"],
//"pear":[6.50, 2, "s"]},
//{"egg":[5.50, 5, "s"],
//"apple":[3.50, 5, "s"],
//"pear":[5.00, 6, "q"]},
//{"egg":[5.00, 3, "s"],
//"apple":[2.50, 6, "s"],
//"pear":[5.50, 2, "q"]},
//{"egg":[5.50, 8, "q"],
//"apple":[3.50, 5, "s"],
//"pear":[5.50, 3, "s"]}]
//每天的账目是列表中的一个元素,包括一个字典,字典的键是货物名称,字典的值的第一个元素是单价,
//第二个元素是数目,第三个元素,如果是s表示该账目没有任何疑问,如果是q表示该账目可能有错误。
//我们要做的是事情是统计每种货物,没有任何疑问的账目的销售总额。也就是对标志位为"s"的账目,
//按照货物名称,计算销售总额。(自己可根据使用的编程语言,确定存储结构,最后结果在屏幕打印
//egg、apple和pear分别的没有任何疑问的帐目的销售总额就可以。)
#include"iostream"
#include"string"
using namespace std;
class Account
{
private:
string name;
double price;
float number;
char flag;
public:
Account(string goodsname, double goodsprice, float goodsnumber, char goodsflag)
{
name = goodsname;
price = goodsprice;
number = goodsnumber;
flag = goodsflag;
}
double getTotalPrice()
{
return price*number;
}
string getName()
{
return name;
}
char getFlag()
{
return flag;
}
};
int main()
{
Account accounts[15] = {
Account("egg",5.00,3,'s'),
Account("apple",2.50,5,'s'),
Account("pear",4.50,2,'s'),
Account("egg",6.00,3,'s'),
Account("apple",3.00,15,'q'),
Account("pear",6.50,2,'s'),
Account("egg",5.50,5,'s'),
Account("apple",3.50,5,'s'),
Account("pear",5.00,6,'q'),
Account("egg",5.00,3,'s'),
Account("apple",2.50,6,'s'),
Account("pear",5.50,2,'q'),
Account("egg",5.50,8,'q'),
Account("apple",3.50,5,'s'),
Account("pear",5.50,3,'s')};
string names[3] = {"egg", "apple", "pear"};
int i=0;
int j;
double count=0;
for(i=0; i<3; i++)
{
count = 0;
for(j=0; j<15; j++)
{
if((accounts[j].getName() == names[i]) && (accounts[j].getFlag() == 's'))
{
count = count + accounts[j].getTotalPrice();
}
}
cout<<names[i]<<"的总价是:"<<count<<endl;
}
return 1;
}