0

So I want to take certain parts of inputs and make them variables. So for example, and input that would go in is '3x^2+4x+6' and I would want the code to take the 3, the 4, and the 6 out of that string. Is there a way I can do this efficiently, as when I make the [1] anything bigger, there is an exception, and when it is [1] or below nothing writes. I just need something simple that takes those specific numbers out at those positions. The 3x^2 could just be an x^2 with no number and the 4x could be just an x. I want to take a '1' in that case. Please let me know if I need to include any more details. That is all the code I currently have in the project.


namespace Factoring_Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Input the equation you want factored(no spaces): ");
            string equation = Console.ReadLine();

            Console.Write(equation);
            string a = equation.Split("x")[1];
            Console.Write(a);
            Console.Read();
        }
    }
}
cradaper
  • 11
  • 1
  • you should look into Regular Expressions – Jonesopolis Nov 03 '20 at 00:35
  • Maybe `var result = Regex.Replace(input,"[346]",string.Empty)` – TheGeneral Nov 03 '20 at 00:36
  • You need to parse the expression into a representation that you can mess with. Maybe start with generating an Abstract Syntax Tree or at least a token stream, then examine it to work out which bits fit your needs. – Corey Nov 03 '20 at 05:43

0 Answers0