利用map和stringstream数据流解题

题目描述

喜闻乐见A+B。
读入两个用英文表示的A和B,计算它们的和并输出。

输入

第一行输入一个字符串,表示数字A;第二行输入一个字符串表示数字B。A和B均为正整数。

输出

输出一个正整数n,表示A+B的和(A+B<100)。

样例输入

one five
four
three four
two six

样例输出

19
60

提示

从0到9的对应的英文单词依次为:zero, one , two , three , four , five , six , seven , eight , nine 。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<map>
 5 #include<cstdio>
 6 #include<sstream>
 7 #include<vector>
 8 using namespace std;
 9 int main()
10 {
11     map <string, int> m;
12     m["one"] = 1, m["two"] = 2, m["three"] = 3, m["four"] = 4, m["five"] = 5;
13     m["six"] = 6, m["seven"] = 7, m["eight"] = 8, m["nine"] = 9, m["zero"] = 0;
14     string a,b;
15     while (getline(cin, a), getline(cin, b))
16     {
17         stringstream j(a), k(b);
18         int A =0, B =0;
19         string temp;
20         while (j >> temp) A = A * 10 + m[temp];
21         while (k >> temp) B = B * 10 + m[temp];
22         cout << A + B << endl;
23     }
24     return 0;
25 }
原文地址:https://www.cnblogs.com/kangdong/p/8744688.html