Giter Site home page Giter Site logo

expression-calculator's Introduction

Expression Calculator

This is an archived copy of a solution to an interview problem that I used to present to new hires.

The problem set:

namespace ExpressionCalculator
{
    using System;
    public class Calculator
    {
        /// <summary>
        /// Calculates the result of the given <paramref name="expression"/>.
        /// </summary>
        /// <example>
        ///     <code>
        ///     int result1 = Calculate("17 * 9");
        ///     int result2 = Calculate("15/6");
        ///     int result3 = Calculate("2+ 4");
        ///     </code>
        /// </example>
        /// <remarks>
        ///   1) You do not need to implement the entire solution in one method.
        ///   2) Feel free to run the program as you go to verify your results.
        ///   3) There are many ways of solving this problem - we are not looking
        ///      for anything extravagant. Feel free to ask questions.
        /// </remarks>
        /// <param name="expression">The expression to calculate.</param>
        /// <returns>The result of the calculated expression.</returns>
        public int Calculate(string expression)
        {
            throw new NotImplementedException("Implement");
        }
    }

    public class Program
    {
        private static void Main(string[] args)
        {
            var calculator = new Calculator();

            Console.WriteLine("Enter an expression: ");
            var exp = Console.ReadLine();
            var result = calculator.Calculate(exp);
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

I was looking for:

  • The ability to split a string
  • Convert string to a double
  • interpret a symbol as an operator (switch statement)
  • providing correct output
  • basic error handling (eg. divide by zero)

My solution is much more complex than necessary, but also shows basic data structures, recursion, loops, and light expressions.

Contribute

If you think this could be better, please open an issue!

Please note that all interactions in this organization fall under our Code of Conduct.

License

MIT © 1996+ Ris Adams

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.