栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言栈计算器

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言栈计算器

思路

采用双栈,一个运算符栈,一个数字栈,实现了 ^ ( ) 和大于10的树的计算

问题

栈的进出顺序有一定逻辑问题,导致与目标的输出结果不一致。

源码
#include 
#include 

#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef struct operate_stack {
  char op[MAXSIZE];
  int top;
} operator, * operator_t;
typedef struct number_stack {
  float data[MAXSIZE];
  int top;
} number, *number_t;

//判断操作符栈满
int isFull_stack_op(operator_t op_S) {
  if (MAXSIZE == op_S->top)
    return OK;
  else
    return 0;
}

//判断操作符栈空
int isEmpty_stack_op(operator_t op_S) {
  if (-1 == op_S->top)
    return OK;
  else
    return 0;
}

//判断数字栈满
int isFull_stack_num(number_t num_S) {
  if (MAXSIZE == num_S->top)
    return OK;
  else
    return 0;
}

//判断数字栈空
int isEmpty_stack_num(number_t num_S) {
  if (-1 == num_S->top)
    return OK;
  else
    return 0;
}
//
初始化操作符栈
// void init_operator(operator_t op_S) {
//     int i = 'A';
//     for (i = 'A'; i < 'G'; i++) {
//         op_S->top++;
//         op_S->op[op_S->top] = i;
//     }
//     return;
// }
//
遍历操作符栈
// void print_operator(operator_t op_S) {
//     for (; op_S->top > -1; op_S->top--) {
//         printf("%c ", op_S->op[op_S->top]);
//     }
//     printf("n");
// }
//
初始化操作数栈
// void init_number(number_t num_S) {
//     for (int i = 0; i < 9; i++) {
//         num_S->top++;
//         num_S->data[num_S->top] = i + 1;
//     }
//     return;
// }
遍历操作数栈
// void print_number(number_t num_S) {
//     for (; num_S->top > -1; num_S->top--) {
//         printf("%.2f ", num_S->data[num_S->top]);
//     }
//     printf("n");
//     return;
// }

//创建操作符栈
operator_t create_operator_stack(void) {
  operator_t op_s = (operator_t)malloc(sizeof(operator));
  op_s->top = 0;
  op_s->op[op_s->top] = '#';
  return op_s;
}

//创建操作数栈
number_t create_number_stack(void) {
  number_t num_s = (number_t)malloc(sizeof(number));
  num_s->top = -1;
  return num_s;
}

//操作符入栈
int pushStack_op(operator_t op_S, char c) {
  if (isFull_stack_op(op_S))
    return -1;
  op_S->top++;
  op_S->op[op_S->top] = c;
  return 0;
}

//操作符出栈
char readStack_op(operator_t op_S) {
  char ch;
  if (isEmpty_stack_op(op_S))
    return 'E';
  ch = op_S->op[op_S->top];
  op_S->top--;
  return ch;
}

//操作数入栈
int pushStack_num(number_t num_S, float a) {
  if (isFull_stack_num(num_S))
    return -1;
  num_S->top++;
  num_S->data[num_S->top] = a;
  return 0;
}

//操作数出栈
float readStack_num(number_t num_S) {
  float a;
  if (isEmpty_stack_num(num_S))
    return -1;
  a = num_S->data[num_S->top];
  printf("栈顶:%.2fn", num_S->data[num_S->top]);  // TODO 打印栈顶元素
  num_S->top--;
  return a;
}

//返回操作符优先级
int operator_priority(char ch) {
  switch (ch) {
    case '#': {
      return -1;
      break;
    }
    case '+': {
      return 2;
      break;
    }
    case '*': {
      return 3;
      break;
    }
    case '-': {
      return 2;
      break;
    }
    case 'x': {
      return 3;
      break;
    }
    case '/': {
      return 3;
      break;
    }
    case '^': {
      return 4;
      break;
    }
    case '(': {
      return 0;
      break;
    }
    case ')': {
      return OK;
      break;
    }
    default:
      printf("优先级寻找失败n");
  }
  return -1;
}

//判断操作符
int isOperator(char ch) {
  int i = 0;
  char s[8] = {'+', '-', 'x', '/', '^', '(', ')', '*'};
  for (i = 0; i < 8; i++) {
    if (s[i] == ch)
      return OK;
  }
  return 0;
}

//判断操作数
int isNum(char ch) {
  if (ch >= '0' && ch <= '9')
    return OK;
  return 0;
}

float op_num(char c, float d1, float d2) {
  switch (c) {
    case '+': {
      return d1 + d2;
      break;
    }
    case '-': {
      return d1 - d2;
      break;
    }
    case 'x': {
      return d1 * d2;
      break;
    }
    case '*': {
      return d1 * d2;
      break;
    }
    case '/': {
      if (0 == d2) {
        printf("被除数不为0!n");
        return 0;
      } else {
        return d1 / d2;
        break;
      }
    }
    case '^': {
      int n = d2;
      int sum = 1;
      while (n--)
        sum *= d1;
      return sum;
      break;
    }
    default:
      printf("暂不提供“%c”支持!n", c);
      return 0;
  }
}

//清洗字符串
void wash_str(char a[]) {
  char* p = a;
  while (1) {
    if ('=' == *p)
      break;
    else {
      if ('' == *(p + 1)) {
        *(++p) = '=';
        break;
      }
    }
    p++;
  }
  // printf("%sn", a);
}

//程序执行函数
void Calculate(void) {
  char* p = NULL;
  float d1, d2, d3;
  float count = 0;
  char t_c;
  int flag = 0;
  operator_t opr = create_operator_stack();
  number_t num = create_number_stack();
  char str[41] = "";
  printf("请输入表达式...n>");
  gets(str);
  //处理输入字符串
  wash_str(str);
  p = str;
  //精华部分,入栈出栈,进行计算!
  while ('' != *p) {
    // sleep(1);
    //操作数处理
    if (isNum(*p)) {
      count = count * 10 + (*p - '0');
      if (isNum(*(p + 1))) {
        p++;
        continue;
      } else {
        pushStack_num(num, count);
        count = 0;
      }
    }
    //操作符处理
    else if (isOperator(*p)) {
      //操作符进栈情况
      if (('(' == *p) ||
          (operator_priority(opr->op[opr->top]) < operator_priority(*p))) {
        pushStack_op(opr, *p);
      }
      //右括号处理
      else if (')' == *p) {
        t_c = readStack_op(opr);
        while ('(' != t_c) {
          d1 = readStack_num(num);
          d2 = readStack_num(num);
          d3 = op_num(t_c, d2, d1);
          pushStack_num(num, d3);
          t_c = readStack_op(opr);
        }
      }
      //优先级高,进行运算并继续比较
      else if (operator_priority(opr->op[opr->top]) >= operator_priority(*p)) {
        d1 = readStack_num(num);
        d2 = readStack_num(num);
        d3 = op_num(readStack_op(opr), d2, d1);
        pushStack_num(num, d3);
        pushStack_op(opr, *p);
        // sleep(1);
      }
    }
    //遇到"="号
    else if ('=' == *p) {
      while ('#' != opr->op[opr->top]) {
        d1 = readStack_num(num);
        d2 = readStack_num(num);
        d3 = op_num(readStack_op(opr), d2, d1);
        pushStack_num(num, d3);
      }
    }
    if ('z' == *p || 'Z' == *p) {
      flag = -1;
      break;
    }
    p++;
  }
  printf("结果是 %gn",
         readStack_num(num));
  if (-1 != flag)
    Calculate();
  return;
}

int main() {
  printf("************************算术表达式求值程序************************n");

  Calculate();
  printf(
      "************************************************************************"
      "*n");
  printf(
      "*                       算术表达式求值程序结束                          "
      "    * n");
  return 0;
}
运行结果 目标结果

实际结果

算法效率分析 时间复杂度 空间复杂度
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934057.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号