当前位置:网站首页>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约束与多表查询实例分析
- Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- [Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
- Architecture evolution from MVC to DDD
- OpenCASCADE7.6编译
- 开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
- Design and implementation of key value storage engine based on LSM tree
- If you want to rewind the video picture, what simple methods can you use?
- Flutter un élément au milieu, l'élément le plus à droite
猜你喜欢
MySQL中一条SQL是怎么执行的
Implementation principle of city selector component
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
SQLite 3 of embedded database
Architecture evolution from MVC to DDD
If you want to rewind the video picture, what simple methods can you use?
1222. Password dropping (interval DP, bracket matching)
Golang lock
golang---锁
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
随机推荐
附加:信息脱敏;
Cross domain? Homology? Understand what is cross domain at once
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
Ubuntu20.04 PostgreSQL 14 installation configuration record
【毕业季】研究生学长分享怎样让本科更有意义
How to build and use redis environment
STM32F103——两路PWM控制电机
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
leetcode2305. 公平分发饼干(中等,周赛,状压dp)
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
Using mongodb in laravel
Opengauss database backup and recovery guide
leetcode2312. Selling wood blocks (difficult, weekly race)
Flutter un élément au milieu, l'élément le plus à droite
Is the knowledge of University useless and outdated?
Electronic Association C language level 1 33, odd even number judgment
Word search applet design report based on cloud development +ppt+ project source code + demonstration video
np. Where and torch Where usage
OpenCASCADE7.6编译
There are spaces in the for loop variable in the shell -- IFS variable