实施bignumber类 [英] Implementing bignumber class

查看:71
本文介绍了实施bignumber类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个BigNumber类,它以非相反的顺序存储最多1000位数的非负整数存储为数组元素。



以下程序有一个奇怪的问题。第二个数字(m)在最后填充0。



例如





1234



4567000000000000000000000000000000000000



数字不为零

n小于m

n不大于m

n小于等于m

n不等于m

n不等于m

程序以退出代码结束:0

无法理解为什么



我的尝试:



I am trying to implement a BigNumber class that stores non negative integers with upto 1000 digits stored as array element in reverse order.

The following program has a weird issue. The second number (m) has padded 0's at the end.

eg


1234

4567000000000000000000000000000000000000

number is not zero
n is less than m
n is not greater than equal to m
n is less than equal to m
n is not equal to m
n is not equal to m
Program ended with exit code: 0
Can't understand why

What I have tried:

#include<iostream>
#include<fstream>
using namespace std;
 
class BigNumber
{
    friend ostream &operator<<(ostream &, const BigNumber &); // stream insertion operator
    // output the number as we would normally read it e.g. output 17 means the number seventeen.
private:
    char digits[100]; // to hold the digits with digits[i] representing the 10^i's place.
    int numDigits; // number of digits in the number, e.g 5 has one digit but 45 has two. ZERO should have zero digits
public:
    BigNumber(int value = 0); // default constructor.
    
    int getNumDigits() const; // return numDigits
    bool isZero() const; // return true iff *this is equal to ZERO
    bool operator< (const BigNumber & c) const; // less than
    bool operator>= (const BigNumber & c) const; // greather than or equal
    bool operator<= (const BigNumber & c) const; // less than or equal
    bool operator== (const BigNumber & c) const; // equal
    bool operator!= (const BigNumber & c) const; // not equal
    BigNumber & operator++(); //preincremental
    BigNumber operator++(int dummy);//postincremental
    BigNumber operator+(const BigNumber & c) const; //plus
    BigNumber& operator+=(const BigNumber & c) const; //plus equals
    BigNumber& operator* (const BigNumber &c) const; //times
    BigNumber& operator*=(const BigNumber &c); //times equal
};
 
//BigNumber.cpp
 
//#include "BigNumber.h"
 
BigNumber::BigNumber(int val)
{
    numDigits = 0;
    int i = 0,rem;
    char str[100];
    
    do
    {
        rem = val % 10;
        str[i++] = rem;
        
    } while ((val = val / 10));
    //reverse the string str ans assign to digits array
    for (int j = 0,k = i-1; j < i; j++)
    {
        digits[j] = str[k--];
    }
    i = 0;
    while ((int)digits[i] >= 0)
    {
        ++i;
    }
    numDigits = i;
}
 
int BigNumber::getNumDigits() const
{
    cout << numDigits;
    return numDigits;
}
 
bool BigNumber::isZero() const
{
    if (numDigits == 0)
        return true;
    else
        return false;
}
 
bool BigNumber::operator< (const BigNumber & c) const
{
    if (numDigits < c.numDigits)
        return true;
    if ((int)digits[0] >= (int)c.digits[0])
        return false;
    for (int i = 1; i < numDigits; i++)
    {
        
        if ((int)digits[i] >=(int)c.digits[i])
            return false;
        continue;
    }
    return true;
}
 
bool BigNumber:: operator>= (const BigNumber & c) const
{
    if (numDigits < c.numDigits)
        return false;
    if ((int)digits[0] < (int)c.digits[0])
        return false;
    for (int i = 0; i < numDigits; i++)
    {
        if (digits[i] >= c.digits[i])
            continue;
        else
            return false;
    }
    return true;
}
 
bool BigNumber:: operator<= (const BigNumber & c) const
{
    if (numDigits > c.numDigits)
        return false;
    if ((int)digits[0] > (int)c.digits[0])
        return true;
    for (int i = 0; i < numDigits; i++)
    {
        if (digits[i] <= c.digits[i])
            continue;
        else
            return false;
    }
    return true;
}
 
bool BigNumber:: operator== (const BigNumber & c) const
{
    if (numDigits != c.numDigits )
        return false;
    for (int i = 0; i < numDigits; i++)
    {
        if (digits[i] != c.digits[i])
            return false;
    }
    return true;
}
 
bool BigNumber:: operator!= (const BigNumber & c) const
{
    if (numDigits != c.numDigits)
        return true;
    for (int i = 0; i < numDigits; i++)
    {
        if ( digits[i] !=c.digits[i] )
            return true;
    }
    return false;
}
 
ostream &operator<<(ostream &out, const BigNumber &Int)
{
    for (int i = 0; i < Int.numDigits; i++)
        out << (int)(Int.digits[i]);
    cout << endl;
    return out;
}
 
//main.cpp
 
//#include "BigNumber.h"
#include<iostream>
using namespace std;
 
int main()
{
    BigNumber n(1234);
    cout << n << endl;
    //getNumDigits();
    // (etc.)test operator overloading
    BigNumber m(4567);
    cout << m << endl;
    
    if (n.isZero())
    {
        cout << "number is zero" << endl;
    }
    else
    {
        cout << "number is not zero" << endl;
    }
    //test < operator
    if ( n < m )
    {
        cout << "n is less than m" << endl;
    }
    else
    {
        cout << "n is not less than m" << endl;
    }
    //test >= operator
    if (n >= m)
    {
        cout << "n is greater than equal to m" << endl;
    }
    else
    {
        cout << "n is not greater than equal to m" << endl;
    }
    //test <= operator
    if (n <= m)
    {
        cout << "n is less than equal to m" << endl;
    }
    else
    {
        cout << "n is not less than equal to m" << endl;
    }
    //test == operator
    if (n == m)
    {
        cout << "n is equal to m" << endl;
    }
    else
    {
        cout << "n is not equal to m" << endl;
    }
    
    //test != operator
    if (n != m)
    {
        cout << "n is not equal to m" << endl;
    }
    else
    {
        cout << "n is equal to m" << endl;
    }
    return 0;
}

推荐答案

问题出在您的 BigNumber :: BigNumber(int val)构造函数:

The problem is located in your BigNumber::BigNumber(int val) constructor:
i = 0;
while ((int)digits[i] >= 0)
{
    ++i;
}
numDigits = i;

您通过检查位数数组的内容来计算位数。但并非所有阵列成员都已初始化。未使用的成员具有可能大于或等于零的随机内容。您必须先将所有成员初始化,以确保未使用的成员为负数:

You are counting the number of digits by checking the content of your digits array. But not all array members has been initialised. The unused members have random content which may be greater or equal to zero. You have to initialise all members before to ensure that unused ones are negative:

memset(digits, 0xff, sizeof(digits));



但是有一个更简单的解决方案。您已经拥有 i 变量中的位数。所以只需用以下代码替换上面的代码:


But there is a simpler solution. You already have the numbers of digits in your i variable. So just replace the above code by

/* Special case zero: i == 1 and array[0] == 0 */
if (i > 1 || digits[0] > 0)
    numDigits = i;
else
    numDigits = 0;

请注意上面也正确地处理零,而你的代码总是将 numDigits 设置为非零。

Note that the above also handles zero properly while your code would always set numDigits to non-zero.


当你不明白你的代码正在做或为什么它做它做的事情,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于实施bignumber类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆