
为什么这份代码会出现这样的问题?
/**
* This file is for "Code Analysis Dev Team" private testing only.
* DO NOT spread this beta cersion through PUBLIC CHANNELS.
*/
#define Test
#define High_Precision_Calc
#ifdef Test
#define UNKNOWN_CASES -1
#define SYSTEM_ERROR -2
#define APPLICATION_FAULT -3
#endif
#ifdef High_Precision_Calc
#define private public
#define protected public
#endif
#include <iostream>
#include <cstring> // Including for memset().
using namespace std;
class calculation
{
public:
int len;
bool flag; // If n >= 0, flag = true. Or flag = false.
short num[1000005]; // For 0 to 9 only, short is enough.
calculation()
{ // Set at 0.
len = 1, flag = true;
memset(num, 0, sizeof num);
return ;
}
calculation(int x)
{ // Set at x.
len = 0, memset(num, 0, sizeof num);
if(x >= 0) flag = true;
else flag = false, x = -x;
while(x > 0) num[++len] = x % 10, x /= 10;
if(!len) len = 1; // If x = 0, we can't go into the while (line 39).
return ;
}
calculation(string s)
{ // Set at s.
if(s[0] == '-') flag = false, s = s.substr(1);
else flag = true;
len = s.size();
for(int i = 1; i <= len; i++)
num[i] = s[len - i] - '0';
return ;
}
~calculation()
{ /**
* In order not to take any risk, although we don't need, write a destructer is necessary.
* Set at 0.
*/
len = 1, flag = true;
memset(num, 0, sizeof num);
return ;
}
};
// This is the unsigned calculating area.
bool unsigned_smaller(calculation a, calculation b)
{
if(a.len != b.len) return a.len < b.len;
for(int i = a.len; i >= 1; i--)
if(a.num[i] != b.num[i])
return a.num[i] < b.num[i];
return false;
}
bool unsigned_bigger(calculation a, calculation b)
{
if(a.len != b.len) return a.len > b.len;
for(int i = a.len; i >= 1; i--)
if(a.num[i] != b.num[i])
return a.num[i] > b.num[i];
return false;
}
bool unsigned_equal(calculation a, calculation b)
{
if(a.len != b.len) return false;
for(int i = 1; i <= a.len; i++)
if(a.num[i] != b.num[i])
return false;
return true;
}
// This is the signed calculating area.
/**
* Because divide needs plus, minus and multiplicate three types of operators,
* so I put the divide calculating area here.
* This is the divide calculating area.
*/
signed main()
{
calculation a(10), b(20);
cout << unsigned_smaller(a, b) << endl;
return 0;
}
使用软件:Dev-C++
编译器:TDM-GCC 4.9.2 64-bit Release