Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C#

Inside the Mathematical Expressions Evaluator

Rate me:
Please Sign up or sign in to vote.
4.88/5 (43 votes)
17 Jan 2008CPOL8 min read 116.3K   3.4K   77  
An article on mathematical expression evaluation
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SevenZ.Calculator;

namespace Calc
{
   public partial class FormMain : Form
   {
      Calculator clc = new Calculator();
      FormVariables formVars;

      public FormMain()
      {
         InitializeComponent();
      }

      private void btnCount_Click(object sender, EventArgs e)
      {
         try
         {
            txtOut.Text = clc.Evaluate(txtIn.Text).ToString();
            lstLog.Items.Insert(0, txtIn.Text);
            txtIn.Clear();
            txtIn.Select();
         }
         catch (CalculateException ex)
         {
            MessageBox.Show(ex.Message, "Calaculator error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            txtIn.Select();
            txtIn.SelectionStart = ex.TokenPosition;
            txtIn.SelectionLength = 1;
         }
         catch (Exception ex)
         {
            MessageBox.Show(ex.Message + "\r\nPlease check if any of binary functions is missing one of it's input parameters", 
            "Calaculator error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            txtIn.Select();
         }
      }

      private void btnVariables_Click(object sender, EventArgs e)
      {
         if (formVars == null || formVars.IsDisposed)
         {
            formVars = new FormVariables(clc);
            formVars.Location = new Point(this.Left + this.Width + 50, this.Top);

            formVars.Show(this);
         }
         else
            formVars.Visible = !formVars.Visible;
            
      }

      private void FormMain_Shown(object sender, EventArgs e)
      {
         btnVariables_Click(null, null);
         this.Select();
      }

      private void lstHistory_SelectedIndexChanged(object sender, EventArgs e)
      {
         if (lstLog.Items.Count > 0)
            txtIn.Text = lstLog.SelectedItem.ToString();
      }

      private void btnReset_Click(object sender, EventArgs e)
      {
         clc.Reset();
         lstLog.Items.Clear();
      }

      private void txtIn_KeyUp(object sender, KeyEventArgs e)
      {
         switch (e.KeyData)
         {
            case Keys.Up:

               if (lstLog.SelectedIndex > 0)
                  lstLog.SelectedIndex = lstLog.SelectedIndex - 1;

               e.Handled = true;
               break;

            case Keys.Down:
               if (lstLog.SelectedIndex < lstLog.Items.Count - 1)
                  lstLog.SelectedIndex = lstLog.SelectedIndex + 1;

               e.Handled = true;
               break;
         }
      }

      private void lstLog_DrawItem(object sender, DrawItemEventArgs e)
      {
         // a nice piece of code from http://www.msdner.com/dev-archive/70/2-8-707629.shtm
         SizeF stringSize = e.Graphics.MeasureString(lstLog.Items[e.Index].ToString(), e.Font);

         // Draw the current item text based on the current Font and the custom brush settings.
         e.Graphics.DrawString(lstLog.Items[e.Index].ToString(), e.Font, Brushes.Black
            , new PointF(e.Bounds.Right - stringSize.Width, e.Bounds.Y));

         e.DrawFocusRectangle(); // If the ListBox has focus, draw a focus rectangle around the selected item.
      }

   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer
Finland Finland
I'm a Master degree student, studying at the University of Joensuu, Finland.

Comments and Discussions