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


i terribly new @ c programming. have stumbled upon few answers. using old syntax.

the problem have create program read text file , use read postfix lines convert infix equation.

the text file this:

6            #this number ofcontainters 1 + 3 4      # it's no_operation_if op!=v 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 read in ubuntu terminal text file input , output infix form.

a few suggestion how accomplish using struct, arrays, , unions. given format of creating struct opnode, vnode, , uniting them. array part i'm clueless how transfer reading array itself. c weird compared java of moment.

[edit]

sorry forgot mention homework... no longer postfix infix. it's postfix solve equation.

without prior knowledge of syntax , used object oriented programming don't know how edit.

#include <stdio.h> #include<stdlib.h> #define maxlength 512  /* codes 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  */ 

/* reads text file string input in terminal * commands text file read * etc. * , else */

int main() { char text[maxlength]; fputs("enter text: ", stdout); fflush(stdout);  int = 0; int f = 0;  if ( fgets(text, sizeof text, stdin) != null ) {     file *fn;     fn = fopen(text, "r"); }      /* code below should body of program  * 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; }  } 

i guess basic algorithm should be:

  • read count number of lines (not sure why necessary, easier keep reading long there indata provided, whatever)
  • for each expected line:
    • parse out expected 4 sub-strings
    • ignore first, seems pointless linenumber
    • print out substrings in shuffled order create "infix" look
  • be done

i don't understand part "v" operator, maybe should clarify part.

this seems bit homework blindly post code ... need show own attempt first, @ least.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -