http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=406
题意:
输入一道式子,输出数字或结果是否太大。
解题:
直接转换成double再对比,简单问题别复杂化。。。
另外,输入a, b, c,三个字符串,中间可能是空格、可能是多个空格。别简单输出"a b c"。
直接输出,别自己组织原数据输出。避免乱改原数据/字符串。
#include <iostream>
#include <sstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
// #define LOCAL_TEST
#define MAX 2147483647
int main()
{
#ifdef LOCAL_TEST
freopen("f:\\in.txt", "r", stdin);
freopen("f:\\out.txt", "w+", stdout);
#endif
double a, b;
char numA[1000], numB[1000];
char op[2];
memset(numA, 0, sizeof(numA));
memset(numB, 0, sizeof(numB));
string str;
while ( getline(cin, str) )
{
cout <<str <<"\n";
istringstream strStream(str);
strStream >>numA >>op >>numB;
a = atof(numA);
b = atof(numB);
if ( a > MAX )
{
cout <<"first number too big" <<"\n";
} // end if
if ( b > MAX )
{
cout <<"second number too big" <<"\n";
} // end if
if ( op[0] == '+' )
{
if ( a+b > MAX )
{
cout <<"result too big" <<"\n";
} // end if
} // end if
else
{
if ( a*b > MAX )
{
cout <<"result too big" <<"\n";
} // end if
} // end else
memset(numA, 0, sizeof(numA));
memset(numB, 0, sizeof(numB));
} // end while
return 0;
}