如何在错误情况下从头开始重新启动main()?

时间:2018-11-19 10:26:27

标签: c

#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;
}

3 个答案:

答案 0 :(得分:7)

回答您的问题。

我不建议致电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;

}

答案 1 :(得分:0)

@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: 最后一个结束}应该是您的主要功能关闭大括号。

答案 2 :(得分:0)

最后解决了。感谢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;
}
}
}