打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
栈实现计算器

栈实现计算器

黑色印记

前段时间利用栈写了一个实现了计算器功能的程序。觉得有意思的,因此今天在这了和大家分享一番。

首先,要写出计算器,你要按照以下几点去写,才能保证运算的优先级不发生错误。

1.自左向右扫描表达式,凡是遇到操作数一律进操作数栈。

2.当遇到运算符时,如果他的优先级比运算符栈栈顶元素的优先级高就栈。反之,取出栈顶运算符和操作数栈顶的两个连续操作数运算,并将结果存入操作数栈,然后继续比较该运算符与栈顶的运算符的优先级。

3.左括号一律进运算符栈,右括号一律不进运算符栈,取出栈顶运算符和操作数栈顶的两个连续操作数运算,并将结果存入操作数栈,直到取出左括号为止。

希望大家可以自己独立完成。因为这个是有难度的,可以写在自己以后简历上。


附上我写的程序,可供大家参考。


/*****************************************************File name:calculatorAuthor:谢艺化 Version:1.0 Date: 2016-6-12Description:可以作为简单的计算器,实现加,减,乘,除,以及带括号的运草Calls : 1.insert_operand () 输入数据 2.insert_oper() 输入操作符 3.compare() 比较操作符优先级 4.deal_date() 进行数据处理*****************************************************/#include /*包含头文件*/#define MAX_SIZE 1024 /*数组长度*/int insert_operand(int *operand , int * top_num ,int num) /*数据压入数据栈*/{ (*top_num) ++; operand[*top_num] = num; /*保存数据*/ return 0; /*正常退出*/}int insert_oper (char * oper , int *top_oper , char ch) /*操作符压入符号栈*/{ (*top_oper)++; oper[*top_oper] = ch; /*保存操作符*/ return 0; /*正常退出*/}int compare(char *oper , int *top_oper , char ch) /*比较操作服优先级*/{ if((oper[*top_oper] == '-' || oper[*top_oper] == '+') /*判断当前优先级是否比栈顶操作符优先级高*/ && (ch == '*' || ch == '/')) { return 0; /*操作符压入栈*/ } else if(*top_oper == -1 || ch == '(' || (oper[*top_oper] == '(' && ch != ')')) /*判断操作符栈是否为空;栈顶操作 符是否为'('*/ { return 0; /*操作符压入栈*/ } else if (oper[*top_oper] =='(' && ch == ')' ) /*判断括号内的表达式是否计算完毕*/ { (*top_oper)--; return 1; /*对()进行处理*/ } else { return -1; /*进行操作符的运算*/ }}int deal_date(int *operand ,char *oper ,int *top_num, int *top_oper) /*进行数据运算*/{ int num_1 = operand[*top_num]; /*取出数据栈中两个数据*/ int num_2 = operand[*top_num - 1]; int value = 0; if(oper[*top_oper] == '+') /*加法操作*/ { value = num_1 + num_2; } else if(oper[*top_oper] == '-') /*减法操作*/ { value = num_2 - num_1; } else if(oper[*top_oper] == '*') /*乘法操作*/ { value = num_2 * num_1; } else if(oper[*top_oper] == '/') /*除法操作*/ { value = num_2 / num_1; } (*top_num) --; /*将数据栈顶下移一位*/ operand[*top_num] = value; /*将得到的值压入数据栈*/ (*top_oper) --; /*将操作符栈顶下移一位*/}int main(){ int operand[MAX_SIZE] = {0}; /*数据栈,初始化*/ int top_num = -1; char oper[MAX_SIZE] = {0}; /*操作符栈,初始化*/ int top_oper = -1; char *str = (char *) malloc (sizeof(char) * 100); /*获取表达式(不带=)*/ scanf('%s',str); char* temp; char dest[MAX_SIZE]; int num = 0; int i = 0; while(*str != '\0') { temp = dest; while(*str >= '0' && *str <= '9')="" 判断是否是数据*/="" {="" *temp="*str;" str="" ++;="" temp="" ++;="" }="" 遇到符号退出*/="" if(*str="" !='(' &&="" *(temp="" -="" 1)="" !='\0' )="" 判断符号是否为'('*/="" {="" *temp='\0' ;="" num="atoi(dest);" 将字符串转为数字*/="" insert_operand(operand,="" &top_num,num);="" 将数据压入数据栈*/="" }="" while(1)="" {="" i="compare(oper,&top_oper,*str);" 判断操作符优先级*/="" if(i="=" 0)="" {="" insert_oper(oper,&top_oper,*str);="" 压入操作符*/="" break;="" }="" else="" if(i="=" 1)="" 判断括号内的表达式是否结束*/="" {="" str++;="" }="" else="" if(i="=" -1)="" 进行数据处理*/="" {="" deal_date(operand,oper,&top_num,&top_oper);="" }="" }="" str="" ++;="" 指向表达式下一个字符*/="" }="" printf('num="%d\n',operand[0]);" 输出结果*/="" return="" 0;="">


如果有不足的地方,欢迎大家留言,帮助我改进。


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
用C语言写解释器(三)——中缀转后缀
606,逆波兰表达式求值
java三元运算符
数据结构栈和队列
JavaScript操作符
C++ 词汇解析集锦
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服