c-为了继续前面的语句,我的返回值是多少



为了让事情更干净,我在这个计算中只包含了+,-运算符。在这个委员会的其他人的帮助下,我终于能够连续地做加法/减法了。

我的问题:

根据我的理解,除了+,-之外,我输入的任何东西都应该给出"不是函数的操作。再试一次"。

我遇到的第一个错误是,我必须输入两次+、-以外的内容才能获得该消息。(因为这是电脑最先识别的东西,所以一次还不够吗?)。在那之后,我的回报值必须是多少才能回到以前的水平?

EX:
0 + 3
= 3
-1
=2
Fsgdf
Not an operation of funcation. try again
+1
=3
     #include <stdio.h>
    /* int askYN (const char *prompt) */
    double process (void);
    int main ()
    {
        int done = 0;
        while (!done)
    {
    double result;
    result = process();
    printf("Final Result = %fn", result);
    }
    return 0;
}
double process (void)
{
    double orig;
    double new_number;
    char symbol;
    orig = 0;
    printf("%.2f", orig);
    while(1==scanf(" %c", &symbol))
    {
    scanf("%lf", &new_number);
            if(symbol=='+') {
                orig+= new_number;
                printf("Result is %.2f n", orig);
                }
            else if(op=='-') {
                result-=num; 
                printf("The new result is %6.2f", result);
                }
            else {
            printf("Not an operation of the function.nTry again.");
                }
    }
}

我认为您需要先检查符号(+/-),然后如果上面有任何符号,则进行其他扫描以获取数字`。若并没有任何上述符号,那个么你们会给出错误信息。

例如

while(1==scanf(" %c", &symbol))
{
    if(symbol=='+' || symbol=='-') 
    {
        scanf("%lf", &new_number);
        //your arithmetic operation according symbol
    }
    else
    {
        //your error message
    }
}

最新更新