VMTEST - Thử máy

Tác giả: flashmt

Ngôn ngữ: C++

#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;

int checkType(string s)
{
	int res = 0;
	for (int i = 0; i < s.size(); i++)
		if (s[i] >= '0' && s[i] <= '9') res |= 1;
		else
			if (tolower(s[i]) >= 'a' && tolower(s[i]) <= 'z') res |= 2;
	return res;
}

double getSum(string s)
{
	double res = 0, x = 0, power = 1;
	s += ' ';
	for (int i = 0, dotted = 0, sign = 1; i < s.size(); i++)
		if (s[i] == ' ') res += x/power*sign, power = 1, x = 0, dotted = 0, sign = 1;
		else 
			if (s[i] == '-') sign = -1;
			else
				if (s[i] == '.') dotted = 1;
				else x = x * 10 + s[i] - '0', power *= (dotted > 0? 10: 1);
	return res;
}

string concatStr(string s)
{
	string res = "";
	for (int i = 0; i < s.size(); i++) 
		if (s[i] != ' ') res += s[i];
	return res;
}

int main()
{
	string s;
	while (getline(cin,s))
	{
		if (s=="?") break;
		
		int type = checkType(s);
		
		if (type == 3) puts("Error!");
		else
			if (type == 1) printf("%.6lf\n",getSum(s));
			else cout << concatStr(s) << endl;
	}
}

Download