我需要帮助实现我的代码。这是C中的代码。我的任务是创建一个反向抛光表示法程序。这是我到目前为止所拥有的。我看到的几个错误是" EXE_BAD_ACCESS(代码= 1,地址= 0x32)"任何帮助都会很有用。
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
typedef struct node
{
int datum;
int isOperator;
struct node* left;
struct node* right;
}*Node;
Node BuildTree(char** argv, int* index);
int isOperator(char ch);
int isadd(int);
int evaluate(Node);
void infix(Node, int);
int main(int argc, char* argv[])
{
int i = 9; //argc - 1;
Node tree = NULL;
char* debugList[] = {NULL,"2","9","+","5","-", "6", "D", "3", "X"};
tree = BuildTree(debugList, & i);
infix(tree, 43);
printf("\n%d", evaluate(tree));
return 0;
}
Node BuildTree(char** argv, int* index)
{
Node node;
if (*index < 1)
return NULL;
if(isOperator(argv[*index][0]))
{
//insert to the right
node = (Node) malloc(sizeof(Node));
node -> datum = (int) argv[*index][0];
(*index)--;
node -> right = BuildTree(argv, index);
node -> left = BuildTree(argv, index);
node -> isOperator = 1;
return node;
}
else
{
node = (Node) malloc(sizeof(Node));
node -> right = NULL;
node -> left = NULL;
node -> datum = (int) argv[*index][0];
node -> isOperator = 0;
(*index)--;
return node;
}
}
// Return true if ch is a operator, otherwise false
int isOperator(char ch)
{
if (ch == '+' || ch == '-' || ch == 'D' || ch == 'X')
return 1;
else
return 0;
}
void infix(Node node, int c){
if(node == NULL)
return;
else if (isadd(node -> datum) && !isadd(c)){
printf("(");
infix(node -> left, node -> datum);
printf("%c", (char) node -> datum);
infix(node -> right, node -> datum);
printf(")");
}
else {
infix(node -> left, node -> datum);
printf("%c", (char) node -> datum);
infix(node -> right, node -> datum);
}
}
int isadd(int c)
{
if(c == 43 || c == 45)
return 1;
else
return 0;
}
int evaluate(Node node)
{
if(node -> isOperator)
{
switch(node -> datum)
{
case 43:
return evaluate(node -> left) + evaluate(node -> right);
case 45:
return evaluate(node -> left) - evaluate(node -> right);
case 68:
return evaluate(node -> left) / evaluate(node -> right);
case 88:
return evaluate(node -> left) * evaluate(node -> right);
default :
return -1;
}
}else{
return node -> datum - 48;
}
}
我遇到错误的部分就在这里。
int main(int argc, char* argv[])
{
int i = 9; //argc - 1;
Node tree = NULL;
char* debugList[] = {NULL,"2","9","+","5","-", "6", "D", "3", "X"};
tree = BuildTree(debugList, & i);
infix(tree, 43); //EXE_BAD_ACCESS(code=1, address=0x32)
printf("\n%d", evaluate(tree));
return 0;
}
在这里。
void infix(Node node, int c){
if(node == NULL)
return;
else if (isadd(node -> datum) && !isadd(c)){ //EXE_BAD_ACCESS(code=1, address=0x32)
printf("(");
infix(node -> left, node -> datum); //EXE_BAD_ACCESS(code=1, address=0x32)
printf("%c", (char) node -> datum);
infix(node -> right, node -> datum); //EXE_BAD_ACCESS(code=1, address=0x32)
printf(")");
}
else {
infix(node -> left, node -> datum); //EXE_BAD_ACCESS(code=1, address=0x32)
printf("%c", (char) node -> datum);
infix(node -> right, node -> datum); //EXE_BAD_ACCESS(code=1, address=0x32)
}
}
答案 0 :(得分:4)
在您的代码中,更改
node = (Node) malloc(sizeof(Node));
到
node = malloc(sizeof(struct node));
Node
的类型为struct node *
。
node = malloc(sizeof *node);