//file CBigInt006.cpp
//date 02/04/2005
//author aou
#include <iostream>
using namespace std;
const int MAX_DIGITS = 50;
const int BASE = 10;
class CBigInt
{
private:
int _base;
char _sign;
char _digits[MAX_DIGITS];
//...
public:
CBigInt(void);//constructor/auto-initializer
void display(void);
void initialize(void);
CBigInt(char s[]); //CBigInt bi("+12345");
void input(void);
bool isValid(char s[]);
void retrieveValue(char s[]);
//add(a, b, c);
//add(a, b);
//add(b);
//CBigInt abs(void);
//CBigInt pow(int expo);
//...
};
void main(void)
{
CBigInt b;
cout << "b = "; b.display();
CBigInt a("667627001667627001667627001");
cout << "a = "; a.display();
CBigInt c("+667627001667627001667627001");
cout << "c = "; c.display();
CBigInt d("-667627001667627001667627001");
cout << "d = "; d.display();
CBigInt e("667-627001667627001667627001");
cout << "e = "; e.display();
a.input();
cout << "a = "; a.display();
//cin >> a; a.input();
//cin >> b;
//cin >> c;
//c = a + b; add(a, b, c);
//cout << c; c.display();
}
void CBigInt::retrieveValue(char s[])
{
/* get big integer from s
assumes that s has a valid number
Algorithm0:
if there is a sign then
get the sign and the remaining digits
else
assume the sign as +ve
get the all the digits
end if
Algorithm1:
if the first digit is + or - then
get the sign from the first digit
get the remaining digits
else
set the sign as +ve
get the all the digits
end if
Algorithm2:
if s[0] is + or - then
sign = s[0]
//get the remaining digits
else
sign = '+'
//get the all the digits
end if
Algorithm3:
if s[0] is + or - then
sign = s[0]
//get the remaining digits
j = MAX_DIGITS-1
for i = length(s)-1 to 1
digits[j] = s[i]
j--
end for
else
sign = '+'
//get the all the digits
j = MAX_DIGITS-1
for i = length(s)-1 to 0
digits[j] = s[i]
j--
end for
end if
*/
if (s[0] == '+' || s[0] == '-')
{
_sign = s[0];
//get the remaining digits
int j = MAX_DIGITS-1;
for (int i = strlen(s)-1; i>=1; i--)
{
_digits[j] = s[i];
j--;
}
}
else
{
_sign = '+';
//get the all the digits
int j = MAX_DIGITS-1;
for (int i = strlen(s)-1; i>=0; i--)
{
_digits[j] = s[i];
j--;
}
}
}
bool CBigInt::isValid(char s[])
{
/*
check if s has a valid integer
possibilities:
first character is +, -, or a digit 0 to 9
rest of the characters should be 0 to 9
Algorithm0:
if s[0] is not a digit or + or - then
return false
else
if rest of the digits are not 0 to 9 then
return false
else
return true
Algorithm1:
if (s[0] is not a digit) and (s[0] <> '+') and (s[0]<>'-') then
return false
else
if rest of the digits are not 0 to 9 then
return false
else
return true
Algorithm2:
if ((s[0]<'0' or s[0]>'9') and (s[0] <> '+') and (s[0]<>'- ') then
return false
else
for i=1 to strlen(s)-1
if s[i] is not 0 to 9 then
return false
end for
return true
*/
if ((s[0]<'0' || s[0]>'9') && (s[0] != '+') && (s[0] != '-'))
return false;
else
{
for (int i=1; i<=strlen(s)-1; i++)
if (s[i]<'0' || s[i]>'9')
return false;
return true;
}
}
void CBigInt::input(void)
{
char s[MAX_DIGITS+2];
cin >> s;
initialize();
if (!isValid(s))
return;
/*
if (s[0] == '-')
_sign = '-';
else
_sign = '+';
*/
_sign = '+';
/*
j = MAX_DIGITS-1
for i = length(s)-1 to 0
digits[j] = s[i]
j--
end for
*/
int j = MAX_DIGITS-1;
for (int i = strlen(s)-1; i>=0; i--)
{
_digits[j] = s[i];
j--;
}
}
CBigInt::CBigInt(char s[])
{
initialize();
if (!isValid(s))
return;
retrieveValue(s);
}
void CBigInt::display(void)
{
cout << _sign;
for (int i=0; i<=MAX_DIGITS-1; i++)
cout << _digits[i];
cout << '[' << _base << "]\n";
}
void CBigInt::initialize(void)
{
_base = BASE;
_sign = '+';
for (int i=0; i<=MAX_DIGITS-1; i++)
_digits[i] = '0';
}
CBigInt::CBigInt(void)
{
initialize();
//_base = BASE;
//_sign = '+';
//for (int i=0; i<=MAX_DIGITS-1; i++)
// _digits[i] = '0';
}
/*
Output:
b = +00000000000000000000000000000000000000000000000000[10]
a = +00000000000000000000000667627001667627001667627001[10]
c = +00000000000000000000000667627001667627001667627001[10]
d = -00000000000000000000000667627001667627001667627001[10]
e = +00000000000000000000000000000000000000000000000000[10]
+123
a = +0000000000000000000000000000000000000000000000+123[10]
Press any key to continue
*/
|