请评价一上这段代码,并提出需要改进的地方,多谢
请评价一下这段代码,并提出需要改进的地方,谢谢
是一个订购程序
------解决方案--------------------
作为一个C程序,从coding standard来说,没有一个显示的命名法则,变量名字随意制定不太好。从格式上来看基本完美。从算法上来看也没啥问题。挺好~~
------解决方案--------------------
是一个订购程序
- C/C++ code
#include <stdio.h> #define CAOXIAN 1.25 #define TIANCAI 0.65 #define LUOBO 0.89 #define ZHEKOU 0.95 char get_choice (void); void count (float caoxian, float tiancai, float luobo); char get_first (void); int get_int (void); int main (void) { char choice; float caoxian, tiancai, luobo; caoxian = tiancai = luobo = 0; printf ("******************************************************\n"); printf ("a) 朝鲜蓟: 1.25美元/磅 b) 甜菜: 0.65美元/磅\n"); printf ("c) 胡萝卜: 0.89美元/磅 q) 结束订购并计算 \n"); printf ("******************************************************\n"); while ((choice = get_choice ()) != 'q') { switch (choice) { case 'a': printf ("您需要多少磅朝鲜蓟: "); caoxian = get_int (); printf ("选购其他或输入q计算费用: "); break; case 'b': printf ("您需要多少磅甜菜: "); tiancai = get_int (); printf ("选购其他或输入q计算费用: "); break; case 'c': printf ("您需要多少磅胡萝卜: "); luobo = get_int (); printf ("选购其他或输入q计算费用: "); break; default: printf ("Program error!\n"); break; } } count (caoxian, tiancai, luobo); system ("pause"); return 0; } char get_choice (void) { char input; input = get_first (); while ((input < 'a' || input > 'c') && input != 'q') { printf ("输入错误,请重新输入!\n"); input = get_first (); } return input; } char get_first (void) { char ch; while ((ch = getchar ()) == '\n') continue; while (getchar () != '\n') continue; return ch; } void count (float caoxian, float tiancai, float luobo) { float caoxian_f, tiancai_f, luobo_f, zf, zz, yf, all; caoxian_f = caoxian * CAOXIAN; tiancai_f = tiancai * TIANCAI; luobo_f = luobo * LUOBO; zf = caoxian_f + tiancai_f + luobo_f; zz = caoxian + tiancai + luobo; if (zz <= 5) yf = 3.5; else if (zz > 5 && zz <= 20) yf = 10; else yf = 8 + zz * 0.1; all = yf + zf; printf ("\n\n您订购了: 朝鲜蓟%6.2f磅 甜菜%6.2f磅 胡萝卜%6.2f磅\n", caoxian, tiancai, luobo); printf ("朝鲜蓟的费用: %.2f\n", caoxian_f); printf (" 甜菜的费用: %.2f\n", tiancai_f); printf ("胡萝卜的费用: %.2f\n", luobo_f); if (zf >= 100) { printf ("因为订单总费用超过100$,您将获得5%%的折扣优惠。\n"); printf ("优惠后的订单总费用为: %.2f\n", zf * ZHEKOU); } else { printf ("订单总费用(不含运费): %.2f\n", zf); } printf ("订单总重%.2f磅,运费为: %.2f\n", zz, yf); printf ("您总共需要支付: %.2f\n\n", all); } int get_int (void) { int num; while (scanf ("%d", &num) != 1) { printf ("输入错误,请重新输入: "); while (getchar () != '\n') continue; } return num; }
------解决方案--------------------
作为一个C程序,从coding standard来说,没有一个显示的命名法则,变量名字随意制定不太好。从格式上来看基本完美。从算法上来看也没啥问题。挺好~~
------解决方案--------------------
- C/C++ code
#include <stdio.h> #define CAOXIAN 1.25f #define TIANCAI 0.65f #define LUOBO 0.89f #define ZHEKOU 0.95f void count(float caoxian, float tiancai, float luobo); int get_int(void); //---------------------------------------- int main(void) { char choice; float caoxian, tiancai, luobo; caoxian = tiancai = luobo = 0.0f; printf("**********************\n"); printf("a) 朝鲜蓟: 1.25美元/磅\n"); printf("b) 甜菜: 0.65美元/磅\n"); printf("c) 胡萝卜: 0.89美元/磅\n"); printf("**********************\n"); printf("q) 结束订购并计算\n"); printf("请按键选择:");fflush(stdout); while (1) { rewind(stdin); choice = getchar(); if ('q'==choice) break; switch (choice) { case 'a': printf("您需要多少磅朝鲜蓟(1..1000):");fflush(stdout); caoxian += get_int(); break; case 'b': printf("您需要多少磅甜菜(1..1000):");fflush(stdout); tiancai += get_int(); break; case 'c': printf("您需要多少磅胡萝卜(1..1000):");fflush(stdout); luobo += get_int(); break; default: printf("按键错误!请按a,b,c,q选择:");fflush(stdout); continue; break; } printf("目前已选购a)朝鲜蓟%.2f磅,b)甜菜%.2f磅,c)胡萝卜%.2f磅\n",caoxian, tiancai, luobo); printf("请继续选购a,b,c或输入q计算总费用:");fflush(stdout); } count(caoxian, tiancai, luobo); system("pause"); return 0; } //---------------------------------------- void count(float caoxian, float tiancai, float luobo) { float caoxian_f, tiancai_f, luobo_f, zf, zz, yf, all; caoxian_f = caoxian * CAOXIAN; tiancai_f = tiancai * TIANCAI; luobo_f = luobo * LUOBO; zf = caoxian_f + tiancai_f + luobo_f; zz = caoxian + tiancai + luobo; if (zz <= 5.0f) yf = 3.5f; else if (5.0f< zz && zz <= 20.0f) yf = 10.0f; else yf = 8.0f + zz * 0.1f; printf("\n\n您订购了: 朝鲜蓟%6.2f磅,甜菜%6.2f磅,胡萝卜%6.2f磅。\n", caoxian, tiancai, luobo); printf("朝鲜蓟的费用: %.2f\n", caoxian_f); printf(" 甜菜的费用: %.2f\n", tiancai_f); printf("胡萝卜的费用: %.2f\n", luobo_f); if (zf >= 100.0f) { zf *= ZHEKOU; printf("因为订单总费用超过100$,您将获得5%%的折扣优惠。\n"); printf("优惠后的订单总费用(不含运费): %.2f\n", zf); } else { printf("订单总费用(不含运费): %.2f\n", zf); } all = yf + zf; printf("订单总重%.2f磅,运费为: %.2f\n", zz, yf); printf("您总共需要支付: %.2f\n\n", all); } //---------------------------------------- int get_int(void) { int num; while (1) { rewind(stdin); if (1==scanf("%d", &num)) { if (0<num && num <=1000) break; } printf("输入错误,请重新输入:");fflush(stdout); } return num; }