Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C++;计算大整数_C++_Vector - Fatal编程技术网

C++ 在C++;计算大整数

C++ 在C++;计算大整数,c++,vector,C++,Vector,我编写了一个程序来练习使用header和class。 所以这个项目有三个文件。 main.cpp biginger.h biginger.cpp 我在main.cpp中将类称为“biginger” 当我从main读取字符串时,它显示正确 但当我使用+,-,*运算符时。 它显示了答案,但在答案前添加了一个“0” 下面是我的代码 //main.cpp #include "BigInteger.h" int main() { BigInteger a("-1234567890"); B

我编写了一个程序来练习使用header和class。 所以这个项目有三个文件。 main.cpp biginger.h biginger.cpp

我在main.cpp中将类称为“biginger”

当我从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;
}
//main.cpp
#包括“BigInteger.h”
int main(){
大整数a(“-1234567890”);
大整数b(“234567891”);
大整数ans1、ans2、ans3、ans4;
大整数c(a),d;
ans1=a+b;
ans2=a-c;
ans3=c-b;
ans4=a*b;
cout
void biginger::print()
{
如果(正==真)

couty如果您想知道
0
是从哪里来的,那么您的赋值运算符在push_-back循环之前从不清除原始内容。而且该循环(实际上是整个运算符)无论如何都是无用的;默认赋值运算符就足够了。如果
s[0]=='-'
那么如果将
正数设置为
真值
似乎有点奇怪。是否应该设置为
假值
?在错误的情况下显示代码就足够了,不需要复制/粘贴整个程序。这样更容易检查。您应该只发布我们需要的代码来帮助您解决问题。
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;
}