【问题标题】:How to restart main() from the beginning on an error condition?如何在错误情况下从头开始重新启动 main()?
【发布时间】:2018-11-19 10:26:27
【问题描述】:
#include <stdio.h>
int main()
{
    printf("Hi!\nWelcome!\nThis is an expression based calculator\ndeveloped by Sankasuvra Bhattacharya\n");
    printf("that performs arithmetic operations on\ntwo numbers.\n");
    float num1;
    float num2;
    float ans = 0.0;
    char symbol;
    char ask;
    printf("Please type the expression you want to calculate: ");
    if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
    {
        printf("\nInvalid input! Please try again...\n\n");
        /* want to restart main() again here */
    }
    else {
    switch(symbol) {
        case '+' : ans = num1 + num2;
                    break;
        case '-' : ans = num1 - num2;
                    break;
        case '*' :
        case 'x' :
                    ans = num1 * num2;
                    break;
        case '/' :
                    if(num2 == 0) {
                        printf("Division by zero is not possible!\nPlease try again...\n\n");
                        return main();
                    }
                    else {
                    ans = num1 / num2;
                    break;
                    }
        default :
                    printf("\nInvalid input! Please try again...\n\n");
                    return main();
    }
    printf("The answer is %g\n",ans);
    printf("\nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...\n");
    scanf("%s",&ask);
    if (ask == 'y' || ask == 'Y') {
        printf("\n");
        main();
    }
        else {
        printf("Thank you for using the program. Please give full marks.");
    }
    }
    return 0;
}

【问题讨论】:

标签: c


【解决方案1】:

回答你的问题。

我不建议调用 main。

您可以创建另一个包含所有代码的函数。

在 main 中,您调用该函数。

您可以在该函数内部调用一个函数(称为递归)

但是,一个简单的循环就可以完成这项工作。

do{
    printf("Hi!\nWelcome!\nThis is an expression based calculator\ndeveloped by Sankasuvra Bhattacharya\n");
    printf("that performs arithmetic operations on\ntwo numbers.\n");
    float num1;
    float num2;
    float ans = 0.0;
    char symbol;
    char ask;
    printf("Please type the expression you want to calculate: ");
    if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
    {
        printf("\nInvalid input! Please try again...\n\n");
    }
    else {
    switch(symbol) {
        case '+' : ans = num1 + num2;
                   break;
        case '-' : ans = num1 - num2;
                   break;
        case '*' :
        case 'x' :
                   ans = num1 * num2;
                   break;
        case '/' :
                   if (num2 == 0) {
                        printf("Division by zero is not possible!\nPlease try again...\n\n");
                        return main();
                    }
                    else {
                        ans = num1 / num2;
                        break;
                    }
        default :
                    printf("\nInvalid input! Please try again...\n\n");
                    return main();
    }
    printf("The answer is %g\n",ans);
    printf("\nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...\n");
    scanf("%s",&ask);
    printf("\n");
    }while(ask == 'y' || ask == 'Y') ;                        
    printf("Thank you for using the program. Please give full marks.");
}

编辑:要回答对此问题的评论,您要做的是:

while(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
{
    printf("\nInvalid input! Please try again...\n\n");
}

并删除else

EDIT2:完整代码。请注意,表达式不能超过 99 个字符。

#include <stdio.h>

int main()
{

float num1;
float num2;
float ans = 0.0;
char symbol;
char ask;
char string[100];
do{
    printf("Hi!\nWelcome!\nThis is an expression based calculator\ndeveloped by Sankasuvra Bhattacharya\n");
    printf("that performs arithmetic operations on\ntwo numbers.\n");

    printf("Please type the expression you want to calculate: ");

    while(1){
        fgets (string , 100 ,stdin);
        if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
            printf("\nInvalid input! Please try again...\n\n");
        else
            break;

    }

    switch(symbol) {
        case '+' : ans = num1 + num2;
                break;
        case '-' : ans = num1 - num2;
                break;
        case '*' :
        case 'x' :
                ans = num1 * num2;
                break;
        case '/' :
                if (num2 == 0) {
                        printf("Division by zero is not possible!\nPlease try again...\n\n");
                        return main();
                    }
                    else {
                        ans = num1 / num2;
                        break;
                    }
        default :
                    printf("\nInvalid input! Please try again...\n\n");
                    return main();
    }
    printf("The answer is %g\n",ans);
    printf("\nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...\n");
    scanf("%s",&ask);
    printf("\n");

}while(ask == 'y' || ask == 'Y') ;  

printf("Thank you for using the program. Please give full marks.");

return 0;

}

【讨论】:

  • 嗨,请注意我指的是这一行... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("\ n无效输入!请重试...\n\n"); }
  • 你在别处调用了 main。让我编辑我的答案。同样的原则也适用
  • 显示无效输入无限号。次...像这样...输入无效。请重试.... 输入无效。请重试.... 输入无效。请重试.... 输入无效。请重试.... 输入无效。请重试.... 输入无效。请再试一次......
  • 如果 scanf 找不到与预期格式匹配的输入,它将返回。使用此循环,您将不再知道输入在哪里,并且可能会无限期地失败。这种模式通常是一个坏主意。考虑使用 fgets 和 sscanf 的基于行的输入。
  • 在运行 Kristjan 的代码时,它说在之前 }
【解决方案2】:

@Kristjan Kica 的回答很好。我认为您在输入中使用了空格,例如 1 + 2。

根据scanf的手册页

 All conversions are introduced by the % (percent sign) character.  The format string may also contain other characters.  White space
 (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input.  Everything else
 matches only itself.  Scanning stops when an input character does not match such a format character.  Scanning also stops when an input
 conversion cannot be made.

删除空格,然后重试。

示例: 1+2 应该通过 kristijan 回答中提到的更改起作用。

编辑: 替换@Kristjan Kica 答案中的行

while(ask == 'y' || ask == 'Y') ;                        

}while(ask == 'y' || ask == 'Y') ;                        

编辑 2: 最后关闭 } 应该是您的主要功能关闭大括号。

【讨论】:

  • 运行 Kristjan 的代码时,它说在之前 }
  • 是的,它正在工作......但是我怎样才能在这一行中做同样的事情...... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3 ) { printf("\n输入无效!请重试...\n\n"); }
  • 你是得到输出还是刚刚编译?
  • 你可以用 if 语句实现同样的效果(在 do-while 中删除 while 并重试)。
【解决方案3】:

终于解决了。感谢 Kristjan Kica...

#include <stdio.h>

 int main(void)
{
float num1;
float num2;
float ans;
char symbol;
char ask;
char string[100];
fflush(stdin);
printf("Hi!\nWelcome!\nThis is an expression based calculator\ndeveloped by 
Sankasuvra Bhattacharya\n");
printf("that performs arithmetic operations on\ntwo numbers.\n");
printf("Please type the expression you want to calculate: ");
fgets (string , 100 ,stdin);
if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
{
        printf("\nInvalid input! Please try again...\n\n");
        main();
}
else
{
switch(symbol) {
    case '+' : ans = num1 + num2;
            break;
    case '-' : ans = num1 - num2;
            break;
    case '*' :
    case 'x' :
            ans = num1 * num2;
            break;
    case '/' :
            if (num2 == 0) {
                    printf("Division by zero is not possible!\nPlease try again...\n\n");
                    return main();
                }
                else {
                    ans = num1 / num2;
                    break;
                }
    default :
                printf("\nInvalid input! Please try again...\n\n");
                return main();
}
printf("The answer is %g\n",ans);
printf("\nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...\n");
scanf("%s",&ask);
printf("\n");
if (ask == 'y' || ask == 'Y')
{
    main();
}
else {
    return 0;
}
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 2020-04-09
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 2020-05-09
    相关资源
    最近更新 更多