顺便把一个四则运算的代码贴出来了,上学期大一写的,核心算法跟我写的重言式我中的virtualCreateTree函数类似,这就不啰嗦一遍了, 代码如下: (可能有bug)
/**/
/********************************************************************
created: 2007/06/09
author: 刺猬
purpose: 四则运算
*********************************************************************/
#include " stdio.h "
#include " stdlib.h "
#include " math.h "
typedef struct node ... {
char data;
struct node *left;
struct node *right;
} binarytree;
typedef struct exp ... {
char data;
int weight;
} express;
int analyse( char before[],express expression[])
... {
char *p=&before[0];
express *loc=&expression[0];
int weight=0;
int len=0;
while(*p!= ‘/0’)
...{
switch(*p)
...{
case '(':
weight=weight+4;
break;
case ')':
weight=weight-4;
break;
case '+':
case '-':
loc->data=*p;
loc->weight=weight+1;
loc++;
len++;
break;
case '*':
case '/':
loc->data=*p;
loc->weight=weight+2;
loc++;
len++;
break;
case '^':
loc->data=*p;
loc->weight=weight+3;
loc++;
len++;
break;
default:
loc->data=*p;
loc->weight=0;
loc++;
len++;
}
p++;
}
return len;
}
int find_lowest(express expression[], int & start, int & end)
... {
int lowest=start+1;
for(int i=start;i<=end;i++)
if(expression[i].weight>0&&expression[i].weight<expression[lowest].weight)
lowest=i;
return lowest;
}
binarytree * create_tree(express expression[], int start, int end)
... {
binarytree *root=(binarytree *)malloc(sizeof(binarytree));
if(start==end)
...{
root->data=expression[start].data;
root->left=NULL;
root->right=NULL;
}
else
...{
int key=find_lowest(expression,start,end);
root->data=expression[key].data;
root->left=create_tree(expression,start,key-1);
root->right=create_tree(expression,key+1,end);
}
return root;
}
double calculator(binarytree * root)
... {
if(root)
...{
if(root->left==NULL&&root->right==NULL)
return atof(&root->data);
else
...{
switch(root->data)
...{
case '*':
return calculator(root->left)*calculator(root->right);
break;
case '/':
return calculator(root->left)/calculator(root->right);
break;
case '+':
return calculator(root->left)+calculator(root->right);
break;
case '-':
return calculator(root->left)-calculator(root->right);
break;
case '^':
return pow(calculator(root->left),calculator(root->right));
default:
break;
}
}
}
}
int main()
... {
int start=0;
int len;
char original_expression[256];
express expression[256];
while(scanf("%s",original_expression))
...{
len=analyse(original_expression,expression);
binarytree *root=create_tree(expression,start,len-1);
//inorder(root);
printf("%lf ",calculator(root));
}
return 0;
}
created: 2007/06/09
author: 刺猬
purpose: 四则运算
*********************************************************************/
#include " stdio.h "
#include " stdlib.h "
#include " math.h "
typedef struct node ... {
char data;
struct node *left;
struct node *right;
} binarytree;
typedef struct exp ... {
char data;
int weight;
} express;
int analyse( char before[],express expression[])
... {
char *p=&before[0];
express *loc=&expression[0];
int weight=0;
int len=0;
while(*p!= ‘/0’)
...{
switch(*p)
...{
case '(':
weight=weight+4;
break;
case ')':
weight=weight-4;
break;
case '+':
case '-':
loc->data=*p;
loc->weight=weight+1;
loc++;
len++;
break;
case '*':
case '/':
loc->data=*p;
loc->weight=weight+2;
loc++;
len++;
break;
case '^':
loc->data=*p;
loc->weight=weight+3;
loc++;
len++;
break;
default:
loc->data=*p;
loc->weight=0;
loc++;
len++;
}
p++;
}
return len;
}
int find_lowest(express expression[], int & start, int & end)
... {
int lowest=start+1;
for(int i=start;i<=end;i++)
if(expression[i].weight>0&&expression[i].weight<expression[lowest].weight)
lowest=i;
return lowest;
}
binarytree * create_tree(express expression[], int start, int end)
... {
binarytree *root=(binarytree *)malloc(sizeof(binarytree));
if(start==end)
...{
root->data=expression[start].data;
root->left=NULL;
root->right=NULL;
}
else
...{
int key=find_lowest(expression,start,end);
root->data=expression[key].data;
root->left=create_tree(expression,start,key-1);
root->right=create_tree(expression,key+1,end);
}
return root;
}
double calculator(binarytree * root)
... {
if(root)
...{
if(root->left==NULL&&root->right==NULL)
return atof(&root->data);
else
...{
switch(root->data)
...{
case '*':
return calculator(root->left)*calculator(root->right);
break;
case '/':
return calculator(root->left)/calculator(root->right);
break;
case '+':
return calculator(root->left)+calculator(root->right);
break;
case '-':
return calculator(root->left)-calculator(root->right);
break;
case '^':
return pow(calculator(root->left),calculator(root->right));
default:
break;
}
}
}
}
int main()
... {
int start=0;
int len;
char original_expression[256];
express expression[256];
while(scanf("%s",original_expression))
...{
len=analyse(original_expression,expression);
binarytree *root=create_tree(expression,start,len-1);
//inorder(root);
printf("%lf ",calculator(root));
}
return 0;
}