-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
128 lines (114 loc) · 2.63 KB
/
main.cpp
File metadata and controls
128 lines (114 loc) · 2.63 KB
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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
struct Row {
Row() = default;
Row(std::vector<std::string>&& cells);
std::vector<std::string> cells;
};
Row::Row(std::vector<std::string>&& cells) : cells(cells)
{
}
struct Table {
std::vector<Row> data;
Table() = default;
void run();
void input();
void modify();
void output();
};
void Table::input() {
std::string input_data("");
// Input data
while (true) {
std::cout << "Enter new line or q to finish input\n";
std::getline(std::cin, input_data);
if (input_data == "q")
break;
// C++20
// if (!input_data.ends_with("\t"))
// input_data.push_back("\t");
if (input_data.back() != '\t')
input_data.push_back('\t');
size_t position = 0;
std::vector<std::string> cells;
while (!input_data.empty()) {
position = input_data.find("\t");
cells.push_back(input_data.substr(0, position));
input_data.erase(0, ++position);
}
this->data.emplace_back(std::move(cells));
}
}
void Table::modify() {
// Edit data
std::string input_data;
size_t string_num = 0;
size_t cell_num = 0;
std::cout << "Enter string's number or q to exit\n";
std::cin >> input_data;
if (input_data == "q")
return;
try {
string_num = std::stoi(input_data);
}
catch (std::invalid_argument) {
std::cout << "Incorrect input!\n";
return;
}
if (string_num > this->data.size()) {
std::cout << "Incorrect input";
string_num = 0;
}
std::cout << "Enter cell's number or q to exit\n";
std::cin >> input_data;
if (input_data == "q")
return;
try {
cell_num = std::stoi(input_data);
}
catch (std::invalid_argument) {
std::cout << "Incorrect input!\n";
return;
}
if (cell_num > this->data[string_num].cells.size()) {
std::cout << "Incorrect input";
cell_num = 0;
}
std::cout << "Enter new cell's data or q to exit\n";
std::cin >> input_data;
if (input_data == "q")
return;
else
this->data[string_num].cells[cell_num] = input_data;
}
void Table::output() {
for (auto& string:this->data) {
for (auto& cell:string.cells) {
std::cout << cell << "\t";
}
std::cout << "\n";
}
}
void Table::run() {
input();
std::string input_data;
while (true) {
std::cout << "Enter m for modify, o for output or q for exit\n";
std::cin >> input_data;
if (input_data == "q")
break;
else if (input_data == "m")
modify();
else if (input_data == "o")
output();
else
std::cout << "Command not found";
}
}
int main() {
Table table;
table.run();
}