|
发表于 2018-5-28 14:05:26
|
显示全部楼层
这个问题我比较感兴趣,就帮你写了
如果我说 你为什么不自己写,你一定会说你不会,为什么不愿意尝试一下呢?
我在写这个程序之前,我也不会,我愿意尝试,写这个程序遇到了各种各样的问题,都一一解决了,这个程序也就写完了
非常建议你自己也动手写一写,把遇到的无法解决的问题提出来,会有人给你解答
我明白,这个程序你就只是来要答案的,而这个程序我比较感兴趣
其他的就不一定了,也许其他的问题会有其他人感兴趣,会帮你写
下面的程序完全按书上的方法写,效率非常不高,就只适合学习原理
Number.hpp
- #ifndef _NUMBER_HPP_
- #define _NUMBER_HPP_
- #include <string>
- class Number
- {
- public:
- Number();
- Number(const Number &s);
- Number(const std::string &s);
- Number(const char *s);
- ~Number();
- operator bool();
- bool operator==(const Number &s);
- bool operator==(const std::string &s);
- bool operator==(const char *s);
- bool operator!=(const Number &s);
- bool operator!=(const std::string &s);
- bool operator!=(const char *s);
- bool operator<(const Number &s);
- bool operator<(const std::string &s);
- bool operator<(const char *s);
- bool operator>(const Number &s);
- bool operator>(const std::string &s);
- bool operator>(const char *s);
- Number operator++(); // 前缀++
- Number operator++(int); // 后缀++
- Number operator--(); // 前缀--
- Number operator--(int); // 后缀--
- Number &operator=(const Number &s);
- Number &operator=(const std::string &s);
- Number &operator=(const char *s);
- Number operator+(const Number &s);
- Number operator+(const std::string &s);
- Number operator+(const char *s);
- Number operator-(const Number &s);
- Number operator-(const std::string &s);
- Number operator-(const char *s);
- Number operator*(const Number &s);
- Number operator*(const std::string &s);
- Number operator*(const char *s);
- Number operator/(const Number &s);
- Number operator/(const std::string &s);
- Number operator/(const char *s);
- Number operator%(const Number &s);
- Number operator%(const std::string &s);
- Number operator%(const char *s);
- Number operator^(const Number &s); // 幂运算
- Number operator^(const std::string &s); // 幂运算
- Number operator^(const char *s); // 幂运算
- friend std::ostream &operator<<(std::ostream &out, const Number &n);
- //std::string &DebugGetNum(void);
- private:
- std::string num;
- bool IsPositiveNumber(const std::string &s);
- std::string SymbolEqualAdd(const std::string &s1, const std::string &s2); // 符号相同的加法
- std::string SymbolNotEqualAdd(const std::string &s1, const std::string &s2); // 符号不同的加法
- std::string StringAdd(const std::string &s1, const std::string &s2);
- std::string StringSub(const std::string &s1, const std::string &s2);
- //std::string::const_reverse_iterator GetMaxR_Iterator(const std::string &s1, const std::string &s2); // 获取最大的字符串的逆序迭代器(失败的设计)
-
- // 获取最大的字符串的逆序迭代器(失败的设计),这已经不是迭代器了,忘了改名字了(^_^),不过无所谓了
- //const std::string *GetMaxR_Iterator(const std::string &s1, const std::string &s2);
- const std::string *GetAbsoluteMaxString(const std::string &s1, const std::string &s2); // 获取绝对值最大的字符串
- std::string OppositeNumber(const std::string &s); // 相反数
- Number OppositeNumber(const Number &n); // 相反数
- bool IsZero(const std::string &s);
- Number AbsoluteValue(const Number &n); // 绝对值
- std::string RemoveZero(const std::string &s); // 去掉数字前面的0,例如 "010",去掉前面的0,变成 "10"
- };
- #endif
复制代码
Number.cpp
- #include "Number.hpp"
- Number::Number()
- {
- }
- Number::Number(const Number &s)
- {
- this->num = s.num;
- }
- Number::Number(const std::string &s)
- {
- this->num = s;
- }
- Number::Number(const char *s)
- {
- this->num = std::string(s);
- }
- Number::~Number()
- {
- }
- Number::operator bool()
- {
- if(this->IsZero(this->num))
- return false;
- else
- return true;
- }
- bool Number::operator==(const Number &s)
- {
- return *this == s.num;
- }
- bool Number::operator==(const std::string &s)
- {
- return this->num == s;
- }
- bool Number::operator==(const char *s)
- {
- return *this == std::string(s);
- }
- bool Number::operator!=(const Number &s)
- {
- return !(*this == s);
- }
- bool Number::operator!=(const std::string &s)
- {
- return !(*this == s);
- }
- bool Number::operator!=(const char *s)
- {
- return !(*this == s);
- }
- bool Number::operator<(const Number &s)
- {
- return *this < s.num;
- }
- bool Number::operator<(const std::string &s)
- {
- Number n = *this - s;
- if(n == "0")
- return false;
- return !(this->IsPositiveNumber(n.num));
- }
- bool Number::operator<(const char *s)
- {
- return *this < std::string(s);
- }
- bool Number::operator>(const Number &s)
- {
- return *this > s.num;
- }
- bool Number::operator>(const std::string &s)
- {
- Number n = *this - s;
- if(n == "0")
- return false;
- return this->IsPositiveNumber(n.num);
- }
- bool Number::operator>(const char *s)
- {
- return *this > std::string(s);
- }
- Number Number::operator++()
- {
- *this = *this + "1";
- return *this;
- }
- Number Number::operator++(int)
- {
- Number t(*this);
- ++*this;
- return t;
- }
- Number Number::operator--()
- {
- *this = *this - "1";
- return *this;
- }
- Number Number::operator--(int)
- {
- Number t(*this);
- --*this;
- return t;
- }
- Number &Number::operator=(const Number &s)
- {
- *this = s.num;
- return *this;
- }
- Number &Number::operator=(const std::string &s)
- {
- this->num = s;
- return *this;
- }
- Number &Number::operator=(const char *s)
- {
- *this = std::string(s);
- return *this;
- }
- Number Number::operator+(const Number &s)
- {
- return *this + s.num;
- }
- Number Number::operator+(const std::string &s)
- {
- std::string sum;
- if(*this == "0")
- return Number(s);
- if(s == "0")
- return Number(*this);
- bool b1 = this->IsPositiveNumber(this->num);
- bool b2 = this->IsPositiveNumber(s);
- if(b1 == b2)
- sum = this->SymbolEqualAdd(this->num, s);
- else
- sum = this->SymbolNotEqualAdd(this->num, s);
- return Number(sum);
- }
- Number Number::operator+(const char *s)
- {
- return *this + std::string(s);
- }
- Number Number::operator-(const Number &s)
- {
- return *this - s.num;
- }
- Number Number::operator-(const std::string &s)
- {
- // 减一个数,等于加这个数的相反数
- return *this + this->OppositeNumber(s);
- }
- Number Number::operator-(const char *s)
- {
- return *this - std::string(s);
- }
- Number Number::operator*(const Number &s)
- {
- return *this * s.num;
- }
- Number Number::operator*(const std::string &s)
- {
- if(*this == "0")
- return Number("0");
- if(s == "0")
- return Number("0");
- Number a = this->AbsoluteValue(*this);
- Number b = this->AbsoluteValue(Number(s));
- Number result("0");
- for(Number i = a; i != "0"; i = i - "1")
- {
- result = result + b;
- }
- bool b1 = this->IsPositiveNumber(this->num);
- bool b2 = this->IsPositiveNumber(s);
- if(b1 == b2)
- return result;
- else
- return this->OppositeNumber(result);
- }
- Number Number::operator*(const char *s)
- {
- return *this * std::string(s);
- }
- Number Number::operator/(const Number &s)
- {
- return *this / s.num;
- }
- Number Number::operator/(const std::string &s)
- {
- if(this->IsZero(this->num))
- return Number("0");
- if(this->IsZero(s))
- throw "除数为0";
-
- Number a = this->AbsoluteValue(*this);
- Number b = this->AbsoluteValue(Number(s));
- Number result;
- for(result = "0"; result * b < a; result = result + "1")
- /* nothing */;
- result = result - "1";
- bool b1 = this->IsPositiveNumber(this->num);
- bool b2 = this->IsPositiveNumber(s);
- if(b1 == b2)
- return result;
- else
- return this->OppositeNumber(result);
- }
- Number Number::operator/(const char *s)
- {
- return *this / std::string(s);
- }
- Number Number::operator%(const Number &s)
- {
- return *this % s.num;
- }
- Number Number::operator%(const std::string &s)
- {
- Number n = *this / s;
- return *this - n * s;
- }
- Number Number::operator%(const char *s)
- {
- return *this % std::string(s);
- }
- Number Number::operator^(const Number &s)
- {
- return *this ^ s.num;
- }
- Number Number::operator^(const std::string &s)
- {
- Number b = this->AbsoluteValue(Number(s));
- Number result = "1";
- for(Number i = "0"; i < b; ++i)
- {
- result = result * (*this);
- }
-
- if(this->IsPositiveNumber(s))
- return result;
- else
- return Number("1") / result;
- }
- Number Number::operator^(const char *s)
- {
- return *this ^ std::string(s);
- }
- bool Number::IsPositiveNumber(const std::string &s)
- {
- if(s[0] == '-')
- return false;
- else
- return true;
- }
- std::string Number::SymbolEqualAdd(const std::string &s1, const std::string &s2)
- {
- std::string sum = this->StringAdd(s1, s2);
- if(this->IsPositiveNumber(s1))
- return sum;
- else
- {
- sum.insert(sum.begin(), '-');
- return sum;
- }
- }
- #if 0
- std::string Number::SymbolNotEqualAdd(const std::string &s1, const std::string &s2)
- {
- return this->StringSub(s1, s2);
- }
- #endif
- std::string Number::SymbolNotEqualAdd(const std::string &s1, const std::string &s2)
- {
- std::string result = this->StringSub(s1, s2);
- result = this->RemoveZero(result);
- // 会出现 "000" 的情况,例如 100 + (-100)
- // 对这类问题特殊处理
- if(this->IsZero(result))
- return std::string("0");
- const std::string *max = this->GetAbsoluteMaxString(s1, s2);
- if(this->IsPositiveNumber(*max))
- return result;
- else
- {
- result.insert(result.begin(), '-');
- return result;
- }
- }
- std::string Number::StringAdd(const std::string &s1, const std::string &s2)
- {
- auto i1_begin = s1.rbegin();
- auto i1_end = s1.rend();
- auto i2_begin = s2.rbegin();
- auto i2_end = s2.rend();
- if((*(i1_end - 1) == '-') || (*(i1_end - 1) == '+'))
- --i1_end;
- if((*(i2_end - 1) == '-') || (*(i2_end - 1) == '+'))
- --i2_end;
- std::string sum;
- bool carry = false; // 进位
- while((i1_begin != i1_end) && (i2_begin != i2_end))
- {
- int tmp = (*i1_begin - '0') + (*i2_begin - '0') + carry;
- sum.insert(sum.begin(), (tmp % 10) + '0');
- if(10 <= tmp)
- carry = true;
- else
- carry = false;
- ++i1_begin;
- ++i2_begin;
- }
-
- while(i1_begin != i1_end)
- {
- int tmp = (*i1_begin - '0') + carry;
- sum.insert(sum.begin(), (tmp % 10) + '0');
- if(10 <= tmp)
- carry = true;
- else
- carry = false;
- ++i1_begin;
- }
- while(i2_begin != i2_end)
- {
- int tmp = (*i2_begin - '0') + carry;
- sum.insert(sum.begin(), (tmp % 10) + '0');
- if(10 <= tmp)
- carry = true;
- else
- carry = false;
- ++i2_begin;
- }
- if(carry)
- sum.insert(sum.begin(), '1');
- return sum;
- }
- // 好吧,此函数无效,重写一个
- #if 0
- // StringSub 工作量太大了,也许应该分给 SymbolNotEqualAdd 一些?
- // 不想改了,目前就这样了
- std::string Number::StringSub(const std::string &s1, const std::string &s2)
- {
- /*std::string::const_reverse_iterator i1_begin = this->GetMaxR_Iterator(s1, s2);
- std::string::const_reverse_iterator i1_end;
- std::string::const_reverse_iterator i2_begin;
- std::string::const_reverse_iterator i2_end;*/
-
- // C++ 要如何判断迭代器所属的对象?
- // 我也没有什么好方法, try catch 一下吧
- /*try
- {
- i1_begin == s1.rbegin();
- i1_end = s1.rend();
- i2_begin = s2.rbegin();
- i2_end = s2.rend();
- }
- catch(...)
- {
- i1_end = s2.rend();
- i2_begin = s1.rbegin();
- i2_end = s1.rend();
- }*/
- // 好吧 try catch 也不行
- // 就只是让 i1_begin 指向最大的字符串开始,i1_end 指向最大的字符串结尾
- // i2_begin 指向最小的字符串开始,i2_end 指向最小的字符串结尾
- // 这样的方法要多少有多少
- /*if(i1_begin == s1.rbegin())
- {
- i1_end = s1.rend();
- i2_begin = s2.rbegin();
- i2_end = s2.rend();
- }
- else
- {
- i1_end = s2.rend();
- i2_begin = s1.rbegin();
- i2_end = s1.rend();
- }*/
- std::string::const_reverse_iterator i1_begin;
- std::string::const_reverse_iterator i1_end;
- std::string::const_reverse_iterator i2_begin;
- std::string::const_reverse_iterator i2_end;
- const std::string *s = this->GetMaxR_Iterator(s1, s2);
- if(s == &s1)
- {
- i1_begin = s1.rbegin();
- i1_end = s1.rend();
- i2_begin = s2.rbegin();
- i2_end = s2.rend();
- }
- else
- {
- i1_begin = s2.rbegin();
- i1_end = s2.rend();
- i2_begin = s1.rbegin();
- i2_end = s1.rend();
- }
-
- if((*(i1_end - 1) == '-') || (*(i1_end - 1) == '+'))
- --i1_end;
- if((*(i2_end - 1) == '-') || (*(i2_end - 1) == '+'))
- --i2_end;
- bool borrow = false; // 借位
- std::string result;
- while((i1_begin != i1_end) && (i2_begin != i2_end))
- {
- int tmp = (*i1_begin - '0') - (*i2_begin - '0') - borrow;
- if(tmp < 0)
- {
- borrow = true;
- tmp += 10;
- }
- else
- {
- borrow = false;
- }
- result.insert(result.begin(), tmp + '0');
- ++i1_begin;
- ++i2_begin;
- }
- while(i1_begin != i1_end)
- {
- int tmp = (*i1_begin - '0') - borrow;
- if(tmp < 0)
- {
- borrow = true;
- tmp += 10;
- }
- else
- {
- borrow = false;
- }
- result.insert(result.begin(), tmp + '0');
- ++i1_begin;
- }
- while(i2_begin != i2_end)
- {
- int tmp = (*i2_begin - '0') - borrow;
- if(tmp < 0)
- {
- borrow = true;
- tmp += 10;
- }
- else
- {
- borrow = false;
- }
- result.insert(result.begin(), tmp + '0');
- ++i2_begin;
- }
- const std::string *max;
- if(s == &s1)
- max = &s1;
- else
- max = &s2;
- /*if(i1_begin == s1.rbegin())
- max = &s1;
- else
- max = &s2;*/
- if(this->IsPositiveNumber(*max))
- {
- return result;
- }
- else
- {
- result.insert(result.begin(), '-');
- return result;
- }
- }
- #endif
- // 绝对值大的减绝对值小的,不管符号
- std::string Number::StringSub(const std::string &s1, const std::string &s2)
- {
- std::string::const_reverse_iterator i1_begin;
- std::string::const_reverse_iterator i1_end;
- std::string::const_reverse_iterator i2_begin;
- std::string::const_reverse_iterator i2_end;
- const std::string *max = this->GetAbsoluteMaxString(s1, s2);
- if(max == &s1)
- {
- i1_begin = s1.rbegin();
- i1_end = s1.rend();
- i2_begin = s2.rbegin();
- i2_end = s2.rend();
- }
- else
- {
- i1_begin = s2.rbegin();
- i1_end = s2.rend();
- i2_begin = s1.rbegin();
- i2_end = s1.rend();
- }
- if((*(i1_end - 1) == '-') || (*(i1_end - 1) == '+'))
- --i1_end;
- if((*(i2_end - 1) == '-') || (*(i2_end - 1) == '+'))
- --i2_end;
- bool borrow = false; // 借位
- std::string result;
- while((i1_begin != i1_end) && (i2_begin != i2_end))
- {
- int tmp = (*i1_begin - '0') - (*i2_begin - '0') - borrow;
- if(tmp < 0)
- {
- borrow = true;
- tmp += 10;
- }
- else
- {
- borrow = false;
- }
- result.insert(result.begin(), tmp + '0');
- ++i1_begin;
- ++i2_begin;
- }
- while(i1_begin != i1_end)
- {
- int tmp = (*i1_begin - '0') - borrow;
- if(tmp < 0)
- {
- borrow = true;
- tmp += 10;
- }
- else
- {
- borrow = false;
- }
- result.insert(result.begin(), tmp + '0');
- ++i1_begin;
- }
- while(i2_begin != i2_end)
- {
- int tmp = (*i2_begin - '0') - borrow;
- if(tmp < 0)
- {
- borrow = true;
- tmp += 10;
- }
- else
- {
- borrow = false;
- }
- result.insert(result.begin(), tmp + '0');
- ++i2_begin;
- }
- return result;
- }
- //std::string::const_reverse_iterator Number::GetMaxR_Iterator(const std::string &s1, const std::string &s2)
- //{
- // size_t len1 = s1.size();
- // size_t len2 = s2.size();
- // if((s1[0] == '-') || (s1[0] == '+'))
- // --len1;
- // if((s2[0] == '-') || (s2[0] == '+'))
- // --len2;
- //
- // if(len1 == len2)
- // {
- // auto i1_begin = s1.rbegin();
- // auto i1_end = s1.rend();
- // auto i2_begin = s2.rbegin();
- // auto i2_end = s2.rend();
- // if((*(i1_end - 1) == '-') || (*(i1_end - 1) == '+'))
- // --i1_end;
- // if((*(i2_end - 1) == '-') || (*(i2_end - 1) == '+'))
- // --i2_end;
- //
- // if(std::string(i1_begin, i1_end) > std::string(i2_begin, i2_end))
- // return s1.rbegin();
- // else
- // return s2.rbegin();
- // }
- // else
- // {
- // if(len1 > len2)
- // return s1.rbegin();
- // else
- // return s2.rbegin();
- // }
- //}
- //const std::string *Number::GetMaxR_Iterator(const std::string &s1, const std::string &s2)
- //{
- // size_t len1 = s1.size();
- // size_t len2 = s2.size();
- // if((s1[0] == '-') || (s1[0] == '+'))
- // --len1;
- // if((s2[0] == '-') || (s2[0] == '+'))
- // --len2;
- //
- // if(len1 == len2)
- // {
- // auto i1_begin = s1.rbegin();
- // auto i1_end = s1.rend();
- // auto i2_begin = s2.rbegin();
- // auto i2_end = s2.rend();
- // if((*(i1_end - 1) == '-') || (*(i1_end - 1) == '+'))
- // --i1_end;
- // if((*(i2_end - 1) == '-') || (*(i2_end - 1) == '+'))
- // --i2_end;
- //
- // if(std::string(i1_begin, i1_end) > std::string(i2_begin, i2_end))
- // return &s1;
- // else
- // return &s2;
- // }
- // else
- // {
- // if(len1 > len2)
- // return &s1;
- // else
- // return &s2;
- // }
- //}
- const std::string * Number::GetAbsoluteMaxString(const std::string & s1, const std::string & s2)
- {
- size_t len1 = s1.size();
- size_t len2 = s2.size();
- if((s1[0] == '-') || (s1[0] == '+'))
- --len1;
- if((s2[0] == '-') || (s2[0] == '+'))
- --len2;
-
- if(len1 == len2)
- {
- std::string::const_iterator i1 = s1.begin();
- std::string::const_iterator i2 = s2.begin();
- if((*i1 == '-') || (*i1 == '+'))
- ++i1;
- if((*i2 == '-') || (*i2 == '+'))
- ++i2;
-
- if(std::string(i1, s1.end()) > std::string(i2, s2.end()))
- return &s1;
- else
- return &s2;
- }
- else
- {
- if(len1 > len2)
- return &s1;
- else
- return &s2;
- }
- }
- //std::string &Number::DebugGetNum(void)
- //{
- // return this->num;
- //}
- std::string Number::OppositeNumber(const std::string &s)
- {
- std::string result(s);
- if(this->IsPositiveNumber(result))
- {
- if(result[0] == '+')
- result[0] = '-';
- else
- result.insert(result.begin(), '-');
- }
- else
- {
- result.erase(result.begin());
- }
- return result;
- }
- Number Number::OppositeNumber(const Number &n)
- {
- return Number(this->OppositeNumber(n.num));
- }
- bool Number::IsZero(const std::string &s)
- {
- for(auto iter = s.begin(); iter != s.end(); ++iter)
- {
- if(*iter != '0')
- return false;
- }
- return true;
- }
- Number Number::AbsoluteValue(const Number &n)
- {
- if(this->IsPositiveNumber(n.num))
- return Number(n);
- else
- return Number(this->OppositeNumber(n.num));
- }
- std::string Number::RemoveZero(const std::string &s)
- {
- std::string result(s);
- while(!result.empty())
- {
- if(result[0] != '0')
- break;
- else
- result.erase(result.begin());
- }
- if(!result.empty())
- return result;
- else
- return std::string("0");
- }
- std::ostream &operator<<(std::ostream &out, const Number &n)
- {
- out << n.num;
- return out;
- }
- #if 0
- int main()
- {
- std::string num = "100";
- Number n1 = "1234";
- Number n2("1234");
- Number n3 = num;
- Number n4(num);
- Number n5 = n4;
- n1 == n2;
- n1 == num;
- n1 == "123";
- n1 = num;
- n1 = n2 = "100";
- n1 + n2;
- n1 + num;
- n1 + "123";
- #if 0
- num = n1.StringAdd("1234", "56");
- std::cout << num << "\t" << 1234 + 56 << std::endl;
- num = n1.StringAdd("1234", "567890");
- std::cout << num << "\t" << 1234 + 567890 << std::endl;
- num = n1.StringAdd("-1234", "56");
- std::cout << num << "\t" << 1234 + 56 << std::endl;
- num = n1.StringAdd("1234", "-56");
- std::cout << num << "\t" << 1234 + 56 << std::endl;
- num = n1.StringAdd("+1234", "56");
- std::cout << num << "\t" << 1234 + 56 << std::endl;
- num = n1.StringAdd("1234", "+56");
- std::cout << num << "\t" << 1234 + 56 << std::endl;
- num = n1.StringAdd("-1234", "-56");
- std::cout << num << "\t" << 1234 + 56 << std::endl;
- num = n1.StringAdd("-1234", "+56");
- std::cout << num << "\t" << 1234 + 56 << std::endl;
- num = n1.StringAdd("+1234", "-56");
- std::cout << num << "\t" << 1234 + 56 << std::endl;
- num = n1.StringAdd("+1234", "+56");
- std::cout << num << "\t" << 1234 + 56 << std::endl;
- n1 = "-1234";
- n2 = "-456789";
- n3 = n1 + n2;
- std::cout << (-1234) + (-456789) << std::endl;
- num = n1.StringSub("1234", "56");
- std::cout << num << "\t" << 1234 - 56 << std::endl;
- num = n1.StringSub("1234", "567890");
- std::cout << num << "\t" << 567890 - 1234 << std::endl;
- num = n1.StringSub("-1234", "56");
- std::cout << num << "\t" << 1234 - 56 << std::endl;
- num = n1.StringSub("1234", "-56");
- std::cout << num << "\t" << 1234 - 56 << std::endl;
- num = n1.StringSub("+1234", "56");
- std::cout << num << "\t" << 1234 - 56 << std::endl;
- num = n1.StringSub("1234", "+56");
- std::cout << num << "\t" << 1234 - 56 << std::endl;
- num = n1.StringSub("-1234", "-56");
- std::cout << num << "\t" << 1234 - 56 << std::endl;
- num = n1.StringSub("-1234", "+56");
- std::cout << num << "\t" << 1234 - 56 << std::endl;
- num = n1.StringSub("+1234", "-56");
- std::cout << num << "\t" << 1234 - 56 << std::endl;
- num = n1.StringSub("+1234", "+56");
- std::cout << num << "\t" << 1234 - 56 << std::endl;
- n2 = "1234";
- n3 = "56";
- n1 = n2 + n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 + 56 << std::endl;
- n2 = "1234";
- n3 = "567890";
- n1 = n2 + n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 + 567890 << std::endl;
- n2 = "-1234";
- n3 = "56";
- n1 = n2 + n3;
- std::cout << n1.DebugGetNum() << "\t" << (-1234) + 56 << std::endl;
- n2 = "1234";
- n3 = "-56";
- n1 = n2 + n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 + (-56) << std::endl;
- n2 = "+1234";
- n3 = "56";
- n1 = n2 + n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 + 56 << std::endl;
- n2 = "1234";
- n3 = "+56";
- n1 = n2 + n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 + 56 << std::endl;
- n2 = "-1234";
- n3 = "-56";
- n1 = n2 + n3;
- std::cout << n1.DebugGetNum() << "\t" << (-1234) + (-56) << std::endl;
- n2 = "-1234";
- n3 = "+56";
- n1 = n2 + n3;
- std::cout << n1.DebugGetNum() << "\t" << (-1234) + 56 << std::endl;
- n2 = "+1234";
- n3 = "+56";
- n1 = n2 + n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 + 56 << std::endl;
- n2 = "+1234";
- n3 = "-56";
- n1 = n2 + n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 + (-56) << std::endl;
- n2 = "1234";
- n3 = "56";
- n1 = n2 - n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 - 56 << std::endl;
- n2 = "1234";
- n3 = "567890";
- n1 = n2 - n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 - 567890 << std::endl;
- n2 = "-1234";
- n3 = "56";
- n1 = n2 - n3;
- std::cout << n1.DebugGetNum() << "\t" << (-1234) - 56 << std::endl;
- n2 = "1234";
- n3 = "-56";
- n1 = n2 - n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 - (-56) << std::endl;
- n2 = "+1234";
- n3 = "56";
- n1 = n2 - n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 - 56 << std::endl;
- n2 = "1234";
- n3 = "+56";
- n1 = n2 - n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 - 56 << std::endl;
- n2 = "-1234";
- n3 = "-56";
- n1 = n2 - n3;
- std::cout << n1.DebugGetNum() << "\t" << (-1234) - (-56) << std::endl;
- n2 = "-1234";
- n3 = "+56";
- n1 = n2 - n3;
- std::cout << n1.DebugGetNum() << "\t" << (-1234) - 56 << std::endl;
- n2 = "+1234";
- n3 = "+56";
- n1 = n2 - n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 - 56 << std::endl;
- n2 = "+1234";
- n3 = "-56";
- n1 = n2 - n3;
- std::cout << n1.DebugGetNum() << "\t" << 1234 - (-56) << std::endl;
- n1 != n2;
- n1 != num;
- n1 != "1234";
- n2 = "3";
- n1 = n2 * "4";
- std::cout << Number("1234") * Number("56") << "\t" << 1234 * 56 << std::endl;
- std::cout << Number("1234") * Number("567890") << "\t" << 1234 * 567890 << std::endl;
- std::cout << Number("-1234") * Number("56") << "\t" << (-1234) * 56 << std::endl;
- std::cout << Number("1234") * Number("-56") << "\t" << 1234 * (-56) << std::endl;
- std::cout << Number("+1234") * Number("56") << "\t" << 1234 * 56 << std::endl;
- std::cout << Number("1234") * Number("+56") << "\t" << 1234 * 56 << std::endl;
- std::cout << Number("-1234") * Number("-56") << "\t" << (-1234) * (-56) << std::endl;
- std::cout << Number("-1234") * Number("+56") << "\t" << (-1234) * 56 << std::endl;
- std::cout << Number("+1234") * Number("-56") << "\t" << 1234 * (-56) << std::endl;
- std::cout << Number("+1234") * Number("+56") << "\t" << 1234 * 56 << std::endl;
- n1 - n2;
- n1 - num;
- n1 - "123";
- n1 * n2;
- n1 * num;
- n1 * "123";
- n1 / n2;
- n1 / num;
- n1 / "123";
- if(Number("1"))
- std::wcout << "1" << std::endl;
- if(Number("0"))
- std::wcout << "0" << std::endl;
- if(Number("123"))
- std::wcout << "123" << std::endl;
- std::cout << Number("10") / Number("3") << "\t" << 10 / 3 << std::endl;
- std::cout << Number("1234") / Number("56") << "\t" << 1234 / 56 << std::endl;
- std::cout << Number("1234") / Number("567890") << "\t" << 1234 / 567890 << std::endl;
- std::cout << Number("-1234") / Number("56") << "\t" << (-1234) / 56 << std::endl;
- std::cout << Number("1234") / Number("-56") << "\t" << 1234 / (-56) << std::endl;
- std::cout << Number("+1234") / Number("56") << "\t" << 1234 / 56 << std::endl;
- std::cout << Number("1234") / Number("+56") << "\t" << 1234 / 56 << std::endl;
- std::cout << Number("-1234") / Number("-56") << "\t" << (-1234) / (-56) << std::endl;
- std::cout << Number("-1234") / Number("+56") << "\t" << (-1234) / 56 << std::endl;
- std::cout << Number("+1234") / Number("-56") << "\t" << 1234 / (-56) << std::endl;
- std::cout << Number("+1234") / Number("+56") << "\t" << 1234 / 56 << std::endl;
- n1 % n2;
- n1 % num;
- n1 % "123";
- std::cout << Number("10") % Number("3") << "\t" << 10 % 3 << std::endl;
- std::cout << Number("1234") % Number("56") << "\t" << 1234 % 56 << std::endl;
- std::cout << Number("1234") % Number("567890") << "\t" << 1234 % 567890 << std::endl;
- std::cout << Number("-1234") % Number("56") << "\t" << (-1234) % 56 << std::endl;
- std::cout << Number("1234") % Number("-56") << "\t" << 1234 % (-56) << std::endl;
- std::cout << Number("+1234") % Number("56") << "\t" << 1234 % 56 << std::endl;
- std::cout << Number("1234") % Number("+56") << "\t" << 1234 % 56 << std::endl;
- std::cout << Number("-1234") % Number("-56") << "\t" << (-1234) % (-56) << std::endl;
- std::cout << Number("-1234") % Number("+56") << "\t" << (-1234) % 56 << std::endl;
- std::cout << Number("+1234") % Number("-56") << "\t" << 1234 % (-56) << std::endl;
- std::cout << Number("+1234") % Number("+56") << "\t" << 1234 % 56 << std::endl;
- n1 = "2";
- n2 = "10";
- num = "10";
- n1 ^ n2;
- n1 ^ num;
- n1 ^ "10";
- std::cout << (Number("2") ^ Number("10")) << "\t" << pow(2, 10) << std::endl;
- std::cout << (Number("10") ^ Number("2")) << "\t" << pow(10, 2) << std::endl;
- std::cout << (Number("-2") ^ Number("10")) << "\t" << pow(-2, 10) << std::endl;
- std::cout << (Number("2") ^ Number("-10")) << "\t" << pow(2, -10) << std::endl;
- std::cout << (Number("+2") ^ Number("10")) << "\t" << pow(2, 10) << std::endl;
- std::cout << (Number("2") ^ Number("+10")) << "\t" << pow(2, 10) << std::endl;
- std::cout << (Number("-2") ^ Number("-10")) << "\t" << pow(-2, -10) << std::endl;
- std::cout << (Number("-2") ^ Number("+10")) << "\t" << pow(-2, 10) << std::endl;
- std::cout << (Number("+2") ^ Number("+10")) << "\t" << pow(2, 10) << std::endl;
- std::cout << (Number("+2") ^ Number("-10")) << "\t" << pow(2, -10) << std::endl;
- #endif
- return 0;
- }
- #endif
复制代码
main.cpp
- #include <iostream>
- #include "Number.hpp"
- int main()
- {
- std::cout << (Number("2") ^ Number("10")) << std::endl; // 2的10次方
- std::cout << (Number("2") ^ Number("16")) << std::endl;
- return 0;
- }
复制代码 |
|