C primer 第七章分支和跳转 习题7.8
#include <stdio.h> #define RATE_BASE_300 0.15f #define RATE_300_TO_450 0.2f #define RATE_FLOW 0.25f int main(void) { int hour,choice; float tax_300=300*RATE_BASE_300; float tax_450=tax_300+150*RATE_300_TO_450; float tax,pay,after_pay,d_per_hour; printf("*****************************************************************\n"); printf("Enter the number corresponding to the desired pay rate or action:\n"); printf("1)$8.75/hr 2)$9.33/hr\n"); printf("3)$10.00/hr 4)$11.20/hr\n"); printf("5)quit\n"); printf("*****************************************************************\n"); while (scanf("%d",&choice)==1) { if (choice<=5 && choice>=1) //控制输入为1~5 { switch(choice) { case 1: d_per_hour=8.75f; break; case 2: d_per_hour=9.33f; break; case 3: d_per_hour=10.0f; break; case 4: d_per_hour=11.20f; break; default: goto end; } } else { printf("Enter the correct num.\n"); continue; //输入不是1~5时 自动转到循环最开始 } printf("How many hours you work per week:"); scanf("%d",&hour); pay=d_per_hour*hour*4; if (pay<=300) tax=RATE_BASE_300*pay; else if (pay<=450) tax=tax_300+(pay-300)*RATE_300_TO_450; else tax=tax_450+(pay-450)*RATE_FLOW; after_pay=pay-tax; printf("You total salary is %.2f,and after tax it is %.2f.That means you have pay $%.2f tax\n",pay,after_pay,tax); printf("*****************************************************************\n"); printf("Enter the number corresponding to the desired pay rate or action:\n"); printf("1)$8.75/hr 2)$9.33/hr\n"); printf("3)$10.00/hr 4)$11.20/hr\n"); printf("5)quit\n"); printf("*****************************************************************\n"); } end: return 0; }
1.使用一个switch语句做选项时,如果希望能退出程序,可以用goto语句。
goto stop; stop: return 0;
其他时候不推荐用goto语句。
改进如下:
#include <stdio.h> #define RATE_BASE_300 0.15f #define RATE_300_TO_450 0.2f #define RATE_FLOW 0.25f int get_choice(void); int main(void) { int hour,choice; float tax_300=300*RATE_BASE_300; float tax_450=tax_300+150*RATE_300_TO_450; float tax,pay,after_pay,d_per_hour; while (choice=get_choice()) { if (choice<=5 && choice>=1) //控制输入为1~5 { switch(choice) { case 1: d_per_hour=8.75f; break; case 2: d_per_hour=9.33f; break; case 3: d_per_hour=10.0f; break; case 4: d_per_hour=11.20f; break; default: goto end; } } else { printf("Enter the correct num.\n"); continue; //输入不是1~5时 自动转到循环最开始 } printf("How many hours you work per week:"); scanf("%d",&hour); pay=d_per_hour*hour*4; if (pay<=300) tax=RATE_BASE_300*pay; else if (pay<=450) tax=tax_300+(pay-300)*RATE_300_TO_450; else tax=tax_450+(pay-450)*RATE_FLOW; after_pay=pay-tax; printf("You total salary is %.2f,and after tax it is %.2f.That means you have pay $%.2f tax\n",pay,after_pay,tax); } end: return 0; } int get_choice(void) { int num; printf("*****************************************************************\n"); printf("Enter the number corresponding to the desired pay rate or action:\n"); printf("1)$8.75/hr 2)$9.33/hr\n"); printf("3)$10.00/hr 4)$11.20/hr\n"); printf("5)quit\n"); printf("*****************************************************************\n"); scanf("%d",&num); return num; }
2.改进主要集中在输入选项的显示,减少了代码量,并且使得程序可读性增强。每次进入循环都会显示选项。下一步改进将集中于在get_choice函数中实现对输入的控制,对不正确的输入进行提示。