Thread: Calculator application

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    20

    Post Calculator application

    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:
    Code:
    
    
    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


  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    I would think - what you are doing in equals click for one operand functions like sin - should be done in function click itself. On calculator you do not do
    '1' '2' '3' 'sin' '=' to see result
    you do
    '1' 2' '3' 'sin' and immediatly - see result in the input box, and can perform next operation on it - you did not implement this logic at all:

    Something like:
    '1' '+' '2' '+' (see "3" in input box) '4' '=' (see "7" in inout box) '+' '1' '2' '=' (see "19" in input box) '1' (see "1" in input box, drop previous result from stack)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    I have done with that ISSUE .Now can anybody will tell me why the dot(.) button is not producing the like output?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-02-2014, 01:06 PM
  2. Replies: 2
    Last Post: 05-18-2012, 04:20 AM
  3. Replies: 1
    Last Post: 07-03-2010, 01:18 PM
  4. Application A interacting with application B
    By Dante Wingates in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2010, 08:01 AM
  5. Convirt my C program to a GUI application/Web Application
    By kapil1089thekin in forum C Programming
    Replies: 6
    Last Post: 07-21-2008, 01:43 AM