UVA465 Overflow

高精度运算,涉及加法、乘法和比大小。

一不留神就写了百行,WA的原因是output没有把输入再打印一遍。不过改的时候也考虑了一下前导零,用了clean()函数。可能不考虑也能A,题里面又没说嘛。

题目:

Overflow

 

 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

 

 解答:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<limits.h>
  4 #include<stdlib.h>
  5 #include<ctype.h>
  6 struct bign
  7 {
  8     int len;
  9     int s[500];
 10 };
 11 char str[1010],it[3][50];
 12 bign a,b,res,iMax;
 13 int max(int a,int b)
 14 {
 15     return (a>b)?a:b;
 16 }
 17 void clean(bign *b)
 18 {
 19     while(b->len&&b->s[b->len-1]==0)
 20         b->len--;
 21 }
 22 void change(char *str,bign *b)
 23 {
 24     int i;
 25     b->len=strlen(str);
 26     for(i=0;i<b->len;i++)
 27         b->s[i]=str[b->len-i-1]-'0';
 28     clean(b);
 29 }
 30 void doit()
 31 {
 32     char tmp[500];
 33     sprintf(it[0],"%s","first number too big");
 34     sprintf(it[1],"%s","second number too big");
 35     sprintf(it[2],"%s","result too big");
 36     sprintf(tmp,"%d",INT_MAX);
 37     change(tmp,&iMax);
 38 }
 39 void mkstr()
 40 {
 41     char tmp[500];
 42     int i,p=0;
 43     for(i=0;isdigit(str[i]);i++)
 44         tmp[i]=str[i];
 45     tmp[i]='\0';
 46     change(tmp,&a);
 47     for(i=i+3;isdigit(str[i]);i++)
 48     {
 49         tmp[p]=str[i];
 50         p++;
 51     }
 52     tmp[p]='\0';
 53     change(tmp,&b);
 54 }
 55 void plus(bign *a,bign *b,bign *res)
 56 {
 57     int i,g;
 58     for(i=0,g=0;g!=0||i<max(a->len,b->len);i++)
 59     {
 60         int x=g;
 61         if(i<a->len)
 62             x+=a->s[i];
 63         if(i<b->len)
 64             x+=b->s[i];
 65         res->s[i]=x%10;
 66         g=x/10;
 67     }
 68     res->len=i;
 69     clean(res);
 70 }
 71 void mult(bign *a,bign *b,bign *res)
 72 {
 73     int i,j;
 74     res->len=a->len+b->len;
 75     for(i=0;i<a->len;i++)
 76     {
 77         for(j=0;j<b->len;j++)
 78         {
 79             res->s[i+j]+=a->s[i]*b->s[j];
 80         }
 81     }
 82     for(i=0;i<res->len-1;i++)
 83     {
 84         res->s[i+1]+=res->s[i]/10;
 85         res->s[i]=res->s[i]%10;
 86     }
 87     clean(res);
 88 }
 89 int bigncmp(bign *a,bign *b)
 90 {
 91     if(a->len!=b->len)
 92     {
 93         if(a->len>b->len)
 94             return 1;
 95         else
 96             return -1;
 97     }
 98     int i;
 99     for(i=a->len-1;i>=0;i--)
100     {
101         if(a->s[i]!=b->s[i])
102         {
103             if(a->s[i]>b->s[i])
104                 return 1;
105             else
106                 return -1;
107         }
108     }
109     return 0;
110 }
111 int main()
112 {
113     doit();
114     while(gets(str)!=NULL)
115     {
116         puts(str);
117         int i;
118         memset(res.s,0,sizeof(res.s));
119         mkstr();
120         if(bigncmp(&a,&iMax)>0)
121         {
122             puts(it[0]);
123         }
124         if(bigncmp(&b,&iMax)>0)
125         {
126             puts(it[1]);
127         }
128         if(strstr(str,"*")!=NULL)
129         {
130             mult(&a,&b,&res);
131             if(bigncmp(&res,&iMax)>0)
132                 puts(it[2]);
133         }
134         else
135         {
136             plus(&a,&b,&res);
137             if(bigncmp(&res,&iMax)>0)
138                 puts(it[2]);
139         }
140     }
141     return 0;
142 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值