Опишите пожалуйста строки(их действие и роль в программе);
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
| #include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
class String{
private:
char *m_Str; // указатель на строку с нулём на коце
int L; // указатель на размер строки
void New( const char *str ); // выделение памяти
void Del(); // удаление памяти
public:
String(); // конструктор по умолчанию
explicit String(const String &str); // конструктор копирования
String( const char * str ); // дополнительный конструктор
~String(); // деструктор
void Set();
void Print();
void ofile();
String & operator=( const String & str );
};
void String::New( const char *str ){
int size = strlen(str);
m_Str = new char[size+1]; // массив для строки символов
L = size; // длинна строки
strcpy( m_Str, str ); // копируем строку
}
void String::Del(){
delete [] m_Str;
;
}
String::String(){ New("\0"); }
String::String(const char *str){ New(str); }
String::String(const String &str){ New(str.m_Str); }
String::~String(){ Del(); }
void String::Print(){
cout << m_Str << '\n';
}
void String::Set(){
Del();
char tmp[1000];
cin.getline( tmp, 1000 );
cin.sync();
New(tmp);
}
void String::ofile(){
ofstream file;
file.open("text.txt",ios::app);
file<<m_Str<<endl;
file.close();
}
String & String::operator =(const String &str){
int j=0;
Del();
char *tmp=new char[str.L];
if(str.L%2==0&&str.L>5){ //проверка условия четности
for (int i=2; i<str.L-2;i++){
tmp[j]=str.m_Str[i];
j++ ;
}
tmp[j]='\0';
New(tmp);
}
else
New("Ne 4etnoe ili malenkaya stroka");
delete [] tmp;
return *this;
};
int main(){
String s1, s2;
s1.Set();
s1.ofile();
s2 = s1; // выполнение задания
s2.Print();
s2.ofile();
getchar(); // пауза
return 0;
} |
|