Question
Create a graphical calculator with buttons that can add, subtract, multiply, divide, +/-, Sin, Cos, Tan, clear and Modulus. Write a method for each of
Create a graphical calculator with buttons that can add, subtract, multiply, divide, +/-, Sin, Cos, Tan, clear and Modulus.
Write a method for each of these calculations that will accept the needed incoming numbers and return the calculated value.
Please note the call to these methods will occur when = is pressed.
You will only need 1 textbox to input numbers and display the results.
Remember to push the push pin so that the toolbox is always showing so you can easily access the different tools. Also, select the A-Z so that the tools are displayed alphabetically.
This assignment requires 11 buttons, and one textbox to input and display the results.
Button1 is for add
Button2 is for subtract
Button3 is for multiply
Button4 is for divide
Button5 is for +/-
Button6 is for Sin
Button7 is for Cos
Button8 is for Tan
Button9 is for Clear
Button10 is for Modulus
Button11 is for =
The first step is to open up the Windows Form. We can change the name of the form by modifying the text property on the form to be Calculator. Notice how the change is reflected automatically on the form.
Lets start by trying to get a simple addition to work on the calculator and then we could add the rest. We need one textbox and two buttons (one for addition and one equal).
Well rename the textbox, DisplayTextBox and the button, AddButton as well as the other, EqualButton. The + sign will appear as text on the AddButton. The = sign will appear on the EqualButton.
Your form should now look like this.
Double click on the AddButton and the code page should appear. You can add code to the Addbutton_Click procedure which occurs when the user clicks on the Add Button. At this time you can take the text in the DisplayTextBox as the first number.
Lets define FirstNumber outside of the procedures so that all the procedures can access it. Since we know that Sin and Cosine and Tangent all require Double arguments, we will define these as doubles. The SecondNumber variable does not need to be defined as an instance variable since its only used in the EqualButton_Click method and can be defined within that only.
Double FirstNumber;
Now we know that the operator is not always going to be a + but can be many operators as listed. This is the reason we will create another variable called mathOperator which can be accessed outside the procedures as well and initialized as . We will use the switch operation to check which math operator was selected and do the correct operation.
Double FirstNumber;
string mathOperator = "";
private void Addbutton_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
mathOperator = "+";
DisplayTextBox.Text = "";
DisplayTextBox.Focus(); // notice how I add the focus() procedure
}
private void EqualButton_Click(object sender, EventArgs e)
{
Double SecondNumber;
SecondNumber = Convert.ToDouble(DisplayTextBox.Text);
switch (mathOperator)
{
case "+":
DisplayTextBox.Text = (FirstNumber + SecondNumber).ToString();
break;
default:
break;
}
}
Now we can move around the buttons to look more like a real calculator.
Remember for the +/- and the sin, cos, and tan functions don't require a second operand, right? So you would do something like this:
private void ToggleButton_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
FirstNumber *= -1;
DisplayTextBox.Text = FirstNumber.ToString();
}
You would NOT need the Equal Button click method to be called at all, right? This would be the same for sin, cos and tan. Remember to use the Math.Sin(), Math.Cos() and Math.Tan() predefined math functions for these.
You can all continue writing the rest of the assignment as youd like.
Remember to include error checking as well.
P.s: I roughly think i know how to do for +, -, /,* but i do not know how to do the cos, tan, sin and plus minuse (+/-). it would be great if you could show me on that. thanks! (:
Calculator Microsoft Visual Studio File Edit View Project Build Debug Data Format Tools Test Window Help Debug Any CPU Solution Explorer Solution X Formi.cs [Designl Start Page Solution 'Calculator project) ual Calculator Calculator Properties References Calculator,cs E Form1.cs da Solution Explorer Class View Output Show output from: Refactor Looking for symbol references in project 'C: Users\FloralDesktop visual Studio Calculator Calculator obj Debug Refactorica Rename 'program to 'Calculator' C: Users Floral Desktop\visual Studio Calculator Calculator Calculator.ca (8, 18 updated definition Looking for symbol references in project 'C:NUsers\Flora DesktopvisualStudio Calculator Calculator obj Debug Refactor Ca Code Definition Window 23Call Browser E Output Ready oolbox TO RadioButton RichTextBox SavefileDialog SerialPort Service Controller SplitContainer l Splitter L Status Strip TabControl TableLayoutPanel abl TextBox ToolStrip Tool StripContainer ToolTip TrackBa Treeview VScrollB Server Explorer Toolbox Properties Form System.Windows.Forms.Form E Minimumsize 0,0 Opacity 100% E Padding 0, 0, 0,0 RightToLeft No RightToLeftLayou False Show con True Showin Taskbar True E Size 300, 300 SizeGrip Style Auto StartPosition Windows DefaultLoca ag Text Calculator TopMost False ansparency Key Text The text associated with the controlStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started