#include <iostream>
#include <assert.h>
#include <locale>
#include <string.h>
using namespace std;
class Decimal {
// перегрузка операторов вывода и ввода
friend ostream &operator<<(ostream &, const Decimal &);
friend istream &operator>>(istream &, Decimal &);
public:
Decimal(int = 5);
Decimal(const Decimal &);
private:
unsigned char *ptr;
int size;
};
// digit(12345)
Decimal::Decimal(int arraySize) {
size = arraySize;
ptr = new unsigned char[size];
assert(ptr != 0);
for (int i = 0; i < size; i++)
ptr[i] = '0';
}
// digit("12345")
Decimal::Decimal(const Decimal &init) {
size = init.size;
ptr = new unsigned char[size];
assert(ptr != 0);
for (int i = 0; i < size; i++)
ptr[i] = init.ptr[i];
}
// cin
istream &operator>>(istream &input, Decimal &a) {
for (int i = 0; i < a.size; i++)
input >> a.ptr[i];
return input;
}
// cout
ostream &operator<<(ostream &output, const Decimal &a) {
for (int i = 0; i < a.size; i ++) {
output << a.ptr[i];
if ((i + 1) % 10 == 0)
output << endl;
}
return output;
}
int main() {
setlocale(0, "");
Decimal x, y;
cout << "Введите Х: ";
cin >> x;
cout << "Введите Y: ";
cin >> y;
cout << "\n";
return 0;
}
У меня при вводе ограничивается ввод до 5-ти символов, а я хочу ввести любое число, любого количества
#include<iterator>
#include<iostream>
#include<algorithm>
#include<memory>
using namespace std;
class Decmal
{
public:
explicit Decmal(size_t size = 5);
Decmal(const Decmal&);
size_t get_size() const
{
return size;
}
// ...
private:
size_t size;
unique_ptr<char> data;
// ...
private:
void resize(size_t sz);
friend istream& operator>>(istream& is, Decmal& dec);
friend ostream& operator<<(ostream& os, const Decmal& dec);
};
Decmal::Decmal(size_t size) : size{ size }, data(new char[size])
{
fill_n(data.get(), size, '0');
}
Decmal::Decmal(const Decmal& rh)
{
size = rh.size;
data.reset(new char[size]);
copy_n(rh.data.get(), size, data.get());
}
void Decmal::resize(size_t new_size)
{
if(size < new_size)
{
data.reset(new char[new_size]);
size = new_size;
}
}
istream& operator>>(istream& is, Decmal& dec)
{
is.putback(is.get());
size_t new_size = static_cast<size_t>(is.rdbuf()->in_avail() - 1);
dec.resize(new_size);
copy_n(istream_iterator<char>{is}, dec.size, dec.data.get());
return is;
}
ostream& operator<<(ostream& os, const Decmal& dec)
{
copy_n(dec.data.get(), dec.size, ostream_iterator<char>{os});
return os;
}
int main()
{
Decmal x;
cin >> x;
cout << x.get_size() << " " << x << endl;
system("pause");
}
Не совсем понял принцип работы этого кода.
#include<iterator>
#include<iostream>
#include<algorithm>
using namespace std;
class Decmal
{
public:
explicit Decmal(size_t size = 5);
Decmal(const Decmal&);
~Decmal(){ delete[] data; }
size_t get_size() const
{
return size;
}
// ...
private:
size_t size;
unsigned char* data = nullptr;
// ...
private:
void resize(size_t sz);
friend istream& operator>>(istream& is, Decmal& dec);
friend ostream& operator<<(ostream& os, const Decmal& dec);
};
Decmal::Decmal(size_t size) : size{ size }, data(new unsigned char[size])
{
fill_n(data, size, '0');
}
Decmal::Decmal(const Decmal& rh)
{
size = rh.size;
delete[] data;
data = new unsigned char[size];
copy_n(rh.data, size, data);
}
void Decmal::resize(size_t new_size)
{
if(size < new_size)
{
delete[] data;
data = new unsigned char[new_size];
size = new_size;
}
}
istream& operator>>(istream& is, Decmal& dec)
{
char c = 0;
size_t sz = 0;
while(is.read(&c, 1) && c != '\n') // \n == 10 \r == 13
{
if(sz == dec.size)
{
unsigned char* tmp = new unsigned char[sz + 1];
copy_n(dec.data, dec.size, tmp);
dec.resize(sz + 1);
copy_n(tmp, dec.size, dec.data);
delete[] tmp;
}
dec.data[sz] = c;
++sz;
}
return is;
}
ostream & operator<<(ostream& os, const Decmal& dec)
{
copy_n(dec.data, dec.size, ostream_iterator<char>{os});
return os;
}
int main()
{
Decmal x;
cin >> x;
cout << x.get_size() << " " << x << "\n";
Decmal y = x;
cout << y << endl;
system("pause");
}