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

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

/* 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. cocos2dx 字符串拼接

    ;i<;i++){ ]; sprintf(str,"%d",i); ]; strcpy(totalFilename, "game_loading") ; ...

  2. centos配置虚拟主机virtualhost,让服务器支持多网站多域名(转)

    如何让centos(redhat)配置虚拟主机,让服务器支持多个网站,针对Apache,只需要你修改apache配置文件/etc/httpd/conf/httpd.conf即可. 里面有个exampl ...

  3. nginx简单代理配置

    原文:https://my.oschina.net/wangnian/blog/791294 前言  Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器, ...

  4. wpf z

    Finding an ancestor of a WPF dependency object This is a simple snippet which helps you to find a sp ...

  5. jsp中简易版本的图片上传程序

    1.下载相应的组件的最新版本 Commons FileUpload 可以在http://jakarta.apache.org/commons/fileupload/下载 附加的Commons IO   ...

  6. 在进行form提交时,根据form的选择,在javascript中进行特定提交

    1.html代码片段 <form name="form1" method="post" action=""> <selec ...

  7. [翻译] 学习iOS开发的建议:如何从菜鸟到专家

    [文章原地址] http://mobile.tutsplus.com/tutorials/iphone/ios-quick-tip-from-novice-to-expert/ 翻译有误之处请勿见笑, ...

  8. 软件系统分析师与架构师技能大PK(您具备了哪些呢?)

    博客转处:http://blog.csdn.net/china_video_expert/article/details/38335613 软件系统分析师与架构师在职责与技能方面没有明显的界线,你中有 ...

  9. 深入分析JavaWeb Item7 -- HttpServletResponse详解

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...

  10. 如何利用启明星Portal门户系统的Page模块构建文档库

    利用启明星门户系统的Page模块构架可以搭建企业内部的文档管理系统. (一)应用背景 企业内部通常都会使用共享网盘的方式来存放不同部门之间的文档,例如管理员在服务器上对人事部门增加人事部文档文件夹. ...