如何用C语言写一个栈计算器?要求能实现带小数带括号的四则运算,以及幂运算
3条回答 默认 最新
关注下面是一个用C语言编写的栈计算器的示例代码,可以实现带小数、带括号的四则运算和幂运算:
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <math.h> // 定义栈结构 typedef struct { double* stack; int top; int capacity; } Stack; // 初始化栈 void initStack(Stack* s, int capacity) { s->stack = (double*)malloc(capacity * sizeof(double)); s->top = -1; s->capacity = capacity; } // 判断栈是否为空 int isEmpty(Stack* s) { return s->top == -1; } // 判断栈是否已满 int isFull(Stack* s) { return s->top == s->capacity - 1; } // 入栈操作 void push(Stack* s, double value) { if (isFull(s)) { printf("Stack is full.\n"); return; } s->stack[++s->top] = value; } // 出栈操作 double pop(Stack* s) { if (isEmpty(s)) { printf("Stack is empty.\n"); return 0; } return s->stack[s->top--]; } // 获取栈顶元素 double top(Stack* s) { if (isEmpty(s)) { printf("Stack is empty.\n"); return 0; } return s->stack[s->top]; } // 判断运算符的优先级 int getPriority(char op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; if (op == '^') return 3; return 0; } // 执行计算 double calculate(double a, double b, char op) { switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; case '^': return pow(a, b); } return 0; } // 栈计算器函数 double stackCalculator(char* expression) { Stack numStack; // 数字栈 Stack opStack; // 运算符栈 initStack(&numStack, 100); initStack(&opStack, 100); int i = 0; while (expression[i] != '\0') { if (isdigit(expression[i])) { double num = 0; while (isdigit(expression[i])) { num = num * 10 + (expression[i] - '0'); i++; } if (expression[i] == '.') { i++; double decimal = 0.1; while (isdigit(expression[i])) { num = num + (expression[i] - '0') * decimal; decimal /= 10; i++; } } push(&numStack, num); } else if (expression[i] == '(') { push(&opStack, expression[i]); i++; } else if (expression[i] == ')') { while (top(&opStack) != '(') { char op = pop(&opStack); double b = pop(&numStack); double a = pop(&numStack); double result = calculate(a, b, op); push(&numStack, result); } pop(&opStack); // 弹出左括号 i++; } else if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] == '/' || expression[i] == '^') { while (!isEmpty(&opStack) && getPriority(top(&opStack)) >= getPriority(expression[i])) { char op = pop(&opStack); double b = pop(&numStack); double a = pop(&numStack); double result = calculate(a, b, op); push(&numStack, result); } push(&opStack, expression[i]); i++; } else { i++; } } while (!isEmpty(&opStack)) { char op = pop(&opStack); double b = pop(&numStack); double a = pop(&numStack); double result = calculate(a, b, op); push(&numStack, result); } double finalResult = pop(&numStack); free(numStack.stack); free(opStack.stack); return finalResult; } int main() { char expression[100]; printf("请输入表达式:"); fgets(expression, sizeof(expression), stdin); double result = stackCalculator(expression); printf("计算结果:%lf\n", result); return 0; }上述代码中使用两个栈,一个用于存储数字,另一个用于存储运算符。通过遍历表达式字符串,将数字和运算符依次入栈,并进行相应的计算操作,最后得到最终的计算结果。请注意,代码中对于错误情况的处理比较简单,可以根据实际需求进行适当修改和完善。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报