当前位置:网站首页>C language uses stack to calculate infix expressions
C language uses stack to calculate infix expressions
2022-08-02 04:02:00 【SevenCold】
cThe language uses a stack to implement computing infix expressions
解决这个问题需要两步:
- Change infix to postfix(The suffix type is more convenient for writing algorithms)
- 计算后缀式.
Because it has to meet multiple digits and decimals,So strings are used.
typedef struct
{
char c[100];
}X;
typedef struct
{
X c[MAXSIZE];
int top;
}Sqstack;
Some functions on the stack will not be written,应该都知道.
The following directly changes the suffix code to the infix code(有注释).
void Change(Sqstack S, X a[], X b[])//Turns an infix expression into a postfix expression,ais the infix form of the input,bis the converted suffix.
{
int i;
for (i = 0; a[i].c[0] != '#'; i++)//遍历输入字符串,无非三种情况,遇到数字、括号(大中小)、运算符.
{
if (a[i].c[0] >= 48 && a[i].c[0] <= 57)//When you encounter numbers, just throw them away
{
b[n] = a[i];
n++;
}
if (a[i].c[0] == '+' || a[i].c[0] == '-' || a[i].c[0] == '*' || a[i].c[0] == '/')//遇到运算符,If the priority is greater than the top operator, it is pushed onto the stack,Otherwise, spit the top element of the stack,再次比较,Until the priority is less than the top operator or the stack is empty,入栈.**(Parentheses have the lowest precedence)**
{
if (Isempty(S))
{
S = Push(S, a[i]);
}
else
{
int j;
for (j = S.top; j >= 0; j--)
{
if (Priority(a[i].c[0], S.c[j].c[0]))
{
S = Push(S, a[i]);
break;
}
else
{
b[n] = S.c[j];
n++;
S = Pop(S);
}
}
if (j == -1)
{
S = Push(S, a[i]);
}
}
}
if (a[i].c[0] == '(' || a[i].c[0] == '[' || a[i].c[0] == '{')//When the left parenthesis is encountered, it is directly pushed onto the stack
{
S = Push(S, a[i]);
}
if (a[i].c[0] == ')')//遇到右括号,Spit out the operators in the stack one by one.
{
if (a[i+1].c[0] == '*' || a[i+1].c[0] == '/')//This sentence is the solution(4+3)*2 这种情况.
{
for (int j = S.top; j >= 0; j--)
{
if (S.c[j].c[0] == '(')
{
S = Pop(S);
break;
}
else
{
b[n] = S.c[S.top];
n++;
S = Pop(S);
}
}
}
else
{
for (int j = S.top; j >= 0; j--)
{
if (S.c[j].c[0] == '(')
{
S = Pop(S);
for (int k = j - 1; S.c[k].c[0] != '['&&S.c[k].c[0] != '{'&&k >= 0; k--)
{
b[n] = S.c[k];
n++;
S = Pop(S);
}
break;
}
else
{
b[n] = S.c[S.top];
n++;
S = Pop(S);
}
}
}
}
if ( a[i].c[0] == ']' )
{
if (a[i + 1].c[0] == '*' || a[i + 1].c[0] == '/')
{
for (int j = S.top; j >= 0; j--)
{
if (S.c[j].c[0] == '[')
{
S = Pop(S);
break;
}
else
{
b[n] = S.c[S.top];
n++;
S = Pop(S);
}
}
}
else
{
for (int j = S.top; j >= 0; j--)
{
if ( S.c[j].c[0] == '[')
{
S = Pop(S);
for (int k = j - 1; S.c[k].c[0] != '{'&&k >= 0; k--)
{
b[n] = S.c[k];
n++;
S = Pop(S);
}
break;
}
else
{
b[n] = S.c[S.top];
n++;
S = Pop(S);
}
}
}
}
if ( a[i].c[0] == '}')
{
if (a[i + 1].c[0] == '*' || a[i + 1].c[0] == '/')
{
for (int j = S.top; j >= 0; j--)
{
if (S.c[j].c[0] == '{')
{
S = Pop(S);
break;
}
else
{
b[n] = S.c[S.top];
n++;
S = Pop(S);
}
}
}
else
{
for (int j = S.top; j >= 0; j--)
{
if (S.c[j].c[0] == '{')
{
S = Pop(S);
for (int k = j - 1; k >= 0; k--)
{
b[n] = S.c[k];
n++;
S = Pop(S);
}
break;
}
else
{
b[n] = S.c[S.top];
n++;
S = Pop(S);
}
}
}
}
}
for (int k = S.top; k >= 0; k--)
{
b[n] = S.c[k];
n++;
S = Pop(S);
}
}
Then there is the algorithm of the operation.(这个就比较简单了)
void Operate(Sqstack S, X b[])//计算表达式
{
int i;
for (i = 0; i < n; i++)
{
if (b[i].c[0] >= 48 && b[i].c[0] <= 57)
{
S = Push(S, b[i]);
}
else//Know that the operator is encountered.Pop the first two elements from the top of the stack, compute the result, and push the result onto the stack.
{
X m, n, x;
strcpy(m.c, S.c[S.top].c);
S = Pop(S);
strcpy(n.c, S.c[S.top].c);
S = Pop(S);
Calculate(m.c, b[i].c[0], n.c, x.c);
S = Push(S,x);
}
}
X y;
y = S.c[S.top];
double t = atof(y.c);
printf("%.2f", t);
}
Another detail is the conversion of string and floating-point data(当然intTypes are also converted,But also count the decimals,So choose floating point)
void Calculate(char b[], char c, char a[],char x[])//计算
{
double m = atof(a);
double n = atof(b);
int dec_pl, sign;
switch (c)
{
case '+':
{
double z = m + n;
_gcvt(z, 8, x);
break;
}
case '-':
{
double z = m - n;
_gcvt(z, 8, x);
break;
}
case '*':
{
double z = m * n;
_gcvt(z, 8, x);
break;
}
case '/':
{
double z = m / n;
_gcvt(z, 8, x);
break;
}
}
}
Some knowledge is quoted in https://blog.csdn.net/coder_dacyuan/article/details/79941743
新手上路,写的比较水,谢谢观看.嘻嘻.
边栏推荐
- [mikehaertl/php-shellcommand] A library for invoking external command operations
- vim编辑模式
- Summary of php function vulnerabilities
- Cookie is used to collect the admin privileges CTF foundation problem
- Alfa: 1 vulnhub walkthrough
- CTF introductory notes ping
- Eric靶机渗透测试通关全教程
- CTF入门笔记之SQL注入
- Phonebook
- hackmyvm: again walkthrough
猜你喜欢
动力:2 vulnhub预排
Alfa: 1 vulnhub walkthrough
利用cookie获取admin权限 CTF基础题
Phpstudy安装Thinkphp6(问题+解决)
Stable and easy-to-use short connection generation platform, supporting API batch generation
SQL: DDL, DML, DQL, DCL corresponding introduction and demonstration
Cookie is used to collect the admin privileges CTF foundation problem
CTF入门之md5
TCP communications program
Thread Pool (Introduction and Use of Thread Pool)
随机推荐
PHP realizes the automatic reverse search prompt of the search box
DarkHole: 2 vulnhub walkthrough
(5) Modules and packages, encoding formats, file operations, directory operations
Turn trendsoft/capital amount of Chinese capital library
12.什么是JS
[league/flysystem] An elegant and highly supported file operation interface
CTF入门之php文件包含
IO stream, encoding table, character stream, character buffer stream
Stable and easy-to-use short connection generation platform, supporting API batch generation
The roll call system and array elements find maximum and minimum values for sorting of objects
13.JS输出内容和语法
Shuriken: 1 vulnhub walkthrough
文件包含漏洞
(5) 模块与包、编码格式、文件操作、目录操作
一次代码审计的笔记(CVE-2018-12613 phpmyadmin文件包含漏洞)
(2) 顺序结构、对象的布尔值、选择结构、循环结构、列表、字典、元组、集合
16. JS events, string and operator
DVWA靶机安装教程
v-bind usage: class dynamic binding object array style style and function method
17. JS conditional statements and loops, and data type conversion