当在处理带有括号的表达式时,你的代码中可能存在一些问题。为了改进你的代码,我会提供一个可以处理带括号的表达式的示例代码。#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <ctype.h>#include <math.h>// 定义栈结构typedef struct { double *data; int top; int capacity;} Stack;// 初始化栈void initStack(Stack *stack, int capacity) { stack->data = (double *)malloc(capacity * sizeof(double)); stack->top = -1; stack->capacity = capacity;}// 判断栈是否为空bool isEmpty(Stack *stack) { return stack->top == -1;}// 判断栈是否已满bool isFull(Stack *stack) { return stack->top == stack->capacity - 1;}// 入栈void push(Stack *stack, double value) { if (isFull(stack)) { printf("Stack overflow\n"); return; } stack->data[++stack->top] = value;}// 出栈double pop(Stack *stack) { if (isEmpty(stack)) { printf("Stack underflow\n"); return 0.0; } return stack->data[stack->top--];}// 获取栈顶元素double peek(Stack *stack) { if (isEmpty(stack)) { printf("Stack is empty\n"); return 0.0; } return stack->data[stack->top];}// 获取运算符的优先级int getPriority(char op) { if (op == '+' || op == '-') return 1; else if (op == '*' || op == '/') return 2; else if (op == '^') return 3; else if (op == '(') return 0; else return -1;}// 执行运算double calculate(double num1, double num2, char op) { switch(op) { case '+': return num1 + num2; case '-': return num1 - num2; case '*': return num1 * num2; case '/': return num1 / num2; case '^': return pow(num1, num2); default: return 0.0; }}// 计算表达式的值double evaluateExpression(char *expression) { Stack operatorStack; // 运算符栈 Stack operandStack; // 操作数栈 initStack(&operatorStack, 100); initStack(&operandStack, 100); int i = 0; while (expression[i] != '\0') { if (isspace(expression[i])) { // 忽略空格 i++; continue; } if (isdigit(expression[i]) || expression[i] == '.') { // 处理数字 double operand = strtod(&expression[i], NULL); push(&operandStack, operand); while (isdigit(expression[i]) || expression[i] == '.') { i++; } continue; } else if (expression[i] == '(') { // 处理左括号 push(&operatorStack, expression[i]); } else if (expression[i] == ')') { // 处理右括号 while (!isEmpty(&operatorStack) && peek(&operatorStack) != '(') { char op = pop(&operatorStack); double num2 = pop(&operandStack); double num1 = pop(&operandStack); double result = calculate(num1, num2, op); push(&operandStack, result); } if (!isEmpty(&operatorStack) && peek(&operatorStack) == '(') { pop(&operatorStack); // 弹出左括号 } } else { // 处理运算符 while (!isEmpty(&operatorStack) && getPriority(expression[i]) <= getPriority(peek(&operatorStack))) { char op = pop(&operatorStack); double num2 = pop(&operandStack); double num1 = pop(&operandStack); double result = calculate(num1, num2, op); push(&operandStack, result); } push(&operatorStack, expression[i]); } i++; } // 处理剩余的运算符 while (!isEmpty(&operatorStack)) { char op = pop(&operatorStack); double num2 = pop(&operandStack); double num1 = pop(&operandStack); double result = calculate(num1, num2, op); push(&operandStack, result); } // 返回最终结果 double finalResult = pop(&operandStack); return finalResult;}int main() { char expression[100]; printf("请输入表达式:"); fgets(expression, sizeof(expression), stdin); double result = evaluateExpression(expression); printf("运算结果为:%.2lf\n", result); return 0;}
这段代码实现了一个基本的栈计算器,可以处理带括号、带小数的四则运算和三角运算(包括幂运算)。你可以根据自己的需求进行修改和扩展。
希望这可以帮助到你!如果有任何疑问,请随时提问。 |