Cześć :)
Mam do napisania program, który wczytuje duże liczby oraz wykonuje na nich podstawowe działania arytmetyczne,
- niestety przy dodawaniu wynik wyświetla o jeden znak za dużo, nie wiem gdzie leży problem,
- jak wrzucić przeładowania operatorów << i >> do pliku .cpp z pliki .h?
i kod źródłowy:
.h:
#ifndef bigNumbers_h
#define bigNumbers_h
#include <iostream>
#include <cstdio>
#define STARTSIZE 10
using namespace std;
class Number {
int size, current;
char *digits;
public:
Number();
~Number();
Number(Number& other);
friend ostream& operator<<(ostream & s, const Number & p1) {
char tmpDigit;
for (int i = 0; i < p1.current; i++) {
tmpDigit = p1.digits[i] + '0';
s << tmpDigit;
}
return s;
}
friend istream& operator>>(istream & s, Number & p1) {
char tmpDig;
do {
tmpDig = s.get();
p1.digits[p1.current] = tmpDig - '0';
p1.current++;
if ((p1.current%STARTSIZE) == 0)
p1.reallocSize();
} while (tmpDig != '\n');
return s;
}
Number& operator=(const Number& p1);
Number& operator+(const Number& other) const;
void reallocSize();
};
.cpp:
#include <iostream>
#include <cstdio>
#include "bigNumbers.h"
using namespace std;
Number::Number() {
current = 0;
size = STARTSIZE;
digits = new char[size];
}
Number::~Number() {
delete[] digits;
}
Number::Number(Number& other) {
this->size = other.size;
current = other.current;
for (int i = 0;i < current; i++)
digits[i] = other.digits[i];
}
Number& Number::operator=(const Number& other) {
if (this == &other)
return *this;
if (digits != NULL)
delete[] digits;
size = other.size;
current = other.current;
digits = new char[size];
for (int i = 0; i < current; i++)
digits[i] = other.digits[i];
return *this;
}
Number& Number::operator+(const Number& other) const {
static Number sum;
if (sum.digits != NULL)
delete[] sum.digits;
int tmpCurrent = current, tmpOtherCurrent = other.current, tmpSum = 0;
if (tmpCurrent == tmpOtherCurrent) {
sum.current = current;
sum.size = size;
sum.digits = new char[sum.size];
int last = (sum.current) - 1;
for (int i = last; i >= 0;i--) {
sum.digits[i] = digits[i] + other.digits[i] + tmpSum;
if (sum.digits[i] > 9) {
sum.digits[i] -= 10;
tmpSum = 1;
}
else
tmpSum = 0;
}
if (tmpSum == 1) {
for (int i = sum.current; i > 0; i--) {
sum.digits[i] = sum.digits[i - 1];
}
sum.digits[0] = tmpSum;
sum.current++;
if ((sum.current%STARTSIZE) == 0)
sum.reallocSize();
}
}
return sum;
}
void Number::reallocSize() {
char *tmpDigits;
int tmpSize = this->size;
tmpDigits = new char[size];
for (int i = 0; i < tmpSize;i++)
tmpDigits[i] = digits[i];
size = size + STARTSIZE;
delete[] digits;
digits = new char[size];
for (int i = 0; i < tmpSize;i++)
digits[i] = tmpDigits[i];
}
wynik dodawania:
source.cpp:
#include <iostream>
#include <cstdio>
#include "bigNumbers.h"
using namespace std;
int main() {
Number first, second, third;
cin >> first;
cin >> second;
third = first + second;
cout << "third = first + second:" << endl << third << endl;
system("pause");
return 0;
}
- d780d30815.png (20 KB) - ściągnięć: 142
operator+
zwraca nowy obiekt, a nie referencję.