0% found this document useful (0 votes)
36 views

Calculator

The document describes the code for a basic calculator application. It includes methods for handling number and operator button clicks, performing calculations, and displaying results. The methods parse user input, perform mathematical operations like addition and logarithms, and display output.

Uploaded by

ayesha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Calculator

The document describes the code for a basic calculator application. It includes methods for handling number and operator button clicks, performing calculations, and displaying results. The methods parse user input, perform mathematical operations like addition and logarithms, and display output.

Uploaded by

ayesha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

using System;

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 standard_one
{
public partial class Form1 : Form
{
double enterFirstValue, enterSecondValue;
String op;

private void EnterNumbers(object sender, EventArgs e)


{
Button num = (Button)sender;
if (txtresult.Text == " 0")
txtresult.Text = " ";
{
if (num.Text == " .")
{
if (!txtresult.Text.Contains("."))

txtresult.Text = txtresult.Text + num.Text;


}
else
{
txtresult.Text = txtresult.Text + num.Text;
}
}
}

private void NumberOperators(object sender, EventArgs e)


{
Button num = (Button)sender;

// Append the clicked number or operator to the text box


txtresult.Text += num.Text.Trim(); // Trim removes any leading or
trailing spaces
}

private void Button_equal_Click(object sender, EventArgs e)


{
// Split the text in the textbox based on the operator
string[] values = txtresult.Text.Split(new[] { "+", "-", "*", "/" },
StringSplitOptions.RemoveEmptyEntries);
if (values.Length != 2)
{
MessageBox.Show("Invalid input!");
return;
}

double firstValue, secondValue;


if (!double.TryParse(values[0], out firstValue)
|| !double.TryParse(values[1], out secondValue))
{
MessageBox.Show("Invalid input!");
return;
}

switch (txtresult.Text.FirstOrDefault(c => c == '+' || c == '-' || c == '*' || c


== '/'))
{
case '+':
txtresult.Text = (firstValue + secondValue).ToString();
break;
case '-':
txtresult.Text = (firstValue - secondValue).ToString();
break;
case '*':
txtresult.Text = (firstValue * secondValue).ToString();
break;
case '/':
if (secondValue == 0)
{
MessageBox.Show("Division by zero!");
return;
}
txtresult.Text = (firstValue / secondValue).ToString();
break;
default:
MessageBox.Show("Invalid operator!");
break;
}
}

private void button_delete_one_letter_Click(object sender, EventArgs e)


{
txtresult.Text = " 0 ";
}

private void button_delete_all_letter_Click(object sender, EventArgs e)


{
txtresult.Text = " 0 ";

string f, s;
f = Convert.ToString(enterFirstValue);
s = Convert.ToString(enterSecondValue);

f = " ";
s = " ";
}

private void button_add_subtract_Click(object sender, EventArgs e)


{
double q = Convert.ToDouble(txtresult.Text);
txtresult.Text = Convert.ToString(-1 * q);
}

private void button_backspace_Click(object sender, EventArgs e)


{
if (txtresult.Text.Length > 0)
{
txtresult.Text = txtresult.Text.Remove(txtresult.Text.Length - 1, 1);
}
}

private void fileToolStripMenuItem_Click(object sender, EventArgs e)


{

private void Form1_Click(object sender, EventArgs e)


{
this.Width = 999; //816
txtresult.Width = 318;
}

private void standardToolStripMenuItem_Click(object sender, EventArgs


e)
{
this.Width = 419; //816
txtresult.Width = 318;

private void scientificToolStripMenuItem_Click(object sender, EventArgs


e)
{
this.Width = 999; //816
txtresult.Width = 318;
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)


{
DialogResult exitCal;
exitCal = MessageBox.Show("Confirm if you want to exit", "Scientific
Calculator",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (exitCal == DialogResult.Yes)
{
Application.Exit();
}

private void button_pi_Click(object sender, EventArgs e)


{
txtresult.Text = " 3.141592653589976323";
}

private void button_log_Click(object sender, EventArgs e)


{
double Log = Convert.ToDouble(txtresult.Text);
Log = Math.Log10(Log);
txtresult.Text = Convert.ToString(Log);
}

private void button_Sqrt_Click(object sender, EventArgs e)


{
double sq = Convert.ToDouble(txtresult.Text);
sq = Math.Sqrt(sq);
txtresult.Text = Convert.ToString(sq);

private void button_x_power_two_Click(object sender, EventArgs e)


{
double x;
if (double.TryParse(txtresult.Text, out x))
{
x = x * x;
txtresult.Text = x.ToString();
}
else
{
MessageBox.Show("Invalid input. Please enter a valid number.");
}
}

private void button_x_power_three_Click(object sender, EventArgs e)


{
double x;
if (double.TryParse(txtresult.Text, out x))
{
x = Math.Pow(x, 3);
txtresult.Text = x.ToString();
}
else
{
MessageBox.Show("Invalid input. Please enter a valid number.");
}
}

private void button_LOG_Of_x_Click(object sender, EventArgs e)


{
double x;
if (double.TryParse(txtresult.Text, out x) && x > 0)
{
double result = Math.Log(x);
txtresult.Text = result.ToString();
}
else
{
MessageBox.Show("Invalid input. Please enter a valid positive
number.");
}
}

private void button_sin_of_h_Click(object sender, EventArgs e)


{
double sh = Convert.ToDouble(txtresult.Text);
sh = Math.Sinh(sh);
txtresult.Text = Convert.ToString(sh);
}

private void button_sin_Click(object sender, EventArgs e)


{
double sin = Convert.ToDouble(txtresult.Text);
sin = Math.Sin(sin);
txtresult.Text = Convert.ToString(sin);
}

private void button_cos_Click(object sender, EventArgs e)


{
double cos = Convert.ToDouble(txtresult.Text);
cos = Math.Cos(cos);
txtresult.Text = Convert.ToString(cos);
}

private void button_tanh_Click(object sender, EventArgs e)


{
double th = Convert.ToDouble(txtresult.Text);
th = Math.Tanh(th);
txtresult.Text = Convert.ToString(th);
}
private void button_tan_Click(object sender, EventArgs e)
{
double tan = Convert.ToDouble(txtresult.Text);
tan = Math.Tan(tan);
txtresult.Text = Convert.ToString(tan);
}

private void button_Dec_Click(object sender, EventArgs e)


{
// Check if the text in the textbox can be parsed as a double
if (double.TryParse(txtresult.Text, out double result))
{
// Format the result with 2 decimal places
txtresult.Text = result.ToString("0.00");
}
else
{
// If the input is not a valid number, display an error message
MessageBox.Show("Invalid input. Please enter a valid number.");
}
}

private void button_cos_of_h__Click(object sender, EventArgs e)


{
double ch = Convert.ToDouble(txtresult.Text);
ch = Math.Cosh(ch);
txtresult.Text = Convert.ToString(ch);
}

private void button_modulus_Click(object sender, EventArgs e)


{
// Check if the text in the textbox can be parsed as a double
if (double.TryParse(txtresult.Text, out double number))
{
// Perform modulus operation by 2
double modulus = number % 2;
txtresult.Text = modulus.ToString();
}
else
{
// If the input is not a valid number, display an error message
MessageBox.Show("Invalid input. Please enter a valid number.");
}
}

private void button_one_by_x_Click(object sender, EventArgs e)


{
// Check if the text in the textbox can be parsed as a double
if (double.TryParse(txtresult.Text, out double number) && number != 0)
{
// Calculate reciprocal (1/x)
double reciprocal = 1 / number;
txtresult.Text = reciprocal.ToString();
}
else if (number == 0)
{
// Handling division by zero case
MessageBox.Show("Cannot divide by zero.");
}
else
{
// If the input is not a valid number, display an error message
MessageBox.Show("Invalid input. Please enter a valid number.");
}
}

public Form1()
{
InitializeComponent();
}
}
}

You might also like