我想創建一個計算器,保持算術運算的順序。我的想法是將中綴表示法轉換爲後綴表示法,以便我可以從左到右解決它而不用擔心括號。在嘗試將中綴轉換爲後綴表示法之前,我想解決一個後綴表示法練習,並嘗試使用節點來解決此問題,但我在將數字和運算符劃分爲節點時遇到問題。 我是新來的指針和結構,所有的事情都讓我困惑。爲什麼我的節點上下文不打印任何東西?
這是一種嘗試的功能來劃分的:
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