Question

In: Computer Science

This is to check to ensure you are current in your work. Please create a simple...

This is to check to ensure you are current in your work.

Please create a simple calculator with only +, - and divide. It has to be able to enter any numbers (including decimals) and be able to do the following functions:

+, -, divide and multiply.

Please have the answers, always rounded to two decimal figures. The calculator will also have to be able to take up to 5 values.

Solutions

Expert Solution

Hi,

I have created a Calculator Project using C# Form

Below is the Form Details:

using System;
using System.Windows.Forms;

namespace NestedLoop
{
    public partial class Calculator : Form
    {
        public Calculator()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Addition of 2 numbers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                //Check Validations
                if (txtNumber1.Text.Trim().Length <= 0)
                {
                    lblErrorMessage.Text = "Please enter Number1";

                }
                else if (txtNumber2.Text.Trim().Length <= 0)
                {
                    lblErrorMessage.Text = "Please enter Number2";
                }
                else
                {
                    //Clear the Error message string
                    lblErrorMessage.Text = string.Empty;

                    //Fetch the 2 numbers
                    int iNumber1 = Convert.ToInt32(txtNumber1.Text);
                    int iNumber2 = Convert.ToInt32(txtNumber2.Text);

                    //Apply Addition logic
                    int outPut = iNumber1 + iNumber2;

                    //Set result to the final text
                    txtNumber3.Text = outPut.ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// Substraction of 2 numbers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSubstraction_Click(object sender, EventArgs e)
        {
            try
            {
                //Check Validations
                if (txtNumber1.Text.Trim().Length <= 0)
                {
                    lblErrorMessage.Text = "Please enter Number1";

                }
                else if (txtNumber2.Text.Trim().Length <= 0)
                {
                    lblErrorMessage.Text = "Please enter Number2";
                }
                else
                {
                    //Clear the Error message string
                    lblErrorMessage.Text = string.Empty;

                    //Fetch the 2 numbers
                    int iNumber1 = Convert.ToInt32(txtNumber1.Text.Trim());
                    int iNumber2 = Convert.ToInt32(txtNumber2.Text.Trim());

                    //Apply substraction logic
                    int outPut = iNumber1 - iNumber2;

                    //set result to the final text
                    txtNumber3.Text = outPut.ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// Division of 2 numbers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDivide_Click(object sender, EventArgs e)
        {
            try
            {
                //Check Validations
                if (txtNumber1.Text.Trim().Length <= 0)
                {
                    lblErrorMessage.Text = "Please enter Number1";

                }
                else if (txtNumber2.Text.Trim().Length <= 0)
                {
                    lblErrorMessage.Text = "Please enter Number2";
                }
                else if (Convert.ToInt32(txtNumber2.Text.Trim()) <= 0)
                {
                    lblErrorMessage.Text = "Number2 should be greater than 0";
                }
                else
                {
                    //Clear the Error message
                    lblErrorMessage.Text = string.Empty;

                    //Fetch the 2 numbers
                    decimal iNumber1 = Convert.ToDecimal(txtNumber1.Text.Trim());
                    decimal iNumber2 = Convert.ToDecimal(txtNumber2.Text.Trim());

                    //Do the Division of 2 numbers
                    decimal ab = new decimal();
                    ab = Math.Round((decimal)(iNumber1 / iNumber2), 2);


                    //set result to the final text
                    txtNumber3.Text = ab.ToString();
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// Clear the Controlls
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            try
            {
                txtNumber1.Text = string.Empty;
                txtNumber2.Text = string.Empty;
                txtNumber3.Text = string.Empty;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
    }
}

Addition, Subtraction and Division buttons has provided so that when user clicks on the Addition button then result will show Addition, when user clicks on the Substraction then result will show Substraction and same for Division.

I have also handled Error cases like Input Number should not be Empty and in case of Division, second number should be greater than 0 to avoid the Divide By 0 Exception

1. Addition

2. Substraction

3. Division


Related Solutions

A simple pencil is? (check all that apply) The end result of countless hours of work...
A simple pencil is? (check all that apply) The end result of countless hours of work from the factory. Difficult to produce because only a few people know how to do it. The end result of a vast and intricate family tree of ancestors. Easy to make B. Who dictates the actions of people needed to produce a pencil? Everyone No one Government A few powerful people C. How many people does it take to make a pencil? It only...
As the CFO, how do you work to ensure your Medical Loss Ratios are where they...
As the CFO, how do you work to ensure your Medical Loss Ratios are where they should be? What needs to happen? And what do you need to do?
Create a 15 slide PowerPoint presentation of the pediatric condition you selected. Ensure that your presentation...
Create a 15 slide PowerPoint presentation of the pediatric condition you selected. Ensure that your presentation addresses: Developmental dysplasia of the hip Description and etiology  of the condition Epidemiology for the condition Pathophysiology Clinical Manifestation Diagnostic evaluation Therapeutic Management: Be sure to address medication as well as surgical and non-surgical management if applicable Care Management to include nursing intervention The presentation should have a title slide and a reference slide. They are not included in the slide count.
Create a 15 slide PowerPoint presentation of the pediatric condition you selected. Ensure that your presentation...
Create a 15 slide PowerPoint presentation of the pediatric condition you selected. Ensure that your presentation addresses: Description and etiology  of the condition Epidemiology for the condition Pathophysiology Clinical Manifestation Diagnostic evaluation Therapeutic Management: Be sure to address medication as well as surgical and non-surgical management if applicable Care Management to include nursing intervention The presentation should have a title slide and a reference slide. They are not included in the slide count. Choose a Pediatric Condition and Create a 15...
What are two strategies you can use to monitor your own work to ensure the required...
What are two strategies you can use to monitor your own work to ensure the required standard of support is maintained?
Please write in C++ as simple as possible I want you to create a Book Class...
Please write in C++ as simple as possible I want you to create a Book Class for a bookstore. I am not going to tell you what variables should go into it, that is for you to figure out (but you should have at least 5+). And then you must create a UML with all the variables and methods (like: the getters and setters, a default constructor, and a constructor that takes all the variables and finally the printValues() )....
Please answer full question thoroughly (A- D) showing detailed work. SUBMIT ORIGINAL work and ensure it...
Please answer full question thoroughly (A- D) showing detailed work. SUBMIT ORIGINAL work and ensure it is correct for thumbs up. a) What is the effect of calling MAX-HEAPIFY(A, i) when the element A[I ]is larger than its children? b) What is the effect of calling MAX-HEAPIFY(A, i) for i > A.heap-size/2? c) The code for MAX-HEAPIFYis quite efficient in terms of constant factors, except possibly for the recursive call in line 10, which might cause some compilers to produce...
Please answer full question thoroughly (A- D) showing detailed work. SUBMIT ORIGINAL work and ensure it...
Please answer full question thoroughly (A- D) showing detailed work. SUBMIT ORIGINAL work and ensure it is correct for thumbs up. a) Show that an n-element heap has height   b) Show that in any subtree of a max-heap, the root of the subtree contains the largest value occurring anywhere in that subtree. c) Where in a max-heap might the smallest element reside, assuming that all elements are distinct? d) Show that, with the array representation for storing an n-element heap,...
Please check my work. The purpose of this exercise is to provide students with the opportunity...
Please check my work. The purpose of this exercise is to provide students with the opportunity to normalize data. Assignment: Identify what fields from the list below belong to which table. If you are uncertain, place the field under the column with your argument on why it should belong. Write your answers in the columns below the table names. COURSE FACULTY LOCATION SECTION STUDENT CourseID CourseNumber CourseName CreditHours Status Description Days StartTime EndTime FacultyID Status FirstName LastName WorkPhone HomePhone CellPhone...
Please answer the following questions. Please write all your work. You will be graded on your...
Please answer the following questions. Please write all your work. You will be graded on your process, NOT on achieving the correct answer. Please staple all pages together. Include your name on every page. 1) Wacky Widgets, Incorporated is considering a new three-year expansion project to make new Widgets. It requires an initial fixed investment of $2.7 million. The fixed asset will be depreciated straight-line to zero over its three-year tax life, after which it will be sold for $50,000...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT