HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Solving quadratic equation using quadratic formula

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
solvingusingquadraticformulaequation

Problem

I've just started learning C# + MVC. Here's my attempt with MVC:

```
using System;

namespace QuadraticSolver
{
class Program
{
static void Main(string[] args)
{
Controller c = new Controller();

c.Welcome();

string keyPressed = null;
do
{
if (keyPressed == "Y")
{
c.DrawLineAfterContinue();
}

c.ProgramProper();
c.Continue();
keyPressed = Console.ReadKey().Key.ToString();

} while (keyPressed == "Y");
}
}

class Controller
{
public void Welcome()
{
View.WelcomeMessage();
}

public void DrawLineAfterContinue()
{
View.DrawHorizontalLine(true);
}

public void ProgramProper()
{
Model.Input i = default(Model.Input);
View.CollectInput(ref i);

Model.Result r = new Model.Result(ref i);
View.DisplayResult(r);
}

public void Continue()
{
View.DisplayContinue();
}
}

class View
{
public static void WelcomeMessage()
{
Console.WriteLine("Welcome to my Quadratic Solver");
DrawHorizontalLine();
}

public static void DrawHorizontalLine()
{
DrawHorizontalLine(false);
}

public static void DrawHorizontalLine(bool isLineBreakNeeded)
{
string s = null;

if (isLineBreakNeeded)
{
s = "\n";
}

Console.WriteLine("{0}----------------------------------", s);
}

public static void CollectInput(ref Model.Input i)
{
Console.WriteLine("\nGeneral form of quadratic equation:");
Console.WriteLine("ax^2 + bx + c = 0");

bool isInputComplete = false;

Solution

Naming:

Names like c, r or s don't mean anything, not to you, not to others. Use meaningful names for your variables. c would become controller and so on.

The var keyword:

From the C# Programming Guide:


The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code.

So lines like:

Controller c = new Controller();


would become:

var controller = new Controller();


Exception handling for parsing?

I don't think secretly throwing/catching exceptions for parsing numeric input is good practice. Use the Double.TryParse method to handle this:

double b;

do
{
    Console.WriteLine("\nValue of b?");
} while (!Double.TryParse(Console.ReadLine(), out b));

i.B = b;


Throw this in a method and everything will look a lot cleaner:

private static double GetInput(string valueOf, bool positiveNumber = false)
{
    double output;

    do
    {
        Console.WriteLine("\nValue of {0}?", valueOf);
    } while (!Double.TryParse(Console.ReadLine(), out output));

    if (positiveNumber && output == 0)
    {
        Console.WriteLine("\nValue of a cannot be zero. Please enter a non zero value for a.");
        return GetInput(valueOf, true);
    }

    return output;
}


Your whole CollectInput method can now be shortened to this:

public static void CollectInput(ref Model.Input i)
{
    Console.WriteLine("\nGeneral form of quadratic equation:");
    Console.WriteLine("ax^2 + bx + c = 0");

    i.A = GetInput("a", true);
    i.B = GetInput("b");
    i.C = GetInput("c");
}


Avoiding ref:

I personally don't like writing code using references. You could rewrite your code for collecting input like this:

public static Input CollectInput()
{
    Console.WriteLine("\nGeneral form of quadratic equation:");
    Console.WriteLine("ax^2 + bx + c = 0");

    var input = new Input();
    input.A = GetInput("a", true);
    input.B = GetInput("b");
    input.C = GetInput("c");

    //Using the object initializer previous lines become:
    var input = new Input { A = GetInput("a", true), B = GetInput("b"), C = GetInput("c") };

    return input;
}

//Usage:
var input = View.CollectInput();


Same goes for the Result.

Nitpicking:

if (coefficient > 0)
{
    return "+";
}
return "-";


can be rewritten to:

return _coefficient > 0 ? "+" : "-";


This also applies to following:

if (d >= 0)
{
    isReal = true;
}
else isReal = false;


This can be shortened to:

_isReal = _d >= 0;

Code Snippets

Controller c = new Controller();
var controller = new Controller();
double b;

do
{
    Console.WriteLine("\nValue of b?");
} while (!Double.TryParse(Console.ReadLine(), out b));

i.B = b;
private static double GetInput(string valueOf, bool positiveNumber = false)
{
    double output;

    do
    {
        Console.WriteLine("\nValue of {0}?", valueOf);
    } while (!Double.TryParse(Console.ReadLine(), out output));

    if (positiveNumber && output == 0)
    {
        Console.WriteLine("\nValue of a cannot be zero. Please enter a non zero value for a.");
        return GetInput(valueOf, true);
    }

    return output;
}
public static void CollectInput(ref Model.Input i)
{
    Console.WriteLine("\nGeneral form of quadratic equation:");
    Console.WriteLine("ax^2 + bx + c = 0");

    i.A = GetInput("a", true);
    i.B = GetInput("b");
    i.C = GetInput("c");
}

Context

StackExchange Code Review Q#90117, answer score: 9

Revisions (0)

No revisions yet.