| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 504 人关注过本帖
标题:有些诡异的问题,求帮忙
取消只看楼主 加入收藏
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:709
专家分:2063
注 册:2010-11-11
结帖率:96.55%
收藏
已结贴  问题点数:20 回复次数:3 
有些诡异的问题,求帮忙
ValueNode.h
程序代码:
#ifndef VALUE_NODE_H_
#define VALUE_NODE_H_

#include <iostream>

const unsigned long long ZERO = 0;

class ValueNode
{
private:
    unsigned long long    capacity;        // 数值字符串中包含无效的0后的字符个数,即当前能够储存的最大位数,容量
    unsigned long long    length;            // 数值字符串除去无效的0后的位数,即当前所表示的数的实际有效位数,数值有效位数。
    char *                value;            // 数值字符串,以小数点开始,存储实际值。
private:
    bool expand();
public:
    ValueNode();
    ValueNode(const char * v);
    ValueNode(const unsigned long long x);
    ValueNode(const ValueNode & x);
    ~ValueNode();
    unsigned long long Length() const;
    ValueNode & Add(const char * v);
    ValueNode & Add(const ValueNode & x);
    ValueNode & operator+=(const char * v);
    ValueNode & operator+=(const ValueNode & x);
    ValueNode & operator+(const char * v);
    ValueNode & Sub(const ValueNode & x);
    ValueNode & Mul(const ValueNode & x);
    ValueNode & operator=(const char * v);
    ValueNode & operator=(const unsigned long long x);
    ValueNode & operator=(const ValueNode & x);

    friend ValueNode & operator+(const ValueNode & x, const ValueNode & y);
    friend bool operator==(const ValueNode & x, const ValueNode & y);
    friend bool operator!=(const ValueNode & x, const ValueNode & y);
    friend bool operator>=(const ValueNode & x, const ValueNode & y);
    friend bool operator>=(const ValueNode & x, const ValueNode & y);
    friend bool operator>(const ValueNode & x, const ValueNode & y);
    friend bool operator<(const ValueNode & x, const ValueNode & y);
    friend std::ostream & operator<<(std::ostream & os, const ValueNode & x);
};
#endif
ValueNode.cpp
程序代码:
#include "ValueNode.h"

// 私有成员函数
bool ValueNode::expand()
{
    char * temp    = new char[this->capacity * 2];
    if (temp == 0)
        return false;

    this->capacity *= 2;
    for(int i = 0; i < this->length; ++i)
        temp[i] = this->value[i];
    memset(temp + this->length, '\0', this->capacity - this->length);
    delete [] this->value;
    this->value = temp;
    return true;
}

// 共有成员函数
ValueNode::ValueNode()
{
    this->capacity    = 2;
    this->length    = 1;
    this->value        = new char[capacity];
    this->value[length - 1] = '0';
    memset(this->value + this->length, '\0', this->capacity - this->length);
}

ValueNode::ValueNode(const char * v)
{
    unsigned long long n = strlen(v);
    if(n == 0)
    {
        this->capacity    = 2;
        this->length    = 1;
        this->value        = new char[capacity];
        this->value[length - 1] = '0';
        memset(this->value + this->length, '\0', this->capacity - this->length);
    }
    else
    {
        unsigned long long p;
        for(p = 0; (v[p] == '0') && (p < n); ++p);
        if(p == n)
        {
            this->capacity    = 2;
            this->length    = 1;
            this->value        = new char[capacity];
            this->value[length - 1] = '0';
            memset(this->value + this->length, '\0', this->capacity - this->length);
        }
        else
        {
            this->capacity    = 2;
            this->length    = 0;
            this->value        = new char[capacity];

            for(unsigned long long i = 0; i < n - p; ++i)
            {
                this->value[i] = v[n - i - 1];
                this->length += 1;
                if(this->length == this->capacity)
                    this->expand();
            }
            memset(this->value + this->length, '\0', this->capacity - this->length);
        }
    }
}

ValueNode::ValueNode(const unsigned long long x)
{
    this->capacity    = 2;
    this->length    = 0;
    this->value        = new char[this->capacity];

    unsigned long long i = 0;
    unsigned long long temp = x;
    do
    {
        this->value[i] = temp % 10 + '0';
        this->length += 1;
        ++i;
        if(this->length == this->capacity)
            this->expand();
        temp /= 10;
    }while(temp);
    memset(this->value + this->length, '\0', this->capacity - this->length);
}

ValueNode::ValueNode(const ValueNode & x)
{
    this->capacity        = x.capacity;
    this->length        = x.length;
    this->value            = new char[this->capacity];
    memcpy(this->value, x.value, this->length);
}

ValueNode::~ValueNode()
{
    delete [] value;
}

unsigned long long ValueNode::Length() const
{
    if(this->value[0] == '0' && this->value[1] == '\0')
        return 0;
    return length;
}

ValueNode & ValueNode::Add(const ValueNode & x)
{
    unsigned long long max_length = (this->length >= x.length) ? this->length : x.length;    

    ValueNode result;
    result.length = 0;

    int carry_out    = 0;
    int carry_in    = 0;
    bool is_zero = true;   
    for(unsigned long long i = 0; i < max_length; ++i)
    {
        carry_in = carry_out;
        carry_out = 0;
        result.value[i] = this->value[i] + x.value[i]  + carry_in;
        if(this->value[i] != '\0')
            result.value[i] -= '0';
        if(x.value[i] != '\0')
            result.value[i] -= '0';
        result.length += 1;
        if(result.value[i] > 9)
        {
            carry_out = 1;
            result.value[i] -= 10;
        }
        if(result.value[i] != 0)
            is_zero = false;
        result.value[i] += '0';
        if(result.length == result.capacity)
            result.expand();
    }
    if(carry_out != 0)
    {
        result.value[result.length] = '1';
        is_zero = false;
        result.length += 1;
        if(result.length == result.capacity)
            result.expand();
        carry_out = 0;
    }

    if(is_zero)
    {
        result.capacity                    = 1;
        result.length                    = 0;
        result.value                    = new char[result.capacity];
        result.value[result.capacity]    = '0';
    }

    *this = result;

    return *this;
}

ValueNode & ValueNode::operator=(const char * v)
{
    unsigned long long n = strlen(v);
    unsigned long long p;
    for(p = 0; (v[p] == '0') && (p < n); ++p);
    if(p == n)
    {
        this->capacity    = 2;
        this->length    = 1;
        this->value        = new char[capacity];
        this->value[length - 1] = '0';
        memset(this->value + this->length, '\0', this->capacity - this->length);
    }
    else
    {
        this->capacity    = 2;
        this->length    = 0;
        this->value        = new char[capacity];

        for(unsigned long long i = 0; i < n - p; ++i)
        {
            this->value[i] = v[n - i - 1];
            this->length += 1;
            if(this->length == this->capacity)
                this->expand();
        }
        memset(this->value + this->length, '\0', this->capacity - this->length);
    }

    return *this;
}

ValueNode & ValueNode::operator=(const unsigned long long x)
{
    this->capacity    = 1;
    this->length    = 0;
    this->value        = new char[this->capacity];

    unsigned long long i = 0;
    unsigned long long temp = x;
    do
    {
        this->value[i] = temp % 10 + '0';
        this->length += 1;
        ++i;
        if(this->length == this->capacity)
            this->expand();
        temp /= 10;
    }while(temp);
    memset(this->value + this->length, '\0', this->capacity - this->length);

    return *this;
}

ValueNode & ValueNode::operator=(const ValueNode & x)
{
    this->capacity        = x.capacity;
    this->length        = x.length;
    this->value            = new char[this->capacity];
    memcpy(this->value, x.value, this->length);

    return *this;
}



// 友元函数
std::ostream & operator<<(std::ostream & os, const ValueNode & x)
{
    char * temp = new char[x.length + 1];
    for(unsigned long long i = 0; i < x.length; ++i)
        temp[x.length - 1 - i] = x.value[i];
    temp[x.length] = '\0';

    os << temp;
    return os;
}
test.cpp
程序代码:
#include <ctime>
#include "ValueNode.h"

using std::cin;
using std::cout;
using std::endl;

int main()
{
    unsigned long long times = 50;
    ValueNode a = "1";
    ValueNode b = "1";
    ValueNode temp;
    unsigned int i = 2;
    cout << "1.\t" << a << "\t\t\t\t" << a.Length() << endl << "2.\t" << b << "\t\t\t\t" << b.Length() << "\n";
    do
    {
        temp = b;
        b.Add(a);
        a = temp;
        cout << i++ << ".\t";
        cout << b << "\t\t\t\t";
        cout << b.Length() << endl;
    }while(i <= times);

    cin.get();
    return 0;
}


我的问题是:在计算斐波那契数列的第12个时出现了错误。看了所有的数据,发现如果前两个数的位数不一致就会出现问题,而其他的数只是因为前一钟情况而出问题。想问到底是为什么?如果单独计算没问题,比如89+144就没有问题。

2011-08-17 22:56
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:709
专家分:2063
注 册:2010-11-11
收藏
得分:0 
8+13是没问题的
2011-08-18 00:58
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:709
专家分:2063
注 册:2010-11-11
收藏
得分:0 
我的电脑是4GB的,可使用3G左右的内存。在我的电脑上result最多可以储存65560位左右的十进制数
2011-08-18 01:20
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:709
专家分:2063
注 册:2010-11-11
收藏
得分:0 
希望帮忙
2011-08-18 23:01
快速回复:有些诡异的问题,求帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035883 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved