Building a calculator in C#, stuck on doing the math stuff.

Started by
49 comments, last by eFoDay 11 years, 9 months ago
Hello,

I usually program in C++ and I only know the basics of C#. I used C# once to write a simple game using XNA. I've been wanting to do a bit more C# programming lately and I thought this problem sounded interesting.

I decided to see if I could write a C# command line program that would take a basic equation and solve it. I was able to make a very basic calculator. It doesn't take into account operator precedence or invalid input.

Here is the code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Very basic calculator
// Doesn't handle operator precedence or invalid input
namespace SimpleCalculator
{
class Program
{
static void Main(string[] args)
{
string userInput = ""; // stores user input

// Loop until the user types 'exit'
while (userInput != "exit")
{
// Get user input
Console.Write("Please enter your equation: ");
userInput = Console.ReadLine();
if (userInput == "exit") {
break; // done
}

// You may now have a string like this: 42 + 43 - 47 + 32 / 5 * 2
// or like this: 4 2 +43 - 4 7 + 32 /5* 2
//
// or with decimals like this: 12.5 + 3.5 /3
// or even like this: 12. 5 +3 .5 / 3

// Remove all the spaces
userInput = userInput.Replace(" ", "");

// Use RegEx to Split the strings
System.Text.RegularExpressions.Regex rNums = new System.Text.RegularExpressions.Regex("[+]|[-]|[/]|
  • ");
    System.Text.RegularExpressions.Regex rSigns = new System.Text.RegularExpressions.Regex("[0-9]+[.]?[0-9]?");

    // Split the numbers and signs into their own lists
    List<string> explodedNums = rNums.Split(userInput).ToList<string>();
    List<string> explodedSigns = rSigns.Split(userInput).ToList<string>();
    explodedSigns.RemoveAll(item => item == ""); //

    // Show the separated numbers
    Console.WriteLine("\nNumbers");
    Console.WriteLine("=======");
    foreach (string match in explodedNums)
    {
    Console.WriteLine("'{0}'", match);
    }

    // Show the separated signs
    Console.WriteLine("\nSigns");
    Console.WriteLine("=====");
    foreach (string match in explodedSigns)
    {
    Console.WriteLine("'{0}'", match);
    }

    //
    double operand1, operand2 = 0.0;

    // Get the first number
    operand1 = System.Convert.ToDouble(explodedNums[0]);

    // For every other number
    for (int i = 1, j = 0; i < explodedNums.Count; i++, j++)
    {
    Console.Write("\n{0} ", operand1); // show first part of equation

    // Get the next number
    operand2 = System.Convert.ToDouble(explodedNums);

    // Perform the operation
    switch (explodedSigns[j])
    {
    case "+":
    {
    operand1 += operand2;
    break;
    }
    case "-":
    {
    operand1 -= operand2;
    break;
    }
    case "/":
    {
    operand1 /= operand2;
    break;
    }
    case "*":
    {
    operand1 *= operand2;
    break;
    }
    }

    // Show final part of equation
    Console.Write("{0} {1} = {2}", explodedSigns[j], operand2, operand1);
    }

    // Show the answer
    Console.WriteLine("\n\nYour final answer is: " + operand1 + "\n");

    } // while loop

    } // Main()

    } // class

    } // namespace
  • This topic is closed to new replies.

    Advertisement