一遍过,用的笨方法写,部分代码可以优化,逻辑都挺简单的,就是题目要求多且复杂,代码运行时空效率都还可以
#include <stdio.h>
#include <string.h>
#define goods_nums 6//定义商品种数
#define money_nums 4//定义纸币种数
/*规格说明>>*/
char goods_name[goods_nums][4]={"A1","A2","A3","A4","A5","A6"};//规定商品名称
int goods_val[goods_nums]={2,3,4,5,8,6};//规定商品价格
int money_name[money_nums]={1,2,5,10};//规定可接受钱币面值
int goods[goods_nums]={0};//存储各商品数
int money[money_nums]={0};//存储各面值纸币数
/*<<规格说明*/
int balance=0;//投币余额
char name[4]={0};//用于存储购买商品时输入的商品名称

void getname(){//获取想要购买的商品名称
    char c;int i=0;
    while(scanf("%c",&c)!=EOF){
        if(c==' '){
            continue;
        }
        if(c!=';'){
            name[i++]=c;
        }
        else{
            break;
        }
    }
}
int getnums(){//获取数字
    char c;int t,number=0;
    while(scanf("%c",&c)!=EOF){
        if(c>='0' && c<='9'){
            t=c-'0';
            number=number*10+t;
        }
        else{
            break;
        }
    }
    return number;
}
int getmoney(int* a){//获取存钱盒1元、2元总余额
    int n=a[0]+2*a[1];
    return n;
}
void Initialization(int* a,int nums){//初始化商品数量或者初始化纸币数
    char c,t;int flag=0,i=0;
    while(scanf("%c",&c)!=EOF){
        if(c>='0' && c<='9'){
            if(flag){
                flag=0;
                a[i++]=(t-'0')*10+c-'0';
            }
            else{
                flag=1;
                t=c;
                continue;
            }
        }
        if(flag){
            flag=0;
            a[i++]=t-'0';
        }
        if(i==nums){
            break;
        }
    }
}
void p(){//投币
    char c;int n,flag=0;
    while(scanf("%c",&c)!=EOF){
        n=getnums();
        if(n!=1 && n!=2 && n!=5 && n!=10){
            printf("E002:Denomination error\n");
            break;
        }
        if(n>2 && n>=getmoney(money)){
            printf("E003:Change is not enough, pay fail\n");
            break;
        }
        for(int i=0;i<goods_nums;i++){
            if(goods[i]){
                flag=1;
                break;
            }
        }
        if(!flag){
            printf("E005:All the goods sold out\n");
            break;
        }
        else{
            balance+=n;
            for(int i=0;i<money_nums;i++){
                if(n==money_name[i]){
                    money[i]++;
                    break;
                }
            }
            printf("S002:Pay success,balance=%d\n",balance);
            break;
        }
    }
}
void buy(){//购买
    int flag=-1;
    getname();
    for(int i=0;i<goods_nums;i++){
        if(strcmp(name,goods_name[i])==0){
            flag=i;
            memset(name,0,sizeof(name));
            break;
        }
    }
    if(flag==-1){
        printf("E006:Goods does not exist\n");
        return;
    }
    if(!goods[flag]){
        printf("E007:The goods sold out\n");
        return;
    }
    if(balance<goods_val[flag]){
        printf("E008:Lack of balance\n");
        return;
    }
    else{
        balance-=goods_val[flag];
        goods[flag]--;
        printf("S003:Buy success,balance=%d\n",balance);
    }
    return;
}
void repay(){//退币
    if(!balance){
        printf("E009:Work failure\n");
    }
    else{
        int n[money_nums];
        for(int i=money_nums-1;i>-1;i--){//遵循最少退币原则,从最大面值开始退
            n[i]=balance/money_name[i];//用于各个面值的存储退币数量
            if(n[i]>=1){
                if(n[i]<=money[i]){
                    balance%=money_name[i];
                }
                else{//存钱盒当前面值货币不足时有多少退多少
                    n[i]=money[i];
                    balance-=money_name[i]*n[i];
                }
                money[i]-=n[i];//更新存钱盒
            }
        }
        for(int i=0;i<money_nums;i++){
            printf("%d yuan coin number=%d\n",money_name[i],n[i]);//打印输出
        }
        balance=0;//清空余额
    }
}
void search(){//查询
    char c;
    scanf("%c",&c);
    if(c!=' '){
        printf("E010:Parameter error\n");
        return;
    }
    scanf("%c",&c);
    if(c=='0'){
        int t=0,flag=0,max1,max2=999;
        int n[goods_nums]={0};//用于从大到小存储商品出现过的数量
        for(int i=0;i<goods_nums;i++){
            max1=-1;
            for(int j=0;j<goods_nums;j++){
                if(max1<goods[j] && goods[j]<max2){
                    flag=1;
                    max1=goods[j];
                }
            }
            if(flag){
                flag=0;
                n[t++]=max1;
                max2=max1; 
            }
        }
        for(int i=0;i<t;i++){
            for(int j=0;j<goods_nums;j++){
                if(goods[j]==n[i]){
                    printf("%s %d %d\n",goods_name[j],goods_val[j],goods[j]);
                }
            }
        }
    }
    else if(c=='1'){
        for(int i=0;i<money_nums;i++){
            printf("%d yuan coin number=%d\n",money_name[i],money[i]);//打印输出
        }
    }
    else{
        printf("E010:Parameter error\n");
    }
}
void cmd(){
    char c;
    while(scanf("%c",&c)!=EOF){
        if(c=='r'){
            Initialization(goods,goods_nums);
            Initialization(money,money_nums);
            printf("S001:Initialization is successful\n");
        }
        else if(c=='p'){
            p();
        }
        else if(c=='b'){
            buy();
        }
        else if(c=='c'){
            repay();
        }
        else if(c=='q'){
            search();
        }
    }
}

int main(){
    cmd();
}