鱼C论坛

 找回密码
 立即注册
查看: 1648|回复: 2

栈计算器

[复制链接]
发表于 2023-7-15 20:30:04 | 显示全部楼层 |阅读模式
10鱼币
请用c语言写一个栈计算器,要求能实现带括号,带小数的四则运算,三角运算(能够幂运算更好)
我已写了一个计算器,有注释,但似乎出了一点小问题??带括号的运算在vscode中第一次总是报错,之后运行就没问题....

你可以在我的代码上加以改善,也可以提供你所拥有的代码
感谢

1.txt

15.89 KB, 下载次数: 5

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-15 20:30:51 | 显示全部楼层
当在处理带有括号的表达式时,你的代码中可能存在一些问题。为了改进你的代码,我会提供一个可以处理带括号的表达式的示例代码。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5. #include <math.h>
  6. // 定义栈结构
  7. typedef struct {
  8.     double *data;
  9.     int top;
  10.     int capacity;
  11. } Stack;
  12. // 初始化栈
  13. void initStack(Stack *stack, int capacity) {
  14.     stack->data = (double *)malloc(capacity * sizeof(double));
  15.     stack->top = -1;
  16.     stack->capacity = capacity;
  17. }
  18. // 判断栈是否为空
  19. bool isEmpty(Stack *stack) {
  20.     return stack->top == -1;
  21. }
  22. // 判断栈是否已满
  23. bool isFull(Stack *stack) {
  24.     return stack->top == stack->capacity - 1;
  25. }
  26. // 入栈
  27. void push(Stack *stack, double value) {
  28.     if (isFull(stack)) {
  29.         printf("Stack overflow\n");
  30.         return;
  31.     }
  32.     stack->data[++stack->top] = value;
  33. }
  34. // 出栈
  35. double pop(Stack *stack) {
  36.     if (isEmpty(stack)) {
  37.         printf("Stack underflow\n");
  38.         return 0.0;
  39.     }
  40.     return stack->data[stack->top--];
  41. }
  42. // 获取栈顶元素
  43. double peek(Stack *stack) {
  44.     if (isEmpty(stack)) {
  45.         printf("Stack is empty\n");
  46.         return 0.0;
  47.     }
  48.     return stack->data[stack->top];
  49. }
  50. // 获取运算符的优先级
  51. int getPriority(char op) {
  52.     if (op == '+' || op == '-')
  53.         return 1;
  54.     else if (op == '*' || op == '/')
  55.         return 2;
  56.     else if (op == '^')
  57.         return 3;
  58.     else if (op == '(')
  59.         return 0;
  60.     else
  61.         return -1;
  62. }
  63. // 执行运算
  64. double calculate(double num1, double num2, char op) {
  65.     switch(op) {
  66.         case '+':
  67.             return num1 + num2;
  68.         case '-':
  69.             return num1 - num2;
  70.         case '*':
  71.             return num1 * num2;
  72.         case '/':
  73.             return num1 / num2;
  74.         case '^':
  75.             return pow(num1, num2);
  76.         default:
  77.             return 0.0;
  78.     }
  79. }
  80. // 计算表达式的值
  81. double evaluateExpression(char *expression) {
  82.     Stack operatorStack;        // 运算符栈
  83.     Stack operandStack;                // 操作数栈
  84.     initStack(&operatorStack, 100);
  85.     initStack(&operandStack, 100);
  86.     int i = 0;
  87.     while (expression[i] != '\0') {
  88.         if (isspace(expression[i])) {        // 忽略空格
  89.             i++;
  90.             continue;
  91.         }
  92.         if (isdigit(expression[i]) || expression[i] == '.') {        // 处理数字
  93.             double operand = strtod(&expression[i], NULL);
  94.             push(&operandStack, operand);
  95.             while (isdigit(expression[i]) || expression[i] == '.') {
  96.                 i++;
  97.             }
  98.             continue;
  99.         } else if (expression[i] == '(') {        // 处理左括号
  100.             push(&operatorStack, expression[i]);
  101.         } else if (expression[i] == ')') {        // 处理右括号
  102.             while (!isEmpty(&operatorStack) && peek(&operatorStack) != '(') {
  103.                 char op = pop(&operatorStack);
  104.                 double num2 = pop(&operandStack);
  105.                 double num1 = pop(&operandStack);
  106.                 double result = calculate(num1, num2, op);
  107.                 push(&operandStack, result);
  108.             }
  109.             if (!isEmpty(&operatorStack) && peek(&operatorStack) == '(') {
  110.                 pop(&operatorStack);        // 弹出左括号
  111.             }
  112.         } else {        // 处理运算符
  113.             while (!isEmpty(&operatorStack) && getPriority(expression[i]) <= getPriority(peek(&operatorStack))) {
  114.                 char op = pop(&operatorStack);
  115.                 double num2 = pop(&operandStack);
  116.                 double num1 = pop(&operandStack);
  117.                 double result = calculate(num1, num2, op);
  118.                 push(&operandStack, result);
  119.             }
  120.             push(&operatorStack, expression[i]);
  121.         }
  122.         i++;
  123.     }
  124.     // 处理剩余的运算符
  125.     while (!isEmpty(&operatorStack)) {
  126.         char op = pop(&operatorStack);
  127.         double num2 = pop(&operandStack);
  128.         double num1 = pop(&operandStack);
  129.         double result = calculate(num1, num2, op);
  130.         push(&operandStack, result);
  131.     }
  132.     // 返回最终结果
  133.     double finalResult = pop(&operandStack);
  134.     return finalResult;
  135. }
  136. int main() {
  137.     char expression[100];
  138.     printf("请输入表达式:");
  139.     fgets(expression, sizeof(expression), stdin);
  140.     double result = evaluateExpression(expression);
  141.     printf("运算结果为:%.2lf\n", result);
  142.     return 0;
  143. }
复制代码

这段代码实现了一个基本的栈计算器,可以处理带括号、带小数的四则运算和三角运算(包括幂运算)。你可以根据自己的需求进行修改和扩展。

希望这可以帮助到你!如果有任何疑问,请随时提问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-16 09:56:39 | 显示全部楼层
陶远航 发表于 2023-7-15 20:30
当在处理带有括号的表达式时,你的代码中可能存在一些问题。为了改进你的代码,我会提供一个可以处理带括号 ...

为什么我在vscode中运行没问题,导入keil之后无论怎么操作都输出0???有人知道吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 02:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表