C 如何编写while循环以读取整个文件,而不是仅读取一行?

C 如何编写while循环以读取整个文件,而不是仅读取一行?,c,file,stack,C,File,Stack,我已经编写了部分代码,扫描了一个充满后缀的数学表达式的文件,并将数字显示在屏幕上 当它执行计算时,它将只读取一行,然后退出。如何修改代码以测试文本文件中的所有算术表达式 我试图在达到EOF、NULL和“\0”时命令代码退出,但它们都在一行退出 这是我的密码: #include <stdio.h> #include <string.h> #include <ctype.h> #include <math.h> #include <stdlib.

我已经编写了部分代码,扫描了一个充满后缀的数学表达式的文件,并将数字显示在屏幕上

当它执行计算时,它将只读取一行,然后退出。如何修改代码以测试文本文件中的所有算术表达式

我试图在达到EOF、NULL和“\0”时命令代码退出,但它们都在一行退出

这是我的密码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>

int top = -1;
float stack[500];

/* push the given data into the stack */
void push (int data) {
    stack[++top] = data;
}

/* Pop the top element from the stack */
float pop () {
    float data;
    if (top == -1)
        return -1;
    data = stack[top];
    stack[top] = 0;
    top--;
    return (data);
}

int main() {

    char str[500];
    FILE *p;
    if((p=fopen("testfile1.txt","r"))==NULL){
        printf("\n Unable to open file string.txt");
        return 1;
    }

    while(fgets(str,500,p)!='\0'){

        float data = -1, operand1, operand2, result;

        for (int i = 0; i < strlen(str); i++) {
            if (isdigit(str[i])) {
                /*
                 * if the i/p char is digit, parse
                 * character by character to get
                 * complete operand
                 */
                data = (data == -1) ? 0 : data;
                data = (data * 10) + (str[i] - 48);
                continue;
            }

            if (data != -1) {
                /* if the i/p is operand, push it into the stack */
                push(data);
            }

            if (str[i] == '+' || str[i] == '-'
                    || str[i] == '*' || str[i] == '/') {
                /*
                 * if the i/p is an operator, pop 2 elements
                 * from the stack and apply the operator
                 */
                operand2 = pop();
                operand1 = pop();
                if (operand1 == -1 || operand2 == -1)
                    break;
                switch (str[i]) {
                    case '+':
                        result = operand1 + operand2;
                        /* push the result into the stack */
                        push(result);
                        break;
                    case '-':
                        result = operand1 - operand2;
                        push(result);
                        break;
                    case '*':
                        result = operand1 * operand2;
                        push(result);
                        break;
                    case '/':
                        result = operand1 / operand2;
                        push(result);
                        break;
                }
            }
            data = -1;
        }
        if (top == 0)
            printf("Output:%3.2f\n", stack[top]);
        else
            printf("have given wrong postfix expression\n");
        return 1;
    }
}
它只适用于第一个方程。在循环结构方面,我可以做些什么来让它从文本文件中读取所有这些方程?

您的while循环结束:

    return 1;
}
这意味着它在读取第一行输入后从函数返回

最合理的修复方法是拆除回路1;总共另一种选择可能是:

    if (top == 0)
        printf("Output:%3.2f\n", stack[top]);
    else
    {
        fprintf(stderr, "have given wrong postfix expression\n");
        return 1;
    }
}
然而,在第一个错误时退出交互式计算器有点苛刻。它将退出程序,因为返回将离开main。注意,错误通常应该在stderr上报告。最好也重复错误的表达:

fprintf(stderr, "have given wrong postfix expression (%s)\n", str);

代码看起来不错,但是for循环中有一个return语句。此return语句将保留整个主函数。

请注意,fgets返回一个char*,虽然它在语法上是有效的,但将“\0”作为空指针常量写入显然是一种惯例。使用0或NULL。是的,我正准备添加它。阅读此文:了解更多乔纳森的意思。另外,您可以显示文本文件的内容吗?您也可以将上述代码提交给,因为它包含其他几个错误。@RolandIllig:code Review用于检查/改进工作代码,而不是修复非工作代码。谢谢。这部分修复了它。它现在正在计算所有行,但返回值为第一行之后的每个表达式提供了错误的后缀表达式。其他表达式中没有错误,因此我不确定它是否会正确计算它们。修复其他问题是一个单独的练习-这解决了循环在第一次迭代后停止的标题问题。修复其他问题现在是调试技能的例行练习。添加一些打印语句,以便了解发生了什么。注意,对于int i=0;ifprintf(stderr, "have given wrong postfix expression (%s)\n", str);