Console calculator Part 3 : The calculator engine

ddanbe 0 Tallied Votes 1K Views Share

This is the last part.
This is the code that sets it al together and lets the parser and scanner do some work.
Happy calculating!

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

namespace ConsoleCalculator
{
    class Program
    {
        static void WriteIntro()
        {
            Console.WriteLine("===============================================================================");
            Console.WriteLine("       ______");
            Console.WriteLine("      /_____/                       MINI CONSOLE CALCULATOR");
            Console.WriteLine("     /*/*/*/");
            Console.WriteLine("    /*/*/*/                         Free software");
            Console.WriteLine("   /*/*/*/                          Danny Marivoet 2008");
            Console.WriteLine("  -------");
            Console.WriteLine("           ");
            Console.WriteLine("  Type ? for help.        ");
            Console.WriteLine("           ");
            Console.WriteLine("===============================================================================");
        }

        static void DisplayHelp()
        {
            Console.WriteLine("===============================================================================");
            Console.WriteLine("Simple console expression calculator in C#");
            Console.WriteLine(" ");
            Console.WriteLine("Based on the Desk Calculator proposed by B. Stroustrup (see p75 of his book)");
            Console.WriteLine("I added the use of functions and commands.I did the symbol tables differently.");
            Console.WriteLine(" ");
            Console.WriteLine("This is by far a complete implementation, but it shows the use of a recursive ");
            Console.WriteLine("descent parser. Follow e.g. the action with your debugger in VS, fascinating!");
            Console.WriteLine("The calculator has 3 commands : '?' shows this help,'LIST' shows a list of the");
            Console.WriteLine("currently available variables and 'END' ends the application.");
            Console.WriteLine("It has one function : SQRT, but feel free to add others.");
            Console.WriteLine("It's easy! Really!");
            Console.WriteLine("The typing of commands and functions is not case sensitive.");
            Console.WriteLine("And I admit error handling could be better, but it's a start.");
            Console.WriteLine("Besides calculations with +,-,* and / ,like 3.56*88 you can do things like:");
            Console.WriteLine("'foo=1.5',after enter, the variable foo is stored with a value of 1.5 ");
            Console.WriteLine("Next you can calculate : '6.1 - sqrt(foo)' which should give 4.87525512860841");
            Console.WriteLine("Enjoy this code. It's free. Use it anyway you see fit.");
            Console.WriteLine("Errors in it are also for free, but if you can find any, please let me know.");
            Console.WriteLine(" ");
            Console.WriteLine("===============================================================================");
        }

        static void Main(string[] args)
        {
            Parser Parse = new Parser();
            WriteIntro();
            while (true)    //loop until the END command is given
            {
                Console.Write("*--->");
                Parse.InitScan();
                Parse.InputLine = Console.ReadLine();
                Parse.curr_tok = Parse.get_token();
                if (Parse.curr_tok == Parser.token_value.END) break;//leave while-loop
                if (Parse.curr_tok == Parser.token_value.LIST)
                {
                    Parse.ListVars(); 
                    continue;   //with loop
                }
                if (Parse.curr_tok == Parser.token_value.HELP)
                {
                    DisplayHelp();
                    continue;   //with loop
                }
                if (Parse.curr_tok == Parser.token_value.PRINT) continue; //with loop
                Console.WriteLine(Parse.expr());    //write result
            }     
        }
    }
}