#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MAX_SIZE 256
enum BOOL{FALSE,TRUE};
typedef struct tagOPERATE{
double Operand;
char Operator;
}OPERATE,*LPOPERATE;
int IsDigit(char);
int isp(char ch);
int icp(char ch);
int Locate(char ch);
void PostSrc(char*src,LPOPERATE lpOperator);
int getOperand(char *s,int *len,double *oprd);
double Calculate(LPOPERATE lpOperator);
static char Operator[]="#(+-*/^"; //?????
static int InPriority[]={0,1,3,3,5,5,7}; //????
static int OutPriority[]={0,10,2,2,4,4,6}; //?????
int Locate(char ch)
{
int i=0;
for(i=0;Operator[i]!='\0';i++)
if(Operator[i]==ch)
return i;
return -1;
}
int isp(char ch)
{
if('A'<=ch&&'Z'>=ch)
return 9;
else
return InPriority[Locate(ch)];
}
int icp(char ch)
{
if('A'<=ch&&'Z'>=ch)
return 8;
else
return OutPriority[Locate(ch)];
}
void PostSrc(char*src,LPOPERATE lpOperator)
{
char *p=src,y;
LPOPERATE lpOptr=lpOperator;
char stack[MAX_SIZE];
int top=-1;
double Operand;
int offset=0;
stack[++top]='#';
while('\0'!=*p)
{
if(IsDigit(*p))
{
getOperand(p,&offset,&Operand);
p+=offset;
lpOptr->Operand=Operand;
lpOptr->Operator=0;
lpOptr++;
}
else
if(')'==*p)
{
for(y=stack[top--];y!='(';y=stack[top--])
(lpOptr++)->Operator=y;
p++;
}
else
{
for(y=stack[top--];isp(y)>icp(*p);y=stack[top--]) //?????
(lpOptr++)->Operator=y;
stack[++top]=y;
stack[++top]=*p++;
}
}
while(top!=-1)
(lpOptr++)->Operator=stack[top--];
}
int IsDigit(char ch)
{
if(('0'<=ch&&'9'>=ch)||'.'==ch)
return TRUE;
return FALSE;
}
int getOperand(char *s,int *len,double *oprd){
char *p = s,ch = *s++;
double z = 0,x = 0;
int bits = 0;
int point = FALSE;
while( IsDigit(ch) == TRUE){
if (ch == '.'){
if (point == TRUE)
return FALSE;
point = TRUE;
}
else {
if (point == TRUE){
x *= 10;
x += ch - '0';
bits++;
}
else {
z *= 10;
z += ch - '0';
}
}
ch = *s++;
}
while(bits-- > 0) x /= 10;
z += x;
*oprd = z;
*len = s - p - 1;
return TRUE;
}
摇曳的蔷薇
胡说叔叔
相关分类