Wednesday, March 20, 2013

C# 2012 : Simple Calculator

As I am learning C#, in my second semester in OSU, I have made a simple calculator which can do simple mathematical calculation. This is my first blog , thank you for viewing! Enjoy the code.





Use this code in your example.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;  //using system.Windows.Forms;


namespace Moder_Machine_Calc
{
    class Calculator
    {

        //Fields
        private double number1;
        private double number2;
        private char op;
        private double result;


        //Empty Constructor

        public Calculator(){}

        //Custom Construtor

        public Calculator(double n1, double n2, char op)
        {
            number1 = n1;
            number2 = n2;
            this.op = op; //this.op is field value ^^^^^^^ op is operator.
            //use can use this above

        }

        //Properties
        public double Number1
        {
            get
            {
                return number1;
            }
            set
            {
                number1 = value;
            }
        }
        public double Number2
        {
            get
            {
                return number2;

            }
            set
            {
                number2 = value;
            }
        }
        public char Op
        {
            get {
                return op;
            }
            set
            {
                op = value;
            }
        }
        public double Result
        {
            get
            {
                return result;
            }
            set
            {

                result = value;
            }
        }
  //Methods
        public void Add()
        {
            result = number1 + number2;
        }

        public void Subtract()
        {

            result = number1 - number2;
        }
        public void product()
        {
            result = number1 * number2;
        }
        public void divide()
        {

            result = number1 / number2;

        }
        public void reciprocal()
        {
            try
            {

                result = 1 / number1;
            }
            catch
            {
                MessageBox.Show("Cannot be dividede by Zeor", "Infinity Error");
            }
        }
        public void squaroot()
        {
            result = Math.Sqrt(number1);
        }
        public string DisplayResult()
        {
            return result.ToString();
        }
       

    }
}
--Use this code in your example.designer.cs file:
namespace Moder_Machine_Calc
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.txtDisplay = new System.Windows.Forms.TextBox();
            this.btnBack = new System.Windows.Forms.Button();
            this.btnClear = new System.Windows.Forms.Button();
            this.btn7 = new System.Windows.Forms.Button();
            this.btn8 = new System.Windows.Forms.Button();
            this.btn9 = new System.Windows.Forms.Button();
            this.btn4 = new System.Windows.Forms.Button();
            this.btn5 = new System.Windows.Forms.Button();
            this.btn6 = new System.Windows.Forms.Button();
            this.btn1 = new System.Windows.Forms.Button();
            this.btn2 = new System.Windows.Forms.Button();
            this.btn3 = new System.Windows.Forms.Button();
            this.btn0 = new System.Windows.Forms.Button();
            this.button13 = new System.Windows.Forms.Button();
            this.button14 = new System.Windows.Forms.Button();
            this.btnDiv = new System.Windows.Forms.Button();
            this.btnPro = new System.Windows.Forms.Button();
            this.btnSub = new System.Windows.Forms.Button();
            this.btnAdd = new System.Windows.Forms.Button();
            this.btnSqrt = new System.Windows.Forms.Button();
            this.btnFrac = new System.Windows.Forms.Button();
            this.btnEqual = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // txtDisplay
            //
            this.txtDisplay.BackColor = System.Drawing.SystemColors.AppWorkspace;
            this.txtDisplay.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtDisplay.ForeColor = System.Drawing.SystemColors.InfoText;
            this.txtDisplay.Location = new System.Drawing.Point(17, 16);
            this.txtDisplay.Margin = new System.Windows.Forms.Padding(4);
            this.txtDisplay.Multiline = true;
            this.txtDisplay.Name = "txtDisplay";
            this.txtDisplay.ReadOnly = true;
            this.txtDisplay.Size = new System.Drawing.Size(327, 44);
            this.txtDisplay.TabIndex = 0;
            this.txtDisplay.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            //
            // btnBack
            //
            this.btnBack.ForeColor = System.Drawing.Color.Red;
            this.btnBack.Location = new System.Drawing.Point(17, 68);
            this.btnBack.Margin = new System.Windows.Forms.Padding(4);
            this.btnBack.Name = "btnBack";
            this.btnBack.Size = new System.Drawing.Size(115, 37);
            this.btnBack.TabIndex = 1;
            this.btnBack.Text = "Back";
            this.btnBack.UseVisualStyleBackColor = true;
            this.btnBack.Click += new System.EventHandler(this.btnBack_Click);
            //
            // btnClear
            //
            this.btnClear.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnClear.ForeColor = System.Drawing.Color.Red;
            this.btnClear.Location = new System.Drawing.Point(231, 68);
            this.btnClear.Margin = new System.Windows.Forms.Padding(4);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(115, 37);
            this.btnClear.TabIndex = 2;
            this.btnClear.Text = "Clear";
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            //
            // btn7
            //
            this.btn7.Location = new System.Drawing.Point(17, 133);
            this.btn7.Name = "btn7";
            this.btn7.Size = new System.Drawing.Size(47, 39);
            this.btn7.TabIndex = 3;
            this.btn7.Text = "7";
            this.btn7.UseVisualStyleBackColor = true;
            this.btn7.Click += new System.EventHandler(this.number_Click);
            //
            // btn8
            //
            this.btn8.Location = new System.Drawing.Point(85, 133);
            this.btn8.Name = "btn8";
            this.btn8.Size = new System.Drawing.Size(47, 39);
            this.btn8.TabIndex = 4;
            this.btn8.Text = "8";
            this.btn8.UseVisualStyleBackColor = true;
            this.btn8.Click += new System.EventHandler(this.number_Click);
            //
            // btn9
            //
            this.btn9.Location = new System.Drawing.Point(147, 133);
            this.btn9.Name = "btn9";
            this.btn9.Size = new System.Drawing.Size(47, 39);
            this.btn9.TabIndex = 5;
            this.btn9.Text = "9";
            this.btn9.UseVisualStyleBackColor = true;
            this.btn9.Click += new System.EventHandler(this.number_Click);
            //
            // btn4
            //
            this.btn4.Location = new System.Drawing.Point(17, 178);
            this.btn4.Name = "btn4";
            this.btn4.Size = new System.Drawing.Size(47, 39);
            this.btn4.TabIndex = 6;
            this.btn4.Text = "4";
            this.btn4.UseVisualStyleBackColor = true;
            this.btn4.Click += new System.EventHandler(this.number_Click);
            //
            // btn5
            //
            this.btn5.Location = new System.Drawing.Point(85, 178);
            this.btn5.Name = "btn5";
            this.btn5.Size = new System.Drawing.Size(47, 39);
            this.btn5.TabIndex = 7;
            this.btn5.Text = "5";
            this.btn5.UseVisualStyleBackColor = true;
            this.btn5.Click += new System.EventHandler(this.number_Click);
            //
            // btn6
            //
            this.btn6.Location = new System.Drawing.Point(147, 178);
            this.btn6.Name = "btn6";
            this.btn6.Size = new System.Drawing.Size(47, 39);
            this.btn6.TabIndex = 8;
            this.btn6.Text = "6";
            this.btn6.UseVisualStyleBackColor = true;
            this.btn6.Click += new System.EventHandler(this.number_Click);
            //
            // btn1
            //
            this.btn1.Location = new System.Drawing.Point(17, 223);
            this.btn1.Name = "btn1";
            this.btn1.Size = new System.Drawing.Size(47, 39);
            this.btn1.TabIndex = 9;
            this.btn1.Text = "1";
            this.btn1.UseVisualStyleBackColor = true;
            this.btn1.Click += new System.EventHandler(this.number_Click);
            //
            // btn2
            //
            this.btn2.Location = new System.Drawing.Point(85, 223);
            this.btn2.Name = "btn2";
            this.btn2.Size = new System.Drawing.Size(47, 39);
            this.btn2.TabIndex = 10;
            this.btn2.Text = "2";
            this.btn2.UseVisualStyleBackColor = true;
            this.btn2.Click += new System.EventHandler(this.number_Click);
            //
            // btn3
            //
            this.btn3.Location = new System.Drawing.Point(147, 223);
            this.btn3.Name = "btn3";
            this.btn3.Size = new System.Drawing.Size(47, 39);
            this.btn3.TabIndex = 11;
            this.btn3.Text = "3";
            this.btn3.UseVisualStyleBackColor = true;
            this.btn3.Click += new System.EventHandler(this.number_Click);
            //
            // btn0
            //
            this.btn0.Location = new System.Drawing.Point(17, 270);
            this.btn0.Name = "btn0";
            this.btn0.Size = new System.Drawing.Size(47, 39);
            this.btn0.TabIndex = 12;
            this.btn0.Text = "0";
            this.btn0.UseVisualStyleBackColor = true;
            this.btn0.Click += new System.EventHandler(this.number_Click);
            //
            // button13
            //
            this.button13.Location = new System.Drawing.Point(85, 270);
            this.button13.Name = "button13";
            this.button13.Size = new System.Drawing.Size(47, 39);
            this.button13.TabIndex = 13;
            this.button13.Text = "+/-";
            this.button13.UseVisualStyleBackColor = true;
            this.button13.Click += new System.EventHandler(this.number_Click);
            //
            // button14
            //
            this.button14.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button14.Location = new System.Drawing.Point(147, 270);
            this.button14.Name = "button14";
            this.button14.Size = new System.Drawing.Size(47, 39);
            this.button14.TabIndex = 14;
            this.button14.Text = ".";
            this.button14.UseVisualStyleBackColor = true;
            this.button14.Click += new System.EventHandler(this.number_Click);
            //
            // btnDiv
            //
            this.btnDiv.BackColor = System.Drawing.SystemColors.Control;
            this.btnDiv.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnDiv.ForeColor = System.Drawing.Color.Red;
            this.btnDiv.Location = new System.Drawing.Point(222, 133);
            this.btnDiv.Name = "btnDiv";
            this.btnDiv.Size = new System.Drawing.Size(47, 39);
            this.btnDiv.TabIndex = 15;
            this.btnDiv.Text = "/";
            this.btnDiv.UseVisualStyleBackColor = false;
            this.btnDiv.Click += new System.EventHandler(this.Operator_Click);
            //
            // btnPro
            //
            this.btnPro.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnPro.ForeColor = System.Drawing.Color.Red;
            this.btnPro.Location = new System.Drawing.Point(222, 178);
            this.btnPro.Name = "btnPro";
            this.btnPro.Size = new System.Drawing.Size(47, 39);
            this.btnPro.TabIndex = 16;
            this.btnPro.Text = "*";
            this.btnPro.UseVisualStyleBackColor = true;
            this.btnPro.Click += new System.EventHandler(this.Operator_Click);
            //
            // btnSub
            //
            this.btnSub.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnSub.ForeColor = System.Drawing.Color.Red;
            this.btnSub.Location = new System.Drawing.Point(222, 223);
            this.btnSub.Name = "btnSub";
            this.btnSub.Size = new System.Drawing.Size(47, 39);
            this.btnSub.TabIndex = 17;
            this.btnSub.Text = "-";
            this.btnSub.UseVisualStyleBackColor = true;
            this.btnSub.Click += new System.EventHandler(this.Operator_Click);
            //
            // btnAdd
            //
            this.btnAdd.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnAdd.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.btnAdd.Location = new System.Drawing.Point(222, 268);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(47, 39);
            this.btnAdd.TabIndex = 18;
            this.btnAdd.Text = "+";
            this.btnAdd.UseVisualStyleBackColor = true;
            this.btnAdd.Click += new System.EventHandler(this.Operator_Click);
            //
            // btnSqrt
            //
            this.btnSqrt.Location = new System.Drawing.Point(297, 133);
            this.btnSqrt.Name = "btnSqrt";
            this.btnSqrt.Size = new System.Drawing.Size(47, 39);
            this.btnSqrt.TabIndex = 19;
            this.btnSqrt.Text = "Sqrt";
            this.btnSqrt.UseVisualStyleBackColor = true;
            this.btnSqrt.Click += new System.EventHandler(this.Operator_Click);
            //
            // btnFrac
            //
            this.btnFrac.Location = new System.Drawing.Point(297, 178);
            this.btnFrac.Name = "btnFrac";
            this.btnFrac.Size = new System.Drawing.Size(47, 39);
            this.btnFrac.TabIndex = 20;
            this.btnFrac.Text = "1/x";
            this.btnFrac.UseVisualStyleBackColor = true;
            this.btnFrac.Click += new System.EventHandler(this.btnFrac_Click);
            //
            // btnEqual
            //
            this.btnEqual.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnEqual.ForeColor = System.Drawing.Color.Red;
            this.btnEqual.Location = new System.Drawing.Point(297, 223);
            this.btnEqual.Name = "btnEqual";
            this.btnEqual.Size = new System.Drawing.Size(47, 84);
            this.btnEqual.TabIndex = 21;
            this.btnEqual.Text = "=";
            this.btnEqual.UseVisualStyleBackColor = true;
            this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);
            //
            // Form1
            //
            this.AcceptButton = this.btnEqual;
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(365, 326);
            this.Controls.Add(this.btnEqual);
            this.Controls.Add(this.btnFrac);
            this.Controls.Add(this.btnSqrt);
            this.Controls.Add(this.btnAdd);
            this.Controls.Add(this.btnSub);
            this.Controls.Add(this.btnPro);
            this.Controls.Add(this.btnDiv);
            this.Controls.Add(this.button14);
            this.Controls.Add(this.button13);
            this.Controls.Add(this.btn0);
            this.Controls.Add(this.btn3);
            this.Controls.Add(this.btn2);
            this.Controls.Add(this.btn1);
            this.Controls.Add(this.btn6);
            this.Controls.Add(this.btn5);
            this.Controls.Add(this.btn4);
            this.Controls.Add(this.btn9);
            this.Controls.Add(this.btn8);
            this.Controls.Add(this.btn7);
            this.Controls.Add(this.btnClear);
            this.Controls.Add(this.btnBack);
            this.Controls.Add(this.txtDisplay);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Margin = new System.Windows.Forms.Padding(4);
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = "Simple dCalculator";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txtDisplay;
        private System.Windows.Forms.Button btnBack;
        private System.Windows.Forms.Button btnClear;
        private System.Windows.Forms.Button btn7;
        private System.Windows.Forms.Button btn8;
        private System.Windows.Forms.Button btn9;
        private System.Windows.Forms.Button btn4;
        private System.Windows.Forms.Button btn5;
        private System.Windows.Forms.Button btn6;
        private System.Windows.Forms.Button btn1;
        private System.Windows.Forms.Button btn2;
        private System.Windows.Forms.Button btn3;
        private System.Windows.Forms.Button btn0;
        private System.Windows.Forms.Button button13;
        private System.Windows.Forms.Button button14;
        private System.Windows.Forms.Button btnDiv;
        private System.Windows.Forms.Button btnPro;
        private System.Windows.Forms.Button btnSub;
        private System.Windows.Forms.Button btnAdd;
        private System.Windows.Forms.Button btnSqrt;
        private System.Windows.Forms.Button btnFrac;
        private System.Windows.Forms.Button btnEqual;
    }
}


----Thank you ! ENJOY the code!

No comments:

Post a Comment