View Code of Problem 16



#include "stdlib.h"
#include "algorithm"
#include "iostream"
#include <cstdio>
#include "string"
#include <iomanip>
#include <map>
#include <set>
#include "math.h"
using namespace std;
map<char, int> mp;
set<string> st;
string a[10] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
int num(string tmp)
{
	int sum = 0;
	int i = 0;
	if ('a' <= tmp[0] && tmp[0] <= 'z')
	{
		for (int i = 0; i < 10; i++)
		{
			if (tmp == a[i])
			{
				sum = i;
				break;
			}
		}
	}
	else sum = atoi(tmp.c_str());
	return sum;
}
int main()
{
	int t, out; cin >> t; string str, str1, tmp;
	while (t--)
	{
		cin >> out;//判断输出的中英文
		getchar();
		getline(cin, str);
		int i = 0;
		while (str[i] != '+')i++;
		str1 = str.substr(0, i);
		str.erase(0, i + 2);
		str.erase(str.length()-1,1);
		i = 0;
		int m = 0;
		while (str1 != "")
		{
			i = 0;
			int k = 0;
			while (str1[i] != ' ')i++;
			tmp = str1.substr(0, i);
			str1.erase(0, i + 1);
			k = num(tmp);
			m = m * 10 + k;
			i++;
		}
		int n = 0;
		while (str != "")
		{
			i = 0;
			int k = 0;
			while (str[i] != ' ')i++;
			tmp = str.substr(0, i);
			str.erase(0, i + 1);
			k = num(tmp);
			n = n * 10 + k;
			i++;
		}
		int q = m + n; int flag = 0;
		if(out==0)cout << q << endl;
		else
		{
			i = 0; int x[100];
			while (q != 0)
			{
				x[i] = q % 10;
				q = q / 10;
				i++;
			}
			i--;
			for (; i >= 0; i--)
			{
				if (flag == 1)cout << " ";
				flag = 1;
				cout << a[x[i]];
			}
			cout << endl;
		}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 16