各水平阶段的都看一下哈
程序代码:
#include <stdio.h> #define TAX 0.05 char *foodMenu[]= {"fish","chips","hamburgers","with cheese"}; float foodPrice[]= {3.00,2.00,3.00,4.00}; int foodOrder[]= {0,0,0,0}; char *drinkMenu[]= {"soft drinks","coffees or teas"}; float drinkPrice[]= {1.00,1.00}; int drinkOrder[]= {0,0}; int OrderFood(char ** foodMenu,int foodOrder[]); float ComputeFood(int foodOrder[]); int OrderDrink(char ** drinkMenu,int drinkOrder[]); float ComputeDrink(int drinkOrder[]); int main(void) { do { system("cls"); printf("CFOOD FISH && CHIPS && BURGERS\n\n"); OrderFood(foodMenu,foodOrder); OrderDrink(drinkMenu,drinkOrder); float priceFood=ComputeFood(foodOrder); float priceDrink=ComputeDrink(drinkOrder); float priceTotal=priceFood+priceDrink; float tax=TAX*priceTotal; float owing=priceTotal*(1.0+TAX); printf("Total Food\t%5.2f\n",priceFood); printf("Total Drink\t%5.2f\n",priceDrink); putchar('\n'); printf("Total Order\t%5.2f\n",priceTotal); printf("Tax\t\t%5.2f\n",tax); printf("----------------------------\n"); printf("Total owing\t%5.2f\n",owing); float money=0.0; float moneyGet; PAY: printf("\nPlease give me the money:"); while(scanf("%f",&moneyGet)!=1) fflush(stdin); money+=moneyGet; if(money<owing) goto PAY; printf("Amount tendered\t%.2f\n\n",money); float change=money-owing; printf("Chang\t\t%5.2f\n",change); fflush(stdin); } while(getchar()!='N'); return 0; } int OrderFood(char ** foodMenu,int foodOrder[]) { printf("--------FOOD\n"); int i; for(i=0; i<sizeof(foodMenu); i++) { printf("How many orders of %s?\tprice:$%.2f\n",foodMenu[i],foodPrice[i]); while(scanf("%d",&foodOrder[i])!=1) fflush(stdin); } putchar('\n'); return 0; } int OrderDrink(char ** drinkMenu,int drinkOrder[]) { printf("--------DRINK\n"); int i; for(i=0; i<sizeof(drinkMenu)-2; i++)//这里不减2就会出现越界读取的问题,不知道为什么,在OrderFood函数里也有类似一句,但它工作正常,我猜应该会有隐含的问题,望高手解答,谢谢 { printf("How many %s?\tprice:$%.2f\n",drinkMenu[i],drinkPrice[i]); while(scanf("%d",&drinkOrder[i])!=1) fflush(stdin); } putchar('\n'); return 0; } float ComputeFood(int foodOrder[]) { float total=0.00; int i; for(i=0; i<sizeof(foodPrice)/sizeof(float); i++) { total+=(foodOrder[i]*foodPrice[i]); if(i==3) { total-=(foodOrder[3]*foodPrice[2]); total+=(foodOrder[3]*foodPrice[5]); } } return total; } float ComputeDrink(int drinkOrder[]) { float total=0.00; int i; for(i=0; i<sizeof(drinkPrice)/sizeof(float); i++) { total+=(drinkOrder[i]*drinkPrice[i]); } return total; }
这是我给下面这个帖子写的解决代码
https://bbs.bccn.net/thread-398467-2-1.html
另立一帖,请高手解决注释中的问题,可用分不多,狠狠抛出35给肯帮我解决问题的高手,就当做给他贺年啦