当前位置:网站首页>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;
}
边栏推荐
- Opencascade7.6 compilation
- Makefile simple induction
- Webgpu (I): basic concepts
- leetcode2312. 卖木头块(困难,周赛)
- mysql列转行函数指的是什么
- [技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
- 自动浏览拼多多商品
- Selection of field types for creating tables in MySQL database
- How to execute an SQL in MySQL
- Experimental reproduction of variable image compression with a scale hyperprior
猜你喜欢

Selection of field types for creating tables in MySQL database

【OpenCV】-5种图像滤波的综合示例

Architecture evolution from MVC to DDD

1069. Division of convex polygons (thinking, interval DP)

An analysis of circuit for quick understanding

479. Additive binary tree (interval DP on the tree)
![[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术](/img/2d/299fa5c76416f74bd1a693c433dd09.png)
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术

Software development life cycle -- waterfall model

软件开发生命周期 --瀑布模型

leetcode2312. Selling wood blocks (difficult, weekly race)
随机推荐
What is the MySQL column to row function
How to build and use redis environment
Number of palindromes in C language (leetcode)
Data analysis on the disaster of Titanic
Is the knowledge of University useless and outdated?
A quick understanding of digital electricity
Niuke - Huawei question bank (51~60)
Volume compression, decompression
【深度学习】infomap 人脸聚类 facecluster
MySQL如何解决delete大量数据后空间不释放的问题
[technology development -21]: rapid overview of the application and development of network and communication technology -1- Internet Network Technology
1222. Password dropping (interval DP, bracket matching)
Based on configured schedule, the given trigger will never fire
Electronic Association C language level 1 33, odd even number judgment
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案
flutter 中間一個元素,最右邊一個元素
附加:信息脱敏;
Laravel artisan common commands
Open那啥的搭建文档