+ -
当前位置:首页 → 问答吧 → C++作业不会做,请高手指教~~~

C++作业不会做,请高手指教~~~

时间:2011-12-13

来源:互联网

1.构造函数或普通成员函数,使得它能存储任意的整数
2.能把这个数显示出来
3.能通过成员函数进行加减运算
4.能进行乘法运算



我做的类是:#include "iostream.h"

 
int n,i,a;




class BigInteger {
public:
BigInteger(int data[10000]);
setData(int data[10000]);
private:
int data[10000];

};

BigInteger::BigInteger(int data[10000]){
for (int i=0; i<10000; i++){
int temp=a%10;
data[9999-i]=temp;
a/=10;
}

}


BigInteger::setData(int data[10000]){

cout<<" data["<<i<<"]"<<data[i]<<endl;

return 0;
}



之后的主函数就不知道怎么了; BigInteger a;又说有问题,请问我的类有错吗?之后改怎么输出那个大数出来啊??

作者: lurenxiaoqiao   发布时间: 2011-12-13

BigInteger a;有问题是因为它没有默认的构造函数啊。

作者: tonywearme   发布时间: 2011-12-13

应该用字符串存储数字, 这个题目主要考的是大数运算, 
你搜一下大数运算

作者: tujiaw   发布时间: 2011-12-13

这里有个大数相乘的例子楼主可以参考下:
http://blog.csdn.net/tujiaw/article/details/6779050

作者: tujiaw   发布时间: 2011-12-13

参考这个大数的四则运算及求模
改改就好。

作者: pathuang68   发布时间: 2011-12-13

写的不太好, 太晚了, 给个参考:
C/C++ code

#include <iostream>
#include <assert.h>

using namespace std;

class BigInteger
{
public:
    BigInteger(char* str);
    BigInteger(int num);
    ~BigInteger();

    void Show();
    BigInteger* operator* (const BigInteger& bi);

private:
    void BigNumMultiply(const char *str1, const char *str2, char *product);

private:
    char *m_integer;
};


BigInteger::BigInteger(char* str)
{
    int len = strlen(str) + 1;
    m_integer = new char[len];
    memset(m_integer, 0, len);
    strcpy(m_integer, str);
}

BigInteger::BigInteger(int num)
{
    m_integer = new char[num+1];
    memset(m_integer, 0, num+1);
}

void BigInteger::Show()
{
    cout << m_integer << endl;
}

BigInteger* BigInteger::operator *(const BigInteger& bi)
{
    char szTemp[128] = {0};
    
    BigNumMultiply(m_integer, bi.m_integer, szTemp);
    BigInteger *ret = new BigInteger(szTemp);

    return ret;
}

BigInteger::~BigInteger()
{
    if (m_integer)
    {
        delete [] m_integer;
        m_integer = NULL;
    }
}

void BigInteger::BigNumMultiply(const char *str1, const char *str2, char *product)
{
    assert(str1 != NULL && str2 != NULL && product != NULL);

    int i, j;
    int len1 = (int)strlen(str1);
    int len2 = (int)strlen(str2);
    int *dest = (int*)malloc(sizeof(int)*(len1+len2+1));
    
    for (i=0; i<len1+len2+1; i++) { dest[i] = 0; }

    for (i=0; i<len1; i++)
    {
        for (j=0; j<len2; j++)
        {
            dest[i+j+1] += (str1[i]-'0')*(str2[j]-'0');
        }
    }

    for (i=len1+len2-1; i>=0; i--)
    {
        // 当i=0时dest[0]=0,这个条件不成立,所以不用担心dest[-1]
        if (dest[i] >= 10)
        {
            dest[i-1] += dest[i]/10;
            dest[i] %= 10;
        }
        product[i] = dest[i]+'0';
    }

    if (product[0] == '0')
    {
        i = 1;
        while (product[i] != '\0')
        {
            product[i-1] = product[i];
            i++;
        }
        product[i-1] = '\0';
    }

    free(dest);
    return;
}

int main(void)
{
    BigInteger bi1("34535");
    BigInteger bi2("10034435");
    BigInteger *bi = bi1*bi2;

    bi->Show();

    delete bi;

    return 0;
}

作者: tujiaw   发布时间: 2011-12-13