iT邦幫忙

0

c++的兩個數字比較,不知道該如何修改?

想請教各位一個c++的題目,
題目內容為輸入兩個數字相加相減,會有錯誤偵測,須符合main檔的要求。

目前雖然程式可以執行,但目前卡在
(1) 執行的時候會產生error
(2) 數字若太大會產生亂數

試了很多次仍找不到該如何修改,請各位前輩指教,感激不盡!

以下為程式碼:
(怕影響觀看,有放程式碼跟連結,兩個內容是相同的)
cpp檔:
.h檔:
.main檔: (題目)

.cpp檔:

#include <iostream>
#include <string>
#include <math.h>
#include "BigInt.h"

using namespace std;

//constructor: initialization "string"
BigInt::BigInt(string s)
{
    number=s=("");
}

//constructor: initialization "long long int"
BigInt::BigInt(long long int n)
{
    a=n=0;
}

//copy constructor
BigInt::BigInt(const BigInt &obj)
{
    number=obj.number;
}

//+
BigInt BigInt::operator+(const BigInt& right)
{
    BigInt demo;
    demo.a=this->a+right.a;
    return demo;
}

//-
BigInt BigInt::operator-(const BigInt& right)
{
    BigInt demo;
    demo.a=this->a-right.a;
    //cout << "error" << demo.a+this->a-demo.a <<endl;
    return demo;
}

//>
bool BigInt::operator > (const BigInt &right ) const
{
    if(a>right.a)
        return true;
    else
        return false;
}

//<
bool BigInt::operator < (const BigInt &right) const
{
    if(a<right.a)
        return true;
    else
        return false;
}

//==
bool BigInt::operator == (const BigInt &right) const
{
    //long long int
    if(a==right.a)
        return true;
    else if (a!=right.a)
        return false;

    //string
    if(number==right.number)
        return true;
    else
        return false;
}

//!=
//compare with bigint
bool BigInt::operator!=(const BigInt &right) const
{
    if(number==right.number)
        return false;
    else
        return true;
}

//!=
//compare with int
bool BigInt::operator!=(const long long int &right) const
{
    if(a==right)
        return false;
    else
        return true;
}

//input (string)
istream &operator >> (istream &input, BigInt &right)
{
    input >> right.number;

    //positive and negative
    if(right.number[0]=='-')
        right.sign=1;
    else
        right.sign=0;

    //Remove the preceding 0
    right.s=right.number;
    if(right.sign==1) //negative
    {
        size_t i=1;

        while(right.number[i]=='0')
            i++;
        right.s=right.s.assign(right.number,i,right.number.length()-i);
    }
    else
    {
        size_t i=0;

        while(right.number[i]=='0')
            i++;
        right.s=right.s.assign(right.number,i,right.number.length()-i);
    }

    //string to int
    for(size_t i=0;i<right.s.length();i++)
    {
        long long int n=right.s[i]-'0';
        for(size_t j=right.s.length()-1-i;j>0;j--)
            n=n*10;
        right.a=right.a+n;
    }

    if(right.sign==1)
        right.a=(-1)*right.a;

    return input;
}

//output (long long int)
ostream &operator << (ostream &output,const BigInt &right)
{
    output << right.a;
    return output;
}

.h檔:

#ifndef BIGINT_H
#define BIGINT_H
#include <string>

class BigInt
{
    friend std::ostream &operator << (std::ostream &, const BigInt &);
    friend std::istream &operator >> (std::istream &, BigInt &);

public:
    explicit BigInt(long long int=0);  //initialization "long long int"

    explicit BigInt(std::string );  //initialization "string"

    BigInt(const BigInt & );  // copy constructor


    BigInt operator-(const BigInt& );
    BigInt operator+(const BigInt& );

    bool operator < (const BigInt &) const;
    bool operator > (const BigInt &) const;
    bool operator==(const BigInt &) const;

    bool operator!=(const BigInt &) const;  //compare with bigint
    bool operator!=(const long long int &) const;  //compare with int

private:
    std::string number;
    int sign;  //+ or -
    long long int a=0;
    std::string s;
};
#endif // BIGINT_H

main檔(題目):

#include <iostream>
#include "BigInt.h"

using namespace std;

int main()
{
    BigInt bigInt1, bigInt2, endSign("*");
    cin >> bigInt1;
    while ( !(bigInt1 == endSign) )
    {
        cin >> bigInt2;

        /*** Output the two big integers ***/
        cout << "Big Integer 1: " <<  bigInt1 << endl;
        cout << "Big Integer 2: " <<  bigInt2 << endl;

        /*** Check and output their relation ***/
        if ( bigInt1 > bigInt2 )        cout << "Big Integer 1 is greater than Big Integer 2." << endl;
        else if ( bigInt1 < bigInt2 )   cout << "Big Integer 1 is less than Big Integer 2." << endl;
        else if ( bigInt1 == bigInt2 )  cout << "Big Integer 1 equals to Big Integer 2." << endl;
        else                            cout << "An error occurs during comparisons." << endl;

        /*** Output their sum and difference ***/
        cout << "The sum of them is: "        << bigInt1 + bigInt2 << endl;
        cout << "The difference of them is: " << bigInt1 - bigInt2 << endl;

        /*** Test for correctness ***/
        if ( bigInt1 + bigInt2 - bigInt2 != bigInt1 ) cout << "Error addition and subtraction." << endl;
        if ( bigInt1 + bigInt2 - bigInt1 != bigInt2 ) cout << "Error addition and subtraction." << endl;
        if ( bigInt1 - bigInt2 + bigInt2 != bigInt1 ) cout << "Error addition and subtraction." << endl;
        if ( bigInt2 - bigInt1 + bigInt1 != bigInt2 ) cout << "Error addition and subtraction." << endl;
        if ( bigInt1 - bigInt1 != 0 ) cout << "Error subtraction." << endl;
        if ( bigInt2 - bigInt2 != 0 ) cout << "Error subtraction." << endl;

        cout << endl;
        cin >> bigInt1;
    }

    /*** Test constructor from long long integer ***/
    long long int ending1 = 1234567890, ending2 = -1234567890;
    cout << BigInt(ending1) << " + " << BigInt(ending2) << " = " << BigInt(ending1) + BigInt(ending2) << endl;

    return 0;
}

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2021-06-16 10:00:37

https://ithelp.ithome.com.tw/upload/images/20210616/20001787RBhbkvTsPH.png

只改了 .cpp 檔
試試看可不可以用

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//constructor: initialization "string"
BigInt::BigInt(string s)
{
    // number=s=("");
    std::string::size_type sz = 0;
    number = s;
    a = stoll(s, &sz, 0);
}

//constructor: initialization "long long int"
BigInt::BigInt(long long int n)
{
    // a=n=0;
    a=n;
    number = to_string(a);
}

//copy constructor
BigInt::BigInt(const BigInt &obj)
{
    number=obj.number;
    a = obj.a;
}

//+
BigInt BigInt::operator+(const BigInt& right)
{
    BigInt demo;
    demo.a=this->a+right.a;
    return demo;
}

//-
BigInt BigInt::operator-(const BigInt& right)
{
    BigInt demo;
    demo.a=this->a-right.a;
    //cout << "error" << demo.a+this->a-demo.a <<endl;
    return demo;
}

//>
bool BigInt::operator > (const BigInt &right ) const
{
    if(a>right.a)
        return true;
    else
        return false;
}

//<
bool BigInt::operator < (const BigInt &right) const
{
    if(a<right.a)
        return true;
    else
        return false;
}

//==
bool BigInt::operator == (const BigInt &right) const
{
    //long long int
    if(a==right.a)
        return true;
    else if (a!=right.a)
        return false;

    //string
    if(number==right.number)
        return true;
    else
        return false;
}

//!=
//compare with bigint
bool BigInt::operator!=(const BigInt &right) const
{
    if(a==right.a)
        return false;
    else if (a!=right.a)
        return true;

    if(number==right.number)
        return false;
    else
        return true;
}

//!=
//compare with int
bool BigInt::operator!=(const long long int &right) const
{
    if(a==right)
        return false;
    else
        return true;
}

//input (string)
istream &operator >> (istream &input, BigInt &right)
{
    input >> right.number;

    //positive and negative
    if(right.number[0]=='-')
        right.sign=1;
    else
        right.sign=0;

    //Remove the preceding 0
    right.s=right.number;
    if(right.sign==1) //negative
    {
        size_t i=1;

        while(right.number[i]=='0')
            i++;
        right.s=right.s.assign(right.number,i,right.number.length()-i);
    }
    else
    {
        size_t i=0;

        while(right.number[i]=='0')
            i++;
        right.s=right.s.assign(right.number,i,right.number.length()-i);
    }

    //string to int
    for(size_t i=0;i<right.s.length();i++)
    {
        long long int n=right.s[i]-'0';
        for(size_t j=right.s.length()-1-i;j>0;j--)
            n=n*10;
        right.a=right.a+n;
    }

    if(right.sign==1)
        right.a=(-1)*right.a;

    return input;
}

//output (long long int)
ostream &operator << (ostream &output,const BigInt &right)
{
    output << right.a;
    return output;
}
macro iT邦新手 5 級 ‧ 2021-06-16 16:33:04 檢舉

謝謝你的幫忙

但好像還是不太行 @@
結果跑出下面這個
https://ithelp.ithome.com.tw/upload/images/20210616/20138102tieUGoe4my.png

程式只有一個地方用到 stoll
你 Google 一下 stoll 的用法
然後修改程式試試看

或者找「把 string 轉成 long long int」的方法
然後把 string s 的值指定給 long long int a

macro iT邦新手 5 級 ‧ 2021-06-17 17:19:16 檢舉

問題解決了! 謝謝 :)

我要發表回答

立即登入回答