weapon_x33

weapon_x33

  • NA
  • 18
  • 0

C# algebric function

Oct 5 2003 6:25 AM
Hello all, I'm pretty much a newbie in the programming world and the C# world, so please excuse my ignorance. I wanted to make a program for my physics class and below is what I have so far (it's almost fully functional...still a few more things I'd like to add.) I was wondering if you guys know if C# has some sort of in-built algebric equation solver, so that I give it a formula and it can work out on its own the answer. In the code I have below, I had to work out all the standard equations, and solve for the unknown variable (as you will see.) This took a lot of time and I'm still not sure if they all work properly. If there is an algebric equation solver function, I'd love to know about it (also for future programs I may make.) Here's the code: using System; namespace ConsoleApplication1 { class Class1 { //Declaring Global Variables static double Vi, Vf, a, t, d; static byte Vi_add, Vf_add, a_add, t_add, d_add; [STAThread] static void Main(string[] args) { Console.WriteLine(".:: Physics Acceleration Equation Solver ::.\n--------------------------------------------\nFor unknown values type '?'\n"); begin: try { //Variables Defined with user's input. Console.Write("Vi = "); string ViRead = Console.ReadLine(); if (ViRead == "?") { Vi_add = 1; } else { double Vi_double = Convert.ToDouble(ViRead); Vi = Vi_double; } Console.Write("Vf = "); string VfRead = Console.ReadLine(); if (VfRead == "?") { Vf_add = 1; } else { double Vf_double = Convert.ToDouble(VfRead); Vf = Vf_double; } Console.Write("a = "); string aRead = Console.ReadLine(); if (aRead == "?") { a_add = 1; } else { double a_double = Convert.ToDouble(aRead); a = a_double; } Console.Write("t = "); string tRead = Console.ReadLine(); if (tRead == "?") { t_add = 1; } else { double t_double = Convert.ToDouble(tRead); t = t_double; } Console.Write("d = "); string dRead = Console.ReadLine(); if (dRead == "?") { d_add = 1; } else { double d_double = Convert.ToDouble(dRead); d = d_double; } /* "If" statements; Deciding what function to utilize. Each statement can answer for two values. A '?' is placed where the value is unknown. Since I could not find a C# algebric function to automatically solve the standard equations, I had to feed the compiler the equations to solve. That meant having to switch all equations around and solving for the unknown variable. An Error Handler was made to capture incorrect values such as letters. */ if (Vi_add == 1 && d_add == 1) { Console.Write("\n--------------------------------------------\nEquations used:\nVf = Vi + at <--To find Vi\nd = 1/2(Vf + Vi)t <--To find d\n\n"); Vi = (Vf) / (a*t); Console.WriteLine("Vi = {0}", Vi); d = ((Vf + Vi)*t) / 2; Console.WriteLine("d = {0}", d); } else if (Vi_add == 1 && t_add == 1) { Console.Write("\n--------------------------------------------\nEquations used:\nVf² = Vi² + 2ad <--To find Vi\nVf = Vi + at <--To find t\n\n"); Vi = (Math.Sqrt(Vf)) + (Math.Sqrt(2*(a*d))); Console.WriteLine("Vi = {0}", Vi); t = (Vf - Vi) / a; Console.WriteLine("t = {0}", t); } else if (Vi_add == 1 && a_add == 1) { Console.Write("\n--------------------------------------------\nEquations used:\nd = 1/2(Vf + Vi)t <--To find Vi\nVf = Vi + at <--To find a\n\n"); Vi = ((2*d)/t); Vi = Vi - Vf; Console.WriteLine("Vi = {0}", Vi); Console.WriteLine("a = {0}", (Vf - Vi)/t ); } else if (Vi_add == 1 && Vf_add == 1) { Console.Write("\n--------------------------------------------\nEquations used:\nd = Vit + 1/2at² <--To find Vi\nVf = Vi + at <--To find Vf\n\n"); Vi = (2*d)/t - Math.Pow(a*t, 2); Console.WriteLine("Vi = {0}", Vi); Vf = Vi + (a*t); Console.WriteLine("Vf = {0}", Vf); } else if (Vf_add == 1 && a_add == 1) { Console.Write("\n--------------------------------------------\nEquations used:\nd = 1/2(Vf + Vi)t <--To find Vf\nVf = Vi + at <--To find a\n\n"); Vf = ((2*d)/t) - Vi; Console.WriteLine("Vf = {0}", Vf); Console.WriteLine("a = {0}", (Vf - Vi)/t); } else if (Vf_add == 1 && t_add == 1) { Console.Write("\n--------------------------------------------\nEquations used:\nVf² = Vi² + 2ad <--To find Vf\nVf = Vi + at <--To find t\n\n"); Vf = Math.Sqrt(Vi + 2*(a*d)); Console.WriteLine("Vf = {0}", Vf); t = (Vf - Vi) / a; Console.WriteLine("t = {0}", t); } else if (Vf_add == 1 && d_add == 1) { Console.Write("\n--------------------------------------------\nEquations used:\nVf = Vi + at <--To find Vf\nd = 1/2(Vf + Vi)t <--To find d\n\n"); Vf = Vi + (a*t); Console.WriteLine("Vf = {0}", Vf); d = ((Vf + Vi)* t) / 2; Console.WriteLine("d = {0}", d); } else if (a_add == 1 && t_add == 1) { Console.Write("\n--------------------------------------------\nEquations used:\nVf² = Vi² + 2ad <--To find a\nVf = Vi + at <--To find t\n\n"); a = (Math.Pow(Vf,2) - Math.Pow(Vi,2)) / 2*d; Console.WriteLine("a = {0}", a); t = (Vf - Vi) / a; Console.WriteLine("t = {0}", t); } else if (a_add == 1 && d_add == 1) { Console.Write("\n--------------------------------------------\nEquations used:\nVf = Vi + at <--To find a\nVf² = Vi² + 2ad <--To find d\n\n"); a = (Vf - Vi)/t; Console.WriteLine("a = {0}", a); d = ((Vf*Vf) - (Vi*Vi)) / (2*a); Console.WriteLine("d = {0}", d); } else if (d_add == 1 && t_add == 1) { Console.Write("\n--------------------------------------------\nEquations used:\nVf² = Vi² + 2ad <--To find d\nVf = Vi + at <--To find t\n\n"); d = ((Vf*Vf) - (Vi*Vi)) / (2*a); Console.WriteLine("d = {0}", d); t = (Vf - Vi) / a; Console.WriteLine("t = {0}", t); } else { Console.WriteLine("You already have all values."); } } catch (System.FormatException e) { Console.WriteLine("{0}\n\n", e.Message); goto begin; } ///////////////////////////// //Prompt if to go on or not// ///////////////////////////// Console.WriteLine("\nDo you wish to continue? Yes or No?"); char[] trimChars = {' ', 'e', 's'}; string userResponse = Console.ReadLine(); userResponse = userResponse.ToLower(); userResponse = userResponse.Trim(trimChars); if (userResponse == "y") { goto begin; } else Console.WriteLine("Ok, Bye!\n"); } } }