wasif rashid

wasif rashid

  • NA
  • 2
  • 9.7k

solve linear and quadratic style equations in C#

Sep 14 2017 3:55 AM

Hi Friends i need your suggestions my requirement is to get the input in string for example X^2 + X^2 + 4X + 9X + X = 10 + 1 * 2 % 4 now we need to slove it and find the value of x like the link below link

we also need to do the validation of input that i already did just need suggestions how can we solve these equations using c#

I split the equation and create the function to get numeric array and operators now I am trying to figure out the logic how can we process it any suggestions?

  1. public class Calculation  
  2.   
  3.    string[] opers = new string[] { "+""-""/""*""%""^" };  
  4.    public string Process(string value)  
  5.    {  
  6.        string result = string.Empty;  
  7.        string[] equations = value.Split('=');  
  8.        calculate(equations[0]);  
  9.        calculate(equations[1]);  
  10.        return result;  
  11.    }  
  12.   
  13.    private string calculate(string eq)  
  14.    {  
  15.        string result = eq;  
  16.        string variable = string.Empty;  
  17.        List<string> NumberList = new List<string>();  
  18.        List<string> OperatorList = new List<string>();  
  19.        GetLists(eq, ref NumberList, ref OperatorList);  
  20.       // logic to do operation return result;  
  21.   
  22.    }  
  23.    private void GetLists(string value, ref List<string> numberlist, ref List<string> operatorList)  
  24.    {  
  25.        string[] operatorrArray = value.Split(' ');  
  26.        foreach (var item in operatorrArray)  
  27.        {  
  28.            if (opers.Contains(item))  
  29.            {  
  30.                operatorList.Add(item);  
  31.            }  
  32.            else  
  33.            {  
  34.                numberlist.Add(item);  
  35.            }  
  36.        }  
  37.    }  
can you please help me out with logic how can we processed the sample input will be in string like following formate
  1. 2X – 1 = 0  
  2. 3X + 8X = 33  
  3. 4X - 7 = 8X - 5  
  4. X / (x+12) = 1 / 13  
  5. X^2 + X^2 + 4X + 9X + X = 10 + 1 * 2 % 4  


 

Answers (1)