给定字符串型的算术表达式,实现中缀转后缀并运算得出结果;
 #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. 栈的应用1——超级计算器(中缀与后缀表达式)C语言

    这里要学的程序主要用来实现一个功能——输入表达式输出结果,也就是一个计算器.效果如下: 这个程序主要有两个步骤:1.把中缀表达式转换为后缀表达式:2.计算后缀表达式的结果. 首先先明白几个问题: 1. ...

  2. 编译原理 #03# 龙书中缀转后缀JS实现版

    // 来自龙书第2章2.5小节-简单表达式的翻译器 笔记 既然是语法制导翻译(Syntax-directed translation),那么最重要的东西当然是描述该语言语法的文法,以下为中缀表达式文法 ...

  3. C语言中缀表达式求值(综合)

    题前需要了解的:中缀.后缀表达式是什么?(不知道你们知不知道,反正我当时不知道,搜的百度) 基本思路:先把输入的中缀表达式→后缀表达式→进行计算得出结果 栈:"先进先出,先进后出" ...

  4. Java数据结构和算法(六)——前缀、中缀、后缀表达式

    前面我们介绍了三种数据结构,第一种数组主要用作数据存储,但是后面的两种栈和队列我们说主要作为程序功能实现的辅助工具,其中在介绍栈时我们知道栈可以用来做单词逆序,匹配关键字符等等,那它还有别的什么功能吗 ...

  5. C++ 中缀转后缀表达式并求值

    //中缀转后缀 #include<iostream> #include<stack> using namespace std; int prio(char x){ ; ; ; ...

  6. Java数据结构和算法(六):前缀、中缀、后缀表达式

    前面我们介绍了三种数据结构,第一种数组主要用作数据存储,但是后面的两种栈和队列我们说主要作为程序功能实现的辅助工具,其中在介绍栈时我们知道栈可以用来做单词逆序,匹配关键字符等等,那它还有别的什么功能吗 ...

  7. java四则运算----前缀、中缀、后缀表达式

    接到一个新需求,需要实现可配置公式,然后按公式实现四则运算. 刚拿到需求,第一反应就是用正则匹配‘(’,‘)’,‘+’,‘-’,‘*’,‘/’,来实现四则运算,感觉不复杂. 然后开始coding.发现 ...

  8. 【C++】朝花夕拾——中缀转后缀

    对于简单的四则运算而言,后缀表达式可以通过使用栈(stack)快速算出结果 ==================================我是分割线======================= ...

  9. 洛谷P1981 表达式求值 题解 栈/中缀转后缀

    题目链接:https://www.luogu.org/problem/P1981 这道题目就是一道简化的中缀转后缀,因为这里比较简单,只有加号(+)和乘号(*),所以我们只需要开一个存放数值的栈就可以 ...

随机推荐

  1. (转)CentOS 日志分析

     1.了解日志文件    linux的日志文件可以说是最有用的了,日志文件可以让我们了解系统所处的状态,比如能查出哪些用户有登入,这也涉及相关的安全问题.如果我们不懂得分析日志,可能我们都不知道有些用 ...

  2. C#中使用SendMessage在进程间传递数据的实例

    原文:C#中使用SendMessage在进程间传递数据的实例 1 新建解决方案SendMessageExample 在解决方案下面新建三个项目:CopyDataStruct,Receiver和Send ...

  3. Tracing JIT

    在一个从Java源码编译到JVM字节码的编译器(如javac.ECJ)里,一个“编译单元”(CompilationUnit)指的是一个Java源文件.而在Dalvik VM的JIT里也有一个结构体名为 ...

  4. cocos2d-x Loading界面实现资源加载

    有时候场景中的资源加载过多的话就会引起游戏进入的时候很卡,因为那是边加载边显示.在tests例子里面有一个很好的例子叫做TextureCacheTest,里面讲解了如何写loading. #inclu ...

  5. AzCopy – 跨帐户复制 Blob

    您可以随时从 aka.ms/AzCopy 下载最新版本. 去年4月发布的版本中的新增功能 支持跨帐户复制 Blob:AzCopy 允许您在相同存储帐户内或不同存储帐户之间复制 Blob(有关跨帐户 B ...

  6. 【转】Win7+Ubuntu12.04.1硬盘安装错误及解决方案----不错

    原文网址:http://blog.csdn.net/ys_073/article/details/8310115 前言: 说起来了,为了在Win7上进行硬盘安装Ubuntu浪费了整整一个晚上的时间.装 ...

  7. Android 对话框简介

    对话框(Dialog)是程序运行过程中弹出的窗口,Android中有好多种对话框,如警告对话框,进度对话框,列表对话框,单选对话框,日期选择对话框,时间选择对话框等: 下面用几个例子来演示一下各种对话 ...

  8. (2)自己写一个简单的servle容器

    自己写一个简单的servlet,能够跑一个简单的servlet,说明一下逻辑. 首先是写一个简单的servlet,这就关联到javax.servlet和javax.servlet.http这两个包的类 ...

  9. 深入理解linux网络技术内幕读书笔记(一)--简介

    Table of Contents 1 基本术语 1.1 本书常用的缩写 2 引用计数 2.1 引用计数函数 3 垃圾回收 3.1 异步 3.2 同步 4 函数指针 4.1 缺点 5 goto语句 5 ...

  10. Hive集成HBase详解

    摘要 Hive提供了与HBase的集成,使得能够在HBase表上使用HQL语句进行查询 插入操作以及进行Join和Union等复杂查询   应用场景 1. 将ETL操作的数据存入HBase 2. HB ...