4
hello, needed to resolve a string with an account (e.g. 2 + 2) in C# and return an integer (e.g. 4)
static void Main(string[] args)
{
string str = "2 + 2";
int resultado = Calcular(str);
Console.WriteLine("Resultado => {0}",resultado);
Console.ReadLine();
}
any hint?
Okay, I tested it here and it worked, just one more thing, if I have something like "solve the average of these values: 2.4", is there any way I can average it, or at least remove the text and just leave the values with the comma?
– Jackson José Gomes Do Valle
@Jacksonjoségomesdovalle a generic method that does this would be complicated, you need to have a rule to know how to extract the numbers from the text. One way would be to take out anything other than a number and a comma, but then if you typed a comma into her sentence, it wouldn’t work anymore. Then maybe you had to find the first occurrence of some number (ex: 2) and take everything you have from there until the last occurrence of some number (in this case it would take 2.4). Then you do
var nums = textoExtraido.Split(',');
and then something likevar media = nums.Select(n => Convert.ToInt32(n)).Average();
.– Alisson
OK, I’ll try thanks for the answer
– Jackson José Gomes Do Valle