我想创建一个计算器,保持算术运算的顺序。我的想法是将中缀表示法转换为后缀表示法,以便我可以从左到右解决它而不用担心括号。在尝试将中缀转换为后缀表示法之前,我想解决一个后缀表示法练习,并尝试使用节点来解决此问题,但我在将数字和运算符划分为节点时遇到问题。 我是新来的指针和结构,所有的事情都让我困惑。为什么我的节点上下文不打印任何东西?
这是一种尝试的功能来划分的:
typedef char* String;
typedef struct node
{
String str;
struct node *next;
} Node;
Node *rpn_divider(String equation, int eq_size)
{
Node *rpn_parts = node_alloc(1); //pointer to first element in the node
Node *part_temp = rpn_parts; //pointer to the lattest element in the node
String temp = malloc(sizeof(char*) * NUM_SIZE);
int i, j; //i = string equation index, j = string temp index
for (i = 0, j = 0; i < eq_size; i++)
{
if (isNum(equation[i]))
temp[j++] = equation[i];
else if (isOper(equation[i]))
{
temp[0] = equation[i];
temp[1] = '\0';
next_node(part_temp, temp);
}
else
{
if (temp == '\0') continue;
temp[j] = '\0';
next_node(part_temp, temp);
j = 0;
}
}
free(part_temp->next);
free(temp);
return rpn_parts;
}
这里是next_node功能:
void next_node(Node *node, String str)
{
node->str = str;
node->next = node_alloc(1);
node = node->next;
free(str);
str = malloc(sizeof(char*) * NUM_SIZE);
str[0] = '\0';
}
,当我试图打印节点上下文,它不会做任何东西:
Node *ptr;
for (ptr = head; ptr != NULL; ptr = ptr->next);
{
printf("The Str = %s", ptr->str);
}
请加什么rpn_parts是,以及为节点和字符串的定义。为什么你在next_node有免费(str)?它可能不会打印,因为你已经释放了所有的str。什么是头? – Myforwik