zoukankan      html  css  js  c++  java
  • 浙大版《C语言程序设计(第3版)》题目集 练习3-8 查询水果价格 (15 分)

    练习3-8 查询水果价格 (15 分)

    给定四种水果,分别是苹果(apple)、梨(pear)、桔子(orange)、葡萄(grape),单价分别对应为3.00元/公斤、2.50元/公斤、4.10元/公斤、10.20元/公斤。

    首先在屏幕上显示以下菜单:

    [1] apple
    [2] pear
    [3] orange
    [4] grape
    [0] exit
    

    用户可以输入编号1~4查询对应水果的单价。当连续查询次数超过5次时,程序应自动退出查询;不到5次而用户输入0即退出;输入其他编号,显示价格为0。

    输入格式:

    输入在一行中给出用户连续输入的若干个编号。

    输出格式:

    首先在屏幕上显示菜单。然后对应用户的每个输入,在一行中按格式“price = 价格”输出查询结果,其中价格保留两位小数。当用户连续查询次数超过5次、或主动输入0时,程序结束。

    输入样例1:

    3 -1 0 2
    

    输出样例1:

    [1] apple
    [2] pear
    [3] orange
    [4] grape
    [0] exit
    price = 4.10
    price = 0.00
    

    输入样例2:

    1 2 3 3 4 4 5 6 7 8
    

    输出样例2:

    [1] apple
    [2] pear
    [3] orange
    [4] grape
    [0] exit
    price = 3.00
    price = 2.50
    price = 4.10
    price = 4.10
    price = 10.20

    思路:多种选项时适合用switch语句。

    代码如下:
    #include<stdio.h>
    int main()
    {
        int choice, i;
        double price;
    	
        printf("[1] apple
    ");
        printf("[2] pear
    ");
        printf("[3] orange
    ");
        printf("[4] grape
    ");
        printf("[0] exit
    ");
        for (i=1;i<=5;i++)
        {
    		scanf("%d",&choice);
    		if (choice==0)
                break; 
    		switch(choice)
            {
                case 1: price = 3.00;break;
    	    	case 2: price = 2.50;break;
    	    	case 3: price = 4.10;break;
    	    	case 4: price = 10.20;break;
    	    	default:price = 0.00;break;	
    		}
    		printf("price = %.2f
    ",price);
        } 
    	
        return 0; 
     }         
    

      

      

     
  • 相关阅读:
    【leetcode刷题笔记】Best Time to Buy and Sell Stock II
    【leetcode刷题笔记】Reverse Integer
    JAVA中的NIO(二)
    标准I/O
    margin的理解
    JAVA中的NIO(一)
    IO模型
    linux网络命令
    linux用户管理命令
    linux中的帮助命令
  • 原文地址:https://www.cnblogs.com/IT-Lead-The-World/p/10349750.html
Copyright © 2011-2022 走看看