题解 | #自动售货系统#
自动售货系统
http://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
#include <bits/stdc++.h>
using namespace std;
typedef struct Price{
int A1;
int A2;
int A3;
int A4;
int A5;
int A6;
Price():A1(2),A2(3),A3(4),A4(5),A5(8),A6(6){}
}T_Price,*P_Price;
typedef struct Product{
int A1;
int A2;
int A3;
int A4;
int A5;
int A6;
int sum;
Product():A1(0),A2(0),A3(0),A4(0),A5(0),A6(0),sum(0){}
}T_Product,*P_Product;
typedef struct RMB{
int one;
int two;
int five;
int ten;
RMB():one(0),two(0),five(0),ten(0){}
}T_RMB,*P_RMB;
/*******************初始化字符串,保存商品 和 金币 *********************/
vector<string> dispose_all_string(string &str);// 最初处理字符串
vector<int> dispose_str(string &str);// 处理商品和金币字符串
void inital_product_and_rmb(string &str,P_Product product,P_RMB rmb);
vector<string> intital_store(string &str,P_Product product,P_RMB rmb);
/*******************初始化字符串,保存商品 和 金币 *********************/
void pay(int& balance,int rmb,P_RMB p_rmb,P_Product p_prod);//投币函数
void buy(int& balance,string& str,P_Price p_price,P_Product p_prod);
void back(int& balance,P_RMB p_rmb);
void find(string& str,P_Price p_price,P_Product p_prod,P_RMB rmb);
int main(void){
string str;
getline(cin, str);
T_Product product;
T_RMB rmb;
T_Price price;
int balance = 0;
// 初始化商品和剩余金币,operat 1开始保存操作字符
vector<string> operat = intital_store(str,&product,&rmb);
for(int i = 1 ; i < operat.size() ;++i){
switch(operat[i][0]){
case 'p':
operat[i].erase(0, 2);
// int p = atoi(operat[i].c_str());
pay(balance, atoi(operat[i].c_str()), &rmb, &product);
break;
case 'b':
operat[i].erase(0, 2);
buy(balance, operat[i], &price, &product);// A1
break;
case 'c':
back(balance, &rmb);
break;
case 'q':
find(operat[i],&price, &product,&rmb);
break;
}
}
}
void find(string& str,P_Price p_price,P_Product p_prod,P_RMB rmb){
//cout << str <<"++++++"<<endl;
if(str[1] == ' ' && str.length() == 3){
if(str[2] == '0'){
for(int i = 0 ; i < 6 ; ++i)
cout << "A" << i+1 << " " << *((int*)p_price + i)<< " " << *((int*)p_prod + i) << endl;
}else if(str[2] == '1'){
cout << "1 yuan coin number=" << rmb->one << endl
<< "2 yuan coin number=" << rmb->two << endl
<< "5 yuan coin number=" << rmb->five << endl
<< "10 yuan coin number=" << rmb->ten << endl;
}
}else
cout << "E010:Parameter error" << endl;
}
void back(int& balance,P_RMB p_rmb){
if(balance == 0)
cout<< "E009:Work failure"<<endl;
else{
int ten = balance / 10;
if(ten < *((int*)p_rmb + 3)){
*((int*)p_rmb + 3) -= ten;
balance %= 10;
}else{
balance -= *((int*)p_rmb + 3) * 10;
*((int*)p_rmb + 3) = 0;
}
int five = balance / 5;
if(five < *((int*)p_rmb + 2)){
*((int*)p_rmb + 2) -= five;
balance %= 5;
}else{
balance -= *((int*)p_rmb + 2) * 5;
*((int*)p_rmb + 2) = 0;
}
int two = balance / 2;
if(two < *((int*)p_rmb + 1)){
*((int*)p_rmb + 1) -= two;
balance %= 2;
}else{
balance -= *((int*)p_rmb + 1) * 2;
*((int*)p_rmb + 1) = 0;
}
int one = balance ;
if(one < *((int*)p_rmb )){
*((int*)p_rmb) -= one;
balance = 0;
}else{
balance -= *((int*)p_rmb);
*((int*)p_rmb) = 0;
}
cout << "1 yuan coin number=" << one << endl;
cout << "2 yuan coin number=" << two << endl;
cout << "5 yuan coin number=" << five << endl;
cout << "10 yuan coin number=" << ten << endl;
}
}
void buy(int& balance,string& str,P_Price p_price,P_Product p_prod){
//指针操作获取结构体中的数值,两种写法
int *p = (int *)p_prod;
//if(str != "A1" || str != "A2" || str != "A3" || str != "A4" || str != "A5" || str != "A6" ){
if(str[0] != 'A' && str[1] < '7' && str[1] > '0'){
cout << "E006:Goods does not exist"<< endl;
}else if( *(p+(str[1]-'1')) == 0){
cout << "E007:The goods sold out"<<endl;
}else if(balance < *((int *)p_price+(str[1]-'1'))){
cout << "E008:Lack of balance"<<endl;
}else{
balance -= *((int *)p_price+(str[1]-'1'));
-- *(p+(str[1]-'1'));
--p_prod->sum;
cout << "S003:Buy success,balance="<<balance<<endl;
}
}
void pay(int& balance,int rmb,P_RMB p_rmb,P_Product p_prod){
if(rmb == 1 || rmb == 2 || rmb == 5 || rmb == 10){
if(rmb > 2 && (p_rmb->one + p_rmb->two *2) < rmb){
cout << "E003:Change is not enough, pay fail"<<endl;
return;
}
if(p_prod->sum == 0){
cout << "E0E005:All the goods sold out"<<endl;
return;
}
balance += rmb;
if(rmb == 1)++ *(int*)p_rmb;
if(rmb == 2)++ *((int*)p_rmb + 1);
if(rmb == 5)++ *((int*)p_rmb + 2);
if(rmb == 10)++ *((int*)p_rmb + 3);
cout << "S002:Pay success,balance="<<balance<<endl;
}else{
cout << "E002:Denomination error"<<endl;
}
}
vector<string> intital_store(string &str,P_Product product,P_RMB rmb){
//product = Product()
vector<string> init_ = dispose_all_string(str);
inital_product_and_rmb(init_[0],product,rmb);
cout << "S001:Initialization is successful"<<endl;
return init_;
}
vector<string> dispose_all_string(string &str){
vector<string> tmp;
string st;
for(auto &c : str){
if(c != ';')st += c;
else{
tmp.push_back(st);
st = "";
}
}
return tmp;
}
void inital_product_and_rmb(string &str,P_Product product,P_RMB rmb){
string tmp;
vector<string> res;
for(auto &c : str){
if(c != ' ')tmp += c;
else{
res.push_back(tmp);
tmp = "";
}
}
res.push_back(tmp);
vector<int> pro = dispose_str(res[1]);
vector<int> rm = dispose_str(res[2]);
product->A1 = pro[0];
product->A2 = pro[1];
product->A3 = pro[2];
product->A4 = pro[3];
product->A5 = pro[4];
product->A6 = pro[5];
product->sum = pro[0]+pro[1]+pro[2]+pro[3]+pro[4]+pro[5];
rmb->one = rm[0];
rmb->two = rm[1];
rmb->five = rm[2];
rmb->ten = rm[3];
}
vector<int> dispose_str(string &str){
int cnt = 0;
for(auto &c:str){
if(c == '-') ++cnt;
}
++cnt;
vector<int> res_int(cnt,0);
string res_str;
cnt = 0;
for(auto &c:str){
if(c != '-') res_str += c;
else {
res_int[cnt] = atoi(res_str.c_str());
++cnt;
res_str = "";
}
}
res_int[cnt] = atoi(res_str.c_str());
return res_int;
}