资源简介
大整数四则运算 BigInt 加减乘除 括号 优先级,Python支持无限精度的运行,Java有BigInteger类,但是对C和C++就只能自己实现,这是一个带加减乘除运算的实现。
代码片段和文件信息
#include
#include
using namespace std;
class BigFigure{
string num1num2;
string outcome;
int precision;
char operation;
public:
BigFigure(){
num1=num2=“0“;
outcome=“0“;
precision=5;
}
string& plus(string & string &);
string& subtration(string & string &);
string& multiplication(string & string &);
string& division(string & string &);
void show();
void BigFigureInterface();
string &revese(string&);
friend istream& operator>>(istream& i BigFigure& a){
return i>>a.num1>>a.operation>>a.num2;
}
~BigFigure(){ }
};
void BigFigure::show(){
cout<<“Result: “< }
void BigFigure::BigFigureInterface(){
BigFigure a;
cout<<“*********************************************************/n“;
cout<<“ Welcome... /n“;
cout<<“ Four Arithmetic Operations of Big Figures/n“;
cout<<“*********************************************************/n/n“;
cout<<“Notes:/n“;
cout<<“ 1. 输入形式为:A[空格或换行]O[空格或换行]B。/n“;
cout<<“ 2. 1中A、B为大数,O为运算符(如输入:123456789 / 432432)。/n“;
cout<<“ 3. 既然处理大数,就没必要输入小数点位了。/n“;
cout<<“ 4. 加减不能处理负号,乘除可以。/n“;
cout<<“ 5. 用于学习交流,若发现错误可联系519916178@qq.com。/n/n“;
cout<<“Now Start Input 0 0 0 to end if you want to quit!/n/n“;
cout<<“[BigFigure #] “;
cin>>a;
while(a.operation!=‘0‘){
switch(a.operation){
case ‘+‘: a.plus(a.num1 a.num2);
a.show(); break;
case ‘-‘: a.subtration(a.num1 a.num2);
a.show(); break;
case ‘*‘: a.multiplication(a.num1 a.num2);
a.show(); break;
case ‘/‘: a.division(a.num1 a.num2);
a.show(); break;
default:cout< }
cout<<“[BigFigure #] “;
cin>>a;
}
// system(“cls“);
cout<<“/n/n/n/n/n/n/t/t Quited.../n/n/n/n/n/n/n“;
//system(“pause“);
}
string& BigFigure::revese(string& s){
char c;
int t=s.size();
for(int i=0; i c=s[i];
s[i]=s[t-i-1];
s[t-i-1]=c;
}
return s;
}
string& BigFigure::plus(string &str1 string &str2){//加法运算,未处理符号
int min=0it=0;
string temp;
outcome.clear();
str1=revese(str1);
str2=revese(str2);
min=str1.size() for(i=0; i temp+=(str1[i]+str2[i]-96+t)%10+48;
t=(st
评论
共有 条评论