0

I have created a method that splits a string a^b and converts a and b to doubles. However, if I input the value of either a or b using a fraction (for example 3/5 instead of 0.6) the method doesn't work; it only allows me to input it like 0.6. Why is this, and is it possible to fix it?

The code is shown below:

public static double Coefficient()
{
    while (true)
    {
        string input = Console.ReadLine();
        string[] items = input.Split('^');

        if (items.Length == 1)
        {
            if (double.TryParse(items[0], out double A))
                return A; 
        }

        else if (items.Length == 2)
        {
            if (double.TryParse(items[0], out double A) &
                double.TryParse(items[1], out double B))
                return Math.Pow(A, B); 
        }

        Console.WriteLine("\nPlease follow the specified input form.");
    }
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Anders
  • 23
  • 4
  • You're splitting on the `^` character, and since `"4/2"` doesn't contain that character, you get that string back, and then you pass it to `double.TryParse`. – Rufus L May 06 '19 at 18:57
  • 3
    Are you saying that you're trying to parse the string `"4/2"` as if it were a string representing a floating point number? Due to the fact that it isn't, that's not going to work. If you want to evaluate expressions, you'll need to use some kind of expression evaluator library. NCalc is one example. – 15ee8f99-57ff-4f92-890c-b56153 May 06 '19 at 18:57
  • Made a mistake in the title, the method converts strings to doubles – Anders May 06 '19 at 18:58
  • Shouldn't you be using a logical AND (||) not a bitwise and (&)? – user3750325 May 06 '19 at 18:59
  • 2
    @user7733611 `||` is `or`, not `and`; you're thinking of `&&`. In C#, [bitwise `&` works like logical `and` on boolean values](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/and-operator). He's fine. But yes, I wouldn't have used that operator either, if only because the nuns who taught me ANSI C thrashed me savagely. In fairness, I was a feral child. – 15ee8f99-57ff-4f92-890c-b56153 May 06 '19 at 19:00
  • @Anders check my answer – Pie May 06 '19 at 19:12
  • `3/5` is fraction but floating point values can't be natively represented that way in C#. If you need just those - https://stackoverflow.com/questions/13903621/convert-a-text-fraction-to-a-decimal is the answer (duplicate), if you want to actually parse math expression - https://stackoverflow.com/questions/3972854/parse-math-expression – Alexei Levenkov May 06 '19 at 19:24

1 Answers1

1

You're trying to parse expressions with methods that only know how to handle specifically formatted numbers as inputs. You need to either write an equation parser or split your inputs appropriately. You are already doing this by splitting ^ - you can do the same with /.

In fact, there are libraries that already do this.

Pie
  • 584
  • 1
  • 6
  • 22
  • 1
    Thanks for the reply. I'm really new and I don't really understand how to use these libraries at all though. – Anders May 06 '19 at 19:15