当前位置:网站首页>c语言用栈实现计算中缀表达式
c语言用栈实现计算中缀表达式
2022-08-02 03:25:00 【SevenCold】
c语言用栈实现计算中缀表达式
解决这个问题需要两步:
- 将中缀式换成后缀式(后缀式更利于写算法)
- 计算后缀式。
因为还要满足多位数和小数,所以用了字符串。
typedef struct
{
char c[100];
}X;
typedef struct
{
X c[MAXSIZE];
int top;
}Sqstack;
关于栈的一些函数就不写了,应该都知道。
下面直接给中缀式换后缀式代码(有注释)。
void Change(Sqstack S, X a[], X b[])//将中缀表达式变为后缀表达式,a是输入的中缀式,b是转换完的后缀式。
{
int i;
for (i = 0; a[i].c[0] != '#'; i++)//遍历输入字符串,无非三种情况,遇到数字、括号(大中小)、运算符。
{
if (a[i].c[0] >= 48 && a[i].c[0] <= 57)//遇到数字直接扔过去就行
{
b[n] = a[i];
n++;
}
if (a[i].c[0] == '+' || a[i].c[0] == '-' || a[i].c[0] == '*' || a[i].c[0] == '/')//遇到运算符,若优先级大于栈顶运算符则进栈,否则吐出栈顶元素,再次比较,直至优先级小于栈顶运算符或栈空,入栈。**(括号优先级最低)**
{
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] == '{')//遇到左括号直接进栈
{
S = Push(S, a[i]);
}
if (a[i].c[0] == ')')//遇到右括号,将栈内的运算符依次吐出。
{
if (a[i+1].c[0] == '*' || a[i+1].c[0] == '/')//此句为解决(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);
}
}
然后再就是运算的算法。(这个就比较简单了)
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//知道遇到运算符。将栈顶前两个元素出栈并计算再将结果压入栈内。
{
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);
}
还有一个细节就是字符串和浮点型数据的转换了(当然int型也有转换,但还要计算小数,所以选用了浮点型)
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;
}
}
}

部分知识引用于 https://blog.csdn.net/coder_dacyuan/article/details/79941743
新手上路,写的比较水,谢谢观看。嘻嘻。
边栏推荐
- 4.PHP数组与数组排序
- Solve the problem of Zlibrary stuck/can't find the domain name/reached the limit, the latest address of Zlibrary
- 二维码生成API接口,可以直接作为A标签连接
- Praying: 1 vulnhub walkthrough
- (3)Thinkphp6数据库
- Phpstudy installs Thinkphp6 (problem + solution)
- hackmyvm: kitty walkthrough
- Advanced Operations on Arrays
- php函数漏洞总结
- 4. The form with the input
猜你喜欢
![[league/climate]一个功能健全的命令行功能操作库](/img/ce/39114b1c74af649223db97e5b0e29c.png)
[league/climate]一个功能健全的命令行功能操作库

hackmyvm: again walkthrough

12. What is JS

ES6介绍+定义变量+不同情况下箭头函数的this指向
![[league/climate] A robust command-line function manipulation library](/img/ce/39114b1c74af649223db97e5b0e29c.png)
[league/climate] A robust command-line function manipulation library

Alfa: 1 vulnhub walkthrough

Stable and easy-to-use short connection generation platform, supporting API batch generation

kali安装IDEA

Phpstudy安装Thinkphp6(问题+解决)

Shuriken: 1 vulnhub walkthrough
随机推荐
二维码生成API接口,可以直接作为A标签连接
Praying: 1 vulnhub walkthrough
Shuriken: 1 vulnhub walkthrough
14. JS Statements and Comments, Variables and Data Types
Thread Pool (Introduction and Use of Thread Pool)
SQL: DDL, DML, DQL, DCL corresponding introduction and demonstration
Turn trendsoft/capital amount of Chinese capital library
关于tp的apache 的.htaccess文件
Various ways of AES encryption
[campo/random-user-agent] Randomly fake your User-Agent
(3) 字符串
CTF入门笔记之ping
PHP的几个有趣的打开方式:从基本到变态
hackmyvm: again walkthrough
17.JS条件语句和循环,以及数据类型转换
Alfa: 1 vulnhub walkthrough
(2) Thinkphp6 template engine ** tag
动力:2 vulnhub预排
Pycharm打包项目为exe文件
SQL classification, DQL (Data Query Language), and corresponding SQL query statement demonstration