我已经使用标题和类编写了一个程序来练习。 所以该项目有三个文件。 main.cpp BigInteger.h BigInteger.cpp
我打电话给班级" BigInteger"在main.cpp中
当我从main读取字符串时,它显示正确。
但是当我使用+, - ,*运算符时。 它显示了答案,但添加了一个' 0'在答案前面。
以下是我的代码
//main.cpp
#include "BigInteger.h"
int main() {
BigInteger a("-1234567890");
BigInteger b("234567891");
BigInteger ans1, ans2, ans3, ans4;
BigInteger c(a), d;
ans1 = a + b;
ans2 = a - c;
ans3 = c - b;
ans4 = a*b;
cout<<"a="; a.print();
cout<<"b="; b.print();
cout<<"c="; c.print();
cout<<"d="; d.print();
cout<<"a+b="; ans1.print();
cout<<"a-c="; ans2.print();
cout<<"c-b="; ans3.print();
cout<<"a*b="; ans4.print();
}
//header
#include <iostream>
#include <vector>
#include <string>
#define MAX_SIZE 80
using namespace std;
static const int ROWS=4, COLS=4;
class BigInteger{
private:
vector <int> digits;
bool positive; //正負數,0的正負設為正
void adding(const vector <int> &x,const vector <int> &y, vector <int> &z);
void subing(const vector <int> &x ,const vector <int> &y, vector <int> &z);
bool max(const vector <int> & x,const vector <int> & y);
void Reverse( vector <int> & r);
public:
BigInteger(string s);
BigInteger();
BigInteger(const BigInteger & r);
BigInteger & operator =(const BigInteger & r);
BigInteger operator +(const BigInteger & r);
BigInteger operator -(const BigInteger & r);
BigInteger operator *(const BigInteger & r);
void print();
};
//BigInteger.cpp
#include "BigInteger.h"
#include "math.h"
BigInteger::BigInteger(string s)
{
for(int i=0;i<s.size();i++) {
if(s[i]!='-') {
digits.push_back(s[i]-'0');
}
}
if(s[0]=='-') positive=true;
else positive=false;
}
BigInteger::BigInteger()
{
digits.push_back(0);
positive=false;
}
BigInteger::BigInteger(const BigInteger & x){
for(int i=0;i<x.digits.size();i++)
digits.push_back(x.digits[i]);
positive=x.positive;
}
BigInteger & BigInteger::operator =(const BigInteger & x){
for(int i=0;i<x.digits.size();i++)
digits.push_back(x.digits[i]);
positive=x.positive;
return (*this);
}
BigInteger BigInteger::operator +(const BigInteger & x){
BigInteger answer;
answer.digits.clear();
bool pre; //if the front one is bigger return true.
if(positive==x.positive){
answer.positive=positive;
adding(digits,x.digits,answer.digits);
}
else{
pre=max(digits,x.digits);
if(pre) subing(digits,x.digits,answer.digits);
else subing(x.digits,digits,answer.digits);
answer.positive=false;
if(positive&&pre) answer.positive=true;
if((!positive)&&(!pre)) answer.positive=true;
}
return answer;
}
BigInteger BigInteger::operator -(const BigInteger & x){
BigInteger answer;
answer.digits.clear();
bool pre=max(digits,x.digits);
if(positive!=x.positive){
answer.positive=true;
if(pre) adding(digits,x.digits,answer.digits);
else adding(x.digits,digits,answer.digits);
}
else if(positive&&x.positive){
if(pre) {
subing(digits,x.digits,answer.digits);
answer.positive=true;}
else {
subing(x.digits,digits,answer.digits);
answer.positive=false;}
}
else {
if(pre){
answer.positive=false;
subing(digits,x.digits,answer.digits);}
else {
answer.positive=true;
subing(x.digits,digits,answer.digits);
}
}
return answer;
}
BigInteger BigInteger::operator *(const BigInteger & x){
BigInteger answer;
long long int a=0,b=0,ans=0;
int ten=1;
answer.digits.clear();
for(int i=digits.size()-1;i>=0;i--)
{
a+=digits[i]*ten;
ten*=10;
}
ten=1;
for(int i=x.digits.size()-1;i>=0;i--)
{
b+=x.digits[i]*ten;
ten*=10;
}
ans=a*b;
if(positive==x.positive) answer.positive=false;
else answer.positive=true;
while(ans>0)
{
answer.digits.push_back(ans%10);
ans=ans/10;
}
Reverse(answer.digits); //cause i use push_back ,it will be backwards
return answer;
}
void BigInteger::adding(const vector <int> & x,const vector <int> & y, vector <int> & z)
{
long long int a=0,b=0,ans=0;
BigInteger answer;
int ten=1;
answer.digits.clear();
for(int i=x.size()-1;i>=0;i--)
{
a+=x[i]*ten;
ten*=10;
}
ten=1;
for(int i=y.size()-1;i>=0;i--)
{
b+=y[i]*ten;
ten*=10;
}
ans=a+b;
while(ans>0)
{
z.push_back(ans%10);
ans=ans/10;
}
Reverse(z); //cause i use push_back ,it will be backwards
}
void BigInteger::subing(const vector <int> & x,const vector <int> & y, vector <int> & z){
long long int a=0,b=0,ans=0;
BigInteger answer;
int ten=1;
answer.digits.clear();
for(int i=x.size()-1;i>=0;i--)
{
a+=x[i]*ten;
ten*=10;
}
ten=1;
for(int i=y.size()-1;i>=0;i--)
{
b+=y[i]*ten;
ten*=10;
}
ans=a-b;
while(ans>0){
z.push_back(ans%10);
ans/=10;
}
Reverse(z); //cause i use push_back ,it will be backwards
}
bool BigInteger::max(const vector <int> & x,const vector <int> & y){
if(x.size()>y.size())
return true;
else return false;
}
void BigInteger::Reverse(vector <int> & z) {
int temp,j=z.size()-1;
for(int i=0;i<z.size()/2;i++)
{
temp=z[i];
z[i]=z[j];
z[j]=temp;
j-=1;
}
}
void BigInteger::print()
{
if(positive==true)
cout<<"-";
else
cout<<"";
//cout<<digits[0]<<endl;
for(int i=0;i<digits.size();i++){
cout<<digits[i];
}
cout<<endl;
// cout<<digits[6]<<digits[7]<<digits[8]<<digits[9]<<endl;
}
答案 0 :(得分:1)
void BigInteger::print()
{
if(positive==true)
cout<<"-";
else
cout<<"";
//cout<<digits[0]<<endl;
for(int i=0;i<digits.size();i++){
cout<<digits[i];
}
cout<<endl;
// cout<<digits[6]<<digits[7]<<digits[8]<<digits[9]<<endl;
}
你可以这样写
void BigInteger::print()
{
if(positive==true)
cout<<"-";
else
cout<<"";
//cout<<digits[0]<<endl;
bool not_zero_detect=false;
for(int i=0;i<digits.size();i++){
if(digits.size()!=1&& digits[i]==0 && not_zero_detect==false)
{
digits[i]=digits[i];
}
else
{
not_zero_detect=true;
cout<<digits[i];
}
}
cout<<endl;
// cout<<digits[6]<<digits[7]<<digits[8]<<digits[9]<<endl;
}
使用bool像&#34; not_zero_detect&#34;检查数字前面是否为零
PS:感谢您分享您的作业: - )