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

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

/* 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. malloc、calloc和realloc比较

    1.先看看它们的原型(stdlib.h): void *malloc( size_t size ); void *calloc( size_t numElements, size_t sizeOfEl ...

  2. SlickOne敏捷开发框架介绍(一) -- 基于Dapper, Mvc和WebAPI 的快速开发框架

    前言:在两年前(最初发布时间:2013年1月9日(csdn),当前文章时间2015年11月10日),项目组推出了基于Dapper,Mvc和WebApi的快速开发框架,随着后续Slickflow产品的实 ...

  3. PPT文化

    PPT文化,yes or no? 知识是有体系的,有的时候刚接触的时候可以 推导技术 ,汇报.吹牛都可以应用上,并且可以让别人想想. 但是实际应用技术,就需要涉及很多详细的技术细节,如果少掉一个看似极 ...

  4. The YubiKey NEO -- Smartcard features

    Smartcard features on the YubiKey NEO YubiKeys are a line of small and low-cost hardware security to ...

  5. linux普通用户获取管理员权限

    原文:http://www.cnblogs.com/likwo/p/3435404.html 测试环境:CentOS 5.5 1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #ad ...

  6. java转换emoji表情

    /** * @Description 将字符串中的emoji表情转换成可以在utf-8字符集数据库中保存的格式(表情占4个字节,需要utf8mb4字符集) * @param str * 待转换字符串 ...

  7. Available Date 相关

    Available Date 写错了怎么办?    http://www.cocoachina.com/bbs/read.php?tid=7224&page=1现在好像不需要改那个availa ...

  8. Android Activity的四种LaunchMode!!!

    本文转自: http://marshal.easymorse.com/archives/2950. 写的非常好,分享给大家!!! 在多Activity开发中,有可能是自己应用之间的Activity跳转 ...

  9. Java Web开发基础(2)-JSP

    上一篇博我粗略的介绍了一下Servlet.粗略是由于博主也刚刚学习这部分的内容,还不是非常懂所以无法讲的非常精细.可是本着二八原则,我还是先继续学习.所以,这篇博客接着JSP的内容.由于.这两个内容关 ...

  10. go test 单元函数测试

    首先安装单元测试包,go get github.com/smartystreets/goconvey/convey 源程序如下,定义了加减乘除4个函数 package test222 import ( ...