栈实现计算器
来源:互联网 发布:软件许可使用证书 编辑:程序博客网 时间:2024/06/10 21:50
栈实现计算器
1.自左向右扫描表达式,凡是遇到操作数一律进操作数栈。
2.当遇到运算符时,如果他的优先级比运算符栈栈顶元素的优先级高就栈。反之,取出栈顶运算符和操作数栈顶的两个连续操作数运算,并将结果存入操作数栈,然后继续比较该运算符与栈顶的运算符的优先级。
3.左括号一律进运算符栈,右括号一律不进运算符栈,取出栈顶运算符和操作数栈顶的两个连续操作数运算,并将结果存入操作数栈,直到取出左括号为止。
希望大家可以自己独立完成。因为这个是有难度的,可以写在自己以后简历上。
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; /*正常退出*/
1.自左向右扫描表达式,凡是遇到操作数一律进操作数栈。
2.当遇到运算符时,如果他的优先级比运算符栈栈顶元素的优先级高就栈。反之,取出栈顶运算符和操作数栈顶的两个连续操作数运算,并将结果存入操作数栈,然后继续比较该运算符与栈顶的运算符的优先级。
3.左括号一律进运算符栈,右括号一律不进运算符栈,取出栈顶运算符和操作数栈顶的两个连续操作数运算,并将结果存入操作数栈,直到取出左括号为止。
希望大家可以自己独立完成。因为这个是有难度的,可以写在自己以后简历上。
#include <stdio.h>
#include<stdlib.h>
#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; /*正常退出*/
0 0
- 【栈】实现高级计算器
- 栈实现计算器
- 用栈实现计算器
- 用栈实现计算器
- 计算器(栈实现)
- 栈实现计算器
- 栈实现计算器
- 双栈实现计算器
- 栈实现计算器
- 栈实现的简单计算器
- 栈实现计算器简单功能
- Java实现栈之计算器
- 巧妙利用栈实现计算器
- 用栈实现科学计算器
- 栈 实现四则运算的计算器
- hdu1237简单计算器-栈实现
- 栈实现简单的计算器
- 用栈实现的计算器
- 448. Find All Numbers Disappeared in an Array*
- 欢迎使用CSDN-markdown编辑器
- 自定义标题栏
- 合并两个排序的链表
- 383. Ransom Note
- 栈实现计算器
- (十七)剑指offer之二叉树中和为某一值的路径
- widerface数据库转voc2007数据集(python/matlab实现)
- DB中插入数据去重复写法mark
- android 文件上传与下载(带进度条)
- HDU 1180 诡异的楼梯(BFS+保存自己的状态)
- java并发编程(十三)----(JUC原子类)引用类型介绍(CAS和ABA的介绍)
- [生存志] 第116节 贾谊进献治安策
- 高级电子版 ---- C# 图解教程 ,PDF 电子书