#include <ctype.h> #include <stdio.h> #include <stdlib.h> #define MAXLEN 1000 int price[] = {2,3,4,5,8,6}; char nm[] = {"A1", "A2", "A3", "A4", "A5", "A6"}; int initialize(char* input, int i, int* shangpin, int* qianbi) { for (int j = 0; j < 6; j++) { int tempnum = input[i] - '0'; if (isdigit(input[i + 1])) tempnum = tempnum * 10 + (input[++i] - '0'); shangpin[j] = tempnum; i += 2; } for (int j = 0; j < 4; j++) { int tempnum = input[i] - '0'; if (isdigit(input[i + 1])) tempnum = tempnum * 10 + (input[++i] - '0'); qianbi[j] = tempnum; i += 2; } printf("S001:Initialization is successful\n"); return (i - 1); } int pay(char* input, int i, int* shangpin, int* qianbi, int* balance) { int bizhong = 4; if (input[i] == '1' && input[i + 1] == '0' && input[i + 2] == ';') { bizhong = 3; i += 2; } else if (input[i] == '5' && input[i + 1] == ';') { bizhong = 2; i++; } else if (input[i] == '2' && input[i + 1] == ';') { bizhong = 1; i++; } else if (input[i] == '1' && input[i + 1] == ';') { bizhong = 0; i++; } else { while (input[i] != ';') i++; printf("E002:Denomination error\n"); return i; } int change = qianbi[0] + qianbi[1] * 2; if (bizhong == 3) { // 投入的是十元,需要查找我们的零钱是否足够10元。 if (change < 10) { printf("E003:Change is not enough, pay fail\n"); return i; } } else if (bizhong == 2) { if (change < 5) { printf("E003:Change is not enough, pay fail\n"); return i; } } int goods = 0; for (int j = 0; j < 6; j++) { goods += shangpin[j]; } if (goods == 0) { printf("E005:All the goods sold out\n"); return i; } qianbi[bizhong]++; if (bizhong == 0) *balance += 1; else if (bizhong == 1) *balance += 2; else if (bizhong == 2) *balance += 5; else *balance += 10; printf("S002:Pay success,balance=%d\n", *balance); return i; } int buy(char* input, int i, int* shangpin, int* qianbi, int* balance) { int type = input[i + 1] - '0'; if (input[i] != 'A' || input[i + 2] != ';' || type > 6 || type < 1) { printf("E006:Goods does not exist\n"); while (input[i] != ';') i++; return i; } type--; if(shangpin[type] == 0) { printf("E007:The goods sold out\n"); } else if(*balance < price[type]) { printf("E008:Lack of balance\n"); } else { *balance -= price[type]; shangpin[type]--; printf("S003:Buy success,balance=%d\n", *balance); } return (i + 2); } void printcoin(int y1, int y2, int y5, int y10) { printf("1 yuan coin number=%d\n", y1); printf("2 yuan coin number=%d\n", y2); printf("5 yuan coin number=%d\n",y5); printf("10 yuan coin number=%d\n",y10); } void change(int* qianbi, int* balance) { int yuan1 = 0; int yuan2 = 0; int yuan5 = 0; int yuan10 = 0; if(*balance != 0) { while(*balance >= 10 && qianbi[3] > 0) { qianbi[3]--; *balance -= 10; yuan10++; } while(*balance >= 5 && qianbi[2] > 0) { qianbi[2]--; *balance -= 5; yuan5++; } while(*balance >= 2 && qianbi[1] > 0) { qianbi[1]--; *balance -= 2; yuan2++; } while(*balance >= 1 && qianbi[0] > 0) { qianbi[0]--; *balance -= 1; yuan1++; } printcoin(yuan1, yuan2, yuan5, yuan10); } else { printf("E009:Work failure\n"); } } int query(char* input, int i, int* shangpin, int* qianbi) { if(input[i] == ' ' && input[i + 1] == '0' && input[i + 2] == ';') { i += 2; for(int j = 0; j < 6; j++) { printf("%s %d %d\n", nm[j], price[j], shangpin[j]); } } else if(input[i] == ' ' && input[i + 1] == '1' && input[i + 2] == ';') { i += 2; } else { while(input[i] != ';') i++; printf("E010:Parameter error\n"); } return i; } int main() { char* input = (char*)malloc(sizeof(char) * MAXLEN); while (fgets(input, MAXLEN, stdin) != NULL) { int shangpin[6] = {0}; int qianbi[4] = {0}; int len = 0; int balance = 0; while (input[len] != '\n') { len++; } input[len] = '\0'; for (int i = 0; i < len; i++) { if (input[i] == 'r') { i = initialize(input, i + 2, shangpin, qianbi); } else if (input[i] == 'p') { i = pay(input, i + 2, shangpin, qianbi, &balance); } else if (input[i] == 'b') { i = buy(input, i + 2, shangpin, qianbi, &balance); } else if (input[i] == 'c') { change(qianbi, &balance); i++; } else if (input[i] == 'q') { i = query(input, i + 1, shangpin, qianbi); } } // printf("%s\n", input); } free(input); return 0; }