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

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

/* 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. .NET程序员提高效率的70多个开发工具

    工欲善其事,必先利其器,没有好的工具,怎么能高效的开发出高质量的代码呢?本文为各ASP.NET 开发者介绍一些高效实用的工具,涉及SQL 管理,VS插件,内存管理,诊断工具等,涉及开发过程的各个环节, ...

  2. 利用Everything开启http服务测试移动端浏览器环境

    一.Everything 简介 Everything本身是一款小巧的文件搜索神器,可以快速的搜索电脑中的文件,速度非常快. 二.使用Everything的http服务 在做移动端浏览器页面时,有时需要 ...

  3. TransactionScope只要一个操作失败,它会自动回滚,Complete表示事务完成

    实事上,一个错误的理解就是Complete()方法是提交事务的,这是错误的,事实上,它的作用的表示本事务完成,它一般放在try{}的结尾处,不用判断前台操作是否成功,如果不成功,它会自己回滚. #re ...

  4. ORA-00918:未明确定义列解决

    ORA-00918:未明确定义列解决 问题:ORA-00918:未明确定义列 eg. select  name,  name  from a  left  join b  on a.flag = b. ...

  5. UVa409_Excuses, Excuses!(小白书字符串专题)

    解题报告 题意: 找包括单词最多的串.有多个按顺序输出 思路: 字典树爆. #include <cstdio> #include <cstring> #include < ...

  6. [html5]使用localStorage兼容低版本Safari无法使用indexeddb的情况

    摘要 简单场景描述:将html5开发的app内嵌入ios app中,有部分数据,需要在本地存储,就想到使用浏览器的localstorage或者indexeddb,另外localstorage存储的方式 ...

  7. 使用jquery dataTable

    jQuery 的插件 dataTables 是一个优秀的表格插件,提供了针对表格的排序.浏览器分页.服务器分页.筛选.格式化等功能.dataTables 的网站上也提供了大量的演示和详细的文档进行说明 ...

  8. Phone重绘机制drawRect 转

    Phone重绘机制drawRect 如何使用iPhone进行绘图.重绘操作iPhone的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩 ...

  9. 【postman】postman访问zuul路由网关,发生Could not get any response 的情况

    情况如下: zuul配置的自定义过滤器,对所有由zuul进行路由转发的请求进行安全验证,如果请求中包含auth,则成功路由,否则失败. 代码如下: package com.swapping.sprin ...

  10. Android之计算两个时间的相差

    参数:   sdate = 2013-07-16 16:14:47 /** * 以友好的方式显示时间 * @param sdate * @return */ public static String ...