能不能把上面那个代码格式化,就是复制粘贴能运行,不用我来回车
时间: 2024-01-04 15:02:26 浏览: 24
好的,这里是格式化后的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct TreeNode {
char data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
typedef struct Stack {
int top;
int size;
TreeNode **data;
} Stack;
Stack *createStack(int size) {
Stack *S = (Stack *)malloc(sizeof(Stack));
S->size = size;
S->top = -1;
S->data = (TreeNode **)malloc(sizeof(TreeNode *) * size);
return S;
}
void push(Stack *S, TreeNode *x) {
if (S->top == S->size - 1) {
printf("Stack overflow\n");
return;
}
S->data[++S->top] = x;
}
TreeNode *pop(Stack *S) {
if (S->top == -1) {
printf("Stack underflow\n");
return NULL;
}
return S->data[S->top--];
}
TreeNode *createNode(char data) {
TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
int isOperator(char data) {
if (data == '+' || data == '-' || data == '*' || data == '/')
return 1;
return 0;
}
TreeNode *buildExpressionTree(char *expression) {
Stack *S = createStack(strlen(expression));
TreeNode *root = NULL;
for (int i = 0; expression[i]; i++) {
if (expression[i] == '(') {
push(S, createNode(expression[i]));
} else if (isdigit(expression[i])) {
TreeNode *node = createNode(expression[i]);
push(S, node);
} else if (isOperator(expression[i])) {
TreeNode *node = createNode(expression[i]);
node->right = pop(S);
node->left = pop(S);
push(S, node);
} else if (expression[i] == ')') {
pop(S);
}
}
if (!isEmpty(S)) {
root = pop(S);
}
free(S->data);
free(S);
return root;
}
int evaluateExpressionTree(TreeNode *root) {
if (!root) {
return 0;
}
if (!root->left && !root->right) {
return root->data - '0';
}
int leftValue = evaluateExpressionTree(root->left);
int rightValue = evaluateExpressionTree(root->right);
switch (root->data) {
case '+': return leftValue + rightValue;
case '-': return leftValue - rightValue;
case '*': return leftValue * rightValue;
case '/': return leftValue / rightValue;
}
return 0;
}
int main() {
char expression[100];
printf("Enter the expression: ");
scanf("%s", expression);
TreeNode *root = buildExpressionTree(expression);
int result = evaluateExpressionTree(root);
printf("Result: %d\n", result);
return 0;
}
```
希望这样更方便你的使用。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![image/jpeg](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)