为什么下面的程序卡在一个循环中? [英] Why is the following program stuck in a loop?

查看:35
本文介绍了为什么下面的程序卡在一个循环中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个练习编写了一个解决方案,该练习要求编写一个程序,该程序充当一个简单的打印"计算器,并检测除以零并检查未知运算符.

当输入预期的运算符时,程序按预期工作.例如:

100 S"打印= 100.000000"

2/"打印= 50.000000"

10 *"打印= 500.000000"

它还检测除以零和未知运算符.

但是,当我以错误的顺序输入运算符时,如下所示:/2"或*10",程序卡在循环中.

如何修复此错误,以便在以错误的顺序输入运算符时,它只会打印未知运算符"?

//编写一个程序,作为一个简单的打印"计算器#include int main (void){浮动acc,b;字符运算符;printf("开始计算\n");而(运算符!= 'E'){scanf ("%f %c", &b, &operator);开关(操作员){case 'S'://设置累加器案例's':acc = b;printf ("= %f\n", acc);休息;case 'E'://结束程序案例e":printf ("= %f\n计算结束.\n", acc);休息;案例+":printf ("= %f\n", acc += b);休息;案件 '-':printf ("= %f\n", acc -= b);休息;案件 '*':printf ("= %f\n", acc *= b);休息;案件 '/':如果 ( b == 0 )printf ("不能被零除.\n");别的printf ("= %f\n", acc/= b);休息;默认:printf ("未知运算符.\n");休息;}}返回0;}

更新:我找到了解决方案

while ( 运算符 != 'E' && 运算符 != 'e' ) {if ( scanf ("%f %c", &b, &operator ) <2 ) {printf ("错误.您需要输入一个数字.\n");休息;}别的 {开关(操作员)...

解决方案

scanf 的结果是分配的字段数.如果 scanf 返回 0,则每次调用它都会为相同的格式字符串返回 0.因为 scanf 将它读取的最后一个与输入序列(%f)不匹配的字符推回,它会反复尝试一遍又一遍地转换相同的字符串.>

这就是你无限循环的原因.您可能想要检查 scanf 的结果.如果小于 2,则出错.

I wrote a solution to an exercise that asks to write a program that acts as a simple "printing" calculator, and also detects division by zero and checks for unknown operators.

The program works as intended when expected operators are entered. For example:

"100 S" prints "= 100.000000"

"2 /" prints "= 50.000000"

"10 *" prints "= 500.000000"

It also detects division by zero and unknown operators.

However, when I enter operators in wrong order, like this: "/ 2" or "* 10", the program is stuck in a loop.

How do I fix this bug so that when the operators are entered in wrong order, it just prints "Unknown operator"?

// Write a program that acts as a simple "printing" calculator

#include <stdio.h>

int main (void)
{
    float acc, b;
    char  operator;

    printf ("Begin Calculations\n");

    while ( operator != 'E') {
        scanf ("%f %c", &b, &operator);

        switch (operator)
        {
            case 'S':       // set accumulator
            case 's':
                acc = b;
                printf ("= %f\n", acc);
                break;
            case 'E':       // end program
            case 'e':
                printf ("= %f\nEnd of Calculations.\n", acc);
                break;
            case '+':
                printf ("= %f\n", acc += b);
                break;
            case '-':
                printf ("= %f\n", acc -= b);
                break;
            case '*':
                printf ("= %f\n", acc *= b);
                break;
            case '/':
                if ( b == 0 )
                    printf ("Can't divide by zero.\n");
                else
                    printf ("= %f\n", acc /= b);
                break;
            default:
                printf ("Unknown operator.\n");
                break;
        }
    }
    return 0;
}

Update: I've found a solution

while ( operator != 'E' && operator != 'e' ) {
        if ( scanf ("%f %c", &b, &operator ) < 2 ) {
            printf ("Error. You need to enter a number.\n");
            break;
        }
        else {

            switch (operator)...

解决方案

The result of scanf is the number of fields assigned. If scanf returns 0, it will return 0 for the same format string every time you call it. Because scanf pushes back the last character it read that does not match the input sequence (%f), it will repeatedly try to convert the same string over and over.

That's why you loop infinitely. You might want to check the result of scanf. If it's less than 2, error out.

这篇关于为什么下面的程序卡在一个循环中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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