中序表达式转后序的代码
来源:互联网 发布:人人商城源码 编辑:程序博客网 时间:2024/06/05 00:38
//p55页#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX 20void intopostfix( char infix_q[], int rear);int compare( char stack_o, char infix_o );char infix_priority[9] = {'#', ')', '+', '-', '*', '/', '^', ' ', '(' };char stack_priority[8] = { '#', '(', '+', '-', '*', '/', '^', ' '};int main(){ int rear = -1; char infix_q[MAX];//储存使用者输入中序者的队列 printf("****这里是分割线哟------------------------\n"); printf("使用的运算符\n"); printf(" ^: 指数\n"); printf(" *: 乘法 /: 除法\n"); printf(" +: 家法 -: 减法\n"); printf(" (: 左括号 ):右括号 "); printf(" -----------这里依旧是分割线"); printf(" 请输入中缀表达式:\n"); /*while( infix_q[rear] != '\n' ) infix_q[++rear] = getchar();*/ gets(infix_q); rear = strlen( infix_q ); infix_q[rear] = '#';//队列结束时插入#作为结束符号 // printf("后缀表达式:\n"); intopostfix( infix_q, rear); printf("\n"); return 0;}void intopostfix(char infix_q[], int rear){ //printf("rear"); int top = 0,ctr, tag = 1; char stack_t[MAX]; stack_t[top] = '#'; for(ctr = 0; ctr <= rear; ctr++) { switch( infix_q[ctr]) { case ')'://输入是左括号时,输出堆栈内运算符,直至堆栈内为左括号 while( stack_t[top] != '(' ) { printf("%c", stack_t[top--]); } top--; break; case '#'://输入#,堆栈内还未输出的运算符输出 while( stack_t[top] != '#' ) { printf("%c", stack_t[top--]); } break; case '(': case '^': case '*': case '/': case '+': case '-': while( compare( stack_t[top], infix_q[ctr] ) )//如果输入的运算符在表达式中的优先权,小于TOP在堆栈内所指向的运算符在堆栈中的优先权,则将堆栈所指运算符输出,若大于,则将输入运算符放入堆栈 printf(" %c",stack_t[top--]); stack_t[++top] = infix_q[ctr]; // tag = 1; break; /*case '+'://这一段代码是从书上抄的,但是加上去就会出错,改完就对了,好开心,也就是说tag没有什么用处呀 case '-': if( tag == 1 ) { stack_t[++top] = infix_q[ctr]; tag = 2; } else { while( compare( stack_t[++top], infix_q[ctr])) printf("%c",stack_t[top--]); stack_t[++top] = infix_q[ctr]; tag = 1; } break;*/ default: printf(" %c", infix_q[ctr]); /*if( tag == 2 ) printf(" %c",stack_t[top--]); tag = 0;*/ break; } }}int compare( char stack_o, char infix_o ){ int index_s = 0, index_i = 0; while( stack_priority[index_s] != stack_o) index_s++; while( infix_priority[index_i] != infix_o) index_i++; return (index_s/2) >= (index_i/2) ? 1 : 0;}
阅读全文
0 0
- 中序表达式转后序的代码
- 中序表达式转后序表达式
- 中序表达式转后序表达式的java实现
- 栈的应用-中序表达式转后序表达式
- 中序表达式转后序表达式(带括号)
- asp中除去html代码的函数(用正则表达式)
- Java中正则表达式的实现_代码
- 内核代码中一个逗号表达式比较特别的用法
- 用正则表达式去除dreamweaver代码中多余的空行
- 颜色的代码表达式
- 表达式计算的代码
- 中序表达式转换成后序表达式问题(栈的应用)
- 中序表达式转换成后序表达式问题(栈的应用)
- 表达式计算器的核心代码
- VS代码中常用 正则表达式
- 在java代码中使用正则表达式
- 中序表达式的直接计算
- 关于代码中空白、注释等字符的统计(正则表达式的使用)
- CDESK激活附加功能T-CODE: CDESK_CUS
- Android集成百度地图(第一节)
- HTML1.1.z
- Server【SVN环境搭建】关于toroiseSVN Checkout 卡死现象
- [My SQL] 使用存储过程
- 中序表达式转后序的代码
- 使用react-native-image-crop-picker上传图片
- deepin系统下部署Python3.5的开发及运行环境
- error RC1015: cannot open include file 'afxres.h'
- cxGrid.OptionsView
- Longest Continuous Increasing Subsequence 问题及解法
- TensorFlow(gpu版本)安装教程 Ubuntu16.04
- CentOS7 安装RocketMQ并使用
- 列表单选效果