zoukankan      html  css  js  c++  java
  • UVa 465

      题目大意:给一个表达式,判断两个运算数和结果是否超过int类型的最大值。

      可以用double类型进行计算。

     1 #include <cstdio>
     2 #include <climits>
     3 
     4 int main()
     5 {
     6 #ifdef LOCAL
     7     freopen("in", "r", stdin);
     8 #endif
     9     char str1[300], str2[300], op;
    10     double a, b;
    11     while (scanf("%s %c %s", str1, &op, str2) != EOF)
    12     {
    13         printf("%s %c %s
    ", str1, op, str2);
    14         double a, b, c;
    15         sscanf(str1, "%lf", &a);
    16         sscanf(str2, "%lf", &b);
    17         if (op == '+')  c = a + b;
    18         else if (op == '*')  c = a * b;
    19         if (a > INT_MAX)  printf("first number too big
    ");
    20         if (b > INT_MAX)  printf("second number too big
    ");
    21         if (c > INT_MAX)  printf("result too big
    ");
    22     }
    23     return 0;
    24 }
    View Code 1

      开始是用bignum类做的,可是一直WA,也不知道怎么回事,先贴上代码,以后在研究吧。

      1 #include <cstdio>
      2 #include <climits>
      3 #include <iostream>
      4 #include <cstring>
      5 #include <algorithm>
      6 using namespace std;
      7 
      8 const int maxn = 300;
      9 
     10 struct bign
     11 {
     12     int len, s[maxn];
     13 
     14     bign()
     15     {
     16         memset(s, 0, sizeof(s));
     17         len = 1;
     18     }
     19 
     20     bign(int num)
     21     {
     22         *this = num;
     23     }
     24 
     25     bign(const char * num)
     26     {
     27         *this = num;
     28     }
     29 
     30     bign operator = (int num)
     31     {
     32         char s[maxn];
     33         sprintf(s, "%d", num);
     34         *this = s;
     35         return *this;
     36     }
     37 
     38     bign operator = (const char * num)
     39     {
     40         len = strlen(num);
     41         for(int i = 0; i < len; i++)
     42             s[i] = num[len-1-i] - '0';
     43         return *this;
     44     }
     45 
     46     bign operator + (const bign& b) const 
     47     {
     48         bign c;
     49         c.len = 0;
     50         int g = 0;
     51         for(int i = 0; g || i < max(len, b.len); i++)
     52         {
     53             int x = g;
     54             if(i < len)   x += s[i];
     55             if(i < b.len)   x += b.s[i];
     56             c.s[c.len++] = x % 10;
     57             g = x / 10;
     58         }
     59         return c; 
     60     }
     61 
     62     void clean()
     63     {
     64         while(len > 1 && !s[len-1])   len--;
     65     }
     66 
     67     bign operator - (const bign& b)
     68     {
     69         bign c;
     70         c.len = 0;
     71         int g = 0;
     72         for(int i = 0; i < len; i++)
     73         {
     74             int x = s[i] - g;
     75             if(i < b.len)   x -= b.s[i];
     76             if(x >= 0)   g = 0;
     77             else 
     78             {
     79                 g = 1;
     80                 x += 10;
     81             }
     82             c.s[c.len++] = x;
     83         }
     84         c.clean();
     85         return c;
     86     }
     87 
     88     bign operator * (const bign& b)
     89     {
     90         bign c;
     91         c.len = len + b.len;
     92         for(int i = 0; i < len; i++)
     93             for(int j = 0; j < b.len; j++)
     94                 c.s[i+j] += s[i] * b.s[j];
     95         for(int i = 0; i < c.len-1; i++)
     96         {
     97             c.s[i+1] += c.s[i] / 10;
     98             c.s[i] %= 10;
     99         }
    100         c.clean();
    101         return c;
    102     }
    103 
    104     bign operator += (const bign& b)
    105     {
    106         *this = *this + b;
    107         return *this;
    108     }
    109 
    110     bool operator < (const bign& b) const
    111     {
    112         if(len != b.len)   return len < b.len;
    113         for(int i = len-1; i >= 0; i--)
    114             if(s[i] != b.s[i])   return s[i] < b.s[i];
    115         return false;
    116     }
    117 
    118     bool operator > (const bign& b) const
    119     {
    120         return b < *this;
    121     }
    122 
    123     bool operator <= (const bign& b) const 
    124     {
    125         return !(b < *this);
    126     }
    127 
    128     bool operator >= (const bign& b) const
    129     {
    130         return !(*this < b);
    131     }
    132 
    133     bool operator == (const bign& b) const 
    134     {
    135         return !(b < *this) && !(*this < b);
    136     }
    137 
    138     bool operator != (const bign& b) const
    139     {
    140         return (b < *this) || (b > *this);
    141     }
    142 
    143     string str() const
    144     {
    145         string res = "";
    146         for(int i = 0; i < len; i++)
    147             res = (char)(s[i] + '0') + res;
    148         if(res == "") res = "0";
    149         return res;
    150     }
    151 };
    152 
    153 istream& operator >> (istream& in, bign& x)
    154 {
    155     string s;
    156     in >> s;
    157     x = s.c_str();
    158     return in;
    159 }
    160 
    161 ostream& operator << (ostream& out, const bign& x)
    162 {
    163     out << x.str();
    164     return out;
    165 }
    166 
    167 int main()
    168 {
    169 #ifdef LOCAL
    170     freopen("in", "r", stdin);
    171 #endif
    172     char op;
    173     bign a, b, c;
    174     while (cin >> a >> op >> b)
    175     {
    176         if (op == '+')  c = a + b;
    177         else if (op == '*')  c = a * b;
    178         cout << a << " " << op << " " << b << endl;
    179         if (a > INT_MAX)  printf("first number too big
    ");
    180         if (b > INT_MAX)  printf("second number too big
    ");
    181         if (c > INT_MAX)  printf("result too big
    ");
    182     }
    183     return 0;
    184 }
    185     
    186     
    View Code 2
  • 相关阅读:
    单源最短路——SPFA算法(Bellman-Ford算法队列优化)
    A Compatible Pair
    欧拉函数知识点总结及欧拉函数打表代码(数论)
    Fafa and the Gates(模拟)
    C语言--链表基础模板
    路由器如何设置上网(TP-LINK)
    学员成绩管理系统
    Bad Cowtractors(最大生成树)
    Agri-Net(最小生成树)
    Median Weight Bead(最短路—floyed传递闭包)
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3281152.html
Copyright © 2011-2022 走看看