A. Joseph Desmond
A. Joseph Desmond

Reputation: 27

How do you write a while loop so that an entire file is read, rather than just one line?

I have partially written code that scans in a file full of mathematical expressions in postfix and displays the number to the screen.

While it performs the computation, it will only read in one line and then exit. How do I modify the code to test all arithmetic expressions in the text file?

I've tried commanding the code to exit when EOF, NULL and '\0' are reached, but they all exit at one line.

Here is my code:

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

Also, the equations I need to read in are:

13 1 - 2 / 3 155 + *
100 100 100 100 + + +
10.33 2 2 2 2 2 * * * * *
30 10 - 10 - 10 - 2 *
300 13.25 - 11 3 - / 4 5 - * 3 /

It works for the first equation only. What can I do in terms of loop structure to make it read all of these equations out of the text file?

Upvotes: 0

Views: 87

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753695

Your while loop ends:

    return 1;
}

This means it returns from the function after reading the first line of input.

The most plausible fix is to remove the return 1; altogether. An alternative might be:

    if (top == 0)
        printf("Output:%3.2f\n", stack[top]);
    else
    {
        fprintf(stderr, "have given wrong postfix expression\n");
        return 1;
    }
}

However, exiting from an interactive calculator on the first mistake is a bit draconian. (It exits the program since the return is leaving main().) Note that errors should usually be reported on stderr. It might be a good idea to echo the faulty expression too:

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

Upvotes: 4

Roland Illig
Roland Illig

Reputation: 41627

The code looks ok, but you have a return statement inside the for loop. This return statement will leave the whole main function.

Upvotes: 3

Related Questions