- •Приложение р (обязательное) Программный код 6.1.1
- •Приложение с (обязательное) Программный код 6.2.1
- •Приложение т (обязательное) Программный код 6.3.1
- •Приложение у (обязательное) Программный код 7.1.1
- •Приложение ф (обязательное) Программный код 7.2.1
- •Приложение х (обязательное) Программный код 8.1.1
Приложение ф (обязательное) Программный код 7.2.1
#include <iostream>
#include <conio.h>
using namespace std;
struct strochki
{
string a;
};
strochki operator+ (const strochki& X, const strochki& Y)
{
strochki c;
c.a = X.a + Y.a;
return c;
}
void operator += (strochki& k, const strochki& l)
{
k.a += l.a;
}
int main()
{
strochki pervaya, vtoraya;
pervaya.a = "Gutten ";
vtoraya.a = "Morgen ";
strochki tri = pervaya + vtoraya;
cout << tri.a << "\n";
pervaya += vtoraya;
cout << pervaya.a;
_getch();
}
Приложение х (обязательное) Программный код 8.1.1
#include <iostream>
using namespace std;
long int StrLen(char* _str)
{
long int size = 0;
for (; _str[size] != 0; size++);
return size;
}
void StrCpy(char* in_str, char* src_str)
{
for (int i = 0; i < StrLen(in_str); i++)
in_str[i] = src_str[i];
}
class MyString
{
private:
char* str;
long int length;
long int capacity;
public:
MyString()
{
str = NULL;
NewStr();
}
MyString(char* str, int length)
{
this->str = str;
this->length = length;
}
void AddChar(char c)
{
if (length + 1 >= capacity)
{
capacity += 128;
char* newStr = new char[capacity];
if (length > 0)
{
StrCpy(newStr, str);
}
delete[] str;
str = newStr;
}
str[length] = c;
length++;
}
void NewStr()
{
// delete str;
str = NULL;
length = 0;
capacity = 0;
char c;
while ((c = getchar()) != '\n')
{
AddChar(c);
}
}
void display()
{
for (int i = 0; i < length; i++)
cout << str[i];
}
void operator+=(const MyString& rhs)
{
int sz = this -> length + rhs.length;
char* ts = new char[sz];
for (int i = 0; i < this->length; i++)
ts[i] = this->str[i];
for (int i = this->length, j = 0; i <= sz; i++, j++)
ts[i] = rhs.str[j];
delete[] this->str;
this->str = ts;
this->length = sz;
}
MyString& operator+(const MyString& rhs)
{
int sz = this-> length + rhs.length;
char* ts = new char[sz];
for (int i = 0; i < this->length; i++)
ts[i] = this->str[i];
for (int ii = this->length, j = 0; ii <= sz; ii++, j++)
ts[ii] = rhs.str[j];
MyString a(ts, sz);
return a;
}
void GetStrPtr()
{
cout << &this->str;
}
};
int main(int argc, char** argv) {
MyString s1;
s1.display();
cout << endl;
MyString s2;
s2.display();
cout << endl;
s1 += s2;
s1.display();
cout << endl;
s2.display();
cout << endl;
s1.NewStr();
s1.display();
cout << endl;
s2 = s2 + s1;
s2.display();
cout << endl;
s2.NewStr();
s2.display();
cout << endl;
s1.GetStrPtr();
cout << endl;
s2.GetStrPtr();
cin.get();
return 0;
}
Москва 2020