[英]C programming expression tree to postfix to solution using lines read from a file
I am terribly new at C programming. 我是C编程的新手。 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. C文件将在Ubuntu终端中读取,其中文本文件是唯一输入,输出是中缀形式。
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.
我们已经获得了创建struct opnode,vnode并将它们组合在一起的格式。 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.
到目前为止,与Java相比,C太奇怪了。
[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;
}
}
I guess the basic algorithm should be: 我猜基本算法应该是:
I don't understand the part about the "v" operator, maybe you should clarify that part. 我不了解有关“ v”运算符的部分,也许您应该对此部分进行澄清。
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. 对于我们来说,盲目地编写代码似乎有点像家庭作业……至少您需要首先展示自己的尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.