给定字符串型的算术表达式,实现中缀转后缀并运算得出结果;
 #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. sqlserver 数据库里面金额类型为什么不建议用float,实例告诉你为什么不能。

    项目当中如果设计到金额类型的数据,你是否有考虑过为什么不能用float类型. 这里举个例子: DECLARE @price1 FLOAT; SET @price1 = 1; SET @price1 = ...

  2. HADOOP之HIVE+MYSQL,HBASE+ZOOKEEPER

    这个搞得有劲哈哈 继续...继续....

  3. 国威电话机WS824(5D)-3型调试文档--可以转行啦

    多了一万多搞的机器,花了我和同事们两三个晚上,最近还要打技术支持得到的经验... 可以转行作弱电啦啦~~~) 一,外线分组调试: 默认设置为所有内线端口可用1,2,13,14,15,16打出.(16个 ...

  4. Powershell访问数组

    数组的元素可以使用索引寻址,第一个元素的索引为0,第i个元素的索引为i-1,最后一个元素的索引为Count-1,但是Powershell为了使用方便,直接可以将 -1 作为最后的一个元素的索引. PS ...

  5. wikioi1082【线段树练习 3 】

    题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数n,接下 ...

  6. windows对象总结

    这篇文章是对windows对象的总结,在winows编程中,windows对象,句柄是一个基本概念,理解这些概念有助于后面的windows编程学习.文章的形式还是以解答问题的方式来组织. 注:在文中, ...

  7. C# 约瑟夫环算法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. 多封装,少开放。强烈建议C++标准添加class之间的注入机制

    近日在改动了一下下引擎代码(为了自己的组件),发现有些接口是仅仅有特定类及其内部函数才去訪问,却不使用友元声明的形式进行数据訪问--当然使用了普通非virtual的形式也就是意味着不建议重载. 故此: ...

  9. 漫话Unity3D(一)

    前言 使用Unity已经有将近半年时间了,尽管项目还仅仅是个半成品,可是Unity差点儿相同玩熟了.这里把使用过程中碰到的问题梳理一遍.不会涉及到太过详细的功能和代码,可是假设开发一个网游又都会涉及到 ...

  10. Android自定义控件(三)——有弹性的ListView

    上一次我们试验了有弹性的ScrollView.详情 这一次,我们来试验有弹性的ScrollView. 国际惯例,效果图: 主要代码: import android.content.Context; i ...