算術演算の順序を保持する電卓を作成しようとしています。私の考えは、括弧を気にせずに左から右に解決できるように、中置記法を後置記法に変換することです。中置記号を後置記号に変換しようとする前に、後置記号の演習を解決したいと思い、ノードを使用してこれを解決しようとしましたが、数値と演算子をノードに分割する際に問題が発生しました。私はポインターと構造体に不慣れで、すべてが私を混乱させます。
これを分割しようとする関数は次のとおりです。
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);
}