C语言编程多项式求值,c语言使用二叉树解析多项式并求值

));

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");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值