数学101使用C

时间:2018-03-26 10:06:33

标签: c math

你能帮我解决下面的代码吗?我正在尝试制作一个程序,用户需要选择以下选项:
1 - 乘法表
2-测试

当用户选择其中任何一个并且它已完成运行时,程序将提示用户选择以下内容:
1 - 另一个乘法表
2 - 另一个测试
3 - 退出

选择第二个选项时出现问题。每当我尝试退出(第3个选项)时,它都会返回第1个选项。当我选择第一个选项然后选择第三个选项时,一切正常。我该如何解决这个问题?

这是我的代码:

int c1;
int num;
int multi;
int num1, num2;
int answer;

printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("Choice: ");
scanf("%d", &c1);

while (c1 != 1 && c1 != 2)
{
    printf("\nInvalid selection. Please choose again.\n");
    printf("1 - Multiplication table\n");
    printf("2 - Test\n");
    printf("Choice: ");
    scanf("%d", &c1);
}

if (c1 == 1)
{
    do
    {
        printf("\nChoose a number ranging from 1 to 12.\n");
        printf("Choice: ");
        scanf("%d", &num);

        while (num > 12)
        {
            printf("\nInvalid selection. Please choose again.\n");
            printf("Choose a number ranging from 1 to 12.\n");
            printf("Choice: ");
            scanf("%d", &num);
        }

        printf("\nMultiply of %d\n", num);
        for (multi = 1; multi <= 12; multi++)
        {
            printf("%d x %d = %d\n", num, multi, num * multi);
        }

        printf("\nNext program?\n");
        printf("1 - Another multiplication table\n");
        printf("2 - Test\n");
        printf("3 - Exit\n");
        printf("Choice: ");
        scanf("%d", &c1);

        while (c1 != 1 && c1 != 2 && c1 != 3)
        {
            printf("\nInvalid selection. Please choose again.\n");
            printf("1 - Another multiplication table\n");
            printf("2 - Test\n");
            printf("3 - Exit\n");
            printf("Choice: ");
            scanf("%d", &c1);
        }

    } while (c1 == 1);
}

if (c1 == 2)
{
    do
    {
        printf("\nEnter 1st number: ");
        scanf("%d", &num1);
        printf("Enter 2nd number: ");
        scanf("%d", &num2);
        printf("Your answer is: ");
        scanf("%d", &answer);

        if (answer == num1 * num2)
        {
            printf("\nYou are correct. The answer for %d times %d is %d.", num1, num2, num1 * num2);
        }
        else if (answer != num1 * num2)
        {
            printf("\nYou are incorrect. The answer for %d times %d is %d, not %d.", num1, num2,
                num1 * num2, answer);
        }

        printf("\nNext program?\n");
        printf("1 - Multiplication table\n");
        printf("2 - Test\n");
        printf("3 - Exit\n");
        printf("Choice: ");
        scanf("%d", &c1);

        while (c1 != 1 && c1 != 2 && c1 != 3)
        {
            printf("\nInvalid selection. Please choose again.\n");
            printf("1 - Multiplication table\n");
            printf("2 - Test\n");
            printf("3 - Exit\n");
            printf("Choice: ");
            scanf("%d", &c1);
        }

        do
        {
            printf("\nChoose a number ranging from 1 to 12.\n");
            printf("Choice: ");
            scanf("%d", &num);

            while (num > 12)
            {
                printf("\nInvalid selection. Please choose again.\n");
                printf("Choose a number ranging from 1 to 12.\n");
                printf("Choice: ");
                scanf("%d", &num);
            }

            printf("\nMultiply of %d\n", num);
            for (multi = 1; multi <= 12; multi++)
            {
                printf("%d x %d = %d\n", num, multi, num * multi);
            }

            printf("\nNext program?\n");
            printf("1 - Another multiplication table\n");
            printf("2 - Test\n");
            printf("3 - Exit\n");
            printf("Choice: ");
            scanf("%d", &c1);

            while (c1 != 1 && c1 != 2 && c1 != 3)
            {
                printf("\nInvalid selection. Please choose again.\n");
                printf("1 - Another multiplication table\n");
                printf("2 - Test\n");
                printf("3 - Exit\n");
                printf("Choice: ");
                scanf("%d", &c1);
            }

        } while (c1 == 1);
    } while (c1 == 2);
}

3 个答案:

答案 0 :(得分:1)

C中的 do..while 循环在检查前执行一次。在第二个 if :“if(c1 == 2)”中,您的代码首先执行测试,然后再次要求选择。即使您的答案为“3”,它也会执行以下 do..while 部分中的代码,这部分实际上是“乘法表”的代码。

您可以使用 while 循环代替 do..while 。但是代码看起来过于复杂,有些部分会重复我建议您查看逻辑(还考虑使用开关语句)。

答案 1 :(得分:0)

找到解决方案。显然,while while循环不能很好地工作。不得不改为while循环。这是代码:

int c1;
int num;
int multi;
int num1,num2;
int answer;

printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("Choice: ");
scanf("%d",&c1);

while(c1!=1 && c1!=2){
    printf("\nInvalid selection. Please choose again.\n");
    printf("1 - Multiplication table\n");
    printf("2 - Test\n");
    printf("Choice: ");
    scanf("%d",&c1);
}

while(c1==1 || c1==2){
if(c1==1){
printf("\nChoose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d",&num);

while(num>12){
printf("\nInvalid selection. Please choose again.\n");
printf("Choose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d",&num);
}

printf("\nMultiply of %d\n",num);
for(multi=1;multi<=12;multi++){
printf("%d x %d = %d\n",num,multi,num*multi);
}

}else if(c1==2){
    printf("\nEnter 1st number: ");
    scanf("%d",&num1);
    printf("Enter 2nd number: ");
    scanf("%d",&num2);
    printf("%d times %d equals to: ",num1,num2);
    scanf("%d",&answer);

    if(answer==num1*num2){
        printf("Your answer is correct! %d times %d equals to %d.",num1,num2,num1*num2);
    }else if(answer!=num1*num2){
        printf("Your answer is incorrect! %d times %d equals to %d, not %d.",num1,num2,num1*num2,answer);
    }
}

printf("\n\nNext program?\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d",&c1);

while(c1!=1 && c1!=2 && c1!=3){
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d",&c1);
}
}

答案 2 :(得分:0)

您不应该依赖于从scanf()获取正确的输入,更安全的选择是使用fgets()读取输入并使用atoi()将值转换为int。

int getValidNumberInput(void)
{
    char buf[BUFSIZ];
    int value = 0;

    if (fgets(buf, sizeof(buf), stdin) != NULL)
        value = atoi(buf);

    return value;
}

atoi()函数返回0表示非数值,因此非常适合您的需要。

正如评论中所提到的,使用while()循环和switch()语句可以轻松简化程序的逻辑。

int main(void)
{
    bool hasLooped = false;
    bool invalidChoice = false;
    int choice;

    while (1)
    {
        /* Display the relevant menu */
        displayProgramMenu(hasLooped, invalidChoice);

        /* Get the selected menu choice */
        choice = getValidNumberInput();

        if (choice < 1 || choice > 3)
            invalidChoice = true;
        else
            invalidChoice = false;

        /* Perform the chosen action */
        switch (choice)
        {
        case 1:
            doFirstOption();
            hasLooped = true;
            break;
        case 2:
            doSecondOption();
            hasLooped = true;
            break;
        case 3:
            exit(0);
            break;
        default:
            break;
        }
    }
}

使用布尔变量可以将复杂性转移到易于推理的函数中。

void displayMenu(bool hasLooped, bool showInvalidMsg)
{
    if (showInvalidMsg)
        printf("\nInvalid selection. Please choose again.\n");
    else if (hasLooped)
        printf("\nNext program?\n");

    if (hasLooped)
        printf("1 - Another multiplication table\n");
    else
        printf("1 - Multiplication table\n");

    printf("2 - Test\n");
    printf("3 - Exit\n");
    printf("Choice: ");
}