2 Replies - 4191 Views - Last Post: 23 May 2010 - 12:41 PM

#1 groer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 10

Feedback on my first project, C# calculator.

Posted 21 May 2010 - 10:22 PM

Hi. I'm a beginner programmer.

For my first real project, I decided to go for a simple calculator.

I've got the calculator working as I want it to, so now I'd like some feedback and suggestions.

Please review it on coding style, readability, commenting etc. and give me tips on how I can improve and things I could do better/differently.

The form:
namespace Calculator
{
    public partial class Form1 : Form
    {
        public string decimalSeparator = System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;
        private Calculator calc = new Calculator();

        public Form1()
        {
            InitializeComponent();
            this.KeyPress += new KeyPressEventHandler(this.form1_KeyPress);
            this.decimalSymbol.Text = decimalSeparator;
            output.Focus();
        }

        // Handle keyboard input.
        private void form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Parse a number or decimal sign
            if ((e.KeyChar >= '0' && e.KeyChar <= '9')
                || (e.KeyChar == decimalSeparator[0] && output.Text.Contains(decimalSeparator[0]) == false))
            {
                if (calc.Summed && calc.Operated == false)
                {
                    calc.Reset();
                    clearOutput();
                }

                output.Text += e.KeyChar.ToString();
            }

            // Remove a character from the output if user pressed Backspace.
            if (e.KeyChar == (char)Keys.Back && (output.Text.Length > 0))
                output.Text = output.Text.Substring(0, output.Text.Length - 1);

            switch (e.KeyChar)
            {
                    // Set the operator to be used, if pressed on the keyboard
                case '+':
                case '/':
                case '-':
                case '*':
                case '%':
                    calc.Operate(output.Text);
                    calc.Operator = e.KeyChar.ToString();
                    clearOutput();
                    break;

                    // Calculate the total if user pressed "=" or Enter.
                case '=':
                case (char)Keys.Return:
                    calc.Total(output.Text);
                    output.Text = calc.Sum.ToString();
                    break;
            }

            output.Focus();
        }

        private void clearOutput()
        {
            output.Text = "";
            output.Focus();
        }

        // When the user clicks one of the number, decimal sign, or +/- buttons
        private void number_Click(object sender, EventArgs e)
        {
            Control c = (Control)sender;

            // Start over if the user enters a number instead of an operator after getting the total sum.
            if (calc.Summed && calc.Operated == false)
                calc.Reset();

            // Skip if the textbox already contains a decimal sign
            if (c.Text == decimalSeparator && output.Text.Contains(decimalSeparator[0]))
                return;

            // The +/- button toggles positive/negative number.
            else if (c.Text == "+/-")
            {
                if (output.Text.StartsWith("-"))
                {
                    char[] a = { '-' };
                    output.Text = output.Text.TrimStart(a);
                }
                else
                    output.Text = "-" + output.Text;
            }

            // append number or decimal sign to the textbox
            else
                output.Text += c.Text;

            output.Focus();
        }

        // When user clicks on one of the operator buttons, parse the contents of the textbox to
        // a temporary variable, set the operator to be used on the following number, and then
        // clear the textbox.
        private void function_Click(object sender, EventArgs e)
        {
            Control c = (Control)sender;

            calc.Operate(output.Text);
            calc.Operator = (sender as Control).Text;

            clearOutput();
        }

        // Calculate the total sum when user clicks button labeled "=".
        private void sumEquals_Click(object sender, EventArgs e)
        {
            calc.Total(output.Text);
            output.Text = calc.Sum.ToString();
            output.Focus();
        }

        // Reset the calculator when user clicks the button labeled "C".
        private void reset_Click(object sender, EventArgs e)
        {
            calc.Reset();
            clearOutput();
        }
    }
}


The Calculator class:
namespace Calculator
{
    public class Calculator
    {
        public string Operator;
        private decimal decimal1;
        private decimal decimal2;
        
        private decimal sum;
        public decimal Sum
        {
            get { return sum; }
        }
        
        private bool summed = false;
        public bool Summed
        {
            get { return summed; }
        }

        private bool operated = false;
        public bool Operated
        {
            get { return operated; }
        }

        public decimal Calculate(decimal d1, decimal d2)
        {
            decimal sum = 0;
            switch (Operator)
            {
                case "/":
                    if (d2 == 0)
                    {
                        MessageBox.Show("Can't divide by zero");
                        return d1;
                    }
                    sum = d1 / d2;
                    break;
                case "*":
                    sum = d1 * d2;
                    break;
                case "-":
                    sum = d1 - d2;
                    break;
                case "+":
                    sum = d1 + d2;
                    break;
                case "%":
                    if (d2 == 0)
                    {
                        MessageBox.Show("Can't divide by zero");
                        return d1;
                    }
                    sum = d1 % d2;
                    break;
            }
            return sum;
            
        }

        public void Operate(string output)
        {
            // if there has already been a previous math operation
            if (operated)
            {
                Decimal.TryParse(output, out decimal2);
                decimal1 = Calculate(decimal1, decimal2);
            }
                // Otherwise
            else
                Decimal.TryParse(output, out decimal1);

            operated = true;
            summed = false;
        }

        public void Total(string output)
        {
            // if the user presses "=" or Enter again, repeat the last operation on the current sum
            if (summed && operated == false)
                decimal1 = sum;

            // Otherwise, calculate the previous and current number together.
            else
            {
                Decimal.TryParse(output, out decimal2);
                operated = false;
            }

            sum = Calculate(decimal1, decimal2);
            summed = true;
        }

        public void Reset()
        {
            sum = 0;
            operated = false;
            summed = false;
        }
    }
}


Edit: Added language and type of project in the topic description

This post has been edited by groer: 22 May 2010 - 12:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Feedback on my first project, C# calculator.

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Feedback on my first project, C# calculator.

Posted 21 May 2010 - 10:35 PM

I'm going to move this to Share My Project :)
Was This Post Helpful? 0
  • +
  • -

#3 groer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 10

Re: Feedback on my first project, C# calculator.

Posted 23 May 2010 - 12:41 PM

It would probably be better and more convenient to attach a file with the project in it instead of using code tags :)

Project attached.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1