创建HugeInteger(大数)类,用一个具有40个元素的数字数组存放最多40位的整数。提供成员函数input(输)、output(输出)、add(加)和substract(减)。为了比较 HugeInteger 对象,提供函数isEqualTo(是等于)isNotEqualTo(是不等于)、isGreaterThan(是大于)、isLessThan(是小于)、isGreaterThanOrEqualTo(是大于等于)和isLessThanOrEqualTo(是小于等于),这几个函数每个都是“判定”函数,如果两个HugeInteger对象间关系成立,则返回true;如果关系不成立,则返回false。
1.首先我们根据题目要求创建HugeInteger(大整数)类的基本框架;
class HugeInteger
{
private:
int array1[40];
public:
HugeInteger();
void input();
void output();
void add(int s,int n);
void add(const HugeInteger number2);
void substract(int s,int n);
void substract( HugeInteger number2);
bool isGreaterThan(HugeInteger number2);
bool isEqual( HugeInteger test2);
void displayMessage();
};
2.编写构造函数,对数据成员进行初始化。并在这里就调用input()和output()函数;
HugeInteger::HugeInteger()
{
for(int i=0;i<40;i++)
{
array1[i]=0;
}
input();
cout << "The numbers you entered is: " << endl;
output();
cout << endl;
}
3.具体写input()和output()函数;注意越线;
void HugeInteger:: input()//输入函数
{
string number;
cout << "Please enter a number that does not exceed 40 digits" << endl;
cin >> number;
int tab=39;//方便从后面开始存放
for(int i=number.size()-1;i>=0;i--)//size()是计算输入长度
{
int n=number[i]-48;
if(n>=0 && n<=9)
{
array1[tab--]=n;
//判断数位是否超过40位
if(tab<0)
{
cout<<"More than 40 digits !"<<endl;
break;
}
}
else
{
cout << array1[i] << "The number is invalid !"<<endl;
array1[i]=0;
}
}
}
void HugeInteger::output()//输出函数
{
bool flag=0;
for(int i=0;i<40;i++)
{
if (flag||array1[i]!=0)
{
cout << array1[i];
flag=1;
}
}
cout << endl;
}
void HugeInteger::add(int s,int n)
{
if (s>=0 && s<=39)
{
if(n>0 && n<=9)
{
if( array1[s]+n > 9)
{
array1[s] = array1[s] + n - 10; //高位的加1
add(s-1, 1);
}
else
{
array1[s] = array1[s] + n;//不进位
}
}
}
cout << endl;
}
4.编写加法和减法函数;
void HugeInteger::add(const HugeInteger number2)//加法函数,用于对两个大数进行相加
{
for(int i=0;i<=39;i++)
{
add(i,number2.array1[i]);
}
}
void HugeInteger::substract(int s,int n)//减法函数and带有退位运算
{
if (s>=0 && s<=39)
{
if(n>0 && n<=9)
{
if (array1[s] < n)
{
//退位
array1[s] = array1[s] + 10 - n;
substract(s-1, 1);//重调用减法函数,高位减1
}
else
{
array1[s] = array1[s] - n;//不用退位的情况
}
}
}
cout << endl;
}
void HugeInteger::substract( HugeInteger number2)
{
if( isGreaterThan(number2) )
{
for(int i=0;i<=39;i++)
{
substract( i, number2.array1[i]);
}
}
else
{
cout<<"Error,because the subtractor is greater than the minuend !" << endl;
}
}
5.编写“判断”函数;
bool HugeInteger::isGreaterThan( HugeInteger number2)
{
for(int i=0;i<40;i++)
{
if(array1[i]>number2.array1[i])
return true;
if(array1[i]<number2.array1[i])
return false;
}
return false;
}
//判断比较两个大数相等
bool HugeInteger::isEqual( HugeInteger test2)
{
for(int i=0;i<40;i++)
{
if(array1[i]!= test2.array1[i])
return false;
}
return true;
}
6.编写displayMessage()进行显示;
void HugeInteger::displayMessage()//显示
{
{
cout<<"***********"<<endl;
cout<<"* a:加法 *"<<endl;
cout<<"* b:减法 *"<<endl;
cout<<"* c:比较 *"<<endl;
cout<<"* f:退出 *"<<endl;
cout<<"***********"<<endl;
}
/*以此类推,可以编写更多的“判定 ”函数*/
};
7.最后,测试程序;
int main()
{
char conmand;
HugeInteger test1;
test1.displayMessage();
cin >> conmand;
while(conmand!='f')
{
HugeInteger test2;
switch (conmand)
{
case 'a':
test1.add(test2);
cout << "The sum of the two numbers is :" << endl;
test1.output();
cout << endl;
break;
case 'b':
test1.substract(test2);
cout << "The sub of the two numbers is :";
test1.output();
cout << endl;
break;
case 'c':
if(test1.isEqual(test2))
cout << "Two numbers is equal !"<<endl;
else if(test1.isGreaterThan(test2))
cout << "The first number is greater than the second number" << endl;
else
cout << "The first number is less than the second number" << endl;
break;
default:
break;
}
cout << "选择继续操作";
cin >> conmand;
}
return 0;
}
好了,下面就是完整的HugeInteger(大整数)类代码
#include<iostream>
#include<cstring>
using namespace std;
class HugeInteger
{
private:
int array1[40];
public:
HugeInteger();
void input();
void output();
void add(int s,int n);
void add(const HugeInteger number2);
void substract(int s,int n);
void substract( HugeInteger number2);
bool isGreaterThan(HugeInteger number2);
bool isEqual( HugeInteger test2);
void displayMessage();
};
HugeInteger::HugeInteger()
{
for(int i=0;i<40;i++)
{
array1[i]=0;
}
input();
cout << "The numbers you entered is: " << endl;
output();
cout << endl;
}
void HugeInteger:: input()//输入函数
{
string number;
cout << "Please enter a number that does not exceed 40 digits" << endl;
cin >> number;
int tab=39;//方便从后面开始存放
for(int i=number.size()-1;i>=0;i--)//size()是计算输入长度
{
int n=number[i]-48;
if(n>=0 && n<=9)
{
array1[tab--]=n;
//判断数位是否超过40位
if(tab<0)
{
cout<<"More than 40 digits !"<<endl;
break;
}
}
else
{
cout << array1[i] << "The number is invalid !"<<endl;
array1[i]=0;
}
}
}
void HugeInteger::output()//输出函数
{
bool flag=0;
for(int i=0;i<40;i++)
{
if (flag||array1[i]!=0)
{
cout << array1[i];
flag=1;
}
}
cout << endl;
}
void HugeInteger::add(int s,int n)
{
if (s>=0 && s<=39)
{
if(n>0 && n<=9)
{
if( array1[s]+n > 9)
{
array1[s] = array1[s] + n - 10; //高位的加1
add(s-1, 1);
}
else
{
array1[s] = array1[s] + n;//不进位
}
}
}
cout << endl;
}
void HugeInteger::add(const HugeInteger number2)//加法函数,用于对两个大数进行相加
{
for(int i=0;i<=39;i++)
{
add(i,number2.array1[i]);
}
}
void HugeInteger::substract(int s,int n)//减法函数and带有退位运算
{
if (s>=0 && s<=39)
{
if(n>0 && n<=9)
{
if (array1[s] < n)
{
//退位
array1[s] = array1[s] + 10 - n;
substract(s-1, 1);//重调用减法函数,高位减1
}
else
{
array1[s] = array1[s] - n;//不用退位的情况
}
}
}
cout << endl;
}
void HugeInteger::substract( HugeInteger number2)
{
if( isGreaterThan(number2) )
{
for(int i=0;i<=39;i++)
{
substract( i, number2.array1[i]);
}
}
else
{
cout<<"Error,because the subtractor is greater than the minuend !" << endl;
}
}
//判断大小
bool HugeInteger::isGreaterThan( HugeInteger number2)
{
for(int i=0;i<40;i++)
{
if(array1[i]>number2.array1[i])
return true;
if(array1[i]<number2.array1[i])
return false;
}
return false;
}
//判断比较两个大数相等
bool HugeInteger::isEqual( HugeInteger test2)
{
for(int i=0;i<40;i++)
{
if(array1[i]!= test2.array1[i])
return false;
}
return true;
}
void HugeInteger::displayMessage()//显示
{
{
cout<<"***********"<<endl;
cout<<"* a:加法 *"<<endl;
cout<<"* b:减法 *"<<endl;
cout<<"* c:比较 *"<<endl;
cout<<"* f:退出 *"<<endl;
cout<<"***********"<<endl;
}
/*以此类推,可以编写更多的“判定 ”函数*/
};
int main()
{
char conmand;
HugeInteger test1;
test1.displayMessage();
cin >> conmand;
while(conmand!='f')
{
HugeInteger test2;
switch (conmand)
{
case 'a':
test1.add(test2);
cout << "The sum of the two numbers is :" << endl;
test1.output();
cout << endl;
break;
case 'b':
test1.substract(test2);
cout << "The sub of the two numbers is :";
test1.output();
cout << endl;
break;
case 'c':
if(test1.isEqual(test2))
cout << "Two numbers is equal !"<<endl;
else if(test1.isGreaterThan(test2))
cout << "The first number is greater than the second number" << endl;
else
cout << "The first number is less than the second number" << endl;
break;
default:
break;
}
cout << "选择继续操作";
cin >> conmand;
}
return 0;
}