By using a C# language create a \'simple\' calculator. The calculator should be
ID: 3664648 • Letter: B
Question
By using a C# language create a 'simple' calculator. The calculator should be able to add, subtract, multiply, and divide two numbers.
You can use whatever format you want to receive the numbers (such as textboxes). You also can use whatever method (such as a button for each operation +, -, *, /) to determine what operation the user wants to accomplish.
The output should be displayed in a similar format to "1 + 2 = 3" or "1 - 2 = -1".
Make sure to use meaningful variable names. Make sure to give your application a meaningful name like"myCalculator". Make sure to change the form name to something meaningful. Include comments in your program that has your name and the assignment number.
For an extra challenge (not required and does not get you more points) try and accept a string from the user (such as "1 + 2") then when the user hits a button, it displays the result.
Explanation / Answer
Program of simple calculator in c#
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
float number1, answer;
int count;
private void buttonC_Clicked(object sender, EventArgs e) //action perform on clearing the textBox1
{
textBox1.Clear();
count = 0;
}
private void buttonminus_Clicked(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
number1 = float.Parse(textBox1.Text);
textBox1.Clear();
textBox1.Focus();
count = 1;
}
}
private void buttonone_Clicked(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 1;
}
private void buttontwo_Clicked(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 2;
}
private void buttonthree_Clicked(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 3;
}
private void buttonplus_Clicked(object sender, EventArgs e)
{
number1 = float.Parse(textBox1.Text);
textBox1.Clear();
textBox1.Focus();
count = 2;
}
private void buttonfour_Clicked(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 4;
}
private void buttonfive_Clicked(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 5;
}
private void buttonsix_Clicked(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 6;
}
private void buttonmultiply_Clicked(object sender, EventArgs e)
{
number1 = float.Parse(textBox1.Text);
textBox1.Clear();
textBox1.Focus();
count = 3;
}
private void buttonseven_Clicked(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 7;
}
private void buttoneight_Clicked(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 8;
}
private void buttonnine_Clicked(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 9;
}
private void buttondivide_Clicked(object sender, EventArgs e)
{
number1 = float.Parse(textBox1.Text);
textBox1.Clear();
textBox1.Focus();
count = 4;
}
private void buttonzero_Clicked(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 0;
}
private void buttonequal_Clicked(object sender, EventArgs e)
{
compute(count);
}
public void compute(int count)
{
switch (count)
{
case 1:
answer= number1 - float.Parse(textBox1.Text);
string number2 = textBox1.Text
textBox1.Text = number1.ToString() + "-" + number2 + "= " + answer.ToString();
break;
case 2:
answer= number1 + float.Parse(textBox1.Text);
string number2 = textBox1.Text
textBox1.Text = number1.ToString() + "+" + number2 + "= " + answer.ToString();
break;
case 3:
answer= number1 * float.Parse(textBox1.Text);
string number2 = textBox1.Text
textBox1.Text = number1.ToString() + "*" + number2 + "= " + answer.ToString();
break;
case 4:
answer= number1 / float.Parse(textBox1.Text);
string number2 = textBox1.Text
textBox1.Text = number1.ToString() + "/" + number2 + "= " + answer.ToString();
break;
default:
break;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.