));
return Method_H;
}
}
}
return str;
}
//构建二叉书
Tnode_t* buildTree(char* str, int len)
{
if (str == NULL || len == 0)
return NULL;
Tnode_t* root = (Tnode_t*)malloc(sizeof(Tnode_t));
char* s_ptr = NULL, * e_ptr = NULL, MethStr[20] = { 0 };
char* str_cp = (char*)malloc(len + 1);
if (str_cp == NULL)
return NULL;
memset(str_cp, 0, len + 1);
memcpy(str_cp, str, len);
printf("str_cp: %s\n", str_cp);
if (checkNum(str_cp, len) || (len >= 2 && str_cp[0] == '-' && str_cp[1] != '\0' && checkNum(str_cp+1, len - 1)))
{
root->method = VLAUE;
root->value = atof(str_cp);
root->Lchild = NULL;
root->Rchild = NULL;
return root;
}
else
{
//寻找根方法
s_ptr = serchRootMethod(str_cp, len, &root->method, MethStr);
//递归方法
if (str_cp != s_ptr)
{
printf("MethStr:【%s】\n", MethStr);
root->Lchild = buildTree(str_cp, s_ptr - str_cp);
root->Rchild = buildTree(s_ptr + strlen(MethStr), strlen(s_ptr) - strlen(MethStr));
if (root->Lchild == NULL || root->Rchild == NULL)
{
if (root->Lchild != NULL)
{
free(root->Lchild);
}
if (root->Rchild != NULL)
{
free(root->Rchild);
}
free(root);
return NULL;
}
}
//去括号
else
{
if (1 == serchBraMethod(str_cp, len, &root->method, MethStr))
{
printf("MethStr:【%s】\n", MethStr);
root->Lchild = buildTree(str_cp + strlen(MethStr), len - 1 - strlen(MethStr));
if (root->Lchild == NULL){
free(root);
return NULL;
}
}
else
{
free(root);
root = NULL;
if (len - 2 >= 0)
{
return buildTree(str_cp + 1, len - 2);
}
}
}
}
free(str_cp);
return root;
}
//替换x变量
char* replace_x(double x, char* fxrule, int rulelen)
{
if (fxrule == NULL || rulelen == 0){
return NULL;
}
char* location_X = strstr(fxrule,"x");
if (location_X == NULL)
return NULL;
char replace[20] = { 0 };
snprintf(replace, sizeof(replace), "%f", x);
char* newRule = (char*)malloc(rulelen + strlen(replace)+20);
memset(newRule, 0, sizeof(rulelen + strlen(replace)));
memcpy(newRule, fxrule, location_X - fxrule);
memcpy(newRule + (location_X - fxrule), replace, strlen(replace));
memcpy(newRule + (location_X - fxrule) + strlen(replace), location_X + 1, strlen(replace));
return newRule;
}
//根据变量与对应法则求值
int equation(double *fx, double X, char* fxrule, int rulelen)
{
char* newR = fxrule, *saveR = NULL;
if (fx == NULL || fxrule == 0){
return -1;
}
do
{
saveR = newR;
newR = replace_x(X, saveR, strlen(saveR));
if (newR == NULL)
{
newR = saveR;
break;
}
if (newR != NULL && saveR != fxrule)
{
free(saveR);
}
} while (newR != NULL && strstr(newR,"x"));
printf("fxrule:%s\n", fxrule);
printf("newRule: %s\n", newR);
Tnode_t* tree = buildTree(newR, strlen(newR));
if (newR != fxrule)
{
free(newR);
}
if (tree != NULL)
{
*fx = cal_tree(tree);
return 0;
}
return -3;
}
int main()
{
double A, B = 1.2;
char a[200], b[200];
while (1)
{
printf("请输入:\nF(x) = ");
scanf("%s", a);
printf("请输入X变量值:\n x = ");
scanf("%s", b);
B = atof(b);
if (0 == equation(&A, B, a, strlen(a)))
{
printf("F(%s) = %f\n", b, A);
}
else
{
printf("输入有误!\n");
}
}
}