
作业社区
探索学习新天地,共享知识资源!
学无止境呀呀呀 的学生作业:
#include #include #include #include // 链栈节点结构 typedef struct StackNode { int data; // 存储数据(整数或字符的ASCII值) struct StackNode *next; // 指向下一个节点 } StackNode; // 链栈结构 typedef struct { StackNode *top; // 栈顶指针 int size; // 栈的大小 } LinkStack; void compute(LinkStack *operand, LinkStack *anOperator); // 初始化栈 LinkStack* init_stack() { LinkStack stack = (LinkStack)malloc(sizeof(LinkStack)); if (stack == NULL) { printf(“Memory allocation failed!\n”); exit(1); } stack->top = NULL; // 栈顶初始化为空 stack->size = 0; // 栈大小初始化为0 return stack; } // 判断栈是否为空 int is_empty(LinkStack *stack) { return stack->top == NULL; } // 入栈操作 void push_stack(LinkStack *stack, int data) { StackNode new_node = (StackNode)malloc(sizeof(StackNode)); if (new_node == NULL) { printf(“Memory allocation failed!\n”); exit(1); } new_node->data = data; // 设置节点数据 new_node->next = stack->top; // 新节点指向原栈顶 stack->top = new_node; // 栈顶指向新节点 stack->size++; // 栈大小增加 } // 出栈操作 int pop_stack(LinkStack *stack) { if (is_empty(stack)) { printf(“Stack is empty, cannot pop!\n”); exit(1); } StackNode *temp = stack->top; int data = temp->data; // 获取栈顶数据 stack->top = temp->next; // 栈顶指向下一个节点 free(temp); // 释放原栈顶节点 stack->size–; // 栈大小减少 return data; } // 获取栈顶元素(不出栈) int peek_stack(LinkStack *stack) { if (is_empty(stack)) { printf(“Stack is empty!\n”); exit(1); } return stack->top->data; } // 销毁栈 void destroy_stack(LinkStack *stack) { while (!is_empty(stack)) { pop_stack(stack); } free(stack); } // 打印栈内容(用于调试) void print_stack(LinkStack *stack, const char *stack_name) { printf("%s: [", stack_name); StackNode *current = stack->top; int first = 1; while (current != NULL) { if (!first) printf(", “); if (strcmp(stack_name, “Operator”) == 0) { printf(”’%c’", (char)current->data); } else { printf("%d", current->data); } current = current->next; first = 0; } printf("]\n"); } // 获取运算符优先级 int get_level(char operator) { switch(operator) { case ‘(’: return 0; //左括号优先级最低,作为栈底边界 case ‘+’: case ‘-’: return 1; // 加减法优先级为1 case ‘*’: case ‘/’: return 2; // 乘除法优先级为2 default: printf(“Invalid operator: %c\n”, operator); return -1; } } void handle_right_parenthesis(LinkStack *operand,LinkStack *operator){ printf(" Processing right parenthesis ‘)’\n"); printf(" Computing until left parenthesis ‘(’ is found…\n"); //持续计算遇到左括号 while (!is_empty(operator)) { char top_op = (char) peek_stack(operator); if(top_op == ‘(’) { //找到了匹配的左括号,将其弹出并丢弃 pop_stack(operator); printf(" Found matching ‘(’, parenthesis pair processed\n"); return; } //如果不是左括号 执行计算 compute(operand,operator); } // 如果到这里说明没有找到匹配的左括号 printf("Error: Unmatched right parenthesis!\n"); exit(1); } // 执行计算操作 void compute(LinkStack *opd, LinkStack *opt) { if (is_empty(opd) || is_empty(opt)) { printf(“Stack is empty, cannot compute!\n”); return; } int data2 = pop_stack(opd); // 第二个操作数出栈 if (is_empty(opd)) { printf("Not enough operands for computation!\n"); push_stack(opd, data2); // 放回数据 return; } int data1 = pop_stack(opd); // 第一个操作数出栈 char c = (char)pop_stack(opt); // 运算符出栈 int result = 0; // 根据运算符执行相应计算 switch(c) { case '+': result = data1 + data2; break; case '-': result = data1 - data2; break; case '*': result = data1 * data2; break; case '/': if (data2 == 0) { printf("Division by zero!\n"); exit(1); } result = data1 / data2; break; default: printf("Unknown operator: %c\n", c); return; } push_stack(opd, result); // 计算结果入操作数栈 printf(" Computed: %d %c %d = %d\n", data1, c, data2, result); } // 处理运算符 void deal_with_operator(LinkStack *operand, LinkStack *operator, char current_op) { printf(" Processing operator: ‘%c’\n", current_op); // 如果运算符栈为空,直接入栈 if (is_empty(operator)) { push_stack(operator, (int)current_op); printf(" Operator stack empty, '%c' pushed directly\n", current_op); return; } char top_op = (char)peek_stack(operator); int current_level = get_level(current_op); int top_level = get_level(top_op); printf(" Comparing: '%c'(level %d) vs '%c'(level %d)\n", current_op, current_level, top_op, top_level); // 当前运算符优先级大于栈顶运算符,直接入栈 if (current_level > top_level) { push_stack(operator, (int)current_op); printf(" '%c' has higher precedence, pushed to stack\n", current_op); } // 当前运算符优先级小于等于栈顶运算符,先计算再入栈 else { printf(" '%c' has equal/lower precedence, computing first\n", current_op); // 重要:如果栈顶是左括号,不能进行计算,直接入栈当前运算符 if (top_op == '(') { push_stack(operator, (int)current_op); printf(" Top is '(', pushing '%c' directly\n", current_op); } else { compute(operand, operator); // 递归处理,因为可能需要连续计算多个运算符 deal_with_operator(operand, operator, current_op); } } } // 判断字符是否为左括号 int is_left_parenthesis(char c) { return (c == ‘(’); } // 判断字符是否为右括号 int is_right_parenthesis(char c) { return (c == ‘)’); } // 判断字符是否为运算符 int is_operator(char c) { return (c == ‘+’ || c == ‘-’ || c == ‘*’ || c == ‘/’); } // 表达式求值主函数 int evaluate_expression(char *expression) { LinkStack *operand = init_stack(); // 操作数栈 LinkStack *operator = init_stack(); // 运算符栈 char *p = expression; printf("Starting to parse expression: %s\n", expression); printf("Length: %zu characters\n", strlen(expression)); printf("========================================\n"); while (*p != '\0') { printf("Current character: '%c' (ASCII: %d)\n", *p, (int)*p); // 跳过空格 if (*p == ' ' || *p == '\t') { //指针取值 printf(" Skipping whitespace\n"); p++; //移动指针 continue; } // 处理数字 if (*p >= '0' && *p = '0' && *p





学无止境呀呀呀 的学生作业:
#include #include #include #include // 链栈节点结构 typedef struct StackNode { int data; // 存储数据(整数或字符的ASCII值) struct StackNode *next; // 指向下一个节点 } StackNode; // 链栈结构 typedef struct { StackNode *top; // 栈顶指针 int size; // 栈的大小 } LinkStack; // 初始化栈 LinkStack* init_stack() { LinkStack stack = (LinkStack)malloc(sizeof(LinkStack)); if (stack == NULL) { printf(“Memory allocation failed!\n”); exit(1); } stack->top = NULL; // 栈顶初始化为空 stack->size = 0; // 栈大小初始化为0 return stack; } // 判断栈是否为空 int is_empty(LinkStack *stack) { return stack->top == NULL; } // 入栈操作 void push_stack(LinkStack *stack, int data) { StackNode new_node = (StackNode)malloc(sizeof(StackNode)); if (new_node == NULL) { printf(“Memory allocation failed!\n”); exit(1); } new_node->data = data; // 设置节点数据 new_node->next = stack->top; // 新节点指向原栈顶 stack->top = new_node; // 栈顶指向新节点 stack->size++; // 栈大小增加 } // 出栈操作 int pop_stack(LinkStack *stack) { if (is_empty(stack)) { printf(“Stack is empty, cannot pop!\n”); exit(1); } StackNode *temp = stack->top; int data = temp->data; // 获取栈顶数据 stack->top = temp->next; // 栈顶指向下一个节点 free(temp); // 释放原栈顶节点 stack->size–; // 栈大小减少 return data; } // 获取栈顶元素(不出栈) int peek_stack(LinkStack *stack) { if (is_empty(stack)) { printf(“Stack is empty!\n”); exit(1); } return stack->top->data; } // 销毁栈 void destroy_stack(LinkStack *stack) { while (!is_empty(stack)) { pop_stack(stack); } free(stack); } // 打印栈内容(用于调试) void print_stack(LinkStack *stack, const char *stack_name) { printf("%s: [", stack_name); StackNode *current = stack->top; int first = 1; while (current != NULL) { if (!first) printf(", “); if (strcmp(stack_name, “Operator”) == 0) { printf(”’%c’", (char)current->data); } else { printf("%d", current->data); } current = current->next; first = 0; } printf("]\n"); } // 获取运算符优先级 int get_level(char operator) { switch(operator) { case ‘+’: case ‘-’: return 1; // 加减法优先级为1 case ‘*’: case ‘/’: return 2; // 乘除法优先级为2 default: printf(“Invalid operator: %c\n”, operator); return -1; } } // 执行计算操作 void compute(LinkStack *opd, LinkStack *opt) { if (is_empty(opd) || is_empty(opt)) { printf(“Stack is empty, cannot compute!\n”); return; } int data2 = pop_stack(opd); // 第二个操作数出栈 if (is_empty(opd)) { printf("Not enough operands for computation!\n"); push_stack(opd, data2); // 放回数据 return; } int data1 = pop_stack(opd); // 第一个操作数出栈 char c = (char)pop_stack(opt); // 运算符出栈 int result = 0; // 根据运算符执行相应计算 switch(c) { case '+': result = data1 + data2; break; case '-': result = data1 - data2; break; case '*': result = data1 * data2; break; case '/': if (data2 == 0) { printf("Division by zero!\n"); exit(1); } result = data1 / data2; break; default: printf("Unknown operator: %c\n", c); return; } push_stack(opd, result); // 计算结果入操作数栈 printf(" Computed: %d %c %d = %d\n", data1, c, data2, result); } // 处理运算符 void deal_with_operator(LinkStack *operand, LinkStack *operator, char current_op) { printf(" Processing operator: ‘%c’\n", current_op); // 如果运算符栈为空,直接入栈 if (is_empty(operator)) { push_stack(operator, (int)current_op); printf(" Operator stack empty, '%c' pushed directly\n", current_op); return; } char top_op = (char)peek_stack(operator); int current_level = get_level(current_op); int top_level = get_level(top_op); printf(" Comparing: '%c'(level %d) vs '%c'(level %d)\n", current_op, current_level, top_op, top_level); // 当前运算符优先级大于栈顶运算符,直接入栈 if (current_level > top_level) { push_stack(operator, (int)current_op); printf(" '%c' has higher precedence, pushed to stack\n", current_op); } // 当前运算符优先级小于等于栈顶运算符,先计算再入栈 else { printf(" '%c' has equal/lower precedence, computing first\n", current_op); compute(operand, operator); // 递归处理,因为可能需要连续计算多个运算符 deal_with_operator(operand, operator, current_op); } } // 判断字符是否为运算符 int is_operator(char c) { return (c == ‘+’ || c == ‘-’ || c == ‘*’ || c == ‘/’); } // 表达式求值主函数 int evaluate_expression(char *expression) { LinkStack *operand = init_stack(); // 操作数栈 LinkStack *operator = init_stack(); // 运算符栈 char *p = expression; printf("Starting to parse expression: %s\n", expression); printf("Length: %zu characters\n", strlen(expression)); printf("========================================\n"); while (*p != '\0') { printf("Current character: '%c' (ASCII: %d)\n", *p, (int)*p); // 跳过空格 if (*p == ' ' || *p == '\t') { //指针取值 printf(" Skipping whitespace\n"); p++; //移动指针 continue; } // 处理数字 if (*p >= '0' && *p = '0' && *p




