C语言 中缀转后缀
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include"stack.h"
#define SIZE 100
#define TYPE char
typedef struct Node* pNode;
typedef struct Node node;
typedef pNode Stack; char postfix[SIZE]; struct Node
{
TYPE data;
struct Node* next;
};
int isEmpty(Stack s);
void Pop(Stack s);
void Push(Stack s,TYPE element);
TYPE Top_of_stack(Stack s);
Stack CreatStack();
void makeEmpty(Stack s); Stack CreatStack()
{
Stack s=(Stack)malloc(sizeof(node));
s->next=NULL;
makeEmpty(s);
return s;
}
void makeEmpty(Stack s)
{
if(s==NULL)
printf("you need creat a stack at first");
while(!isEmpty(s))
Pop(s);
}
int isEmpty(Stack s)
{
return s->next==NULL;
}
void Pop(Stack s)
{
if(isEmpty(s))
printf("Stack is empty");
else
{
pNode temp=s->next;
s->next=s->next->next;
free(temp);
} }
void Push(Stack s,TYPE element)
{
pNode temp=(Stack)malloc(sizeof(node));
if(temp)
{
temp->data=element;
temp->next=s->next;
s->next=temp;
}
}
TYPE Top_of_stack(Stack s)
{
if(isEmpty(s))
{
printf("Stack is empty");
return ;
}
else
return s->next->data;
} #endif // STACK_H_INCLUDED
stack.h
#include <stdio.h>
#include <stdlib.h>
#include"stack.h"
#define SIZE 100 char postfix[SIZE]; // if op > instack push else pop;
// return the priority of operator of expression minus
//that of stack 栈外优先级减去栈内优先级
int priority(char op_of_expression,char ch_in_stack)
{
int op=;
int ch=;
if(op_of_expression=='+'||op_of_expression=='-')
op=;
else if(op_of_expression=='*'||op_of_expression=='/')
op=;
else if(op_of_expression=='(')
op=;
else
printf("wrong operator"); if(ch_in_stack=='+'||ch_in_stack=='-')
ch=;
else if(ch_in_stack=='*'||ch_in_stack=='/')
ch=;
else if(ch_in_stack=='(')
ch=;
else
printf("wrong operator"); return op-ch;
}
int isOperator(char ch)
{
switch (ch)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
return ;
break;
default:
return ;
break;
}
}
void Infix_to_Pofix(char* s)
{
int index=; char ch;
Stack stack=CreatStack();
makeEmpty(stack);
while((ch=*s)!='\0')
{
if(!isOperator(ch))
{
postfix[index++]=ch;
s++;
if(isOperator(*s)||*s=='\0')
postfix[index++]='_';
}
else
{
if(isEmpty(stack))
{
Push(stack,ch);
s++;
continue;
}
else
{
if(ch==')')
{
while(!isEmpty(stack)&&Top_of_stack(stack)!='(')
{
postfix[index++]=Top_of_stack(stack);
Pop(stack);
}
Pop(stack);
s++;
continue;
}
else if(priority(ch,Top_of_stack(stack))>)
{
Push(stack,ch);
s++;
continue;
}
else
{
while(!isEmpty(stack)&&priority(ch,Top_of_stack(stack))<=)
{
postfix[index++]=Top_of_stack(stack);
Pop(stack);
}
Push(stack,ch);
s++;
continue;
}
// else if(priority(ch,Tops))
}
} }
while(!isEmpty(stack))
{
postfix[index++]=Top_of_stack(stack);
Pop(stack);
}
//postfix[index++]='\0';
} int compute(char *str)
{
#undef TYPE
#define TYPE int
Stack s=CreatStack();
makeEmpty(s);
int temp_int;
while((*str)!='\0')
{ if(isdigit(*str))
{
Push(s,(int)atof(str));
//printf("ss%d",(int)atof(str));
while(((*str)!='_'))
{
str++;
}
str++;
}
else if(isOperator((*str)))
{
switch(*str)
{
case'*':
temp_int=Top_of_stack(s);
Pop(s);
//printf("%d\n",Top_of_stack(s));
temp_int*=Top_of_stack(s);
Pop(s);
Push(s,temp_int);
//printf("%d\n",Top_of_stack(s));
break;
case'-':
temp_int=Top_of_stack(s);
Pop(s);
//printf("%d\n",Top_of_stack(s));
temp_int=Top_of_stack(s)-temp_int;
Pop(s);
Push(s,temp_int);
//printf("%d\n",Top_of_stack(s));
break;
case'/':
temp_int=Top_of_stack(s);
Pop(s);
//printf("%d\n",Top_of_stack(s));
temp_int=Top_of_stack(s)/temp_int;
Pop(s);
Push(s,temp_int);
//printf("%d\n",Top_of_stack(s));
break;
case'+':
temp_int=Top_of_stack(s);
Pop(s);
//printf("%d\n",Top_of_stack(s));
temp_int+=Top_of_stack(s);
Pop(s);
Push(s,temp_int);
//printf("%d\n",Top_of_stack(s));
break;
default:
printf("wrong");
break;
}
str++;
} }
return temp_int;
} int main()
{
char ch[]="10+(3*6-8)/2";
Infix_to_Pofix(ch);
printf("%s\n",postfix);
//postfix="134.4";
// printf("%d\n",(int)atof("34_34*"));
printf("result:%d\n",compute(postfix)); return ;
}
中缀转后缀运算
C语言 中缀转后缀的更多相关文章
- 栈的应用1——超级计算器(中缀与后缀表达式)C语言
这里要学的程序主要用来实现一个功能——输入表达式输出结果,也就是一个计算器.效果如下: 这个程序主要有两个步骤:1.把中缀表达式转换为后缀表达式:2.计算后缀表达式的结果. 首先先明白几个问题: 1. ...
- 编译原理 #03# 龙书中缀转后缀JS实现版
// 来自龙书第2章2.5小节-简单表达式的翻译器 笔记 既然是语法制导翻译(Syntax-directed translation),那么最重要的东西当然是描述该语言语法的文法,以下为中缀表达式文法 ...
- C语言中缀表达式求值(综合)
题前需要了解的:中缀.后缀表达式是什么?(不知道你们知不知道,反正我当时不知道,搜的百度) 基本思路:先把输入的中缀表达式→后缀表达式→进行计算得出结果 栈:"先进先出,先进后出" ...
- Java数据结构和算法(六)——前缀、中缀、后缀表达式
前面我们介绍了三种数据结构,第一种数组主要用作数据存储,但是后面的两种栈和队列我们说主要作为程序功能实现的辅助工具,其中在介绍栈时我们知道栈可以用来做单词逆序,匹配关键字符等等,那它还有别的什么功能吗 ...
- C++ 中缀转后缀表达式并求值
//中缀转后缀 #include<iostream> #include<stack> using namespace std; int prio(char x){ ; ; ; ...
- Java数据结构和算法(六):前缀、中缀、后缀表达式
前面我们介绍了三种数据结构,第一种数组主要用作数据存储,但是后面的两种栈和队列我们说主要作为程序功能实现的辅助工具,其中在介绍栈时我们知道栈可以用来做单词逆序,匹配关键字符等等,那它还有别的什么功能吗 ...
- java四则运算----前缀、中缀、后缀表达式
接到一个新需求,需要实现可配置公式,然后按公式实现四则运算. 刚拿到需求,第一反应就是用正则匹配‘(’,‘)’,‘+’,‘-’,‘*’,‘/’,来实现四则运算,感觉不复杂. 然后开始coding.发现 ...
- 【C++】朝花夕拾——中缀转后缀
对于简单的四则运算而言,后缀表达式可以通过使用栈(stack)快速算出结果 ==================================我是分割线======================= ...
- 洛谷P1981 表达式求值 题解 栈/中缀转后缀
题目链接:https://www.luogu.org/problem/P1981 这道题目就是一道简化的中缀转后缀,因为这里比较简单,只有加号(+)和乘号(*),所以我们只需要开一个存放数值的栈就可以 ...
随机推荐
- CSS3 Flexbox布局那些事
相信研究过CSS3的同学对Flexbox布局一定不会陌生(作为一个未来主流的布局方式,至少有所耳闻).最近完成了两个项目:一个是移动端H5项目,一个是嵌入HTML页面的mac客户端项目.为了庆祝这两个 ...
- cf Strings of Power
http://codeforces.com/contest/318/problem/B #include <cstdio> #include <cstring> #includ ...
- C#调用WebService服务(动态调用)
原文:C#调用WebService服务(动态调用) 1 创建WebService using System; using System.Web.Services; namespace WebServi ...
- Powershell 条件操作符
Powershell 中的比较运算符-eq :等于-ne :不等于-gt :大于-ge :大于等于-lt :小于-le :小于等于-contains :包含-notcontains :不包含 进行比较 ...
- javascript学习笔记——chrome等提示找不到“getElementsByTagName”的一种解决方法
最近学习是写了一个小网页,前台有个下拉框是通过后天的xml配置的,在写好代码后使用发现在IE9以及之前的IE浏览器都可以正常获取,但是IE10,chrome和firefox都会在获取一个标签时报get ...
- CSS中display:block的使用介绍
在CSS的规定中,每个页面元素都有一个display的属性,用于确定这个元素的类型是行内元素,还是块级元素: (1)行内元素:元素在一行内水平排列,依附于其他块级元素存在,因此,对行内元素设置高度.宽 ...
- bzoj1148
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1148 很常见的排序贪心题...... 假设我们得到了一个最优序列,记s[n]=w[1]+w[2 ...
- JVM基础和调优(六)
JVM设置过程中的一般的规范 在JVM的设置中,年轻代的设置比较的重要,因为年轻代存储空间分配的比较的块,可以说触发GC的机会比较的大. 默认的情况下:-XX:NewRatio 默认为2 说明:年轻 ...
- linux下 tags相关
在vim中配置好了YouCompleteMe插件,发现把光标移动到函数名下再按ctrl+],并不能跳转到该函数的定义处.解决办法: 1.先查看有没有安装ctags,运行ctags –version查看 ...
- day49
几天没写了 这几天比较麻木呢 各种课程的再看 想买一直不舍得money 今天下定决心买了 这样我也静下心好好备战把 一天背的东西好多 政治和作文也是背了就忘记 尽力把 今天的买的课很悬乎 就不在这说了 ...