需求
编程实现计算器,当输入一个表达式时,可以得出计算结果。(实现加、减、乘、除、取余以及负号运算)
思路
1. 维护两个栈,一个栈my_dig用于push数字,另一个栈my_op用于push运算符。栈中元素结构如下:
typedef struct tag_stack1 { int dig_arr[1024]; int dig_top; }DIG, *pDIG; typedef struct tag_stack2 { char op_arr[1024]; int op_top; }OP, *pOP;
2. 遍历表达式字符串,当遇到数字时,将数字push到栈my_dig中,这个没有问题。对于运算符需要讨论:
1)如果运算符是右括号,则my_op需要一直弹栈计算,直到左括号出栈。
2)如果运算符不是右括号,则需判断当前运算符与栈顶运算符的优先级高低,如果当前运算符优先级高,则入栈;否则弹栈计算,直到当前运算符优先级低于栈顶运算符优先级为止,此时当前运算符入栈。
注意
需要区分负号与其他运算符。本代码处理负号的入栈时,将其替换成‘@’,以便区分其与减号。
1. 减号与负号,两者优先级不同。
2. 对于负号的计算,每次从数字栈中只取一个操作数,而对于其他运算符(包括减号)的计算,每次从数字栈中需要取两个操作数。
代码
/************************************************************************* > File Name: main.c > Author: KrisChou > Mail:zhoujx0219@163.com > Created Time: Wed 10 Sep 2014 07:44:10 PM CST ************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct tag_stack1 { int dig_arr[1024]; int dig_top; }DIG, *pDIG; typedef struct tag_stack2 { char op_arr[1024]; int op_top; }OP, *pOP; static int is_ok[8][8] = { // + - * / % ( # @ ) /* + */ 0,0,0,0,0,1,1,0, /* - */ 0,0,0,0,0,1,1,0, /* * */ 1,1,0,0,0,1,1,0, /* / */ 1,1,0,0,0,1,1,0, /* % */ 1,1,0,0,0,1,1,0, /* ( */ 1,1,1,1,1,1,1,1, /* # */ 0,0,0,0,0,0,0,0, /* @ */ 1,1,1,1,1,1,1,0 }; static char op_arr[9] = {'+','-','*','/','%','(','#','@',')'}; static int find_index(char op) { int index; for(index = 0; index < 9; index++) { if(op == op_arr[index]) { return index; } } return -1; } static int is_myspace(char c) { if(c == ' ' || c == ' ' || c == ' ' || c == 'v') { return 1; }else { return 0; } } static void trim_space(char *str) { int slow = -1; int fast = 0; while(str[fast] != '