资源简介
数据结构表达式求值,c语言版,能计算多位数,下载后可以直接运行,注释明确
代码片段和文件信息
#include
#include
#include
//操作数栈的定义及操作
#define max 1000
typedef struct
{
double data[max];
int top;
}SeqStack*PseqStack;
//初始化一个空栈
PseqStack Init_Stack(void)
{
PseqStack s;
s=(PseqStack)malloc(sizeof(SeqStack));
if(s)
s->top=-1;
return s;
}
//判断栈是否为空
int Empty_Stack(PseqStack s)
{
if(s->top==-1)
return 1;
else
return 0;
}
//元素入栈
int Push_Stack(PseqStack sdouble x)
{
if(s->top==max-1)
{
printf(“栈满,不能入栈!\n“);
return 0; //栈满不能入栈
}
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
//元素出栈
int Pop_Stack(PseqStack sdouble *x)
{
if(Empty_Stack(s))
{
printf(“栈空,无元素出栈!\n“);
return 0;
}
else
{
*x=s->data[s->top];
s->top--;
return 1;
}
}
//取栈顶元素
int Get_Stack(PseqStack sdouble *x)
{
if(Empty_Stack(s))
{
printf(“栈空!\n“);
return 0;
}
else
{
*x=s->data[s->top];
return 1;
}
}
//销毁栈
void Destory_Stack(PseqStack *s)
{
if(*s)
free(*s);
*s=NULL;
}
#define M 100
//操作符栈的定义及操作
typedef struct
{
char data[M];
int top;
}seqstack*pseqstack;
pseqstack Init_stack()
{
pseqstack s;
s=(pseqstack)malloc(sizeof(seqstack));
if(s)
s->top=-1;
return s;
}
//判断栈空
int Empty_stack(pseqstack s)
{
if(s->top==-1)
return 1;
else
return 0;
}
//入栈
int Push_stack(pseqstack schar x)
{
if(s->top==M-1)
{
printf(“栈满!“);
return 0;
}
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
//元素出栈
int Pop_stack(pseqstack schar *x)
{
if(Empty_stack(s))
{
return 0;
}
else
{
*x=s->data[s->top];
s->top--;
return 1;
}
}
//获取栈顶元素
int Gettop_stack(pseqstack schar *x)
{
if(Empty_stack(s))
{
return 0;
}
else
{
*x=s->data[s->top];
return 1;
}
}
//销毁栈
void Destory_stack(pseqstack *s)
{
if(*s)
free(*s);
*s=NULL;
return ;
}
//判断读入的字符是否是数字
int IsNum(char c)
{
if(c>=‘0‘&&c<=‘9‘)
return 1;
else
return 0;
}
//返回运算符优先级
int Priority(char op)
{
switch(op)
{
case‘=‘:return 1;
case‘)‘:return 2;
case‘+‘:
case‘-‘:return 3;
case‘*‘:
case‘/‘:return 4;
case‘(‘:return 5;
default:return 0;
}
}
//对中缀表达式直接求值
double Infix_exp_value(char *infixexp)
{
PseqStack s1; //操作数
pseqstack s2; //操作符
char wtopelementch;
double abcsumresult;
s1=Init_Stack();
s2=Init_stack();
if(!s1||!s2)
{
printf(“ to initialize a stack is false!“);
return 0;
}
Push_stack(s2‘=‘);
w=*infixexp;
while((Gettop_stack(s2&ch)ch)!=‘=‘||w!=‘=‘) //取栈顶元素,判断是否是数字
{
if(IsNum(w)) //对数字处理
{
sum=0;
sum=w-‘0‘;
infixexp++;
w=*infixexp;
while(IsNum(w))
{
sum=sum*10+(w-‘0‘);
infixexp++;
w=*infixexp;
}
Push_Stack(s1sum);
}
else //对运算符的处理
{
if(Priority((Gettop_stack(s2&ch)ch))>Priority(w)&&(Gettop_stack(s2&ch)ch)!=‘
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 192613 2013-05-30 21:16 infixexp_value_wei\Debug\infixexp_value_wei.exe
文件 201664 2013-05-30 21:16 infixexp_value_wei\Debug\infixexp_value_wei.ilk
文件 16229 2013-05-30 21:16 infixexp_value_wei\Debug\infixexp_value_wei.obj
文件 225888 2012-06-06 19:56 infixexp_value_wei\Debug\infixexp_value_wei.pch
文件 476160 2012-06-18 09:42 infixexp_value_wei\Debug\infixexp_value_wei.pdb
文件 41984 2013-05-30 21:17 infixexp_value_wei\Debug\vc60.idb
文件 53248 2012-06-18 09:42 infixexp_value_wei\Debug\vc60.pdb
文件 4528 2012-06-18 09:42 infixexp_value_wei\infixexp_value_wei.cpp
文件 4428 2012-03-28 13:49 infixexp_value_wei\infixexp_value_wei.dsp
文件 561 2012-03-28 12:28 infixexp_value_wei\infixexp_value_wei.dsw
文件 41984 2013-05-30 21:17 infixexp_value_wei\infixexp_value_wei.ncb
文件 48640 2013-05-30 21:17 infixexp_value_wei\infixexp_value_wei.opt
文件 955 2013-05-30 21:16 infixexp_value_wei\infixexp_value_wei.plg
目录 0 2013-08-01 11:31 infixexp_value_wei\Debug
目录 0 2013-08-01 11:31 infixexp_value_wei
----------- --------- ---------- ----- ----
1308882 15
- 上一篇:编译原理firstselectfollow实验源代码
- 下一篇:MFC_酒店管理系统
相关资源
- C语言课程设计_学生选修课系统
- 银行管理系统——数据结构C
- c语言银行管理系统
- 严蔚敏.吴伟民等《数据结构(c语言版
- C++数据结构与算法(第4版) 完整版
- 数据结构栈、队列、二叉树、顺序查
- 课程设计:C++学籍管理系统
- 数据结构实验和作业严蔚敏C)
- C航空订票系统-课程设计
- C语言课程设计C语言课程设计.
- C++课程设计分数类
- c语言课程设计迷宫求解.zip
- c++课程设计 职工工资管理系统
- 胡学刚版 数据结构 实验3代码 合工大
- 数据结构题集答案(C语言版)严蔚敏
- 图书管理系统C语言+数据结构与算法
- 《数据结构(c语言版)习题答案》严
- C语言课程设计五子棋游戏带源代码
- 银行账户管理系统c++)
- 数据结构二叉树家谱
- 基于Huffman树的文件压缩C语言源码数据
- 基于顺序表的学生信息管理系统.cpp
- 数据结构课程设计 排序综合(C++)
- 二叉排序树C语言版的!.c
- C语言 机房收费管理系统
- 网络安全课程设计之漏洞扫描
- 操作系统课程设计(最佳适应算法的
- c语言课程设计通讯录管理系统设计报
- 马的极小满覆盖源代码C++编写
- C/C++课程设计-学生成绩管理系统
评论
共有 条评论