-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.cpp
More file actions
209 lines (173 loc) · 4.87 KB
/
printer.cpp
File metadata and controls
209 lines (173 loc) · 4.87 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
#include "printer.h"
#include <map>
#include <stdlib.h>
map<string, vector<Preferential*> > mapPrefer;
map<string, Goods> mapGoods;
double Buy2Handsel1::pay(Item & it)
{
double num = it.getNum(); //get the number of goods
double price = it.getPrice(); //get the price of goods
int lessNum = (num)/3; //buy 2 then handsel 1,so the Dividend is 3
return (num - lessNum)*price;
}
int Buy2Handsel1::saveNumber(Item & it)
{
return (int)(it.getNum()/3);
}
double Discounts::pay(Item & it)
{
int num = it.getNum(); //get the number of goods
int price = it.getPrice(); //get the price of goods
return (num*price*percent);
}
void Discounts::print(double money, double savemoney)
{
cout<<"小计: "<<money<<"(元)"<<",节省"<<savemoney<<"(元)"<<endl;
}
void parseJson(string json, vector<string> & vecresult);
void printTicket(string inputdata)
{
vector<string> vec;
parseJson(inputdata, vec);
map<string, double> itemnum;
map<string, double>::iterator iter;
string code;
double num = 0;
for(int i = 0; i < vec.size(); ++i)
{
//获取条形码
int pos = vec[i].find('-');
if(string::npos != pos) //条形码-数量形式
{
code = vec[i].substr(0, pos);
string strnum = vec[i].substr(pos+1);
num = atof(strnum.c_str());
}
else
{
code = vec[i];
num = 1;
}
if (itemnum.end() == (iter = itemnum.find(code))) //条形码之前不存在
{
itemnum.insert(make_pair<string, int>(code, num));
}
else
{
iter->second = iter->second + num;
}
}
//根据map,迭代创建商品对象
vector<Item *> vecItem;
//开始输出第一部分
cout<<"***<没钱赚商店>购物清单"<<endl;
for(iter = itemnum.begin(); iter != itemnum.end(); ++iter)
{
Item * pItem = new Item(mapPrefer.find(iter->first)->second, mapGoods.find(iter->first)->second, iter->second);
pItem->calculate();
pItem->print();
vecItem.push_back(pItem);
}
cout<<"----------------------------"<<endl;
//开始输出第二部分,是否有买二赠一商品
//顺便统计总算以及节省的钱数
bool isFirst = true;
double paymoney = 0, savemoney = 0;
for (vector<Item *>::iterator it = vecItem.begin(); it != vecItem.end(); ++it)
{
Item * pIt = *it;
paymoney += pIt->getSum();
savemoney += pIt->getSaveMoney();
if (0 != pIt->getSaveNum())
{
if (isFirst)
{
cout<<"买二赠一商品:"<<endl;
isFirst = false;
}
pIt->printSaveNum();
}
}
if (!isFirst)
{
cout<<"----------------------"<<endl;
}
cout<<"总计:"<<paymoney<<"(元)"<<endl;
if ( savemoney > -0.0000001 && savemoney < 0.000001) //double与0比较,近似等于0,说明没有节省
{
;
}
else
{
cout<<"节省:"<<savemoney<<"(元)"<<endl;
}
cout<<"**************************************"<<endl;
//释放对象
for (vector<Item *>::iterator it = vecItem.begin(); it != vecItem.end(); ++it)
{
Item * pIt = *it;
delete pIt;
}
}
void parseJson(string json, vector<string> & vecresult)
{
if ('[' == json[0])
{
int i = 1;
bool start = false;
string str;
while(json[i] != ']')
{
if ('\'' == json[i] && false == start) //如何区分字符串的开始或者结束呢?加1个变量控制
{
//字符串开始
start = true; //标识下一个是写入字符串中的字符
}
else if('\'' != json[i] && start)
{
str.push_back(json[i]);
}
else if('\'' == json[i] && start) //字符串的结束
{
vecresult.push_back(str);
str.clear();
start = false;
}
++i;
}
}
}
int main(int argc, char ** argv)
{
//设置优惠信息
Preferential* pSend = new Buy2Handsel1(40);
Preferential* pDis = new Discounts(0.95, 30);
//设置条形码和优惠之间的关系
vector<Preferential* > vecTemp;
vecTemp.push_back(pSend);
vecTemp.push_back(pDis);
mapPrefer.insert(make_pair<string, vector<Preferential*> >("ITEM000001", vecTemp));
vecTemp.clear();
vecTemp.push_back(pDis);
mapPrefer.insert(make_pair<string, vector<Preferential*> >("ITEM000002", vecTemp));
vecTemp.clear();
vecTemp.push_back(pSend);
mapPrefer.insert(make_pair<string, vector<Preferential*> >("ITEM000003", vecTemp));
vecTemp.clear();
mapPrefer.insert(make_pair<string, vector<Preferential*> >("ITEM000004", vecTemp));
//设置条形码与商品之间的关系
//可口可乐
Goods cole = Goods("可口可乐", "瓶", 3, "ITEM000001");
//羽毛球
Goods tenis = Goods("羽毛球", "个", 1, "ITEM000002");
//苹果
Goods apple = Goods("苹果", "斤", 5.5, "ITEM000003");
//书籍
Goods fight = Goods("奋斗", "本", 28, "ITEM000004");
mapGoods.insert(make_pair<string, Goods>("ITEM000001", cole));
mapGoods.insert(make_pair<string, Goods>("ITEM000002", tenis));
mapGoods.insert(make_pair<string, Goods>("ITEM000003", apple));
mapGoods.insert(make_pair<string, Goods>("ITEM000004", fight));
//根据数据解析打印
printTicket(argv[1]);
}