C programming expression tree to postfix to solution using lines read from a file

c expression-trees infix-notation postfix-notation

Question

I am terribly new at C programming. I have stumbled upon a few answers. Some using the old syntax.

The problem is I have to create a program the will read a text file and use the read postfix lines to convert to an infix equation.

The text file would be something like this:

6            #this is the number ofcontainters
1 + 3 4      # it's no_operation_if op!=v then read value of nos mention
2 + 5 6 
3 v 2.1 
4 v 2.4
5 v 3.5 
6 v 1.5

The C file will be read in the Ubuntu terminal where the text file is the only input and the output is the infix form.

A few suggestion as to how I will accomplish this using struct, arrays, and unions. We were already given a format of creating struct opnode, vnode, and uniting them. The array part I'm clueless how to transfer from reading to the array itself. C is so weird compared to java as of this moment.

[EDIT]

Sorry I forgot to mention that this is homework... no longer postfix to infix. It's postfix to solve the equation.

Without prior knowledge of syntax and used to object oriented programming I don't know how to edit.

#include <stdio.h>
#include<stdlib.h>
#define MAXLENGTH 512

/* Codes by DocM
 * struct opnode, vnode, union
 */

struct opnode{
char operator
int loperand;
int roperand;
};
struct vnode {
char letterv;
double value;
};
union {
struct opnode op;
struct vnode val;
} nodes[100];

/*node[2].op.loperand
 *node[6].val.value
 */

/* This reads text file string input in terminal * Then commands the text file be read * etc. * and everything else actually */

int main()
{
char text[MAXLENGTH];
fputs("enter some text: ", stdout);
fflush(stdout);

int i = 0;
int f = 0;

if ( fgets(text, sizeof text, stdin) != NULL )
{
    FILE *fn;
    fn = fopen(text, "r");
}

    /* The code below should be the body of the program
 * Where everything happens.
 */


fscanf (text, "%d", &i);
int node[i];

for(int j = 0; j<i;j++)
{
    int count = 0;
    char opt[MAXLENGTH];
    fscanf(text,"%d %c", &count, &opt);
    if(opt == -,+,*,)
    {
        fscanf(text,"%d %d", &node[j].op.loperand,&node[j].op.roperand);
        node[j].op,operator = opt;
    }
    else
    {
        fscanf(text, "%lf", &node[j].val.value);
    }
    fscanf(text,"%lf",&f);
}
evaluate(1);
return 0;
}

/* Code (c) ADizon below
 *
 */

double evaluate(int i)
{
if(nodes[i].op.operator == '+' | '*' | '/' | '-')
{
    if (nodes[i].op.operator == '+')
    return evaluate(nodes, nodes[i].op.loperator) + evaluate(nodes[i].op.roperator);
    if (nodes[i].op.operator == '*')
    return evaluate(nodes, nodes[i].op.loperator) * evaluate(nodes[i].op.roperator);
    if (nodes[i].op.operator == '/')
    return evaluate(nodes, nodes[i].op.loperator) / evaluate(nodes[i].op.roperator);
    if (nodes[i].op.operator == '-')
    return evaluate(nodes, nodes[i].op.loperator) - evaluate(nodes[i].op.roperator);
}
else
{
    printf nodes[i].val.value;
    return nodes[i].val.value;
}

}

Popular Answer

I guess the basic algorithm should be:

  • Read the count for number of lines (not sure why this is necessary, would be easier to just keep reading for as long as there is indata provided, but whatever)
  • For each expected line:
    • Parse out the expected four sub-strings
    • Ignore the first, which seems to be a pointless linenumber
    • Print out the substrings in a shuffled order to create the "infix" look
  • Be done

I don't understand the part about the "v" operator, maybe you should clarify that part.

This seems a bit too much like homework for us to just blindly post code ... You need to show your own attempt first, at least.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why