Click here to Skip to main content
15,886,799 members
Articles / Programming Languages / C#

A Command Line Implementation of C# Made for Teaching. Also a Command Line Calculator (.NET 2)

Rate me:
Please Sign up or sign in to vote.
2.82/5 (6 votes)
10 Nov 2005CPOL5 min read 36.9K   359   13   5
A command line calculator I am writing to help me teach a 9 year old to code
Sample Image - Fletexec.jpg

Introduction

The executable for this project is in the bin directory.

The source was developed and compiled in Visual Studio 2005 C# Express beta 2 and works in the release version.

This is my first article here and is of a different style than many others, so I hope I don’t mess up too badly. Sorry for the bad grammar.

One day when I was at a track meet, I let it slip that I was a programmer. I usually keep this quiet because the idea of a 14 year old programming scares the heck out of most of my friends. So anyway, the coach’s son Gabriel happened to also be a programmer and he wanted to learn C#, couldn’t blame him he had been working in chipmonk basic. So I needed a way to get this kid who thinks programming is linear to think C#. He had never seen a “for” loop and I needed a method of instant gratification. So I set out to design the idea of a “FLet” a little function whose return value is instantly displayed on the screen. I knew I could get this through Python, but he wanted C#. This project is the result of that.

Using this Program

The purpose of the program is that you can type in C# code, press enter and get a result. It launches set up as a command line calculator so you can type 5*5 and see 25 in the answer field.

A Quick Overview of the UI

The first box labelled Answer contains the value of ans.ToString();. There is a big green or red area that is when red notifies you that you are in the advanced edit mode and when green notifies you that you are in the simple edit mode. Edit modes are described below.

Below this top band, you have the main edit box which has the code for your functionlet (the stuff that gets run).

Inside the Main edit box, there are several buttons. From top to bottom, there is the set mode button that when clicked, brings up a list box. Below it, the items math and language (see “the language modes” for a description below). Then there is the run button which runs the code in the main edit box. There is also a help button which displays help.

The Edit Modes

Using this program is quite simple. There are two edit modes “Simple” and “Advanced”. From a programmer’s point of view, there is a C# function that you are writing and the text you put in the text box will be part of that function. In advanced edit mode, the code for making the function is this:

C++
ftext += "double ans = (double)Parameters[0];";
ftext += code;
ftext += "return ans;";

So whatever the previous value of ans was is the first parameter, therefore ans == the last this ans was. In advanced edit mode, you could type “ans=5*5;” and “ans=ans*4;” and ans would be 100. In simple mode, you would just have to write “5*5” and “ans*4” to make ans equal to 100. In simple mode, the program adds “ans=” and “;” around the code.

The advantage of advanced mode is that you can have all the logic of C# which can be useful.

The Language Modes

There are 3 differences between object mode and math mode.

  1. In math mode, the type of ans is double. In object mode, the type of ans is object
  2. In math mode, all numbers are cast to double, i.e. in math mode, 5*5 is actually (double)5*(double)5
  3. In math mode, the math functions described in help are replaced with System.Math.functionname

The Code Behind this Program

I first needed to know how to compile C# on the fly. I learned how here http://www.developerfusion.co.uk/show/4529/1/ and have hammered his code into the subproject fexec. This should be fairly easy to understand.

Since Gabriel is a math genius, I wanted to make the program a sort of calculator. In the precompiler project, I have functions for both precompiling with answer being an object and precompiling as answer being a double. The calculator proved difficult. I found that in order to do simple equations, I needed to change all numbers to double. This proves dicey when indexing arrays (maybe I need to make the program context sensitive, i.e., not cast inside brackets). One of the reasons I am publishing this article is to see if there is enough interest in this project to figure out how to fix that problem.

Another problem I had was the use of math functions. I wanted my calculator fully capable of the system.math functions without writing system.math every time. Since Cos could appear in a variable name, I made two functions getwords and setwords.

C++
private static string[] Getwords(string page, char[] grammar, char[] letters)
        {
            System.Collections.ArrayList words = new System.Collections.ArrayList();
            bool end = false;
            int i = 0;
            while (!end)
            {
                int word = page.IndexOfAny(letters, i);
                if (word == -1)
                    end = true;
                else
                {
                    int endword = page.IndexOfAny(grammar, word);
                    words.Add(page.Substring(word, endword-word));
                    i = endword;
                }
            }
            return (string[])words.ToArray(typeof(string));
        }

private static string Setwords(string page, char[] grammar, char[] letters, 
                               string[] words)
        {
            bool end = false;
            int i = 0;
            int wordindex = 0;
            while (!end)
            {
                int word = page.IndexOfAny(letters, i);
                if (word == -1)
                    end = true;
                else
                {
                    int endword = page.IndexOfAny(grammar, word);
                    page = page.Remove(word, endword-word);
                    if (wordindex >= words.Length)
                        return null;
                    page = page.Insert(word, words[wordindex]);
                    i = word + words[wordindex].Length;
                    wordindex++;
                }
            }
            return page;
        }
    }
}

These functions make an array of the words in a functionlet and allow you to edit that array and reinsert the new words. I can then replace all instances of math functions with system.math functions.

C++
//replace math functions with System.Math functions
            string[] cl2csgrammar = new string[] { "Abs", "Acos", "Asin", 
"Atan", "Atan", "Cos", "Cosh", "Log", "Log10", "Sign", "Sinh", "Sin", 
"Sqrt", "Tan", "Tanh", "Truncate", "E", "PI" };//the math functions of system.math

            string Functionpath = "System.Math.";

            char[] Chars = new char[] { 'q', 'w', 'e', 'r', 't', 'y', 'u', 
         'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 
         'c', 'v', 'b', 'n', 'm', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 
         'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 
         'C', 'V', 'B', 'N', 'M', '.' };

            char[] Punctuation = new char[] { ' ', ';', '+', '-', '*', '/', 
                                          '(', ')', '=', '&', '{', '}' };
            string[] words = Getwords(code, Punctuation, Chars);

            int wordindex = 0;
            foreach (string word in words)
            {
                foreach (string function in cl2csgrammar)
                {
                    if (word.ToLower() == function.ToLower())
                        words[wordindex] = Functionpath + function;
                }
                wordindex++;
            }
            code = Setwords(code, Punctuation, Chars, words);

That’s about it at this point. I am hoping to see a response here on how bad my code is and how much time I wasted doing x. Plus I need help writing! It is way out of date and I don't know where to start. ;)

Versions

Coming Soon

  • XML file format
  • Support for embedded functionlets GUI and libraries.
  • Code coloring and code completion

Releases So Far

  • Beta 2.3 cleaner code formatting and fixed a focus bug
  • Beta 2.2 UI redesign bug fixes performance fixes
  • Beta 2.1 features and bug fixes. Lots...
  • Beta 2.0 basically untested feature release
  • Still the same program, edited article for grammar, spelling, and readability
  • Beta 1.1 fixed bug when changing modes

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
Generalexcellent work Pin
Member 43331810-Nov-08 14:02
Member 43331810-Nov-08 14:02 
So amazing that the author is just 14 years old and can do this difficult work.
Keep on this work, another Bill Gates will appear
QuestionVery interesting Pin
pascalpub29-Jan-07 14:16
pascalpub29-Jan-07 14:16 
QuestionBad rating but no comments? Pin
Timothy the lion16-Nov-05 10:24
Timothy the lion16-Nov-05 10:24 
Generalgood work Pin
Elias Bachaalany25-Oct-05 20:46
Elias Bachaalany25-Oct-05 20:46 
GeneralRe: good work Pin
Timothy the lion26-Oct-05 11:17
Timothy the lion26-Oct-05 11:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.