当前位置:网站首页>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;
}
边栏推荐
- 479. Additive binary tree (interval DP on the tree)
- 734. Energy stone (greed, backpack)
- Flutter un élément au milieu, l'élément le plus à droite
- [C #] use regular verification content
- 医药管理系统(大一下C语言课设)
- Ks006 student achievement management system based on SSM
- DNS domain name resolution
- Software No.1
- JPM 2021 most popular paper released (with download)
- Parted command
猜你喜欢
![[graduation season] graduate seniors share how to make undergraduate more meaningful](/img/03/9adc44476e87b2499aa0ebb11cb247.png)
[graduation season] graduate seniors share how to make undergraduate more meaningful

Software development life cycle -- waterfall model

leetcode2312. Selling wood blocks (difficult, weekly race)

How to solve MySQL master-slave delay problem

如何用一款产品推动「品牌的惊险一跃」?

JPM 2021 most popular paper released (with download)

479. Additive binary tree (interval DP on the tree)

734. Energy stone (greed, backpack)

Opengauss database backup and recovery guide

Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing
随机推荐
leetcode2312. Selling wood blocks (difficult, weekly race)
* and & symbols in C language
1222. Password dropping (interval DP, bracket matching)
自动浏览拼多多商品
跨域?同源?一次搞懂什么是跨域
Experimental reproduction of variable image compression with a scale hyperprior
Construction and maintenance of business websites [14]
Email picture attachment
The middle element and the rightmost element of the shutter
Sword finger offer 31 Stack push in and pop-up sequence
Laravel artisan common commands
1069. Division of convex polygons (thinking, interval DP)
Deep learning: a solution to over fitting in deep neural networks
SQLite 3 of embedded database
Based on configured schedule, the given trigger will never fire
Opengauss database backup and recovery guide
Niuke - Huawei question bank (51~60)
The concept, function, characteristics, creation and deletion of MySQL constraints
Bash bounce shell encoding
Implementation principle of city selector component