《C++程序设计语言》6.6_3 (名字,值)对

来源:互联网 发布:淘宝仓管是做什么的 编辑:程序博客网 时间:2024/06/03 01:11
/*-------------------------------------------------------读入一系列由空白分隔的(名字,值)对,其中每个名字是由空白分隔开的一个单词,值是一个整数或者一个浮点数。计算并打印出对应于每个名字的所有值之和与平均值,以及所有名字的和与平均值。-------------------------------------------------------*/#include <iostream>#include <string>#include <map>using std::cout;using std::cin;using std::endl;using std::string;using std::map;void create(map<string, double>& cmap){string cstr;double cdou;cout << "Please input date(name and double-value):\n";cin >> cstr;while (cstr != "quit"){cin >> cdou;cmap[cstr] = cdou;cin >> cstr;}}void show(map<string, double>& smap){map<string, double>::iterator iter;cout << "\nLet's show date of the map:\n" << "NameValue" << endl;for (iter = smap.begin(); iter != smap.end(); iter++)cout << iter->first << "" << iter->second << endl;cout << endl;}void calculate(map<string, double>& cmap, double& csum, double& caverage){map<string, double>::iterator iter;for (iter = cmap.begin(); iter != cmap.end(); iter++)csum += iter->first.size();caverage = csum/cmap.size();cout << "Name of the map included " << csum << " bytes!\n" << "the average of name is " << caverage << " byte!\n";csum = caverage = 0;for (iter = cmap.begin(); iter != cmap.end(); iter++)csum += iter->second;caverage = csum/cmap.size();cout << "\nthe sum of double-value of the map is " << csum << ".\n" << "the average of sum is " << caverage << ".\n";csum = caverage = 0;}int main(){map<string, double> mmap;double sum = 0, average = 0;create(mmap);show(mmap);calculate(mmap, sum, average);return 0;}



原创粉丝点击