当前位置:网站首页>Calculation (computer) code of suffix expression
Calculation (computer) code of suffix expression
2022-07-02 02:09:00 【Abright_】
Ideas
The calculation of suffix expression is realized by stack
① Scan the next element from left to right , Until all the elements are processed
② If the operand is scanned, it is pushed onto the stack , And back to ①; Otherwise execution ③
③ If operator is scanned , Then two stack top elements pop up , Perform the corresponding operation , The operation result is pushed back to the top of the stack , go back to ①
#include <iostream>
#include <string.h>
using namespace std;
#define MaxSize 10
typedef struct{
char data[MaxSize];// Static arrays hold the elements in the stack
int top;// To the top of the stack
} SqStack;
void InitStack(SqStack &S){
S.top = -1;// Initialize the stack top pointer
memset(S.data,'0', MaxSize);
}
bool StackEmpty(SqStack S){
if(S.top==-1)// Empty stack
return true;
else
return false;
}
bool Push(SqStack &S,char x){
if (S.top==MaxSize-1)
return false;
S.top += 1;
S.data[S.top] = x;
return true;
}
bool Pop(SqStack &S,char &x){
if(S.top==-1)
return false;
x = S.data[S.top];
S.top -= 1;
return true;
}
// Determine character type
int StrType(char c){
if(isdigit(c)){
return 1;
}
if(c=='('||c==')'){
return 2;
}
if(c=='+'||c=='*'||c=='-'||c=='/'){
return 3;
}
return 0;
}
char Calculate(char numa,char numb,char oper){
int inta = numb - 48;
int intb = numa - 48;
switch (oper)
{
case '+':
return (inta + intb) + 48;
break;
case '-':
return (inta - intb) + 48;
break;
case '*':
return (inta * intb) + 48;
break;
case '/':
return (inta / intb) + 48;
break;
default:
return '0';
break;
}
}
char HzCalculate(string str){
SqStack S;
InitStack(S);
for (size_t i = 0; i < str.size(); i++)
{
char cur = str.at(i);
int opt = StrType(cur);
if(opt==1){
Push(S, cur);
continue;
}
if(opt==3){
char numa, numb;
Pop(S,numa);
Pop(S, numb);
char result = Calculate(numa, numb, cur);
Push(S, result);
}
}
char result;
Pop(S, result);
return result;
}
int main(int argc, char const *argv[])
{
//"1+2*(4-3)-6/3"
char res = HzCalculate("1243-*+63/-");
cout << res << endl;
return 0;
}
边栏推荐
- MySQL constraints and multi table query example analysis
- Openssl3.0 learning XXI provider encoder
- golang---锁
- Data analysis on the disaster of Titanic
- leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
- * and & symbols in C language
- [question] - why is optical flow not good for static scenes
- Software development life cycle -- waterfall model
- Five skills of adding audio codec to embedded system
- Sword finger offer 62 The last remaining number in the circle
猜你喜欢
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/56/87bc8fca9ceeab6484f567f7231fdb.png)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing

WebGPU(一):基本概念

OpenCASCADE7.6编译

Architecture evolution from MVC to DDD

Redis环境搭建和使用的方法

1069. Division of convex polygons (thinking, interval DP)
![[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology](/img/2d/299fa5c76416f74bd1a693c433dd09.png)
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology

leetcode2309. The best English letters with both upper and lower case (simple, weekly)

开发工具创新升级,鲲鹏推进计算产业“竹林”式生长

Cross domain? Homology? Understand what is cross domain at once
随机推荐
【LeetCode 43】236. The nearest common ancestor of binary tree
new和malloc的区别
AR增强现实可应用的场景
How to execute an SQL in MySQL
* and & symbols in C language
剑指 Offer II 031. 最近最少使用缓存
剑指 Offer 47. 礼物的最大价值
Construction and maintenance of business websites [14]
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
Sword finger offer 29 Print matrix clockwise
剑指 Offer 29. 顺时针打印矩阵
Exception handling of class C in yyds dry goods inventory
Redis环境搭建和使用的方法
Deep learning: a solution to over fitting in deep neural networks
【毕业季】研究生学长分享怎样让本科更有意义
How to batch add background and transition effects to videos?
Ks006 student achievement management system based on SSM
Architecture evolution from MVC to DDD
分卷压缩,解压
Ar Augmented Reality applicable scenarios