aboutsummaryrefslogtreecommitdiff
path: root/additional2/в).cpp
blob: 5d3c667be52720013e360934d8537e17b30776b1 (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <cstring>

void resize(char**& text, int& size) {
	char** biggerText = new char*[size * 2];
	for (int i = 0; i < size; i++) {
		biggerText[i] = text[i];
	}
	delete[] text;
	text = biggerText;
	size *= 2;
}

// б) подточка

void skipWhiteSpace(char*& str) {
	while (str[0] == ' ') {
		str++;
	}
}

void extractCommand(char* line, char**& commandAndParameters, int& size) {
	skipWhiteSpace(line);

	if (line[0] != ':') {
		commandAndParameters = nullptr;
		size = 0;
		return;
	}
	line++;

	skipWhiteSpace(line);

	size = 0;
	int reservedSize = 2;
	commandAndParameters = new char*[reservedSize];

	commandAndParameters[0] = new char[2];
	commandAndParameters[0][0] = line[0];
	commandAndParameters[0][1] = '\0';
	size++;


	line++;

	skipWhiteSpace(line);

	// в този цикъл изкарваме конкретните аргументи
	while (line[0] != '\0') {
		// ако commandAndParameters не може да побере още един параметър
		if (size == reservedSize) {
			resize(commandAndParameters, reservedSize);
		}

		// дължината на сегашния параметър
		int parameterLength = 0;
		for (int i = 0; line[i] != ' ' && line[i] != '\0'; i++) {
			parameterLength++;
		}

		// вкарваме сегашния параметър в commandAndParameters
		commandAndParameters[size] = new char[parameterLength+1];
		for (int i = 0; i < parameterLength; i++) {
			commandAndParameters[size][i] = line[i];
		}
		commandAndParameters[size][parameterLength] = '\0';
		size++;

		line += parameterLength;
		skipWhiteSpace(line);
	}
}

// в) подточка

int digitsLength(int num) {
	if (num == 0) return 1;
	int length = 0;
	while (num != 0) {
		num /= 10;
		length++;
	}
	return length;
}

void printNumber(int num, int digitLength) {
	int printZeroes = digitLength - digitsLength(num);
	while (printZeroes > 0) {
		std::cout << "0";
	}
	std::cout << num << " ";
}

void print(char** text, int count) {
	int countLength = digitsLength(count);
	for (int i = 0; i < count; i++) {
		printNumber(i+1, countLength);
		std::cout << text[i] << std::endl;
	}
}

void tryExcessParametersError(int parametersSize, int maxSize) {
	// Изваждаме 1, защото parametersSize включва и самата буква, определяща командата
	if (parametersSize - 1 > maxSize) {
		std::cout << "; Excess parameters" << std::endl;
	}
}

int main() {
	int size = 2;  // колко клетки сме заделили (колко редове можем да вкараме)
	int count = 0; // колко клетки използваме (колко редове сме вкарали)
	char** text = new char*[2];

	char buf[1024];
	std::cin.getline(buf, 1024, '\n');
	while (buf[0] != 'Q') {
		// ако text не може да запази още един ред
		if (count == size) {
			resize(text, size);
		}

		char** commandAndParameters;
		int commandAndParametersSize;
		extractCommand(buf, commandAndParameters, commandAndParametersSize);
		// ако сегашния ред не е команда
		if (commandAndParametersSize == 0) {
			// вкарваме сегашния ред в text
			int lineLength = strlen(buf);

			text[count] = new char[lineLength + 1];
			for (int i = 0; i < lineLength; i++) {
				text[count][i] = buf[i];
			}
			text[count][lineLength] = '\0';
			count++;
		}
		else {
			switch (commandAndParameters[0][0]) {
				// в) подточка
				case 'P': tryExcessParametersError(commandAndParametersSize, 0);
				          print(text, count);
				          break;
				default: std::cout << "; Not an editor command" << std::endl; break;
			}

			// освобождаваме памет
			for (int i = 0; i < commandAndParametersSize; i++) {
				delete[] commandAndParameters[i];
			}
			delete[] commandAndParameters;
		}

		// вземаме следващия ред
		std::cin.getline(buf, 1024, '\n');
	}

	// изкарваме всички редове
	for (int i = 0; i < count; i++) {
		std::cout << text[i] << std::endl;
	}

	// освобождаваме памет
	for (int i = 0; i < count; i++) {
		delete[] text[i];
	}
	delete[] text;
}