4
: .
: , >> , << , int double, (==, >, >=, <, <=,!=), . , , . , . . , .
.
. , . +, ++, +=.
#include <iostream>
using namespace std;
class Number
{
int *digits; //
int count; //
void setDigits(char *str)
{
// -
if (this->digits!= NULL)
delete[] digits;
int len = strlen(str);
int pos = 0;
// ( )
while (pos < len && str[pos] == '0')
pos++;
if (pos >= len)
{
// ,
//
this->count = 1;
this->digits = new int[1];
this->digits[0] = 0;
return;
}
this->count = len - pos;
this->digits = new int[this->count];
for (int i=0; i<this->count; i++)
this->digits[i] = (int)str[i + pos] - (int)'0';
return;
}
int SubNumber(Number n)
{
// , n
// 0 -
// 1 -
// -1 -
// -
//
if (this->count > n.count)
return 1;
if (this->count < n.count)
return -1;
// ,
// "".
// ,
//
//
//
int perenos = 0;
int result = 0;
int resr;
for (int i = this->count - 1; i >= 0; i--)
{
resr = this->digits[i] - n.digits[i] - perenos;
if (resr < 0)
{
result |= resr + 3;
perenos = 1;
}
else
{
result |= resr;
perenos = 0;
}
}
// ,
|
|
if (perenos == 0 && result == 0)
return 0;
// T n,
//
if (perenos == 0 && result!= 0)
return 1;
// n
return -1;
}
char* AddNumber(Number n)
{
// , n
// ( )
int MaxLength = (this->count > n.count)? this->count: n.count;
//
// 1
// 1
char *str = new char[MaxLength + 2];
//
for (int i=0; i<=MaxLength; i++)
str[i] = '0';
//
str[MaxLength+1] = 0;
int perenos = 0;
int resr, cha, chb;
for (int i=0; i<MaxLength; i++)
{
cha = (i < this->count)? this->digits[this->count - i - 1]: 0;
chb = (i < n.count)? n.digits[n.count - i - 1]: 0;
resr = cha + chb + perenos;
perenos = resr / 3;
str[MaxLength - i] = (char)((resr % 3) + '0');
}
if (perenos!= 0)
str[0] = (char)(perenos + '0');
return str;
}
public:
Number()
{
this->digits = NULL;
this->count = 0;
}
Number(char *str)
{
this->digits = NULL;
this->count = 0;
this->setDigits(str);
}
//
friend istream & operator>>(istream &input, Number &num);
//
friend ostream & operator<<(ostream &output, Number &num);
operator int()
{
if (this->digits == NULL)
return 0;
int result = 0;
for (int i=0; i<this->count; i++)
{
result *= 3;
result += this->digits[i];
}
return result;
}
operator double()
{
if (this->digits == NULL)
return 0.0;
double result = 0.0;
for (int i=0; i<this->count; i++)
{
result *= 3.0;
result += this->digits[i];
}
return result;
}
bool operator == (Number &n)
{
int res = this->SubNumber(n);
return (res == 0)? true: false;
}
bool operator!= (Number &n)
{
int res = this->SubNumber(n);
return (res!= 0)? true: false;
}
bool operator > (Number &n)
{
int res = this->SubNumber(n);
return (res == 1)? true: false;
}
bool operator >= (Number &n)
{
int res = this->SubNumber(n);
return (res == 0 || res == 1)? true: false;
}
bool operator <= (Number &n)
{
int res = this->SubNumber(n);
return (res == 0 || res == -1)? true: false;
}
bool operator < (Number &n)
{
int res = this->SubNumber(n);
return (res == -1)? true: false;
}
Number operator + (Number &n)
{
return Number(this->AddNumber(n));
}
void operator += (Number &n)
{
this->setDigits(this->AddNumber(n));
}
Number operator ++(int)
{
this->setDigits(this->AddNumber(Number("1")));
return *this;
}
};
istream & operator>>(istream &input, Number &num)
|
|
{
const int dsize = 10;
int size = 0;
int msize = dsize;
char *str = (char*)malloc(msize + 1);
char c;
/* */
do {
//
input.get(c);
//
if (c == '\n' || c == '\0') break;
//
str[size] = c;
size++;
//
// -
if (size >= msize)
{
msize+=dsize;
str = (char*)realloc(str, msize + 1);
}
} while (1);
str[size]=0;
/* */
for (int i=0; i<size; i++)
{
if (str[i]< '0' || str[i] > '2')
{
cout << "Incorrect input" << endl;
free(str);
return input;
}
}
/* Number */
num.setDigits(str);
free(str);
return input;
}
ostream & operator<<(ostream &output, Number &num)
{
if (num.digits!= NULL)
{
for (int i=0; i<num.count; i++)
output << num.digits[i];
output << endl;
}
return output;
}
int main()
{
Number a,b,c;
int action;
while (1)
{
//
system("cls");
cout<<"Main menu (0 - exit)" <<endl<<
"1. Input A"<<endl<<"2. Input B"<<endl<<"3. Output A"<<endl<<
"4. Output B"<<endl<<"5. Output C"<<endl<<endl<<"6. A==B"<<endl<<
"7. A!=B"<<endl<<"8. A>B"<<endl<<"9. A<B"<<endl<<
"10. A>=B"<<endl<<"11. A<=B"<<endl<<"12. A++"<<endl<<
"13. B++"<<endl<<"14. A+=B"<<endl<<"15. C=A+B"<<endl;
//
cin>>action; cin.clear(); cin.sync();
switch (action)
{
case 0: return 0;
case 1: cout<<"Input A, please: "; cin>>a; break;
case 2: cout<<"Input B, please: "; cin>>b; break;
case 3: cout<<"Number A: " << a; cin.get(); break;
case 4: cout<<"Number B: " << b; cin.get(); break;
case 5: cout<<"Number C: " << c; cin.get(); break;
case 6: cout<<"A==B: " << ((a==b)? "Yes": "No"); cin.get(); break;
case 7: cout<<"A!=B: " << ((a!=b)? "Yes": "No"); cin.get(); break;
case 8: cout<<"A>B: " << ((a>b)? "Yes": "No"); cin.get(); break;
case 9: cout<<"A<B: " << ((a<b)? "Yes": "No"); cin.get(); break;
case 10: cout<<"A>=B: " << ((a>=b)? "Yes": "No"); cin.get(); break;
case 11: cout<<"A<=B: " << ((a<=b)? "Yes": "No"); cin.get(); break;
case 12: cout<<"A++: "; a++; cout<<a; cin.get(); break;
case 13: cout<<"B++: "; b++; cout<<b; cin.get(); break;
case 14: cout<<"A+=B: "; a+=b; cout<<a; cin.get(); break;
case 15: cout<<"C=A+B: "; c=a+b; cout<<c; cin.get(); break;
}
}
return 0;
}
1. .
2. .
3. .
4. .
.
1. , . +, ++, +=.
2. , . +, ++, +=.
|
|
3. , . -, --, -=.
4. , . -, --, -=.
5. , . *, *=.
6. , . *, *=.
7. , . +, ++, +=.
8. , . +, ++, +=.
9. , . -, --, -=.
10. , . -, --, -=.
11. , . *, *=.
12. , . *, *=.
13. , . +, ++, +=.
14. , . +, ++, +=.
15. , . -, --, -=.
16. , . -, --, -=.
17. , . *, *=.
18. , . *, *=.
19. , . +, ++, +=.
20. , . +, ++, +=.
21. , . -, --, -=.
22. , . -, --, -=.
23. , . *, *=.
24. , . *, *=.
25. , . +, ++, +=.
26. , . +, ++, +=.
27. , . -, --, -=.
28. , . -, --, -=.
29. , . *, *=.
30. , . *, *=.