Skip to content

Instantly share code, notes, and snippets.

@kevinkjt2000
Last active September 18, 2019 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinkjt2000/a10cd5c0d1dd34e9d087625397472d07 to your computer and use it in GitHub Desktop.
Save kevinkjt2000/a10cd5c0d1dd34e9d087625397472d07 to your computer and use it in GitHub Desktop.
My first "real" program that was not on a TI-83. Written in C# on my first laptop during the summer of 2009.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class QuadForm
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to the Quadratic Formula Program!");
Console.WriteLine("Equation: ax² + bx + c = 0");
Console.WriteLine("Formula: x = (-b ± √(b² - 4*a*c))/(2*a)");
InputWrapper iw = new InputWrapper();
double a, b, c, d;
int i = 0;
a = iw.getDouble("a: ");
b = iw.getDouble("b: ");
c = iw.getDouble("c: ");
d = b*b-(4*a*c);
a = 2 * a;
if (d < 0)
{
i = 1;
d = 0 - d;
}
double e = 2;
double f = 1;
while (Math.Pow(e, 2) < d)
{
if (Math.IEEERemainder(d , Math.Pow(e, 2)) == 0)
{
d = d / Math.Pow(e, 2);
f = e * f;
e = e - 2 + Convert.ToDouble(e == 3);
}
e = e + 2 - Convert.ToDouble(e == 2);
}
double g;
e = 2;
g = Math.Max(b, f);
g = Math.Max(a, g);
while (e <= g)
{
if (Math.IEEERemainder(b, e) == 0 & (Math.IEEERemainder(f, e) == 0 | f == 1) & Math.IEEERemainder(a, e) == 0)
{
b = b / e;
if (f != 1)
{
f = f / e;
}
a = a / e;
e = e - 2 + Convert.ToDouble(e == 3);
}
e = e + 2 - Convert.ToDouble(e == 2);
}
if (d == 0)
{
Console.WriteLine("Only one answer:");
if (a > 1)
{
Console.WriteLine("{0} / {1}", 0 - b, a);
}
if (a == 1)
{
Console.WriteLine("{0}", 0 - b);
}
Console.ReadLine();
return;
}
if (d > 1)
{
if (a > 1)
{
Console.Write("(");
}
if (b != 0)
{
Console.Write("{0} ± (", 0 - b);
}
if (f > 1)
{
Console.Write("{0}*", f);
}
if (i == 1)
{
Console.Write("i*");
}
Console.Write("√({0})", d);
if (a > 1)
{
Console.Write(") / ({0})", a);
}
}
Console.WriteLine("\nEnd of Program!");
Console.ReadLine();
return;
}
// Definition for InputWrapper Class
//
// Class to wrap simple stream input
// Datatype supported:
// int
// double
// decimal
// string
public class InputWrapper
{
public int getInt(string prompt)
{
Console.Write(prompt);
string buf = Console.ReadLine();
return Convert.ToInt32(buf);
}
public double getDouble(string prompt)
{
Console.Write(prompt);
string buf = Console.ReadLine();
return Convert.ToDouble(buf);
}
public decimal getDecimal(string prompt)
{
Console.Write(prompt);
string buf = Console.ReadLine();
return Convert.ToDecimal(buf);
}
public string getString(string prompt)
{
Console.Write(prompt);
string buf = Console.ReadLine();
return buf;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment