c语言编程练习题:7-55 查询水果价格-LMLPHP

#include <stdio.h>
int main(){
    int count=0;
    int n=0;
    double prices[5] = {0,3.00,2.50,4.10,10.20};
    char names[][7] = {"exit","apple","pear","orange","grape"};
    int sequences[5] = {1,2,3,4,0};
    int i;
    int r=0;
    char *s;
    // 输出编号和名字
    for (i=0;i<5;i++){
        r = sequences[i];
        s = names[r];
        printf("[%d] %s\n",r,s);
    }
    // 保存查询的num
    //while (scanf("%d",&n)!=EOF && count<5){
    while (scanf("%d", &n) != EOF) {
        
        if (count>=5){break;}
        if (n==0){break;}
        //printf("n=%d %.2f\n",n, prices[n]);
        //if (n>4){printf("price = 0.00\n");}
        //if (n<0){printf("price = 0.00\n");}
        if (n<=4 && n>0){printf("price = %.2f\n",prices[n]);}
        else{printf("price = 0.00\n");}
        count++;
    }
    return 0;
}

代码来自:https://yunjinqi.top/article/180

05-29 18:27