Написал свою реализацию vector
с функциями, которые мне были нужны. При вызове деструктора в строке delete data
выбрасывается исключение. При попытке продолжить Crt
выдает ошибку:
Expression: _CrtIsValidHeapPointer (block).
Уже голову сломал, а все равно не пойму, где я налажал.
#pragma once
#include <iostream>
class MyVector {
int* Data;
int size;
public:
MyVector() {
size = 0;
}
MyVector(int Msize) {
size = Msize;
Data = new int[size];
for (int i = 0; i < size; i++) {
Data[i] = 0;
}
}
~MyVector() {
delete Data;
}
int& operator[] (int index) {
if (index >= 0 && index < size) {
return Data[index];
}
else {
std::cout << "ошибка доступа" << std::endl;
return Data[0];
}
}
int& operator[] (int index) const {
if (index >= 0 && index < size) {
return Data[index];
}
else {
std::cout << "ошибка доступа" << std::endl;
return Data[0];
}
}
int Size() {
return size;
}
void push_back(int Number) {
size++;
int* tempData = new int[size];
for (int i = 0; i < size - 1; i++) {
tempData[i] = Data[i];
}
delete[] Data;
tempData[size - 1] = Number;
Data = tempData;
}
void pop_back() {
size--;
int* tempData = new int[size];
for (int i = 0; i < size; i++) {
tempData[i] = Data[i];
}
delete[] Data;
Data = tempData;
}
};
Класс который использует MyVector
:
#pragma once
#include "MyVector.h"
#include <string>
using namespace std;
class BigInt
{
public:
friend std::istream& operator>> (std::istream &in, BigInt &Number);
friend std::ostream& operator<< (std::ostream &out, const BigInt &Number);
void Input();
void Output();
void Add(BigInt Number1, BigInt Number2);
void Sub(BigInt Number1, BigInt Number2);
void Mul(BigInt Number1, BigInt Number2);
void Div(BigInt Number1, BigInt Number2);
BigInt& operator + (BigInt Number2);
BigInt& operator = (BigInt Number2);
BigInt& operator - (BigInt Number2);
BigInt& operator * (BigInt Number2);
BigInt& operator / (BigInt Number2);
BigInt& operator += (BigInt Number2);
BigInt& operator -= (BigInt Number2);
BigInt& operator *= (BigInt Number2);
BigInt& operator /= (BigInt Number2);
BigInt& operator - ();
bool operator > (BigInt Number2);
bool operator < (BigInt Number2);
bool operator >= (BigInt Number2);
bool operator <= (BigInt Number2);
bool operator == (BigInt Number2);
bool operator != (BigInt Number2);
BigInt& operator++ ();
BigInt operator++ (int);
BigInt& operator-- ();
BigInt operator-- (int);
BigInt(long long xxx);
BigInt();
~BigInt();
private:
int length;
void SearchZero();
const int ChunkSize = 1;
MyVector Number;
int Sign;
};
.сpp класса:
#include "BigInt.h"
#include <iostream>
#include <string>
#include "MyVector.h"
using namespace std;
void BigInt::Input()
{
bool correct = false;
string str;
while (!correct)
{
cout << "Введите корректное число:" << endl;
cin >> str;
correct = true;
if (str.length() == 1) {
if (((str[0] < 48) || (str[0] > 57)) && (str[0] != 45)) {
correct = false;
}
}
else {
for (int i = 0; i < str.length(); i++)
{
if (i == 0)
{
if (((str[i] < 49) || (str[i] > 57)) && (str[i] != 45))
{
correct = false;
}
}
else
{
if ((str[i] < 48) || (str[i] > 57))
{
correct = false;
}
}
}
}
}
if (str[0] == '-')
{
Sign = -1;
str.erase(str.begin());
}
else
{
Sign = 1;
}
for (int i = str.length() - ChunkSize; i > 0; i -= ChunkSize)
{
Number.push_back(stoi(str.substr(i, ChunkSize)));
}
Number.push_back(stoi(str.substr(0, 1)));
length = Number.Size();
}
void BigInt::Output()
{
if (Sign == -1) {
cout << '-';
}
for (int i = length - 1; i >= 0; i--)
{
cout << Number[i];
}
if (length == 0) {
cout << 0;
}
}
void BigInt::SearchZero() {
length = Number.Size();
for (int i = length - 1; i >= 0; i--) {
if (Number[i] != 0) {
break;
}
if (Number[i] == 0) {
Number.pop_back();
}
}
length = Number.Size();
}
void BigInt::Add(BigInt Number1, BigInt Number2)
{
if ((Number1.Sign == 1) && (Number2.Sign == 1))
{
bool FirstIsBigger = false;
if (Number1.length > Number2.length)
{
FirstIsBigger = true;
}
if (FirstIsBigger)
{
for (int i = 0; i < Number1.length; i++)
{
if (i < Number2.length)
{
Number1.Number[i] = Number1.Number[i] + Number2.Number[i];
}
if (Number1.Number[i] >= 10)
{
Number1.Number[i] = Number1.Number[i] - 10;
if (i == Number1.length - 1) {
Number1.Number.push_back(1);
}
else {
Number1.Number[i + 1] += 1;
}
}
}
Number = Number1.Number;
length = Number1.length;
this->SearchZero();
Sign = Number1.Sign;
}
else
{
for (int i = 0; i < Number2.length; i++)
{
if (i < Number1.length)
{
Number2.Number[i] = Number1.Number[i] + Number2.Number[i];
}
if (Number2.Number[i] >= 10)
{
Number2.Number[i] = Number2.Number[i] - 10;
if (i == Number2.length - 1) {
Number2.Number.push_back(1);
}
else {
Number2.Number[i + 1] += 1;
}
}
}
Number = Number2.Number;
length = Number2.length;
this->SearchZero();
Sign = Number2.Sign;
}
}
if ((Number1.Sign == -1) && (Number2.Sign == 1))
{
Number1.Sign = 1;
this->Sub(Number2, Number1);
}
if ((Number1.Sign == 1) && (Number2.Sign == -1))
{
Number2.Sign = 1;
this->Sub(Number1, Number2);
}
if ((Number1.Sign == -1) && (Number2.Sign == -1))
{
Number1.Sign = 1;
Number2.Sign = 1;
this->Add(Number1, Number2);
this->Sign = -1;
}
}
void BigInt::Sub(BigInt Number1, BigInt Number2)
{
if ((Number1.Sign == 1) && (Number2.Sign == 1))
{
bool FirstIsBigger = false;
if (Number1.length > Number2.length)
{
FirstIsBigger = true;
}
if (Number1.length == Number2.length) {
int countDischarge = Number1.length - 1;
while (countDischarge >= 0) {
if (Number1.Number[countDischarge] != Number2.Number[countDischarge]) {
if (Number1.Number[countDischarge] > Number2.Number[countDischarge]) {
FirstIsBigger = true;
break;
}
else {
FirstIsBigger = false;
break;
}
}
countDischarge--;
}
}
if (FirstIsBigger)
{
for (int i = 0; i < Number1.length; i++)
{
if (i < Number2.length)
{
Number1.Number[i] = Number1.Number[i] - Number2.Number[i];
}
if (Number1.Number[i] < 0)
{
Number1.Number[i] = Number1.Number[i] + 10;
Number1.Number[i + 1] -= 1;
}
}
Number = Number1.Number;
length = Number1.length;
this->SearchZero();
Sign = 1;
}
else
{
for (int i = 0; i < Number2.length; i++)
{
if (i < Number1.length)
{
Number2.Number[i] = Number2.Number[i] - Number1.Number[i];
}
if (Number2.Number[i] < 0)
{
Number2.Number[i] = Number2.Number[i] + 10;
Number2.Number[i + 1] -= 1;
}
}
Number = Number2.Number;
length = Number2.length;
this->SearchZero();
if (length != 0) {
Sign = -1;
}
else {
Sign = 1;
}
}
}
if ((Number1.Sign == -1) && (Number2.Sign == 1))
{
Number1.Sign = 1;
this->Add(Number1, Number2);
Sign = -1;
}
if ((Number1.Sign == 1) && (Number2.Sign == -1))
{
Number2.Sign = 1;
this->Add(Number1, Number2);
}
if ((Number1.Sign == -1) && (Number2.Sign == -1))
{
Number2.Sign = 1;
this->Add(Number2, Number1);
}
}
void BigInt::Mul(BigInt Number1, BigInt Number2) {
int maxLength = 0;
bool FirstIsBigger = false;
if (Number1.length <= Number2.length) {
maxLength = Number2.length;
FirstIsBigger = false;
}
else {
maxLength = Number1.length;
FirstIsBigger = true;
}
MyVector tempNumber(2 * maxLength);
MyVector temp(2 * maxLength);
for (int i = 0; i < 2 * maxLength; i++) {
for (int i = 0; i < temp.Size(); i++) {
temp[i] = 0;
}
if (FirstIsBigger) {
for (int j = 0; j < Number2.length; j++) {
if (i < maxLength) {
temp[j] = Number2.Number[j] * Number1.Number[i];
}
}
for (int j = 0; j < Number2.length; j++) {
while (temp[j] >= 10) {
temp[j + 1] += 1;
temp[j] -= 10;
}
}
}
else {
for (int j = 0; j < Number1.length; j++) {
if (i < maxLength) {
temp[j] = Number1.Number[j] * Number2.Number[i];
}
}
for (int j = 0; j < Number2.length; j++) {
while (temp[j] > 10) {
temp[j + 1] += 1;
temp[j] -= 10;
}
}
}
for (int k = 0; k < i; k++) {
for (int m = 2 * maxLength - 2; m >= 0; m--) {
temp[m + 1] = temp[m];
if (m == 0) {
temp[m] = 0;
}
}
}
for (int i = 0; i < 2 * maxLength; i++)
{
if (i < 2 * maxLength)
{
tempNumber[i] = tempNumber[i] + temp[i];
}
if (tempNumber[i] >= 10) {
tempNumber[i + 1] += 1;
tempNumber[i] -= 10;
}
}
}
Number = tempNumber;
if (Number1.Sign != Number2.Sign) {
Sign = -1;
}
else {
Sign = 1;
}
this->SearchZero();
length = Number.Size();
}
void BigInt::Div(BigInt Number1, BigInt Number2) {
if (Number2 == 0) {
cout << "ошибка деления" << endl;
*this = Number1;
}
else {
int temp;
*this = BigInt(0);
if (Number1.Sign != Number2.Sign) {
temp = -1;
}
else {
temp = 1;
}
Number1.Sign = 1; Number2.Sign = 1;
Number1 -= Number2;
while (Number1 >= 0) {
++*this;
Number1 -= Number2;
}
this->Sign = temp;
}
}
BigInt &BigInt::operator+ (BigInt Number2) {
this->Add(*this, Number2);
return *this;
}
BigInt& BigInt::operator - (BigInt Number2) {
this->Sub(*this, Number2);
return *this;
}
BigInt& BigInt::operator * (BigInt Number2) {
this->Mul(*this, Number2);
return *this;
}
BigInt& BigInt::operator / (BigInt Number2) {
this->Div(*this, Number2);
return *this;
}
BigInt& BigInt::operator /= (BigInt Number2) {
this->Div(*this, Number2);
return *this;
}
BigInt &BigInt::operator*= (BigInt Number2) {
this->Mul(*this, Number2);
return *this;
}
BigInt &BigInt::operator+= (BigInt Number2) {
this->Add(*this, Number2);
return *this;
}
BigInt& BigInt::operator -= (BigInt Number2) {
this->Sub(*this, Number2);
return *this;
}
bool BigInt::operator == (BigInt Number2) {
BigInt temp = *this;
temp -= Number2;
int i = temp.length - 1;
while (i >= 0) {
if (temp.Number[i] != 0) {
return false;
}
i--;
}
return true;
}
bool BigInt::operator != (BigInt Number2) {
if (*this == Number2) {
return false;
}
else {
return true;
}
}
bool BigInt::operator > (BigInt Number2) {
BigInt temp = *this;
temp -= Number2;
if (temp == 0) {
return false;
}
else {
if (temp.Sign == 1) {
return true;
}
else {
return false;
}
}
}
bool BigInt::operator >= (BigInt Number2) {
BigInt temp = *this;
temp -= Number2;
if (temp == 0) {
return true;
}
else {
if (temp.Sign == 1) {
return true;
}
else {
return false;
}
}
}
bool BigInt::operator < (BigInt Number2) {
if (*this >= Number2) {
return false;
}
else {
return true;
}
}
bool BigInt::operator <= (BigInt Number2) {
if (*this > Number2) {
return false;
}
else {
return true;
}
}
BigInt& BigInt::operator ++ () {
BigInt Number = 1;
this->Add(*this, Number);
return *this;
}
BigInt BigInt::operator ++ (int) {
BigInt temp = *this;
++(*this);
return temp;
}
BigInt& BigInt::operator-- () {
BigInt Number = 1;
this->Sub(*this, Number);
return *this;
}
BigInt BigInt::operator-- (int) {
BigInt temp = *this;
--(*this);
return temp;
}
BigInt& BigInt::operator - () {
this->Sign *= -1;
return *this;
}
BigInt& BigInt::operator = (BigInt Number2) {
this->Sign = Number2.Sign;
this->Number = Number2.Number;
this->length = Number2.length;
return *this;
}
BigInt::BigInt(long long xxx)
{
if (xxx < 0)
{
Sign = -1;
}
else
{
Sign = 1;
}
xxx *= Sign;
int ost;
while (xxx != 0)
{
ost = xxx % 10;
Number.push_back(ost);
xxx /= 10;
}
length = Number.Size();
}
BigInt::BigInt()
{
}
BigInt::~BigInt() {
}
std::ostream& operator<< (std::ostream &out, const BigInt &Number) {
if (Number.Sign == -1) {
out << '-';
}
for (int i = Number.length - 1; i >= 0; i--)
{
out << Number.Number[i];
}
if (Number.length == 0) {
out << 0;
}
return out;
}
std::istream& operator>> (std::istream &in, BigInt &Number) {
bool correct = false;
string str;
while (!correct)
{
cout << "Введите корректное число:" << endl;
in >> str;
correct = true;
if (str.length() == 1) {
if (((str[0] < 48) || (str[0] > 57)) && (str[0] != 45)) {
correct = false;
}
}
else {
for (int i = 0; i < str.length(); i++)
{
if (i == 0)
{
if (((str[i] < 49) || (str[i] > 57)) && (str[i] != 45))
{
correct = false;
}
}
else
{
if ((str[i] < 48) || (str[i] > 57))
{
correct = false;
}
}
}
}
}
if (str[0] == '-')
{
Number.Sign = -1;
str.erase(str.begin());
}
else
{
Number.Sign = 1;
}
for (int i = str.length() - Number.ChunkSize; i > 0; i -= Number.ChunkSize)
{
Number.Number.push_back(stoi(str.substr(i, Number.ChunkSize)));
}
Number.Number.push_back(stoi(str.substr(0, 1)));
Number.length = Number.Number.Size();
return in;
}
main():
#include <iostream>
#include "BigInt.h"
#include <locale.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "RUS");
BigInt N1, N2;
cin >> N2 >> N1;
cout << "Результат:" << endl;
cout << N2/N1;
return 0;
}