栈的应用实例——中缀表达式转换为后缀表达式
声明:本程序读入一个中缀表达式,将该中缀表达式转换为后缀表达式并输出后缀表达式。
注意:支持+、-、*、/、(),并且输入时每输入完一个数字或符号都要加一个空格,特别注意的是在整个表达式输入完成时也要加一个空格后再回车。这是该程序的一个不足之处,有待改进。
/* infix_to_postfix.c */ #include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h> struct op_node
{
char op;
int level;
};
struct stack_record
{
int capacity;
int top_of_stack;
struct op_node *array;
}; struct stack_record *
create_stack( int max_elements )
{
struct stack_record *s; if(max_elements < 5)
{
printf(" stack size is too samll\n");
exit(0);
} s = malloc(sizeof(struct stack_record));
if(s == NULL)
{
perror("malloc");
exit(1);
} s->array = malloc(sizeof(struct op_node) * max_elements);
if(s->array == NULL)
{
perror("malloc");
exit(1);
} s->capacity = max_elements;
s->top_of_stack = -1; return s;
} void
push( struct op_node x, struct stack_record *s)
{
if(s->top_of_stack + 1 == s->capacity)
{
printf("full stack\n");
exit(0);
}
else
{
s->array[++s->top_of_stack] = x;
}
} struct op_node
top( struct stack_record *s)
{
if(s->top_of_stack == -1)
{
printf("top : empty stack\n");
exit(1);
}
else
{
return s->array[s->top_of_stack];
}
} void
pop( struct stack_record *s)
{
if(s->top_of_stack == -1)
{
printf("pop : empty stack\n");
exit(1);
}
else
{
s->top_of_stack--;
}
} struct op_node
top_and_pop( struct stack_record *s)
{
if(s->top_of_stack == -1)
{
printf("top_and_pop : empty stack\n");
exit(1);
}
else
{
return s->array[s->top_of_stack--];
}
} int
main(void)
{
int i;
static int j;
char c;
char data_string[100];
char postfix_expression[100];
struct stack_record *op_stack;
struct op_node op1, op2;
int flag; op_stack = create_stack(100); printf("Please input an infix-expression end with a space: \n"); i = 0;
j = 0;
for(c = getchar(); c != '\n'; c = getchar())
{
switch(c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
flag = 1;
data_string[i++] = c;
break;
case ' ':
if(flag == 1)
{
data_string[i] = '\0';
i = 0;
while(data_string[i] != '\0')
{
postfix_expression[j++] = data_string[i++];
}
postfix_expression[j++] = ' '; i = 0;
data_string[0] = '\0';
}
break;
case '+':
flag = 0;
op1.op = c;
op1.level = 1;
if(op_stack->top_of_stack == -1)
{
push( op1, op_stack );
}
else
{
op2 = top( op_stack );
while( op1.level <= op2.level )
{
if(op2.op != '(')
{
pop( op_stack );
postfix_expression[j++] = op2.op;
postfix_expression[j++] = ' ';
}
else
{
break;
}
if(op_stack->top_of_stack == -1)
{
break;
}
else
{
op2 = top( op_stack );
}
}
push( op1, op_stack );
}
break; case '-':
flag = 0;
op1.op = c;
op1.level = 1;
if(op_stack->top_of_stack == -1)
{
push( op1, op_stack );
}
else
{
op2 = top( op_stack );
while( op1.level <= op2.level )
{
if(op2.op != '(')
{
pop( op_stack );
postfix_expression[j++] = op2.op;
postfix_expression[j++] = ' ';
}
else
{
break;
}
if(op_stack->top_of_stack == -1)
{
break;
}
else
{
op2 = top( op_stack );
}
}
push( op1, op_stack );
}
break; case '*':
flag = 0;
op1.op = c;
op1.level = 2;
if(op_stack->top_of_stack == -1)
{
push( op1, op_stack );
}
else
{
op2 = top( op_stack );
while( op1.level <= op2.level )
{
if(op2.op != '(')
{
pop( op_stack );
postfix_expression[j++] = op2.op;
postfix_expression[j++] = ' ';
}
else
{
break;
}
if(op_stack->top_of_stack == -1)
{
break;
}
else
{
op2 = top( op_stack );
}
}
push( op1, op_stack );
}
break; case '/':
flag = 0;
op1.op = c;
op1.level = 2;
if(op_stack->top_of_stack == -1)
{
push( op1, op_stack );
}
else
{
op2 = top( op_stack );
while( op1.level <= op2.level )
{
if(op2.op != '(')
{
pop( op_stack );
postfix_expression[j++] = op2.op;
postfix_expression[j++] = ' ';
}
else
{
break;
} if(op_stack->top_of_stack == -1)
{
break;
}
else
{
op2 = top( op_stack );
}
}
push( op1, op_stack );
}
break;
case '(':
flag = 0;
op1.op = c;
op1.level = 3;
push( op1, op_stack );
break;
case ')':
flag = 0;
op2 = top_and_pop( op_stack );
while(op2.op != '(')
{
postfix_expression[j++] = op2.op;
postfix_expression[j++] = ' ';
op2 = top_and_pop( op_stack );
}
break; }
} while(!(op_stack->top_of_stack == -1))
{
op2 = top_and_pop( op_stack );
postfix_expression[j++] = op2.op;
postfix_expression[j++] = ' ';
}
postfix_expression[j] = '\0'; printf("postfix_expression is: %s\n", postfix_expression); }
测试结果如下:
再次提醒:输入中缀表达式时一定要以空格结尾,比如下面的输入中输入完7后需要再输入一个空格,然后再按回车。

栈的应用实例——中缀表达式转换为后缀表达式的更多相关文章
- 练习3.20 a 将中缀表达式转换为后缀表达式
//将中缀表达式转换为后缀表达式 int main() { ; ]={,,,,,,,}; char tmp; PtrToStack s; s = CreateStack( MaxSize ); ) { ...
- 利用stack结构,将中缀表达式转换为后缀表达式并求值的算法实现
#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...
- 数据结构Java实现06----中缀表达式转换为后缀表达式
本文主要内容: 表达式的三种形式 中缀表达式与后缀表达式转换算法 一.表达式的三种形式: 中缀表达式:运算符放在两个运算对象中间,如:(2+1)*3.我们从小做数学题时,一直使用的就是中缀表达式. 后 ...
- 中缀表达式转换为后缀表达式(python实现)
中缀表示式转换为后缀表达式 需要一个存放操作符的栈op_stack,输出结果的列表output 步骤: 从左到右遍历表达式: 1. 若是数字,直接加入到output 2. 若是操作符,比较该操作符和o ...
- javascript使用栈结构将中缀表达式转换为后缀表达式并计算值
1.概念 你可能听说过表达式,a+b,a+b*c这些,但是前缀表达式,前缀记法,中缀表达式,波兰式,后缀表达式,后缀记法,逆波兰式这些都是也是表达式. a+b,a+b*c这些看上去比较正常的是中缀表达 ...
- Infix to postfix conversion 中缀表达式转换为后缀表达式
Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...
- Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算
中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...
- 中缀表达式转后缀表达式(Java代码实现)
后缀表达式求值 后缀表达式又叫逆波兰表达式,其求值过程可以用到栈来辅助存储.例如要求值的后缀表达式为:1 2 3 + 4 * + 5 -,则求值过程如下: 遍历表达式,遇到数字时直接入栈,栈结构如下 ...
- NYOJ--257--郁闷的C小加(一)(中缀表达式变后缀表达式 )
郁闷的C小加(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说 ...
随机推荐
- linux下插入的mysql数据乱码问题及第三方工具显示乱码问题
一.lampp环境下的数据库乱码问题 问题描述: 在做mysql练习的时候发现新创建的数据库中插入数据表中的记录中文出现乱码的问题,如下图: 经过多方查证,整里如下文挡: 前提: 我自己的环境是使用的 ...
- 使用postMessage进行react和iframe的数据通信.md
将react的数据传递给iframe 1.首先在父组件(react文件)内引入iframe <iframe style={{border:0,width:"100%",hei ...
- chrome 浏览器 console 加入 jquery 测试调试 一介布衣
chrome 浏览器 console 加入 jquery 测试调试 一介布衣 var jquery = document.createElement('script'); jquery.src = ...
- FastBoot BootLoader Recovery 模式解释
理论上,所有的Android设备都存在着Fastboot/Bootloader模式,不过,由于Android操作系统的开源特性,各厂商的对 自家的相关Android设备都有着各自不同的Fastboot ...
- ASP.NET Web API教程 分页查询
首先增加支持分页的API方法 public IEnumerable<UserInfo> GetUserInfos(int pageindex, int size) { ...
- 用资源管理器右键编译 Visual Studio 解决方案文件
每次改动 VC 工程之后都要重新编译,每次 VS 又会生成调试数据库文件,很费时间,于是研究了一下如何在资源管理器中直接编译,还真发现了解决办法. 以下是适用 Visual Studio 2008 的 ...
- mq刷盘方式
Broker 在收到Producer发送过来的消息后,会存入CommitLog对应的内存映射区中,见CommitLog类的putMessage方法.该方法执行OK后,会判断存储配置中刷盘模式:同步or ...
- Arcgis10.5 python按属性分割图层,属性相同分为一个图层
# coding=utf-8 """ Source code for potential gp tool to create outputs based on attri ...
- 算法:Rate of Growth
Rate of growth describes how an algorithm’s complexity changes as the input size grows. This is comm ...
- RuntimeError: Working outside of application context.
flask执行错误: 问题:RuntimeError: Working outside of application context. 方法: from flask import Flask, cur ...