|
|
#2
2010-04-28 23:41
|
程序代码:#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW -2
#define OK 1
#define TURE 1
#define Error 0
typedef char ElemType;
typedef ElemType OperandType; /*操作数*/
typedef char OperatorType;
typedef struct
{ float *top;
float *base;
int stacksize;
}Sqstack;
typedef struct
{ char *top;
char *base;
int stacksize;
}SSqstack;
char OP[8]={'+','-','*','/','(',')','#','\0'};
int m[7][7]={1,1,2,2,2,1,1,
1,1,2,2,2,1,1,
1,1,1,1,2,1,1,
1,1,1,1,2,1,1,
2,2,2,2,2,0,-1,
1,1,1,1,-1,1,1,
2,2,2,2,2,-1,0};/*1 > 2 < 0 = -1 不存在*/
void initstack(Sqstack *S)
{ S->base=(float *)malloc(STACK_INIT_SIZE*sizeof (float));
if(!S->base)exit(OVERFLOW);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
void Initstack(SSqstack *M)
{ M->base=(char)malloc(STACK_INIT_SIZE*sizeof (char));
if(!M->base)exit(OVERFLOW);
M->top=M->base;
M->stacksize=STACK_INIT_SIZE;
}
void push(Sqstack *S,float n)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(float *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(float));
if(!S->base)exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=n;
}
void Push(SSqstack *M,char e)
{
if(M->top-M->base>=M->stacksize)
{
M->base=(char *)realloc(M->base,(M->stacksize+STACKINCREMENT)*sizeof(char));
if(!M->base)exit(OVERFLOW);
M->top=M->base+M->stacksize;
M->stacksize+=STACKINCREMENT;
}
*M->top++=e;
}
char pop(Sqstack *S,float n)
{
if(S->top==S->base)
return Error;
n=*--S->top;
return n;
}
char Pop(SSqstack *M,char e)
{
if(M->top==M->base)
return Error;
e=*--M->top;
return e;
}
char Gettop(SSqstack *M)
{ char e;
if(M->top==M->base)
return Error;
e=*(M->top-1);
return e;
}
float gettop(Sqstack *S)
{ float e;
if(S->top==S->base)
return Error;
e=*(S->top-1);
return e;
}
char In(char c)
{
if(c>=35 && c<=47)
return 1;
else return 0;
}
char Precede(char i,char j,char *OP)
{
int a,b; char *p;
for(p=OP,a=0;*p!='\0';p++,a++)
if(*p==i) break;
for(p=OP,b=0;*p!='\0';p++,b++)
if(*p==j) break;
if(m[a][b]==1) return '>';
else if(m[a][b]==2) return '<';
else if(m[a][b]==0) return '=';
}
char Operate(char a,char theta,char b)
{
if(a>47) a=atoi(&a);
if(b>47) b=atoi(&b);
switch(theta)
{
case '+': return a+b;
break;
case '-': return a-b;
break;
case '*': return a*b;
break;
case '/': return a/b;
break;
}
}
float EvaluateExpression(Sqstack *S,SSqstack *M)
{
OperandType a,b,c; OperatorType theta;
Initstack(M); Push(M,'#');
initstack(S); c=getchar();
while (c!='#' || Gettop(M)!='#')
{
if (!In(c)){push(S,c);c=getchar();}
else
switch(Precede(Gettop(M),c,OP))
{
case '<' :
Push(M,c); c = getchar();
break;
case '=' :
Pop(M,c); c = getchar();
break;
case '>' :
Pop(M,theta);
pop(S,b); pop(M,a);
push(S,Operate(a,theta,b));
break;
}
}
return gettop(S);
}
void main()
{
float a;
Sqstack S;
SSqstack M;
printf("(ENd with #)\n");
printf("Enter:\n");
a=(float)EvaluateExpression(S,M);
printf("%d",a);
getch();
}
用栈写的表达式求值,例如:5+6*(9-3)-9/3进行求值 当输入#时结束#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW -2
#define OK 1
#define TURE 1
#define Error 0
typedef char ElemType;
typedef ElemType OperandType; /*操作数*/
typedef char OperatorType;
typedef struct
{ float *top;
float *base;
int stacksize;
}Sqstack;
typedef struct
{ char *top;
char *base;
int stacksize;
}SSqstack;
char OP[8]={'+','-','*','/','(',')','#','\0'};
int m[7][7]={1,1,2,2,2,1,1,
1,1,2,2,2,1,1,
1,1,1,1,2,1,1,
1,1,1,1,2,1,1,
2,2,2,2,2,0,-1,
1,1,1,1,-1,1,1,
2,2,2,2,2,-1,0};/*1 > 2 < 0 = -1 不存在*/
void initstack(Sqstack *S)
{ S->base=(float *)malloc(STACK_INIT_SIZE*sizeof (float));
if(!S->base)exit(OVERFLOW);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
void Initstack(SSqstack *M)
{ M->base=(char)malloc(STACK_INIT_SIZE*sizeof (char));
if(!M->base)exit(OVERFLOW);
M->top=M->base;
M->stacksize=STACK_INIT_SIZE;
}
void push(Sqstack *S,float n)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(float *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(float));
if(!S->base)exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=n;
}
void Push(SSqstack *M,char e)
{
if(M->top-M->base>=M->stacksize)
{
M->base=(char *)realloc(M->base,(M->stacksize+STACKINCREMENT)*sizeof(char));
if(!M->base)exit(OVERFLOW);
M->top=M->base+M->stacksize;
M->stacksize+=STACKINCREMENT;
}
*M->top++=e;
}
char pop(Sqstack *S,float n)
{
if(S->top==S->base)
return Error;
n=*--S->top;
return n;
}
char Pop(SSqstack *M,char e)
{
if(M->top==M->base)
return Error;
e=*--M->top;
return e;
}
char Gettop(SSqstack *M)
{ char e;
if(M->top==M->base)
return Error;
e=*(M->top-1);
return e;
}
float gettop(Sqstack *S)
{ float e;
if(S->top==S->base)
return Error;
e=*(S->top-1);
return e;
}
char In(char c)
{
if(c>=35 && c<=47)
return 1;
else return 0;
}
char Precede(char i,char j,char *OP)
{
int a,b; char *p;
for(p=OP,a=0;*p!='\0';p++,a++)
if(*p==i) break;
for(p=OP,b=0;*p!='\0';p++,b++)
if(*p==j) break;
if(m[a][b]==1) return '>';
else if(m[a][b]==2) return '<';
else if(m[a][b]==0) return '=';
}
char Operate(char a,char theta,char b)
{
if(a>47) a=atoi(&a);
if(b>47) b=atoi(&b);
switch(theta)
{
case '+': return a+b;
break;
case '-': return a-b;
break;
case '*': return a*b;
break;
case '/': return a/b;
break;
}
}
float EvaluateExpression(Sqstack *S,SSqstack *M)
{
OperandType a,b,c; OperatorType theta;
Initstack(M); Push(M,'#');
initstack(S); c=getchar();
while (c!='#' || Gettop(M)!='#')
{
if (!In(c)){push(S,c);c=getchar();}
else
switch(Precede(Gettop(M),c,OP))
{
case '<' :
Push(M,c); c = getchar();
break;
case '=' :
Pop(M,c); c = getchar();
break;
case '>' :
Pop(M,theta);
pop(S,b); pop(M,a);
push(S,Operate(a,theta,b));
break;
}
}
return gettop(S);
}
void main()
{
float a;
Sqstack S;
SSqstack M;
printf("(ENd with #)\n");
printf("Enter:\n");
a=(float)EvaluateExpression(S,M);
printf("%d",a);
getch();
}
望高手看一下 帮忙修改一下
大致看了下楼主的代码,感觉楼主像是学完了线性表,刚刚接触栈和队列,许多地方都编得很繁琐。还有一些变量上设定的太循规蹈矩。