声明:本程序读入一个中缀表达式,将该中缀表达式转换为后缀表达式并输出后缀表达式。

注意:支持+、-、*、/、(),并且输入时每输入完一个数字或符号都要加一个空格,特别注意的是在整个表达式输入完成时也要加一个空格后再回车。这是该程序的一个不足之处,有待改进。

/* 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后需要再输入一个空格,然后再按回车。

栈的应用实例——中缀表达式转换为后缀表达式的更多相关文章

  1. 练习3.20 a 将中缀表达式转换为后缀表达式

    //将中缀表达式转换为后缀表达式 int main() { ; ]={,,,,,,,}; char tmp; PtrToStack s; s = CreateStack( MaxSize ); ) { ...

  2. 利用stack结构,将中缀表达式转换为后缀表达式并求值的算法实现

    #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...

  3. 数据结构Java实现06----中缀表达式转换为后缀表达式

    本文主要内容: 表达式的三种形式 中缀表达式与后缀表达式转换算法 一.表达式的三种形式: 中缀表达式:运算符放在两个运算对象中间,如:(2+1)*3.我们从小做数学题时,一直使用的就是中缀表达式. 后 ...

  4. 中缀表达式转换为后缀表达式(python实现)

    中缀表示式转换为后缀表达式 需要一个存放操作符的栈op_stack,输出结果的列表output 步骤: 从左到右遍历表达式: 1. 若是数字,直接加入到output 2. 若是操作符,比较该操作符和o ...

  5. javascript使用栈结构将中缀表达式转换为后缀表达式并计算值

    1.概念 你可能听说过表达式,a+b,a+b*c这些,但是前缀表达式,前缀记法,中缀表达式,波兰式,后缀表达式,后缀记法,逆波兰式这些都是也是表达式. a+b,a+b*c这些看上去比较正常的是中缀表达 ...

  6. Infix to postfix conversion 中缀表达式转换为后缀表达式

    Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...

  7. Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算

    中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...

  8. 中缀表达式转后缀表达式(Java代码实现)

    后缀表达式求值 后缀表达式又叫逆波兰表达式,其求值过程可以用到栈来辅助存储.例如要求值的后缀表达式为:1 2 3 + 4 * + 5 -,则求值过程如下: 遍历表达式,遇到数字时直接入栈,栈结构如下 ...

  9. NYOJ--257--郁闷的C小加(一)(中缀表达式变后缀表达式 )

    郁闷的C小加(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说 ...

随机推荐

  1. jProfiler远程连接Linux监控jvm1运行状态

    第一步:下载软件官网地址:https://www.ej-technologies.com/download/jprofiler/files,下载一个linux服务端,一个windows客户端 GUI界 ...

  2. Android Studio2.3.3卡在Building 'xxx' Gradle project info的解决方法

    Android Studio版本:V2.3.3 操作系统环境:Ubuntu14.04  64bit 新安装好Android Studio后,在创建新的项目时或者在导入他人的项目代码时,Android ...

  3. sql prompt5安装好了,也破解完成了,然后到SQL里面还是没有提示是为什么?

    勾上这个,祝你成功!

  4. 解决mysql控制台查询数据乱码的问题,有图有真相

    在mysql  控制台当 当为gbk的时候查询的数据是汉字,假设不是则为乱码.  set  names  gbk;  那么查询出来的数据则为汉字

  5. 7407 74LS07 74LV07 74LVC07

    SN7407 Convert TTL Voltage Levels to MOS LevelsHigh Sink-Current CapabilityInput Clamping Diodes Sim ...

  6. 关于myBatis的问题There is no getter for property named 'USER_NAME' in 'class com.bky.model.实例类'

    现在流行的 ssm(spring + struts2 + myBatis)  持久层的mybatis是需要配置映射器的,找了个demo连接的数据库是MySQL 于是就修改了一下弄成了连接Oracle的 ...

  7. 点赞和吐糟Adblock Plus~进阶教程

    前言:Adblock Plus后文都简称ABP,这是一篇ABP进阶教程!用ABP实现flashBlock和NoScript.推荐有相当基础的阅读.刚開始学习的人先看懂这里:http://adblock ...

  8. iOS内存优化及排查方法

    1.IBOutlet 对象需要release 2.不停的往UIView,特别是UIScrollView上add相同SubView.一定要记得清除之前的SubView,并且在dealloc函数中执行该方 ...

  9. ubuntu C++开发环境

    最近在VM中装了Ubuntu,为了开发程序,于是在网上找了些由于C/C++开发环境搭建的资料,供大家参考. 以下文字主要讲如何搭建Code::Blocks+wxWidgets. 搭建步骤: 1.安装编 ...

  10. 进程外Session保存和全局文件错误捕获

    Session深入学习,进程外的Session 当用户登入页面跳转时候,我们会将用户登录信息保存在服务端一个键值对的Session(Session池)中.那么Session池又是在哪里呢? 它最终默认 ...