是否有任何C函数可以求值标准库中定义的字符串表达式? [英] Is there any C function that can evalute a string expression defined in the standard library?

查看:51
本文介绍了是否有任何C函数可以求值标准库中定义的字符串表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个可以将字符串表达式评估为整数的函数,例如f.e.:

  10/2 + 4 * 3-(12 + 5);//等于0 

插入值为 0 int 类型的对象.

具有复杂功能的功能将是可取的,即使这并不需要小时:

  2 ^(1/2)-99 


在标准C库(如果已编译,则为C89,C90)中是否定义了这样的定义?

是否有具有这种功能的非标准库?

表达式支持的字符越多.

但是即使是+,-,*,/,()之类的基本操作也可以.

解决方案

在标准C库中是否定义了这样的定义(如果已编译,则为C89,C90)?

否,甚至在最新的C11或C18标准库中也没有.

是否有具有这种功能的非标准库?

我也找不到一个.您必须编写自己的函数才能完成此任务.

也许扫描字符串,然后在第一步中将其中的每个值存储到单独的字符串中.

正如我在评论中已经建议的那样, strtol 用于将字符串中的 11 之类的整数值转换为 int .您可能需要利用它.库中还有一个 atoi 函数,用于将字符串中的整数值转换为 int ,但是此函数更容易出错,因此不应该使用./p>

算术运算符也应存储在单独的字符串中,但稍后由自制例程进行评估.

但是可以在此处找到很好的示例和建议,在该示例中,已经向用户询问了类似(如果不相同)的问题:

C:转换数学将表达式字符串转换为带有结果的int

I am looking for a function that can evaluate a string expression into an integer, like f.e.:

10/2+4*3-(12+5); //Equals 0

into an object of type int with the value of 0.

A function for complex operation would be preferable even though that's not the need of the hour:

2^(1/2)-99


Is there any such definition defined in the standard C library (C89, C90, if it's compiled)?

Is there any non-standard library which has such an function?

The more the characters the expression supports the better it is.

But even basic operation like +,-,*,/,() would do.

解决方案

Is there any such definition defined in the standard C library (C89, C90, if it's compiled)?

No, not even in the more recent standard libraries of C11 or C18.

Is there any non-standard library which has such an function?

I couldn´t found one either. You have to write your own function to accomplish that.

Maybe scan the string and store every value in it to a separate string at the first step.

As I already suggested in the comments, strtol is used to convert an integer value like 11 in a string to a value of type int. You might need to take use of that. In the libraries there is also a function of atoi to convert an integer value inside a string to an int but this function is more susceptible for errors and should not be used.

The arithmetic operators shall be stored in separate strings also, but later evaluated by a self-made routine.

But good examples and recommendations can be found here, where a user have been asked a similar (if not the same) question:

C: convert a mathematical expression string into an int with the result

这篇关于是否有任何C函数可以求值标准库中定义的字符串表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆