Другие языки программирования и технологии
Программирование c++. Решение задачи
Определить класс string и перегрузить операции ввода/вывода и операцию '+' сложения строк (добавление строки)
#include <iostream>
using namespace std;
class String {
public:
String() : box_(new char[limit]), capacity_(limit), size_(0U) {}
String(const String& str)
: box_(new char[str.size()]), capacity_(str.size()), size_(str.size()) {
for (auto i = 0U; i < size_; ++i) box_[i] = str.box_[i];
}
String(const char* box)
: box_(new char[limit]), capacity_(limit), size_(0U) {
for (auto i = 0U; box[i]; ++i) {
box_[i] = box[i];
++size_;
if (size_ == capacity_) {
capacity_ += limit;
auto tmp = new char[capacity_];
for (auto i = 0U; i < size_; ++i) tmp[i] = box_[i];
delete[] box_;
box_ = move(tmp);
tmp = nullptr;
}
}
}
~String() {
if (box_) {
delete[] box_;
box_ = nullptr;
}
}
size_t size()const { return size_; }
private:
char* box_;
size_t capacity_;
size_t size_;
static const auto limit = 12U;
friend ostream& operator<<(ostream& out, const String& str) {
for (auto i = 0U; i < str.size_; ++i) out << str.box_[i];
return out;
}
friend istream& operator>>(istream& in, String& str) {
auto n = 0U;
while (str.box_[n] = in.get()) {
if (str.box_[n] == '\n' || str.box_[n] == '\0') break;
++str.size_;
if (++n == str.capacity_) {
str.capacity_ += limit;
auto tmp = new char[str.capacity_];
for (auto i = 0U; i < str.size_; ++i) tmp[i] = str.box_[i];
delete[] str.box_;
str.box_ = move(tmp);
tmp = nullptr;
}
}
return in;
}
friend String operator+(String& a, const String& b) {
auto size = a.size_ + b.size_;
auto buffer = size + 1;
auto tmp = new char[buffer];
size_t i;
for (i = 0U; i < a.size_; ++i) tmp[i] = a.box_[i];
auto j = 0U;
do tmp[i] = b.box_[j++]; while (++i < size);
tmp[i] = 0;
String str(tmp);
return str;
}
};
int main() {
cout << "Strig a: ";
String a;
cin >> a;
cout << "Strig b: ";
String b;
cin >> b;
String c = a + b;
cout << "a + b: " << c << '\n';
system("pause");
}
using namespace std;
class String {
public:
String() : box_(new char[limit]), capacity_(limit), size_(0U) {}
String(const String& str)
: box_(new char[str.size()]), capacity_(str.size()), size_(str.size()) {
for (auto i = 0U; i < size_; ++i) box_[i] = str.box_[i];
}
String(const char* box)
: box_(new char[limit]), capacity_(limit), size_(0U) {
for (auto i = 0U; box[i]; ++i) {
box_[i] = box[i];
++size_;
if (size_ == capacity_) {
capacity_ += limit;
auto tmp = new char[capacity_];
for (auto i = 0U; i < size_; ++i) tmp[i] = box_[i];
delete[] box_;
box_ = move(tmp);
tmp = nullptr;
}
}
}
~String() {
if (box_) {
delete[] box_;
box_ = nullptr;
}
}
size_t size()const { return size_; }
private:
char* box_;
size_t capacity_;
size_t size_;
static const auto limit = 12U;
friend ostream& operator<<(ostream& out, const String& str) {
for (auto i = 0U; i < str.size_; ++i) out << str.box_[i];
return out;
}
friend istream& operator>>(istream& in, String& str) {
auto n = 0U;
while (str.box_[n] = in.get()) {
if (str.box_[n] == '\n' || str.box_[n] == '\0') break;
++str.size_;
if (++n == str.capacity_) {
str.capacity_ += limit;
auto tmp = new char[str.capacity_];
for (auto i = 0U; i < str.size_; ++i) tmp[i] = str.box_[i];
delete[] str.box_;
str.box_ = move(tmp);
tmp = nullptr;
}
}
return in;
}
friend String operator+(String& a, const String& b) {
auto size = a.size_ + b.size_;
auto buffer = size + 1;
auto tmp = new char[buffer];
size_t i;
for (i = 0U; i < a.size_; ++i) tmp[i] = a.box_[i];
auto j = 0U;
do tmp[i] = b.box_[j++]; while (++i < size);
tmp[i] = 0;
String str(tmp);
return str;
}
};
int main() {
cout << "Strig a: ";
String a;
cin >> a;
cout << "Strig b: ";
String b;
cin >> b;
String c = a + b;
cout << "a + b: " << c << '\n';
system("pause");
}
Похожие вопросы
- Недавно начал изучать программирование (не с полного нуля), но мои решения задач слишком громоздкое, это нормально?
- Решение задач по программированию в VBA...помогите....Пожалуйста
- Решение задачи на языке программирования С++.
- Помогите с решением задачи на C++
- Программирование задач на языке программирования C++.
- Помогите пожалуйста оптимизировать решение задачи (Зайчик) на C++
- Как в C++ разбить число на цифры и вывести их через пробел? Решение задачи реализовать с помощью конструкции switch.
- Помогите с программированием. Составить решения для задачи С++.
- Стоит ли изучать язык программирования C++ ?И какое преимущество этого языка? Где он мне пригодится ?
- Основные различия языка программирования C# от С++.