码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 465-- Overflow (atof 函数)

时间:2014-08-09 21:35:29      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   

 Overflow 

Write a program that reads an expression consisting of two non-negative integer and an operator. Determine if either integer or the result of the expression is too large to be represented as a ``normal‘‘ signed integer (type integer if you are working Pascal, type int if you are working in C).

Input

An unspecified number of lines. Each line will contain an integer, one of the two operators + or *, and another integer.

Output

For each line of input, print the input followed by 0-3 lines containing as many of these three messages as are appropriate: ``first number too big‘‘, ``second number too big‘‘, ``result too big‘‘.

Sample Input

300 + 3
9999999999999999999999 + 11

Sample Output

300 + 3
9999999999999999999999 + 11
first number too big
result too big

本来是道模拟题,wa了7次没过去 无奈直接上atof函数了 ,此函数将输入的字符串转化为浮点型(double)

#include<cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
const int MAX=2147483647;
int main()
{
	char op,a[1100],b[1100];
	while(cin>>a>>op>>b)
	{
		cout<<a<<" "<<op<<" "<<b<<endl;
		double x=atof(a),y=atof(b);
		if(x>MAX)
			cout<<"first number too big"<<endl;
		if(y>MAX)
			cout<<"second number too big"<<endl;
		if(op=='+')
		{
			double ans=x+y;
			if(ans>MAX)
				cout<<"result too big"<<endl;
		}
		if(op=='*')
		{
			double ans=x*y;
			if(ans>MAX)
				cout<<"result too big"<<endl;
		}

	}
	return 0;
}


UVA 465-- Overflow (atof 函数),布布扣,bubuko.com

UVA 465-- Overflow (atof 函数)

标签:des   style   http   color   os   io   strong   for   

原文地址:http://blog.csdn.net/qq_16255321/article/details/38459249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!