zoukankan      html  css  js  c++  java
  • 【C语言程序设计试验与习题指导】试验十三 1

    自动售货机

    #include <stdio.h>
    
    /*  通过商品索引 返回价格 */
    int goods_price(int index){
        switch (index) {
            case 1:case 2:case 3:
                return 1;
                break;
            case 4:case 5:
                return 2;
                break;
            case 6:case 7:case 8:
                return 3;
                break;
            case 9:case 10:
                return 3;
                break;
            default:
                return 0;;
        }
    }
    
    
    /*
     in message
     1 1 2 2 5 5 10 10 -1
     1 2 3 5 1 6 9 10 -1
     */
    
    int main(void){
        
        int in_money,cost, num, i;
        
        char *names[] = {"Table-water", "Table-water", "Table-water", "Coca-Cola", "milk",
            "Beer", "Orange-Juice", "Sprite","Oolong-Tra", "Green-Tea"};
        
        int count_num[10] = {0};
        
        in_money = cost = 0;
        
        while (1) {
            scanf("%d", &num);
            if (num != -1) {
                in_money += num;
            }else
                break;
        }
        
        while (1) {
            scanf("%d", &num);
            if (num != -1) {
                cost += goods_price(num);
                count_num[num-1]++;
            }else
                break;
        }
        
        printf("Total: %dyuan, change: %dyuan
    ", in_money, in_money - cost);
        
        for (i = 0; i < 10; i++) {
            if (count_num[i] > 0) {
                printf("%s: %d; ", names[i], count_num[i]);
            }
        }
        puts("");
        
        return 0;
    }
  • 相关阅读:
    字符串和编码
    Python基础
    输入和输出
    Python代码运行助手
    使用文本编辑器
    第一个Python程序
    pycharm中创建并设置docker解释器
    docker 在windows上的使用
    Python time datetime模块
    Linux安装字体文件
  • 原文地址:https://www.cnblogs.com/sidianok/p/15356496.html
Copyright © 2011-2022 走看看