这代码为啥出错了
在网上看了各种教程
理论上不应该出错啊
windows 11 22H2,VS2022,ISOC++ 20
代码水平不高,请见谅
BigInt.h
#pragma once
#include <iostream>
#include <sstream>
#include <string>
class BigInt
{
friend std::istream& operator>>(std::istream& in, BigInt& value);
friend std::ostream& operator<<(std::ostream& out, const BigInt& value);
public:
const static BigInt inf;
const static BigInt ZERO;
const static BigInt ONE;
const static BigInt TWO;
const static BigInt TEN;
public:
BigInt();
BigInt(const std::string& str);
BigInt(const unsigned char* value, size_t length);
BigInt(size_t length);
BigInt(const BigInt& bigInt);
~BigInt();
BigInt operator+(const BigInt& bigInt) const;
private:
unsigned char* value;
size_t length;
};
std::istream& operator>>(std::istream& in, BigInt& value);
std::ostream& operator<<(std::ostream& out, const BigInt& value);
std::string getTo(std::istream& in, int (*func)(int c));
BigInt.cpp
#include "BigInt.h"
const BigInt BigInt::inf = BigInt();
const BigInt BigInt::ZERO = BigInt("0");
const BigInt BigInt::ONE = BigInt("1");
const BigInt BigInt::TWO = BigInt("2");
const BigInt BigInt::TEN = BigInt("10");
BigInt::BigInt()
{
this->length = 0;
this->value = new unsigned char[this->length];
}
BigInt::BigInt(const std::string& str)
{
this->length = str.length();
this->value = new unsigned char[this->length];
size_t i = 0L;
for (size_t j = this->length - 1; i < this->length; i++, j--)
{
char c = str[j];
if (c < '0' || c > '9')
{
break;
}
this->value[i] = c - '0';
}
for (; i < this->length; i++)
{
this->value[i] = 0;
}
}
BigInt BigInt::operator+(const BigInt& bigInt) const
{
size_t maxlen, minlen;
if (bigInt.length < this->length)
{
minlen = bigInt.length;
maxlen = this->length;
}
else
{
minlen = this->length;
maxlen = bigInt.length;
}
BigInt out(maxlen);
size_t i;
for (i = 0; i < minlen; i++)
{
out.value[i] = this->value[i] + bigInt.value[i];
}
if (i < this->length)
{
for (; i < this->length; i++)
{
out.value[i] = this->value[i];
}
}
if (i < bigInt.length)
{
for (; i < bigInt.length; i++)
{
out.value[i] = bigInt.value[i];
}
}
// 这里还没写完
return BigInt();
}
BigInt::BigInt(const unsigned char* value, const size_t len)
{
this->length = len;
this->value = new unsigned char[this->length];
for (size_t i = 0L; i < this->length; i++)
{
this->value[i] = value[i];
}
}
BigInt::BigInt(const size_t len)
{
this->length = len;
this->value = new unsigned char[this->length];
}
BigInt::BigInt(const BigInt& bigInt)
{
this->length = bigInt.length;
this->value = new unsigned char[this->length];
for (size_t i = 0L; i < this->length; i++)
{
this->value[i] = bigInt.value[i];
}
}
BigInt::~BigInt()
{
if (this->value != nullptr)
{
delete[] this->value;
this->value = nullptr;
}
this->length = 0;
}
std::istream& operator>>(std::istream& in, BigInt& value)
{
char cbuf = '\n';
while (isspace(in.peek())) cbuf = in.get();
std::stringstream ss;
ss << cbuf;
while ((cbuf = in.peek()) != EOF)
{
if (cbuf < '0' || cbuf > '9')
{
break;
}
cbuf = in.get();
ss << cbuf;
}
std::string str = ss.str();
value = BigInt(str);
return in;
}
std::ostream& operator<<(std::ostream& out, const BigInt& value)
{
if (value.length == 0)
{
out << "inf";
return out;
}
size_t i = value.length - 1;
for (; value.value[i] == 0 && i > 0; i--);
for (; i > 0; i--)
{
out << (char)(value.value[i] + '0');
}
out << (char)(value.value[i] + '0');
return out;
}
std::string getTo(std::istream& in, int(*func)(int c))
{
char cbuf = '\n';
std::stringstream ss;
while ((cbuf = in.peek()) != EOF)
{
if (func(cbuf))
{
break;
}
cbuf = in.get();
ss << cbuf;
}
std::string str = ss.str();
return str;
}
MathCalculator.cpp
#include "BigInt.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
while (true)
{
BigInt num1, num2, answer;
string oper;
cout << "> ";
cin >> num1;
oper = getTo(cin, isdigit);
string::iterator end_pos = remove(oper.begin(), oper.end(), ' ');
oper.erase(end_pos, oper.end());//移除空格
cin >> num2;
if (oper == "+")
{
// answer = num1 + num2;
}
else
{
cout << "没有这个符号【" << oper << "】" << endl;
continue;
}
cout << num1 << ' ' << oper << ' ' << num2 << " = " << answer << endl;
}
return 0;
}
感谢,我忘了写operator=了
BigInt.h
BigInt& operator=(const BigInt& bigInt);
BigInt.cpp
BigInt& BigInt::operator=(const BigInt& bigInt)
{
this->~BigInt();
this->length = bigInt.length;
this->value = new unsigned char[this->length];
for (size_t i = 0L; i < this->length; i++)
{
this->value[i] = bigInt.value[i];
}
return *this;
}
operator>>里的 value = BigInt(str);这一句再研究哈。