-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
123 lines (87 loc) · 2.11 KB
/
main.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <fstream>
using namespace std;
const char input[] = "input.txt";
const char output[] = "output.txt";
const int MAX_SIZE = 2000;
struct String
{
public:
void addChar(const char & inputChar) {
if (inputChar == '\n') {
return;
}
this->text[this->index] = inputChar;
this->index++;
}
bool isMarker(const char & inputChar) {
return inputChar == this->marker;
}
void removeInnerSpaces() {
int i = 0;
int firstLetter = -1, lastLetter = -1;
while (!this->isMarker(this->text[i])) {
if (this->text[i] != ' ') {
if (firstLetter == -1) {
firstLetter = i;
}
lastLetter = i;
}
i++;
}
int j = firstLetter + 1;
while (j <= lastLetter) {
if (this->text[j] == ' ' && this->text[j - 1] == ' ') {
this->shiftLeft(j);
lastLetter--;
} else {
j++;
}
}
}
void shiftLeft(int startIndex = 0) {
unsigned i = startIndex;
while(!this->isMarker(this->text[i])) {
this->text[i] = this->text[i+1];
i++;
}
this->index--;
}
void saveToFile(const string& outputFileName) {
ofstream outputFile(outputFileName, ios_base::app);
if (outputFile.is_open()) {
int i = 0;
while (i <= this->index) {
outputFile << this->text[i];
if (this->isMarker(this->text[i])) {
break;
}
i++;
}
outputFile << endl;
outputFile.close();
}
};
private:
char text[MAX_SIZE]{};
int index = 0;
char marker = '@';
};
int main () {
cout << "В тексте из файла ввода будут заменены все группы пробелов на одинарный, например, 'два пробела' станет 'два пробела'\n"
<< "Результат будет записан в output.txt";
ifstream inputFile(input);
ofstream clearFile(output, ios_base::trunc);
clearFile.close();
String line;
if (inputFile.is_open()) {
char inputChar;
while(inputFile >> noskipws >> inputChar) {
line.addChar(inputChar);
}
line.addChar('@');
inputFile.close();
}
line.removeInnerSpaces();
line.saveToFile(output);
}