开关的情况下菜单 [英] Switch case menu

查看:92
本文介绍了开关的情况下菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做出提示用户的这样的选项菜单程序

  ************ ***************************
输入与期望的工资率或行动的数量:
1)$ 8.75 /小时2)$ 9.33 /小时
3)$ 10.00 /小时4)$ 11.20 /小时
5)戒烟
************************************************** ***************

然后计算净工资,工资总额和税收,按照工资率。我所有的工资率做,但利率的实际的菜单是给我problems.If用户输入5就应该退出,如果通过5进入任何其他1,然后再利用和正确选择再问。我有一个问题,如果回收利用输入大于1的任何其它至5。

 的#includestdafx.h中
的#includestdio.h中
#包括stdlib.h中
#包括文件ctype.h
#定义HOURLY0 8.75
#定义HOURLY1 9.33
#定义HOURLY2 10
#定义HOURLY3 11.20
#定义TAXRATE 0.15
#定义TAXRATE2 0.20
#定义TAXRATE3 0.25
#定义加班15
INT _tmain(INT ARGC,_TCHAR *的argv [])
{
    漂浮小时,grossPay = 0,netPay,税= 0,tax3 = 0,每小时;
    INT菜单= 0,错= 1;
    烧焦退出;    的printf(请输入对应于希望的工资率或行动的数量:\\ n \\ N1)$ 8.75 /小时2)$ 9.33 /小时\\ N3)$ 10.00 /小时4)$ 11.20 /小时\\ N5)退出\\ n \\ n);
    scanf_s(%d个,&安培;菜单);
        而(菜单!= 5)
        {
            做
            {
                开关(菜单)
                {
                情况1:
                    小时= HOURLY0;
                    打破;
                案例2:
                    小时= HOURLY1;
                    打破;
                案例3:
                    小时= HOURLY2;
                    打破;
                情况4:
                    小时= HOURLY3;
                    打破;
                默认:
                    的printf(从1到5只\\ n输入正确的选择);
                    的printf(请输入对应于希望的工资率或行动的数量:\\ n \\ N1)$ 8.75 /小时2)$ 9.33 /小时\\ N3)$ 10.00 /小时4)$ 11.20 /小时\\ N5)退出\\ n \\ n);
                    scanf_s(%d个,&安培;菜单);
                    错误;
                    打破;
                }
            }而(错了!); / *我怎样才能使它回收IF输入不为1-5 * /
            的printf(曾在一周\\ n输入小时:);
            scanf_s(%F,&安培;小时);            如果(小时→40)
            {
                grossPay =(40 *小时)+((小时 - 40)*加班费);                如果(grossPay< = 300)
                {
                    税= grossPay * TAXRATE;
                }
                如果(grossPay高于300&放大器;&放大器; grossPay&所述; 450)
                {
                    税=(300 * TAXRATE)+((grossPay - 300)* TAXRATE2);
                }
                如果(grossPay> 450)
                {
                    tax3 = grossPay - 450;
                    税=(300 * TAXRATE)+((grossPay - 300 - tax3)* TAXRATE2)+((grossPay - 300 - 150)* TAXRATE3);
                }            }
            否则,如果(小时< 40)
            {
                grossPay =(小时*小时);                如果(grossPay< = 300)
                {
                    税= grossPay * TAXRATE;
                }                如果(grossPay高于300&放大器;&放大器; grossPay&所述; 450)
                {
                    税=(300 * TAXRATE)+((grossPay - 300)* TAXRATE2);
                }
                如果(grossPay> 450)
                {
                    tax3 = grossPay - 450;
                    税=(300 * TAXRATE)+((grossPay - 300 - tax3)* TAXRATE2)+((grossPay - 300 - 150)* TAXRATE3);
                }
            }
            netPay = grossPay - 税;
            的printf(\\ nGross收费:%2.3f \\ nTax:%13.3f \\ NNET收费:%10.3f \\ n \\ n,grossPay,税务,netPay);
            系统(CLS);
            的printf(请输入对应于希望的工资率或行动的数量:\\ n \\ N1)$ 8.75 /小时2)$ 9.33 /小时\\ N3)$ 10.00 /小时4)$ 11.20 /小时\\ N5)退出\\ n \\ n);
            scanf_s(%d个,&安培;菜单);
        }
    系统(暂停);
    返回0;
}


解决方案

一,提高了code方式是将用户输入处理成一个子程序。这样一来,所有的杂乱的错误处理进入子程序和只处理有效的输入。

在下面的code时, GetUserInput 函数将永远循环下去,直到用户输入一个有效的数字,或最终的文件或错误在 scanf函数。从 GetUserInput 返回值可以的只有的是1直通5的值,所以不需要处理任何意外的值。

  INT GetUserInput(无效)
{
    INT菜单;    为(;;)
    {
        菜单= 0;
        的printf(请输入对应于希望的工资率或行动的数量:\\ n \\ N1)$ 8.75 /小时2)$ 9.33 /小时\\ N3)$ 10.00 /小时4)$ 11.20 /小时\\ N5)退出\\ n \\ n);
        如果(scanf的(%d个,&放大器;!菜单)= 1)
            出口(1);        如果(菜单> = 1和;&放大器;菜单&下; = 5)
            返回菜单;        的printf(从1到5只\\ n输入正确的选择);
    }
}INT主要(无效)
{
    INT菜单= 0;    而(菜单!= 5)
    {
        菜单= GetUserInput();
        开关(菜单)
        {
            案例1:输出(\\ n ***您选择了1 *** \\ n \\ n);打破;
            案例2:输出(\\ n ***您选择了2 *** \\ n \\ n);打破;
            案例3:输出(\\ n ***您选择了3 *** \\ n \\ n);打破;
            案例4:输出(\\ n ***您选择了4 *** \\ n \\ n);打破;
            案例5:输出(再见\\ n);打破;
        }
    }
}

I am trying to make a program that prompts the user a menu of options like this

*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr 2) $9.33/hr
3) $10.00/hr 4) $11.20/hr
5) quit
*****************************************************************

And then calculate the net pay, gross pay and tax, according to the pay rate. I have all the pay rates done but the actual menu of rates is giving me problems.If the user enter 5 it should quit and if enters anything other and 1 through 5 then recycle and ask again for proper choice. I have a problem recycling if input is any other than 1 through 5.

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#define HOURLY0   8.75
#define HOURLY1  9.33
#define HOURLY2  10
#define HOURLY3  11.20
#define TAXRATE  .15
#define TAXRATE2 .20
#define TAXRATE3 .25
#define OVERTIME 15


int _tmain(int argc, _TCHAR* argv[])
{
    float hours, grossPay = 0, netPay, tax = 0, tax3 = 0,hourly;
    int menu = 0,wrong =1;
    char quit;

    printf("Enter the number corresponding to the desired pay rate or action : \n\n1) $8.75/hr          2) $9.33/hr \n3) $10.00/hr         4) $11.20/hr \n5) quit\n\n");
    scanf_s("%d", &menu);
        while (menu != 5 )
        {
            do
            {
                switch (menu)
                {
                case 1:
                    hourly = HOURLY0;
                    break;
                case 2:
                    hourly = HOURLY1;
                    break;
                case 3:
                    hourly = HOURLY2;
                    break;
                case 4:
                    hourly = HOURLY3;
                    break;
                default:
                    printf("Enter right choice from 1 to 5 only\n");
                    printf("Enter the number corresponding to the desired pay rate or action : \n\n1) $8.75/hr          2) $9.33/hr \n3) $10.00/hr         4) $11.20/hr \n5) quit\n\n");
                    scanf_s("%d", &menu);
                    wrong;
                    break;
                }
            } while (!wrong);/* HOW CAN I MAKE IT TO RECYCLE IF INPUT IS OTHER THAN 1-5*/
            printf("\nEnter hours worked in the week: ");
            scanf_s("%f", &hours);

            if (hours > 40)
            {
                grossPay = (40 * hourly) + ((hours - 40) * OVERTIME);

                if (grossPay <= 300)
                {
                    tax = grossPay * TAXRATE;
                }
                if (grossPay > 300 && grossPay < 450)
                {
                    tax = (300 * TAXRATE) + ((grossPay - 300)*TAXRATE2);
                }
                if (grossPay > 450)
                {
                    tax3 = grossPay - 450;
                    tax = (300 * TAXRATE) + ((grossPay - 300 - tax3)*TAXRATE2) + ((grossPay - 300 - 150)*TAXRATE3);
                }

            }
            else if (hours < 40)
            {
                grossPay = (hours * hourly);

                if (grossPay <= 300)
                {
                    tax = grossPay * TAXRATE;
                }

                if (grossPay > 300 && grossPay < 450)
                {
                    tax = (300 * TAXRATE) + ((grossPay - 300)*TAXRATE2);
                }
                if (grossPay > 450)
                {
                    tax3 = grossPay - 450;
                    tax = (300 * TAXRATE) + ((grossPay - 300 - tax3)*TAXRATE2) + ((grossPay - 300 - 150)*TAXRATE3);
                }
            }
            netPay = grossPay - tax;
            printf("\nGross Pay : %2.3f\nTax: %13.3f\nNet Pay: %10.3f\n\n", grossPay, tax, netPay);
            system("cls");
            printf("Enter the number corresponding to the desired pay rate or action : \n\n1) $8.75/hr          2) $9.33/hr \n3) $10.00/hr         4) $11.20/hr \n5) quit\n\n");
            scanf_s("%d", &menu);
        }
    system("pause");
    return 0;
}

解决方案

One way to improve the code is to move user input processing into a subroutine. That way, all of the messy error handling goes into the subroutine and main only has to process valid inputs.

In the code below, the GetUserInput function will loop forever, until either the user enters a valid number, or an end-of-file or error occurs in the scanf. The return value from GetUserInput can only be a value from 1 thru 5, so main doesn't need to handle any unexpected values.

int GetUserInput( void )
{
    int menu;

    for (;;)
    {
        menu = 0;
        printf("Enter the number corresponding to the desired pay rate or action : \n\n1) $8.75/hr          2) $9.33/hr \n3) $10.00/hr         4) $11.20/hr \n5) quit\n\n");
        if ( scanf( "%d", &menu ) != 1 )
            exit( 1 );

        if ( menu >= 1 && menu <= 5 )
            return menu;

        printf( "Enter right choice from 1 to 5 only\n" );
    }
}

int main( void )
{
    int menu = 0;

    while ( menu != 5 )
    {
        menu = GetUserInput();
        switch ( menu )
        {
            case 1:  printf( "\n*** You selected 1 ***\n\n" ); break;
            case 2:  printf( "\n*** You selected 2 ***\n\n" ); break;
            case 3:  printf( "\n*** You selected 3 ***\n\n" ); break;
            case 4:  printf( "\n*** You selected 4 ***\n\n" ); break;
            case 5:  printf( "Bye\n" ); break;
        }
    }
}

这篇关于开关的情况下菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆