数据结构 栈 无括号算术表达式处理算法 C语言版
来源:互联网 发布:windows phone刷安卓 编辑:程序博客网 时间:2024/06/15 14:35
#include "seqstack.h"
#include "stdio.h"
#include <conio.h>
char ch;
int ExpEvaluation()/*读入一个简单算术表达式并计算
其值。operatsign和operatdata分别为运算符栈和运算
数栈OPS为运算符集合*/
{
char x,y;
char op;
int a,b,v;
#include "stdio.h"
#include <conio.h>
char ch;
int ExpEvaluation()/*读入一个简单算术表达式并计算
其值。operatsign和operatdata分别为运算符栈和运算
数栈OPS为运算符集合*/
{
char x,y;
char op;
int a,b,v;
nStack operatdata;
strStack operatsign;
nInitStack(&operatdata);
strInitStack(&operatsign);
strPush(&operatsign,'#');
strStack operatsign;
nInitStack(&operatdata);
strInitStack(&operatsign);
strPush(&operatsign,'#');
printf("\nPlease input an expression (Ending with #) :\n");
ch=getchar();
ch=getchar();
strGetTop(&operatsign,&y);
while(ch!='#'||y!='#') /* strGetTop()通过函数值返回栈顶元素*/
{
if(!In(ch)) /*不是运算符是运算数*/
{
{
if(!In(ch)) /*不是运算符是运算数*/
{
int temp; /*存放数字的临时变量*/
temp=ch-'0';
/*将字符转换为十进制数*/
fflush(stdin);
ch=getchar();
while(!In(ch)) //用ch逐个读入运算数的各位
数码并转化为十进制数temp
{
temp=temp*10+ch-'0'; // 将逐个读入运算数
的各位转化为十进制数
fflush(stdin);
ch=getchar();
}
nPush(&operatdata,temp);
}
else
switch(Compare(y,ch))
{
case '<':
strPush(&operatsign,ch);
fflush(stdin);
ch=getchar();
break;
case '=':
strPop(&operatsign,&x);
fflush(stdin);
ch=getchar();
break;
case '>':
strPop(&operatsign,&op);
nPop(&operatdata,&b);
nPop(&operatdata,&a);
v=Execute(a,op,b); /* 对a和b进行op运算 */
nPush(&operatdata,v);
break;
}
strGetTop(&operatsign,&y);
}
nGetTop(&operatdata,&v);
return (v);
}
void main()
{
int result;
result=ExpEvaluation();
printf("\n%d",result);
}
temp=ch-'0';
/*将字符转换为十进制数*/
fflush(stdin);
ch=getchar();
while(!In(ch)) //用ch逐个读入运算数的各位
数码并转化为十进制数temp
{
temp=temp*10+ch-'0'; // 将逐个读入运算数
的各位转化为十进制数
fflush(stdin);
ch=getchar();
}
nPush(&operatdata,temp);
}
else
switch(Compare(y,ch))
{
case '<':
strPush(&operatsign,ch);
fflush(stdin);
ch=getchar();
break;
case '=':
strPop(&operatsign,&x);
fflush(stdin);
ch=getchar();
break;
case '>':
strPop(&operatsign,&op);
nPop(&operatdata,&b);
nPop(&operatdata,&a);
v=Execute(a,op,b); /* 对a和b进行op运算 */
nPush(&operatdata,v);
break;
}
strGetTop(&operatsign,&y);
}
nGetTop(&operatdata,&v);
return (v);
}
void main()
{
int result;
result=ExpEvaluation();
printf("\n%d",result);
}
阅读全文
0 0
- 数据结构 栈 无括号算术表达式处理算法 C语言版
- 数据结构-栈的应用-算术表达式小括号匹配
- 学习笔记------数据结构(C语言版)栈应用 括号匹配
- 数据结构C语言版之括号匹配
- 数据结构C语言版算法2.1
- 数据结构C语言版算法2.2
- 《数据结构(C语言版)》- 栈
- 学习笔记------数据结构(C语言版)栈应用 表达式求值
- 数据结构C语言版之表达式求值
- 数据结构---栈:一般算术表达式
- c语言版数据结构(奇迹冬瓜)-栈实战(3)括号匹配的检测
- 数据结构与算法(C语言版)__栈
- 数据结构与算法(C语言版)__链式栈
- Dijkstra的双栈算术表达式(未省略括号)求值算法(algs4)
- C/C++数据结构通过顺序栈实现括号()匹配算法
- 数据结构中利用栈计算算术表达式
- java中栈的应用-带括号的算术表达式
- c语言版数据结构(奇迹冬瓜)-栈实战(4)表达式求值
- Java8新特性——StreamAPI(一)
- quick select to find the kth smallest element in array
- View绘制流程源码解析
- Java子父类初始化顺序,及重写方法后的访问修饰符,异常抛出
- Java并发编程学习记录#3
- 数据结构 栈 无括号算术表达式处理算法 C语言版
- 电脑突然没有声音了
- 文本编辑三剑客之一 ———sed
- 机器学习中的KNN算法
- 2017秋招笔试题(php)部分题
- options.js合并各个option选项
- CSS3盒子模型
- 给UIButton添加URL图片、给UIImageView添加URL图片
- HTTP返回状态码简介