- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
*
*因为程序退出,就释放进程所有内存,作为演示程序,就不释放内存了
*
*
*
*
**/
typedef struct _node
{
struct _node* parent;
struct _node* left;
struct _node* right;
char opt;
char c1;
char c2;
int data;
}node;
node* root;
void error_exit();
int getint(char** p);
void exec_use_callback(char* p); //通过递归方式计算,因为简单,回头再写
void exec_use_tree(char* p);
void tree_insert_char(char p,node** pnode);
void tree_insert_int(int n,node** pnode);
int tree_result(node* pnode);
void reset_root(node* pnode);
int main(int argc, char* argv[])
{
char buf[1024];
printf("start test program for compute\n");
if( argc<2)
{
printf("arg is error!\n");
exit(1);
}
memset(buf,0,1024);
if(strlen(argv[1])>1023) {
printf("cmd is too long ,can't big than 1023\n");
exit(1);
}
strncpy(buf,argv[1],1023);
printf("the expression is: %s\n",buf);
exec_use_tree(buf);
exec_use_callback(buf); //暂未实现
return 0;
}
void error_exit()
{
printf("error,exit!........\n") ;
exit(1);
}
void exec_use_callback(char* p)
{
char* ptmp;
ptmp = p;
return ;
}
void exec_use_tree(char* p)
{
char* ptmp;
int n;
node* tmp_node;
ptmp = p;
root= NULL;
tmp_node=root;
while(*ptmp!=0)
{
switch(*ptmp)
{
case '+':
case '-':
case '*':
case '/':
case ')':
case '(':
{
reset_root(tmp_node);
tree_insert_char(*ptmp,&tmp_node);
ptmp++;
}
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
{
n= getint(&ptmp);
if( n<=0) error_exit();
reset_root(tmp_node);
tree_insert_int(n,&tmp_node);
}
break;
default:
error_exit();
break;
}
}
//采用中序遍历二叉树求和
reset_root(tmp_node);
n=tree_result(root);
printf("the result use tree is: %d\n",n);
return ;
}
void tree_insert_char(char p,node** pnode)
{
node* tmp_node;
node* tmp_node2;
switch(p)
{
case ')':
{
if(*pnode==NULL) error_exit();
tmp_node = (**pnode).parent;
while (tmp_node!=NULL)
{
if(tmp_node->c1=='(') {
*pnode = tmp_node;
tmp_node->c2=')';
if(tmp_node->opt==0)
{
error_exit();
}
return;
}
tmp_node=tmp_node->parent;
}
error_exit();
}
break;
case '+':
case '-':
{
if( *pnode==NULL){//演示程序,不考虑带符号整数的情况
printf("error expression,exit\n");
exit(1);
}
if( (**pnode).parent==NULL)
{ //根结点时
tmp_node= (node*)malloc(sizeof(node));
memset(tmp_node,0,sizeof(node));
tmp_node->left = *pnode;
(**pnode).parent = tmp_node;
*pnode =tmp_node;
tmp_node->opt = p;
}else{
tmp_node = (**pnode).parent;
while (tmp_node!=NULL&&tmp_node->opt!=0)
{
tmp_node = tmp_node->parent;
}
if( tmp_node==NULL)
{
tmp_node= (node*)malloc(sizeof(node));
memset(tmp_node,0,sizeof(node));
tmp_node->left = root;
root = tmp_node;
*pnode =tmp_node;
tmp_node->opt = p;
}else{
tmp_node->opt = p;
*pnode=tmp_node;
}
}
}
break;
case '(':
{
if( *pnode==NULL){
*pnode= (node*)malloc(sizeof(node));
memset(*pnode,0,sizeof(node));
(**pnode).c1 = p;
root = *pnode;
}else{
tmp_node= (node*)malloc(sizeof(node));
memset(tmp_node,0,sizeof(node));
tmp_node->parent=*pnode;
tmp_node->c1='(';
if((**pnode).left==NULL){
(**pnode).left = tmp_node;
}else if((**pnode).right==NULL)
{
(**pnode).right = tmp_node;
}else{
error_exit();
}
*pnode=tmp_node;
}
}
break;
case '*':
case '/':
{
if( *pnode==NULL){
printf("error expression,exit\n");
exit(1);
}
tmp_node= (node*)malloc(sizeof(node));
memset(tmp_node,0,sizeof(node));
(*tmp_node).opt = p;
tmp_node->parent=(**pnode).parent;
tmp_node->left = *pnode;
if((**pnode).parent!=NULL)
{
tmp_node2 = (**pnode).parent;
if( tmp_node2->left==*pnode)
{
tmp_node2->left =tmp_node;
}else{
tmp_node2->right = tmp_node;
}
}
(**pnode).parent = tmp_node;
*pnode = tmp_node;
}
break;
default:
{
printf("unknow char,exit!\n");
exit(1);
}
}
return ;
}
void tree_insert_int(int n,node** pnode)
{
node* tmp_node;
tmp_node= (node*)malloc(sizeof(node));
memset(tmp_node,0,sizeof(node));
tmp_node->data = n;
tmp_node->parent = *pnode;
if( *pnode==NULL)
{
root =tmp_node;
}else if((**pnode).left==NULL)
{
(**pnode).left = tmp_node;
}else if ((**pnode).right==NULL)
{
(**pnode).right = tmp_node;
}else
{
error_exit();
}
*pnode = tmp_node;
return ;
}
int getint(char** p)
{
int ret;
ret=0;
while((**p)>='0'&&(**p)<='9')
{
ret=ret*10+(**p)-'0';
(*p)++;
}
return ret;
}
//递归计算树中数据和
int tree_result(node* pnode)
{
int ret;
ret = 0;
if(pnode==NULL ) error_exit();
if( pnode->right==NULL&&pnode->left==NULL) return pnode->data;
if( pnode->right==NULL|| pnode->left==NULL ) error_exit();
switch(pnode->opt)
{
case '+':
{
return (tree_result(pnode->left)+tree_result(pnode->right));
}
break;
case '-':
{
return (tree_result(pnode->left)-tree_result(pnode->right));
}
break;
case '*':
{
return (tree_result(pnode->left)*tree_result(pnode->right));
}
break;
case '/':
{
return (tree_result(pnode->left)/tree_result(pnode->right));
}
break;
default:
error_exit();
break;
}
return ret;
}
void reset_root(node* pnode)
{
root = pnode;
if( root==NULL ) return;
while (root->parent!=NULL)
{
root = root->parent;
}
}
/*
按照算法, ((1+2*3)*4+5)*(6+7*(8+9))+10 表达式生成的树形状如下:
--------------------------------------------------------------
+
/ \
/ \
* 10
/ \
/ \
(+) (+)
/ \ / \
/ \ / \
* 5 6 *
/ \ / \
/ \ / \
(+) 4 7 (+)
/ \ / \
/ \ / \
1 * 8 9
/ \
/ \
2 3
-------------------------------------------------------------------------
按照算法,1+(2+3)*(4+5)*6表达式生成的树如下:
-------------------------------------------------------------------------
+
/ \
/ \
1 *
/ \
/ \
(+) *
/ \ / \
/ \ / \
2 3 (+) 6
/ \
/ \
4 5
*/ |
|