如何从字符串中创建表达式并在c ++中对其进行求值 [英] How to make an expression off a string and evaluate it in c++

查看:76
本文介绍了如何从字符串中创建表达式并在c ++中对其进行求值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有疑问。我搜索了很多,但无法找到确切的输出我有一个int数[4]数组。比如说



number [0] = 5;

number [1] = 7;

number [2] = 8;

数字[3] = 9;



现在的情况是我必须乘以/除/加/减去这些数字到一些所需的总和。



数字[0] operator1数字[1] operator2数字[2] operator3数字[3]



这些运营商将由用户选择,或者可以是随机的,这些运营商可以在任何地方。像运算符可以是*,+等



现在有一种方法可以将算术运算符存储在一个数组中,这样我们就可以直接生成这样的正则表达式:



number [0] op [0] number [1] op [1] number [2] op [2] number [3]



考虑到op是一系列运算符:



这样编译器就会处理BODMAS本身而我们没有担心这个







有没有办法转换像这样的字符串6 * 4-4 / 5英寸到c ++中的表达式,这样我可以直接得到整数变量的结果吗?

I am having a doubt. I searched a lot but unable to find the exact output I have a int number[4] array. say for example

number[0] = 5;
number[1] = 7;
number[2] = 8;
number[3] = 9;

Now the scenario is that I have to multiply/divide/add/subtract these numbers to some desired sum.

number[0] operator1 number[1] operator2 number[2] operator3 number[3]

These operators are to be selected by the user or can be random and these operators can be at any place. Like operators can be *, + etc

Now is there a way to store the arithmetic operators in an array so that we can make directly make a regular expression like this:

number[0] op[0] number[1] op[1] number[2] op[2] number[3]

considering op is an array of operators:

In this way the compiler will handle the BODMAS itself and we don't have to worry about that

OR

Is there a way to convert a string like this "6*4-4/5" to an expression in c++ such that I can directly get the result in an integer variable?

推荐答案

不知道你要去哪里但是



字符数[8] =6 * 4-4 / 5;



是非常的答案....无所事事



您提出了一个问题,您实际上已经回答了问题。



看看调试器下的数字,你会看到这个

Not sure where you are going with this but

char Number[8] = "6*4-4/5";

IS LITERALLY THE ANSWER .... THERE IS NOTHING TO DO

YOU ASKED A QUESTION YOU ACTUALLY GAVE THE ANSWER TOO.

Look at Number under the debugger and you will see this
Number[0] = '6';
Number[1] = '*';
Number[2] = '4';
Number[3] = '-';
Number[4] = '4';
Number[5] = '/';
Number[6] = '5';
Number[7] = 0;





你需要做的就是做一个字符串评估函数...你知道就像



All you need to do is make a string evaluate function ... you know like

bool evaluate(const char *expression, int &result)
{
...
}





取出该字符串并将其剥离并逐个字符地进行评估。我甚至会给你一个提示,如果数字大于1个字符你必须做数字位,因为两个阶段找到开始并找到结束。



如果你谷歌它会有成千上万的东西在评估字符串序列,因为它是任何计算机编程粗略的一部分。



我怀疑这是作业,这就是为什么我不会填写函数为你。



Take that string and strip it and evaluate it character by character. I will even give you a hint if the number is bigger than 1 character you have to do the number bit as two stages find start and find end.

If you google it there will be thousands of things on evaluating a string sequence because it is part of any computer programming coarse.

I suspect this is homework which is why I wont fill the function in for you.


不,编译器肯定不会处理任何只能在运行时评估的东西!



1你需要一个类型来表示运算符,没有内置类型。

2.然后你需要一个解析器,可以区分运算符中的数字并从字符串中读取它们,一个接一个。

3.然后你需要解析/解释结果表达式。如果您不希望为此目的依赖现有框架(请参阅解决方案1),那么请为自己做一些大量的工作,构建表达式树。

4.按照解释的表达方式工作树一步一步地执行表达式的每个部分。



注意:在 RPN [ ^ ]。也许你想首先尝试解决这个问题。
No, the compiler will definitely not handle anything that can only be evaluated at runtime!

1. You need a type to represent the operators, there is no built-in type for that.
2. Then you need a parser that can distinguish the numbers from the operators and read them from the string, one by one.
3. Then you need to parse/interpret the resulting expression. If you don't wish to rely on existing frameworks for that purpose (see solution 1), then brace yourself for some extensive work, building an expression tree.
4. Work your way through the interpreted expression tree to execute each part of the expression, step by step.

Note: it's much easier to interpret and evaluate expressions in RPN[^]. Maybe you'd like to try your hand on that problem first.


使用 Boost.Spirit [ ^ ]或 Ultragram [ ^ ]。但我不知道你的经验水平如何使用它们。
Use Boost.Spirit[^] or Ultragram[^]. But I don't know how your experience level is to use them.


这篇关于如何从字符串中创建表达式并在c ++中对其进行求值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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