Create a graphical calculator with buttons that can add, subtract, multiply, div
ID: 3855981 • Letter: C
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 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.
Let’s 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).
We’ll 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.
Let’s 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 it’s 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 you’d 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: UsersFloralDesktop visual Studio Calculator Calculator obj Debug Refactorica Rename 'program to 'Calculator' C: Users Floral Desktopisual Studio Calculator Calculator Calculator.ca (8, 18 updated definition Looking for symbol references in project 'C:NUsersFlora 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 control.Explanation / Answer
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ScienceCalculator
{
public partial class ScCalc : Form
{
int sign_Indicator = 0;
double variable1;
double variable2;
int additionPart = 0;
int subbtractionPart = 0;
int multiplicationPart = 0;
int divisionPart = 0;
int modBit = 0;
Boolean fl = false;
String s, x;
public ScCalc()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (sign_Indicator == 0)
{
txtInput.Text = txtInput.Text + Convert.ToString(1);
}
else if (sign_Indicator == 1)
{
txtInput.Text = Convert.ToString(1);
sign_Indicator = 0;
}
fl = true;
}
private void button2_Click(object sender, EventArgs e)
{
if (sign_Indicator == 0)
{
txtInput.Text = txtInput.Text + Convert.ToString(2);
}
else if (sign_Indicator == 1)
{
txtInput.Text = Convert.ToString(2);
sign_Indicator = 0;
}
fl=true;
}
private void button3_Click(object sender, EventArgs e)
{
if (sign_Indicator == 0)
{
txtInput.Text = txtInput.Text + Convert.ToString(3);
}
else if (sign_Indicator == 1)
{
txtInput.Text = Convert.ToString(3);
sign_Indicator = 0;
}
fl=true;
}
private void button13_Click(object sender, EventArgs e)
{
if (sign_Indicator == 0)
{
txtInput.Text = txtInput.Text + Convert.ToString(4);
}
else if (sign_Indicator == 1)
{
txtInput.Text = Convert.ToString(4);
sign_Indicator = 0;
}
fl=true;
}
private void button12_Click(object sender, EventArgs e)
{
if (sign_Indicator == 0)
{
txtInput.Text = txtInput.Text + Convert.ToString(5);
}
else if (sign_Indicator == 1)
{
txtInput.Text = Convert.ToString(5);
sign_Indicator = 0;
}
fl=true;
}
private void button11_Click(object sender, EventArgs e)
{
if (sign_Indicator == 0)
{
txtInput.Text = txtInput.Text + Convert.ToString(6);
}
else if (sign_Indicator == 1)
{
txtInput.Text = Convert.ToString(6);
sign_Indicator = 0;
}
fl=true;
}
private void button9_Click(object sender, EventArgs e)
{
if (sign_Indicator == 0)
{
txtInput.Text = txtInput.Text + Convert.ToString(7);
}
else if (sign_Indicator == 1)
{
txtInput.Text = Convert.ToString(7);
sign_Indicator = 0;
}
fl=true;
}
private void button8_Click(object sender, EventArgs e)
{
if (sign_Indicator == 0)
{
txtInput.Text = txtInput.Text + Convert.ToString(8);
}
else if (sign_Indicator == 1)
{
txtInput.Text = Convert.ToString(8);
sign_Indicator = 0;
}
fl=true;
}
private void button7_Click(object sender, EventArgs e)
{
if (sign_Indicator == 0)
{
txtInput.Text = txtInput.Text + Convert.ToString(9);
}
else if (sign_Indicator == 1)
{
txtInput.Text = Convert.ToString(9);
sign_Indicator = 0;
}
fl=true;
}
private void button5_Click(object sender, EventArgs e)
{
if (sign_Indicator == 0)
{
txtInput.Text = txtInput.Text + Convert.ToString(0);
}
else if (sign_Indicator == 1)
{
txtInput.Text = Convert.ToString(0);
sign_Indicator = 0;
}
fl=true;
}
private void reset_SignBits()
{
additionPart = 0;
subbtractionPart = 0;
multiplicationPart = 0;
divisionPart = 0;
modBit = 0;
fl = false;
}
private void button4_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
calculate();
reset_SignBits();
additionPart = 1;
sign_Indicator = 1;
}
}
private void button20_Click(object sender, EventArgs e)
{
txtInput.Clear();
sign_Indicator = 0;
variable1 = 0;
variable2 = 0;
reset_SignBits();
}
private void button10_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
variable2 = Convert.ToDouble(txtInput.Text);
calculate();
reset_SignBits();
subbtractionPart = 1;
sign_Indicator = 1;
}
}
private void button19_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
calculate();
reset_SignBits();
multiplicationPart = 1;
sign_Indicator = 1;
}
}
private void button6_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
calculate();
reset_SignBits();
divisionPart = 1;
sign_Indicator = 1;
}
}
private void button18_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
calculate();
reset_SignBits();
}
sign_Indicator = 1;
}
private void button21_Click(object sender, EventArgs e)
{
int i = 0;
char chr = '';
int decimal_Indicator = 0;
int l = txtInput.Text.Length-1;
if (sign_Indicator != 1)
{
for (i = 0; i <= l; i++)
{
chr = txtInput.Text[i];
if (chr == '.')
{
decimal_Indicator = 1;
}
}
if (decimal_Indicator != 1)
{
txtInput.Text = txtInput.Text + Convert.ToString(".");
}
}
}
private void button15_Click(object sender, EventArgs e)
{
double s,l;
l = Convert.ToDouble(txtInput.Text);
s = Math.Sqrt(l);
txtInput.Text = Convert.ToString(s);
}
private void button16_Click(object sender, EventArgs e)
{
txtInput.Text = (1 / Convert.ToDouble(txtInput.Text)).ToString();
}
private void button17_Click(object sender, EventArgs e)
{
try
{
long fact = 1;
for (int i = 1; i <= Convert.ToInt32(txtInput.Text); i++)
{
fact = fact * i;
}
txtInput.Text = Convert.ToString(fact);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button22_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Log(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button23_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Log10(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button14_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Exp(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form6_Load(object sender, EventArgs e)
{
}
private void btnSin_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Sin(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnCos_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Cos(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnTan_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Tan(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnRound_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Round(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnFloor_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Floor(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnTruncate_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Truncate(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnCeil_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Ceiling(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnInvSin_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Asin(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnSinh_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Sinh(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnCosh_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Cosh(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button24_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Tanh(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnInvCos_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Acos(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnInvTan_Click(object sender, EventArgs e)
{
try
{
if (txtInput.Text.Length != 0)
{
double l;
l = Math.Atan(Convert.ToDouble(txtInput.Text));
txtInput.Text = Convert.ToString(l);
}
sign_Indicator = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void calculate()
{
if (txtInput.Text != ".")
{
variable2 = Convert.ToDouble(txtInput.Text);
if (fl == false)
{
variable1 = variable2;
}
else if (additionPart == 1)
{
variable1 = variable1 + variable2;
}
else if (subbtractionPart == 1)
{
variable1 = variable1 - variable2;
}
else if (multiplicationPart == 1)
{
variable1 = variable1 * variable2;
}
else if (divisionPart == 1)
{
variable1 = variable1 / variable2;
}
else if (modBit == 1)
{
variable2 = Convert.ToInt32(txtInput.Text);
variable1 = Convert.ToInt32(variable1 % variable2);
}
else
{
variable1 = variable2;
}
txtInput.Text = Convert.ToString(variable1);
}
}
private void button25_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
calculate();
reset_SignBits();
modBit = 1;
sign_Indicator = 1;
}
}
private void button26_Click(object sender, EventArgs e)
{
txtInput.Text = Math.PI.ToString();
sign_Indicator = 1;
}
private void button27_Click(object sender, EventArgs e)
{
s = txtInput.Text;
int l = s.Length;
for (int i = 0; i < l - 1; i++)
{
x += s[i];
}
txtInput.Text = x;
x = "";
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.