-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
214 lines (203 loc) · 5.97 KB
/
input.cpp
File metadata and controls
214 lines (203 loc) · 5.97 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "stdafx.h"
#include "header.h"
using namespace std;
void string_ckecker(string name_dish)
{
if ((name_dish.find('=') == -1) || ((name_dish[name_dish.size() - 1])<'0') || ((name_dish[name_dish.size() - 1])>'9')) // что за 2 и 3 проверки??
{
cout << "Ошибка формата файла." << endl;
system("pause");
exit(-1);
}
}
void isNumberPositive(int num)
{
if (num < 0)
{
cout << "Найдено отрицательное число. Ни одно число не может быть отрицательным." << endl;
system("pause");
exit(-1);
}
}
void isNumberPositive(double num)
{
if (num < 0)
{
cout << "Найдено отрицательное число. Ни одно число не может быть отрицательным." << endl;
system("pause");
exit(-1);
}
}
void isSumCorrect(one_check check, map<string, double> menu)
{
double ctrlSum = 0;
for (list<position>::iterator it = check.positions.begin(); it != check.positions.end(); ++it)
{
ctrlSum += it->count * menu[it->name];
}
if (ctrlSum != check.sum)
{
cout << "В заказе #" << check.number << " неверно посчитана сумма заказа.\nПробито " << check.sum << "руб.\nРеальная стоимость заказа " << ctrlSum << "руб." << endl;
system("pause");
exit(-1);
}
}
map <string, double> input_menu()
{
map <string, double> menu;
map <string, double>::iterator iter;
string file_name, name_dish;
int count;
double price;
cout << "Введите имя файла меню." << endl;
cin >> file_name;
ifstream fin(file_name);
if (!fin.is_open())
{
cout << "Файл меню не найден." << endl;
system("pause");
exit(-1);
}
while (!fin.eof())//чтение менюшки
{
getline(fin, name_dish); // получает строку типа "имя блюда кол-во"
string_ckecker(name_dish);
price = stod(name_dish.substr(name_dish.find_last_of(' = '))); // отрезает число в конце и переводит его в double
isNumberPositive(price);
name_dish = name_dish.substr(0, -2 + name_dish.find_last_of(' '));//отрезает число в конце и оставляет строчку
if (((name_dish.find_last_not_of(" ")) == -1) || (name_dish.find("=") == 0))
{
cout << "Отсутствует наименование блюда в меню." << endl;
system("pause");
exit(-1);
}
iter = menu.find(name_dish);
if (iter == menu.end())
{
menu[name_dish] = price;
}
else
{
cout << "Дублируется наименование в меню." << endl;
system("pause");
exit(-1);
}
}
fin.close();
return menu;
}
list <position> input_kitchen(map <string, double> menu)
{
list <position> kitchen;
string file_name;
int count;
double price;
ifstream fin;
cout << "Введите имя файла кухни." << endl;
cin >> file_name;
fin.open(file_name);
position pos;
bool flag;
if (!fin.is_open())
{
cout << "Файл кухни не найден." << endl;
system("pause");
exit(-1);
}
while (!fin.eof())
{
flag = 0;
getline(fin, pos.name); // получает строку типа "имя блюда кол-во"
string_ckecker(pos.name);
pos.count = stoi(pos.name.substr(pos.name.find_last_of(' = '))); // отрезает число в конце и переводит его в инт
isNumberPositive(pos.count);
pos.name = pos.name.substr(0, -2 + pos.name.find_last_of(' '));//отрезает число в конце и оставляет строчку
if (menu.find(pos.name) == menu.end())
{
cout << "Ошибка структуры файла." << endl;
system("pause");
exit(-1);
}
if (kitchen.empty())
{
kitchen.push_front(pos);
}
else
{
for (std::list<position>::iterator it = kitchen.begin(); it != kitchen.end(); ++it)
{
if ((*it).name == pos.name)
{
(*it).count = (*it).count + pos.count;
flag = 1;//если мы вставили элемент он для нас стал 1
}
}
if (!flag)
{
kitchen.push_front(pos);
}
}
}
fin.close();
return kitchen;
}
list<one_check> input_check(map <string, double> menu)
{
list<one_check> checks;
string file_name;
ifstream fin;
bool flag;
position pos;
cout << "Введите имя файла кассы." << endl;
cin >> file_name;
fin.open(file_name);
if (!fin.is_open())
{
cout << "Файл меню не найден." << endl;
system("pause");
exit(-1);
}
one_check check;
list<one_check>::iterator it;
while (!fin.eof())
{
flag = 0;
getline(fin, pos.name);
check.number = stoi(pos.name.substr(1 + pos.name.find_last_of('№'))); // отрезает число в конце и переводит его в инт
isNumberPositive(check.number);
getline(fin, pos.name);
while ((pos.name.find("итого")) == -1)
{ //чтение позиций
string_ckecker(pos.name);
pos.count = stoi(pos.name.substr(pos.name.find_last_of(' = '))); // отрезает число в конце и переводит его в инт
isNumberPositive(pos.count);
pos.name = pos.name.substr(0, -2 + pos.name.find_last_of(' = '));//отрезает число в конце и оставляет строчку
if (menu.find(pos.name) == menu.end())
{
cout << "Ошибка структуры файла." << endl;
system("pause");
exit(-1);
}
check.positions.push_front(pos);
getline(fin, pos.name); // получает строку типа "имя блюда кол-во"
}
check.sum = stod(pos.name.substr(pos.name.find_last_of(' ')));
isNumberPositive(check.sum);
isSumCorrect(check, menu);
getline(fin, pos.name);
if (pos.name.find("====")==-1)
{
pos.count = stoi(pos.name.substr(pos.name.find_last_of(' ')));
check.sale = pos.count;
getline(fin, pos.name);
}
else
{
check.sale = 0;
}
checks.push_front(check);
check.positions.clear();
}
fin.close();
return checks;
}