主题:(原创)栈的应用(算术表达式求值)
谁有数据结构题集的实习部分答案 给我份做参考啊共享下 有的联系我QQ327489180
自己写了个和大家共享下
认为好用的 给我顶下~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdlib.h>
#include<stdio.h>
#define stack_init_size1 40
#define stack_init_size2 20
#define stackincrement1 40
#define stackincrement2 20
typedef struct{
int *base;
int *top;
int stacksize;
}s_stack;
typedef struct{
char *base;
char *top;
int stacksize;
}f_stack;
static char youxian[7][7]=
{
{'>','>','<','<','<','>','>'},{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','>'},{'>','>','>','>','>','>','>'},
{'<','<','<','<','<','>','='}
};
int in(char c)
{
switch(c)
{
case '+': return 0;
case '-': return 1;
case '*': return 2;
case '/': return 3;
case '(': return 4;
case ')': return 5;
case '#':return 6;
default:return 9;
}
}
void s_push(s_stack *s,int e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(int *)realloc(s->base,(s->stacksize+stackincrement1)*sizeof(int));
if(!s->base) exit(0);
s->top=s->base+s->stacksize;
s->stacksize+=stackincrement1;
}
*s->top=e; s->top++;
/*printf("the s_stack has the data:%d\n",*(s->top-1));*/
}
void f_push(f_stack *f,char e)
{
if(f->top-f->base>=f->stacksize)
{
f->base=(char *)realloc(f->base,(f->stacksize+stackincrement2)*sizeof(char));
if(!f->base) exit (0);
f->top=f->base+f->stacksize;
f->stacksize+=stackincrement2;
}
*f->top=e;f->top++;
/* printf("the f_stack has the sign:%c\n",*(f->top-1));*/
}
int s_gettop(s_stack *s)
{
s->top--;
return *s->top;
}
char f_gettop(f_stack *f)
{
f->top--;
return *f->top;
}
char pduan(f_stack *f,char c)
{
int m,n;
char w;
m=in(c);
w=*(f->top-1);
n=in(w);
return youxian[n][m];
}
int yunsuan(int p,char r,int q)
{
switch(r)
{
case '+': return q+p;
case '-': return q-p;
case '*': return q*p;
case '/': return q/p;
}
}
自己写了个和大家共享下
认为好用的 给我顶下~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdlib.h>
#include<stdio.h>
#define stack_init_size1 40
#define stack_init_size2 20
#define stackincrement1 40
#define stackincrement2 20
typedef struct{
int *base;
int *top;
int stacksize;
}s_stack;
typedef struct{
char *base;
char *top;
int stacksize;
}f_stack;
static char youxian[7][7]=
{
{'>','>','<','<','<','>','>'},{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','>'},{'>','>','>','>','>','>','>'},
{'<','<','<','<','<','>','='}
};
int in(char c)
{
switch(c)
{
case '+': return 0;
case '-': return 1;
case '*': return 2;
case '/': return 3;
case '(': return 4;
case ')': return 5;
case '#':return 6;
default:return 9;
}
}
void s_push(s_stack *s,int e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(int *)realloc(s->base,(s->stacksize+stackincrement1)*sizeof(int));
if(!s->base) exit(0);
s->top=s->base+s->stacksize;
s->stacksize+=stackincrement1;
}
*s->top=e; s->top++;
/*printf("the s_stack has the data:%d\n",*(s->top-1));*/
}
void f_push(f_stack *f,char e)
{
if(f->top-f->base>=f->stacksize)
{
f->base=(char *)realloc(f->base,(f->stacksize+stackincrement2)*sizeof(char));
if(!f->base) exit (0);
f->top=f->base+f->stacksize;
f->stacksize+=stackincrement2;
}
*f->top=e;f->top++;
/* printf("the f_stack has the sign:%c\n",*(f->top-1));*/
}
int s_gettop(s_stack *s)
{
s->top--;
return *s->top;
}
char f_gettop(f_stack *f)
{
f->top--;
return *f->top;
}
char pduan(f_stack *f,char c)
{
int m,n;
char w;
m=in(c);
w=*(f->top-1);
n=in(w);
return youxian[n][m];
}
int yunsuan(int p,char r,int q)
{
switch(r)
{
case '+': return q+p;
case '-': return q-p;
case '*': return q*p;
case '/': return q/p;
}
}