Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponetModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Calc {
- //is a basic calculator
- public partial class Form1 : Form {
- public Form1() {
- InitializeComponent();
- }
- private string AddValues(double lhs,double rhs){
- double result = lhs + rhs;
- txtExpr.Text = $"{lhs} + {rhs}";
- return result.ToString();
- }
- private String SubtractValues(double lhs,double rhs) {
- double result = lhs - rhs;
- txtExpr.Text = $"{lhs} - {rhs}"
- return result.ToString();
- }
- private string DivideValues(double lhs,double rhs) {
- double result = lhs / rhs;
- txtExpr.Text = $"{lhs} / {rhs}";
- return result.ToString();
- /*
- Getting unused local varible when I try
- double result;
- try{
- result = lhs/ rhs;
- } catch (DivideByZeroException ex){
- //insert a messagebox here
- }
- //set Expression TextBox
- return result.ToString() // Gives unused Local varible warning
- */
- }
- private string SquareRootValues(double lhs){
- double result = Math.Sqrt(lhs);
- txtExpr.Text = $"The Square root of {lhs}";
- return result.ToString();
- }
- private string RaisePowerValues(double lhs,double rhs) {
- double result = Math.Pow(lhs,rhs);
- txtExpr.Text =$"{lhs} raised to the power of {rhs}";
- return result.ToString();
- }
- private void DoCalculate(double lhs, double rhs) {
- if (radAdd.Checked) {
- txtResult.Text = AddValues(double.Parse(txtLeft.Text), double.Parse(txtRight.Text));
- }else if (radDiv.Checked) {
- try {
- txtResult.Text = DivideValues(double.Parse(txtLeft.Text), double.Parse(txtRight.Text));
- }catch (DivideByZeroException ex) {
- MessageBox.Show("Cannot Divide by Zero", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- txtResult.Text = String.Empty;
- txtExpr.Text = String.Empty;
- }
- }else if (radMult.Checked) {
- txtResult.Text = MultiplyValues(double.Parse(txtLeft.Text), double.Parse(txtRight.Text));
- }else if (radSub.Checked) {
- txtResult.Text = SubtractValues(double.Parse(txtLeft.Text), double.Parse(txtRight.Text));
- }else if (radSqrt.Checked) {
- txtRight.Enabled = false;
- txtResult.Text = SquareRootValues(double.Parse(txtLeft.Text));
- }else if (radPow.Checked) {
- txtResult.Text = RaisePowerValues(double.Parse(txtLeft.Text), double.Parse(txtRight.Text));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement