matlab找零钱,自动售货系统

//注意:1、E004:Pay the balance is beyond the scope biggest指的是投币错误,我之前看出了balance大于10报错,这样理解是错误的

//2、E009 和E10这两个输出后不要换行

//3、按照指示一点点敲代码吧,我写了300行,有点长,但是是自己一点点敲出来的,时间有限,我就懒得再简化了

#include

#include

#include

using namespace std;

vector split(string str, const char c){

vector sstr;

int pos = 0;

while (pos < str.length()){

int end;

for (end = pos; (end < str.length())&&(str[end] != c); end++);

sstr.push_back(str.substr(pos, end - pos));

pos = end + 1;

}

return sstr;

}

void judgeInit(string init,vector>&inf){//info[0]记录商品数量,info[1]记录钱币数量

vector two_init = split(init, ' ');

vectorfirst = split(two_init[0], '-');

vectorsecond = split(two_init[1], '-');

int i = 0;

for (auto e : first){

int n = atoi(e.c_str());

inf[0][i++] = n;

}

i = 0;

for (auto e : second){

int n = atoi(e.c_str());

inf[1][i++] = n;

}

cout << "S001:Initialization is successful" << endl;

}

bool goodsSoldOut(const vector>inf){

for (int i = 0; i < 6; i++){

if (inf[0][i] > 0){

return false;

}

}

return true;

}

void judgePay(int num, int &balance, vector>&inf){//num为硬币面值

if (num != 1 && num != 2 && num != 5 && num != 10){

cout << "E002:Denomination error" << endl;

return;

}

else if (inf[1][0]+inf[1][1]*2 < num){

cout << "E003:Change is not enough, pay fail" << endl;

return;

}

else if (num > 10){//投币大于10元

cout << "E004:Pay the balance is beyond the scope biggest" << endl;

return;

}

else if (goodsSoldOut(inf)){

cout << "E005:All the goods sold out" << endl;

return;

}

else{

switch (num){

case 1:inf[1][0]++; balance += num; break;

case 2:inf[1][1]++; balance += num; break;

case 5:inf[1][2]++; balance += num; break;

case 10:inf[1][3]++; balance += num; break;

}

cout << "S002:Pay success,balance=" << balance << endl;

return;

}

}

void judgeBuy(string good, int& balance, vector>&inf){

//买商品,balance以及相应的商品数量会发生相应的变化。

if (good != "A1" && good != "A2" && good != "A3" && good != "A4" && good != "A5" && good != "A6"){

cout << "E006:Goods does not exist" << endl;

return;

}

else if (good == "A1"){

if (inf[0][0] == 0){//商品没了

cout << "E007:The goods sold out" << endl;

}

else if (balance < 2){//钱不够了

cout << "E008:Lack of balance" << endl;

}

else{//商品还有,钱也够,那就找零钱吧,所以要更新balance的数量,商品的数量,钱币的数量在退币的时候发生变化

balance -= 2;

inf[0][0]--;

cout << "S003:Buy success,balance=" << balance << endl;

}

return;

}

else if (good == "A2"){

if (inf[0][1] == 0){//商品没了

cout << "E007:The goods sold out" << endl;

}

else if (balance < 3){//钱不够了

cout << "E008:Lack of balance" << endl;

}

else{//商品还有,钱也够,那就找零钱吧,所以要更新balance的数量,商品的数量,钱币的数量在退币的时候发生变化

balance -= 3;

inf[0][1]--;

cout << "S003:Buy success,balance=" << balance << endl;

}

return;

}

else if (good == "A3"){

if (inf[0][2] == 0){//商品没了

cout << "E007:The goods sold out" << endl;

}

else if (balance < 4){//钱不够了

cout << "E008:Lack of balance" << endl;

}

else{//商品还有,钱也够,那就找零钱吧,所以要更新balance的数量,商品的数量,钱币的数量在退币的时候发生变化

balance -= 4;

inf[0][2]--;

cout << "S003:Buy success,balance=" << balance << endl;

}

return;

}

else if (good == "A4"){

if (inf[0][3] == 0){//商品没了

cout << "E007:The goods sold out" << endl;

}

else if (balance < 5){//钱不够了

cout << "E008:Lack of balance" << endl;

}

else{//商品还有,钱也够,那就找零钱吧,所以要更新balance的数量,商品的数量,钱币的数量在退币的时候发生变化

balance -= 5;

inf[0][3]--;

cout << "S003:Buy success,balance=" << balance << endl;

}

return;

}

else if (good == "A5"){

if (inf[0][4] == 0){//商品没了

cout << "E007:The goods sold out" << endl;

}

else if (balance < 8){//钱不够了

cout << "E008:Lack of balance" << endl;

}

else{//商品还有,钱也够,那就找零钱吧,所以要更新balance的数量,商品的数量,钱币的数量在退币的时候发生变化

balance -= 8;

inf[0][4]--;

cout << "S003:Buy success,balance=" << balance << endl;

}

return;

}

else if (good == "A6"){

if (inf[0][5] == 0){//商品没了

cout << "E007:The goods sold out" << endl;

}

else if (balance < 6){//钱不够了

cout << "E008:Lack of balance" << endl;

}

else{//商品还有,钱也够,那就找零钱吧,所以要更新balance的数量,商品的数量,钱币的数量在退币的时候发生变化

balance -= 6;

inf[0][5]--;

cout << "S003:Buy success,balance=" << balance << endl;

}

return;

}

}

void refound(int&balance, vector>&inf){

if (balance <= 0){

cout << "E009:Work failure";//注意这里不用换行

return;

}

else{

int a10 = 0,a5=0,a2=0,a1=0;

while (balance>0){

if (balance >= 10){

if (inf[1][3] > 0){

inf[1][3]--;

balance -= 10;

a10++;

}

else if (inf[0][2] > 0){

inf[1][2]--;

balance -= 5;

a5++;

}

else if (inf[1][1] > 0){

inf[1][1]--;

balance -= 2;

a2++;

}

else if (inf[0][0] > 0){

inf[1][0]--;

balance -= 1;

a1++;

}

else{

balance = 0;

break;

}

}

else if(balance>=5){

if (inf[1][2] > 0){//先判断有没有5元硬币

inf[1][2]--;

balance -= 5;

a5++;

}

else if (inf[0][1] > 0){//再判断有没有2元硬币

inf[1][1]--;

balance -= 2;

a2++;

}

else if (inf[1][0] > 0){//再判断有没有1元硬币

inf[1][0]--;

balance -= 1;

a1++;

}

else{//再判断是否终止退币

balance = 0;

break;

}

}

else if (balance >= 2){

if (inf[1][1] > 0){//再判断有没有2元硬币

inf[1][1]--;

balance -= 2;

a2++;

}

else if (inf[1][0] > 0){//再判断有没有1元硬币

inf[1][0]--;

balance -= 1;

a1++;

}

else{//再判断是否终止退币

balance = 0;

break;

}

}

else{//balance >= 1

if (inf[1][0] > 0){//再判断有没有1元硬币

inf[1][0]--;

balance -= 1;

a1++;

}

else{//再判断是否终止退币

balance = 0;

break;

}

}

}

cout << "1 yuan coin number=" <

cout << "2 yuan coin number=" <

cout << "5 yuan coin number=" <

cout << "10 yuan coin number=" <

}

}

void query(string flag, vector>&inf){

if (flag == "0"){

cout << "A1 2 " << inf[0][0] << endl;

cout << "A2 3 " << inf[0][1] << endl;

cout << "A3 4 " << inf[0][2] << endl;

cout << "A4 5 " << inf[0][3] << endl;

cout << "A5 8 " << inf[0][4] << endl;

cout << "A6 6 " << inf[0][5] << endl;

}

else if (flag == "1"){

cout << "1 yuan coin number=" << inf[1][0] << endl;

cout << "2 yuan coin number=" << inf[1][1] << endl;

cout << "5 yuan coin number=" << inf[1][2] << endl;

cout << "10 yuan coin number=" << inf[1][3] << endl;

}

else{

cout << "E010:Parameter error";//注意这个输出不用换行

}

}

int main(){

string inputs;

while (getline(cin, inputs)){

vectorss1 = split(inputs, ';');

vector>info(2,vector(6,0));//info[0]存储A1-A6的饮料数量,info[1]存储1,2,5,10元纸币的数量

int balance = 0;

for (auto e : ss1){

if (e[0] == 'r'){//初始化

judgeInit(e.substr(2), info);//初始化成功

}

else if (e[0] == 'p'){//投入钱币,注意balance和info[1]会因为投币而发生相应变化

int pay = atoi(e.substr(2).c_str());

judgePay(pay, balance, info);

}

else if (e[0] == 'b'){//买物品

string good = e.substr(2);

judgeBuy(good, balance, info);

}

else if (e[0] == 'c'){//退钱

refound(balance, info);

}

else if (e[0] == 'q'){//查询商品

query(e.substr(2), info);

}

}

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值