博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构-算术表达式求值
阅读量:6680 次
发布时间:2019-06-25

本文共 3097 字,大约阅读时间需要 10 分钟。

#include<stdio.h>

#define ERROR 0
#define OK 1
#define STACKINCREAMENT 10
#define STACK_INT_SIZE 100
#define OVERFLOW -2
typedef int status;
typedef char SElemtype;
#include<malloc.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<windows.h>
typedef struct{
SElemtype *base;
SElemtype *top;
status stacksize;
}sqstack;
typedef struct{
int *base;
int *top;
status stacksize;
}whstack;
void init(sqstack *s){
s->base=(SElemtype*)malloc(STACK_INT_SIZE*sizeof(SElemtype));
if(!s->base) exit(OVERFLOW);
s->top=s->base;
s->stacksize=STACK_INT_SIZE;
}
void Seinit(whstack *s){
s->base=(int*)malloc(STACK_INT_SIZE*sizeof(int));
if(!s->base) exit(OVERFLOW);
s->top=s->base;
s->stacksize=STACK_INT_SIZE;
}
char gettop(sqstack *s,SElemtype *e){
if(s->top==s->base)
return ERROR;
e=s->top-1;
return *e;
}

status Segettop(whstack *s,int *e){

if(s->top==s->base)
return ERROR;
e=s->top-1;
return *e;
}
void push(sqstack *s,SElemtype e){
if(s->top-s->base>=s->stacksize){
s->base = (SElemtype*)realloc(s->base,(s->stacksize+STACKINCREAMENT)*sizeof(SElemtype));
if(!s->base) exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREAMENT;}
*s->top++=e;
}

void Sepush(whstack *s,SElemtype e){

if(s->top-s->base>=s->stacksize){
s->base = (int*)realloc(s->base,(s->stacksize+STACKINCREAMENT)*sizeof(int));
if(!s->base) exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREAMENT;}
*s->top++=e;
}

status pop(sqstack *s,SElemtype *e){

if(s->top==s->base) return ERROR;
*e=*--s->top;
return OK;
}
status Sepop(whstack *s,int *e){
if(s->top==s->base)
return ERROR;
*e=*--s->top;
return OK;
}

int char_to_num(char s){

if(s>=49&&s<=57){
s=s-48;
return s;
}
else
return 0;
}

 

char precede(char a,char b){

int i,j;
char compare[8][8]={' ','+','-','*','/','(',')','#',
'+','>','>','<','<','<','>','>',
'-','>','>','<','<','<','>','>',
'*','>','>','>','>','<','>','>',
'/','>','>','>','>','<','>','>',
'(','<','<','<','<','<','>',' ',
')','>','>','>','>',' ','>','>',
'#','<','<','<','<','<',' ','=',
};
for(i=0;i<8;i++)
if(compare[0][i]==a) break;
for(j=0;j<8;j++)
if(compare[j][0]==b) break;
return compare[j][i];
}
int operate(int a,char x,int c){
int y;
switch(x)
{
case '+':y=a+c;break;
case '-':y=a-c;break;
case '*':y=a*c;break;
case '/':y=a/c;break;
default:return 0;break;
}
return y;
}

int evaluateexpression(){

char c[100];
sqstack OPTR;
whstack OPND;
int a,b,d,i=0;
char x,theta,ch,y;
init(&OPTR);
push(&OPTR,'#');
Seinit(&OPND);

printf("请输入表达式(以#结尾):\n");
scanf("%s",c);
while(1){
if(char_to_num(c[i]))
{
Sepush(&OPND,char_to_num(c[i++]));
}else if(c[i]=='+'||c[i]=='-'||c[i]=='*'||c[i]=='/'||c[i]=='('||c[i]==')'||c[i]=='#')
{
switch(precede(c[i],gettop(&OPTR,&ch)))
{
case '<':push(&OPTR,c[i++]);break;
case '=':pop(&OPTR,&x);break;
case '>':pop(&OPTR,&theta);
Sepop(&OPND,&b);
Sepop(&OPND,&a);
Sepush(&OPND,operate(a,theta,b)); break;
default:break;
}
}
if(c[i]=='#'&&gettop(&OPTR,&y)=='#'){
printf("结果=%d\n",Segettop(&OPND,&d));
return OK;
}

}
}

int main(){

printf(" 算术表达式求值\n");

evaluateexpression();

getch();
}

转载于:https://www.cnblogs.com/xufeng123/p/7822602.html

你可能感兴趣的文章
MySQL数据库服务器优化详细
查看>>
Socket异步通信——使用SocketAsyncEventArgs
查看>>
alfresco_百度百科
查看>>
导入Excel数值读不到,找不到可安装的 ISAM错误
查看>>
[xcode]安装xcode出现一个错误:The Installation Failed.
查看>>
Mysql:bit类型的查询与插入
查看>>
poj2551
查看>>
手动爆库详细流程以及语句解析
查看>>
java模式之装饰模式
查看>>
ubuntu13.04下载android4.0.1源码过程
查看>>
【编程珠玑】读书笔记 第二章 算法
查看>>
百度搜索结果页url参数详解
查看>>
无线Ad-hoc网络的关键技术之路由(转)
查看>>
多线程处理数据来提高后台处理速度
查看>>
openal 基础知识2
查看>>
AIX6.1 线程模型说明
查看>>
jQuery插件开发
查看>>
MVC左边导航,左边内容变,通过AJAX方法实现
查看>>
随笔——学习的一些步骤及注意点(不断更新)
查看>>
16款最受关注的智能手表 苹果iWatch领衔
查看>>