Hi all,

I am developing calculator app using C#.I have successfuly implemented its standard arithmetic functions but a bit trapped in implementing scientific functions such as sin,cos.tan etc.
I take an operand1,then operator and finally second opernad to workout arithmetic calculations.In the very first attempt to do scientific calculation i for the sake of programme format first takes input followed by sin/cos/tan etc and then out result.
Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {

        string input = string.Empty;
        string operand1 = string.Empty;
        string operand2 = string.Empty;
        char operaton;
        double result = 0.0;


        public Form1()
        {
            InitializeComponent();
        }

        private void one_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = " ";
            input += "1";
            this.textBox1.Text += input;
        }

        private void two_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = " ";
            input += "2";
            this.textBox1.Text += input;
        }

        private void three_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = " ";
            input += "3";
            this.textBox1.Text += input;
        }

        private void four_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = " ";
            input += "4";
            this.textBox1.Text += input;
        }

        private void five_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = " ";
            input += "5";
            this.textBox1.Text += input;

        }

        private void six_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = " ";
            input += "6";
            this.textBox1.Text += input;
        }

        private void seven_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = " ";
            input += "7";
            this.textBox1.Text += input;
        }

        private void eight_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = " ";
            input += "8";
            this.textBox1.Text += input;
        }

        private void nine_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = " ";
            input += "9";
            this.textBox1.Text += input;
        }

        private void dot_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = " ";
            input += ".";
            this.textBox1.Text += input;
        }

        private void zero_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = " ";
            input += "0";
            this.textBox1.Text += input;
        }

        private void clear_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "0";
            this.input = string.Empty;
            this.operand1 = string.Empty;
            this.operand2 = string.Empty;


        }

        private void plus_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operaton = '+';
            input = string.Empty;
        }

        private void minus_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operaton = '-';
            input = string.Empty;
        }

        private void mul_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operaton = '*';
            input = string.Empty;
        }

        private void div_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operaton = '/';
            input = string.Empty;
        }
        private void mod_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operaton = '%';
            input = string.Empty;
        }

        private void sine_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operaton = 's';
            input = string.Empty;
        }

        private void cosine_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operaton = 'c';
            input = string.Empty;
        }

        private void tan_Click(object sender, EventArgs e)
        {
            operand1 = input;
            operaton = 't';
            input = string.Empty;
        }



        private void equals_Click(object sender, EventArgs e)
        {
            operand2 = input;
            double num1, num2;
            double.TryParse(operand1, out num1);
            double.TryParse(operand2, out num2);

            if (operaton == '+')
            {
                result = num1 + num2;
               textBox1.Text = result.ToString();
            }
            else if (operaton == '-')
            {
                result = num1 - num2;
                textBox1.Text = result.ToString();
            }
            else if (operaton == '*')
            {
                result = num1 * num2;
                textBox1.Text = result.ToString();
            }
            else if (operaton == '/')
            {
                if (num2 != 0)
                {
                    result = num1 / num2;
                    textBox1.Text = result.ToString();
                }
                else
                {
                    textBox1.Text = "DIV/Zero!";
                }
            }

            else if (operaton == '%')
            {

                result = num1 % num2;
                textBox1.Text = result.ToString();


            }
            else if (operaton == 's')
            {   
                int inp;
                int.TryParse(operand1, out inp);
                result = Math.Sin(inp);
                textBox1.Text = result.ToString();

            }
            else if (operaton == 'c')
            {
                int inp;
                int.TryParse(operand1, out inp);
                result = Math.Cos(num1);
                textBox1.Text = result.ToString();

            }
            else if (operaton == 't')
            {
                int inp;
                int.TryParse(operand1, out inp);
                result = Math.Tan(num1);
                textBox1.Text = result.ToString();

            }










        }



    }     












    }

I want to mention here that my output for scientific functions is not correct.Also,kindly tell me error in this format as in second attempt i will convert it to standard scientific mode i.e. pressing sin/cos/tan and then operand.

Regards

Recommended Answers

All 5 Replies

What do you mean by not correct could you give an example?
Could you post the layout of your form?
and an extra, what happens if you click the '.' button twice?

Well the issue is resolved.
But even a single click is not including a dot in the number.y so?

Having everything laid in a literal fashion like that is starting to make your code very unwieldy.

You can use one event handler to handle each set of button's click events. Simply use the Text property of the button as the value.

Add a line like this:

one.Click += num_Click;

for each button. If you change the names to a pattern("num1","num2",etc.) you can use a foreach loop to iterate through the buttons and add the handler to each one.

The handler would look something like this:

private void num_Click(object sender, EventArgs e)
{
    Button clickedButton = (Button)sender;
    //Allow only one decimal point
    if(clickedButton.Text == "." && textBox1.Text.Contains("."))
        return;
    textBox1.Text += clickedButton.Text;
}

You can do something similar for the other buttons as well.

commented: would you suggest me a change using my 'own code' instead of making prompt changes.I would switch to more sophisticated tech.later on +0

Instead of (example for the 8 button

 private void eight_Click(object sender, EventArgs e)
78.        {
79.            this.textBox1.Text = " ";
80.            input += "8";
81.            this.textBox1.Text += input;
82.        }

you could write

 private void eight_Click(object sender, EventArgs e)
78.        {
81.            this.textBox1.Text += "8";
82.        }

Make the textBox1 empty and fill in operand1 in the operator handlers.
Extra question: what would happen if you click "=" after the first digit input and before clicking an operator?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.