# Overflow, ultraviolet A 465, method 2, error correction

Source: Internet
Author: User

Note:

This questionUseTwo solutions are provided. The first one is referred to a very common solution on the Internet. That is, the atof function is used to convert two numeric strings into two floating-point numbers and then directly compare them with the maximum value of Int. This method is relatively simple, but it also works when the data is small. The second is a more common solution that I write myself. In fact, it is simply a high-precision operation based on strings. If you use a lot of data for testing, there is no error, but the AC cannot be used and you do not know why. Hope you can advise me !!!

Question:

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 (TypeIntegerIf you are working Pascal, TypeIntIf 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 values of these three messages as are appropriate :''First number too big'',''Second number too big'',''Result too big''.

Sample Input
` 300 + 39999999999999999999999 + 11 `
Sample output
` 300 + 39999999999999999999999 + 11 first number too bigresult too big `

Source code: (solution 1)

` # Include <stdio. h> # include <stdlib. h> # define int 2147483647int main () {double A, B; char X , Y ; char operator; while (scanf ("% S % C % s", X, operator, y) = 3) {A = atof (x); B = atof (y ); if (A> INT) printf ("first number too big \ n"); If (B> INT) printf ("second number too big \ n "); if (operator = '+' & A + B> int | Operator = '*' & a * B> INT) printf ("result too big \ n");} return 0 ;} `

(Solution 2)

`# Include <stdio. h> # include <string. h> # define maxn 1000 + 5 char X [maxn], Y [maxn]; char operator; char ans [maxn]; // string that stores the answer, it may not start with Char int [] = "2147483647"; char * P; // point to the location where the answer string starts in ANS int int_cmp (char *); // compare the size of the input string and Int. If the value is greater than 1, 0 void add (); void multip (); int main () {// freopen ("data ", "r", stdin); While (scanf ("% S % C % s", X, & operator, y) = 3) {printf ("% S % C % s \ n", X, operator, Y); If (int_cmp (x) printf ("first numbe R too big \ n "); If (int_cmp (y) printf (" second number too big \ n "); If (operator = '+') add (); else if (operator = '*') multip (); If (int_cmp (p) printf ("result too big \ n");} return 0 ;} int int_cmp (char * s) {int len1 = strlen (INT); int len2 = strlen (s); int I; If (len2> len1) return 1; else if (len1> len2) return 0; else {for (I = 0; I <len1; I ++) if (INT [I] <s [I]) return 1; else if (INT [I]> S [I]) return 0; return 0 ;}} Vo Id add () {// Addition of two strings Int J = MAXN-1; int carry, sum; int len1 = strlen (x), len2 = strlen (y); int I, max; max = len1> len2? Len1: len2; ans [j --] = '\ 0'; carry = 0; for (I = 0; I <Max; I ++) {// from the end of the two strings, the result is put from the end of the ANS sum = carry; If (len1-i> 0) sum + = x [len1-i-1]-'0 '; if (len2-i> 0) sum + = Y [len2-i-1]-'0'; ans [j --] = sum % 10 + '0'; carry = sum/10 ;} while (carry) {// place the remaining carry in sequence ans [j --] = carry % 10 + '0'; carry/= 10 ;} P = ans + J + 1; while (* P = '0') P ++; If (* P = '\ 0') p --; return ;} void multip () {int I, j, left, K, POs, product, carry; int x_len, y_len; ans [MAXN-1] = '\ 0'; K = left = MAXN-1; x_len = strlen (x); y_len = strlen (y); for (I = 0; I <MAXN-1; I ++) ans [I] = '0'; for (I = y_len-1; I> = 0; I --) {k --; // tag the rightmost position of each multiplier Pos = K; carry = 0; For (j = x_len-1; j> = 0; j --) {Product = (Y [I]-'0 ') * (X [J]-'0') + ans [POS]-'0' + carry; ans [POS] = product % 10 + '0'; pos --; carry = product/10;} while (carry) {carry + = ans [POS]-'0'; ans [POS] = carry % 10 + '0'; pos --; carry/= 10;} left = left <= POS + 1? Left: POS + 1;} p = ans + left; while (* P = '0') // if there is 0 in front of the answer string, remove P ++; if (* P = '\ 0') p --; return ;}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.