当前位置:网站首页>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;
}
边栏推荐
- Architecture evolution from MVC to DDD
- leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
- leetcode2312. Selling wood blocks (difficult, weekly race)
- Niuke - Huawei question bank (51~60)
- CSDN insertion directory in 1 second
- Cross domain? Homology? Understand what is cross domain at once
- Sword finger offer 31 Stack push in and pop-up sequence
- leetcode2312. 卖木头块(困难,周赛)
- There are spaces in the for loop variable in the shell -- IFS variable
- Based on configured schedule, the given trigger will never fire
猜你喜欢

5g/4g pole gateway_ Smart pole gateway

MySQL中一条SQL是怎么执行的

OpenCASCADE7.6编译

Ks006 student achievement management system based on SSM

CSDN article underlined, font color changed, picture centered, 1 second to understand

leetcode2310. The one digit number is the sum of integers of K (medium, weekly)

leetcode2312. 卖木头块(困难,周赛)

Sword finger offer 62 The last remaining number in the circle

Medical management system (C language course for freshmen)

AR增强现实可应用的场景
随机推荐
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
leetcode2312. 卖木头块(困难,周赛)
leetcode2309. The best English letters with both upper and lower case (simple, weekly)
leetcode373. Find and minimum k-pair numbers (medium)
自动浏览拼多多商品
The middle element and the rightmost element of the shutter
What is the MySQL column to row function
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology
The difference between new and malloc
JPM 2021 most popular paper released (with download)
CSDN article underlined, font color changed, picture centered, 1 second to understand
Regular expression learning notes
Sword finger offer II 031 Least recently used cache
If you want to rewind the video picture, what simple methods can you use?
Software No.1
大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里
flutter 中间一个元素,最右边一个元素
1218 square or round
Email picture attachment
Ubuntu20.04 PostgreSQL 14 installation configuration record