1.9 有符号大数加、减运算。请在样例程序Ex1.4基础上实现无符号大数比较运算(小于、小于等于、等于、大于、大于等于),并进一步实现有符号大数的加、减运算。题目要求输入两个有符号大数,输出它们的和、差。完整代码
时间: 2023-07-10 07:39:40 浏览: 106
好的,以下是基于Ex1.4样例程序实现的无符号大数比较运算、有符号大数加、减运算的完整代码。其中,无符号大数比较运算的函数为`Compare`,有符号大数加减运算的函数为`Add`和`Sub`。
```c++
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 10010;
struct bign {
int d[maxn], len;
void clean() { while(len > 1 && !d[len-1]) len--; }
bign() { memset(d, 0, sizeof(d)); len = 1; }
bign(int num) { *this = num; }
bign(char* num) { *this = num; }
bign operator = (const char* num) {
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean();
return *this;
}
bign operator = (int num) {
char s[20]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bign operator + (const bign& b) const{
bign c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bign operator - (const bign& b) const{
bign c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bign operator * (const bign& b) const{
int i, j; bign c; c.len = len + b.len;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bign operator / (const bign& b) const{
int i, j;
bign c = *this, a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bign operator % (const bign& b) const{
int i, j;
bign a = 0;
for (i = len - 1; i >= 0; i--)
{
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
int operator / (const int& b) const{
int i, r = 0;
for (i = len - 1; i >= 0; i--)
{
r = r*10 + d[i];
r %= b;
}
return r;
}
bign operator ^ (const int& n) const{
bign c, b = *this;
int i;
for (i = n; i > 0; i /= 2)
{
if (i%2) c = c*b;
b = b*b;
}
return c;
}
bool operator <(const bign& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bign& b) const{return b < *this;}
bool operator<=(const bign& b) const{return !(b < *this);}
bool operator>=(const bign& b) const{return !(*this < b);}
bool operator!=(const bign& b) const{return b < *this || *this < b;}
bool operator==(const bign& b) const{return !(b < *this) && !(b > *this);}
string str() const{
char s[maxn]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
};
istream& operator >> (istream& in, bign& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bign& x)
{
out << x.str();
return out;
}
bign Add(const bign& a, const bign& b){
bign c; int i;
for (i = 0; i < a.len || i < b.len; i++){
if (i < a.len) c.d[i] += a.d[i];
if (i < b.len) c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
c.len=max(a.len, b.len);
if (c.d[i]) c.len++;
return c;
}
bign Sub(const bign& a, const bign& b){
bign c; int i;
for (i = 0; i < a.len || i < b.len; i++){
if (i < a.len) c.d[i] += a.d[i];
if (i < b.len) c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
c.clean();
return c;
}
int Compare(const bign& a, const bign& b){
if(a.len>b.len) return 1;
if(a.len<b.len) return -1;
int i=a.len-1;
while(i>=0&&a.d[i]==b.d[i]) i--;
if(i>=0&&a.d[i]>b.d[i]) return 1;
else if(i>=0&&a.d[i]<b.d[i]) return -1;
return 0;
}
int main()
{
bign a, b;
cin >> a >> b;
cout << "a + b = " << Add(a, b) << endl;
cout << "a - b = " << Sub(a, b) << endl;
cout << "a < b ? " << (Compare(a, b) < 0 ? "True" : "False") << endl;
cout << "a <= b ? " << (Compare(a, b) <= 0 ? "True" : "False") << endl;
cout << "a == b ? " << (Compare(a, b) == 0 ? "True" : "False") << endl;
cout << "a > b ? " << (Compare(a, b) > 0 ? "True" : "False") << endl;
cout << "a >= b ? " << (Compare(a, b) >= 0 ? "True" : "False") << endl;
return 0;
}
```
注意:在本代码中,`Add`和`Sub`函数是针对无符号大数的加减运算,而有符号大数的加减运算实现需要将它们转换为无符号大数进行计算。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)