![]() |
#2
stophin2011-08-18 00:17
|

#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#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

#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// 私有成员函数
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;
}

#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;
}
#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就没有问题。