C# Program . Create a Windows application that has the functionality of a calcul
ID: 3890704 • Letter: C
Question
C# Program. Create a Windows application that has the functionality of a calculator and works with integral values. Allow the user to select buttons representing numeric values. If the user attempts to divide by zero, throw and handle an exception.
I have developed the code I am receiving the following error CS 1513 } expected
The Code:
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 Calculator2
{
public partial class Form1 : Form
{
Double resultValue = 0;
String operationPerformed = ""; //perform addition,substract, division or muliplication
bool isOperationPerformed = false; //has add, substract,division or muliplication been performed
public Form1()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
if ((txtResult.Text == "0") || (isOperationPerformed))
txtResult.Clear();
isOperationPerformed = false;
Button button = (Button)sender; // tie number buttons to one click (method) event
if (button.Text == ".")
{
if (!txtResult.Text.Contains("."))
txtResult.Text = txtResult.Text + button.Text;
} else
txtResult.Text = txtResult.Text + button.Text;
}
private void operator_click(object sender, EventArgs e)
{
Button button = (Button)sender;//tie operator (+,-,*,/) buttons to one click (method)
if (resultValue != 0)
{
btnEqual.PerformClick();
operationPerformed = button.Text;
lblOperation.Text = resultValue + "" + operationPerformed;
isOperationPerformed = true;
}
else
{
operationPerformed = button.Text;
resultValue = Double.Parse(txtResult.Text);
lblOperation.Text = resultValue + "" + operationPerformed;
isOperationPerformed = true;
}
}
private void btnClearEntry_Click(object sender, EventArgs e)
{
txtResult.Text = "0"; //clear last entry
}
private void btnClear_Click(object sender, EventArgs e)
{
txtResult.Text = "0";//clear last entry
resultValue = 0;//and reset text box reuslt to zero
}
//Perform math operations
private void btnEqual_Click(object sender, EventArgs e)
{
try
{
switch (operationPerformed)
{
case "+":
txtResult.Text = (resultValue + Double.Parse(txtResult.Text)).ToString();
break;
case "-":
txtResult.Text = (resultValue - Double.Parse(txtResult.Text)).ToString();
break;
case "*":
txtResult.Text = (resultValue * Double.Parse(txtResult.Text)).ToString();
break;
case "/":
txtResult.Text = (resultValue / Double.Parse(txtResult.Text)).ToString();
break;
default:
break;
} // user define excpetion
throw new DivideException("Divide by zero" + " is not possible");
// Catch the exception when a usr try to divide a number by zero
catch (DivideException ex)
{
MessageBox.Show(ex.Message);
resultValue = Int32.Parse(txtResult.Text);
operationPerformed = "";
lblOperation.Text = "";
}//end of switch
}// end of btnEqual_Click
}//end of class
}//End of Calculator2
Form Designer
namespace Calculator2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtResult = new System.Windows.Forms.TextBox();
this.btnSeven = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.btnClearEntry = new System.Windows.Forms.Button();
this.btnDivide = new System.Windows.Forms.Button();
this.btnNine = new System.Windows.Forms.Button();
this.btnEight = new System.Windows.Forms.Button();
this.btnTimes = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.btn6 = new System.Windows.Forms.Button();
this.btnTwo = new System.Windows.Forms.Button();
this.btnThree = new System.Windows.Forms.Button();
this.btnOne = new System.Windows.Forms.Button();
this.btnZero = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.btnDecimal = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.btnSubstract = new System.Windows.Forms.Button();
this.btnFour = new System.Windows.Forms.Button();
this.btnFive = new System.Windows.Forms.Button();
this.lblOperation = new System.Windows.Forms.Label();
this.btnEqual = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtResult
//
this.txtResult.BackColor = System.Drawing.SystemColors.ButtonHighlight;
this.txtResult.Location = new System.Drawing.Point(24, 40);
this.txtResult.Name = "txtResult";
this.txtResult.ReadOnly = true;
this.txtResult.Size = new System.Drawing.Size(326, 22);
this.txtResult.TabIndex = 0;
this.txtResult.Text = "0";
this.txtResult.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// btnSeven
//
this.btnSeven.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnSeven.Location = new System.Drawing.Point(24, 113);
this.btnSeven.Name = "btnSeven";
this.btnSeven.Size = new System.Drawing.Size(54, 23);
this.btnSeven.TabIndex = 1;
this.btnSeven.Text = "7";
this.btnSeven.UseVisualStyleBackColor = true;
this.btnSeven.Click += new System.EventHandler(this.button_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(134, 209);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(8, 8);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
//
// btnClearEntry
//
this.btnClearEntry.Location = new System.Drawing.Point(313, 113);
this.btnClearEntry.Name = "btnClearEntry";
this.btnClearEntry.Size = new System.Drawing.Size(54, 23);
this.btnClearEntry.TabIndex = 3;
this.btnClearEntry.Text = "CE";
this.btnClearEntry.UseVisualStyleBackColor = true;
this.btnClearEntry.Click += new System.EventHandler(this.btnClearEntry_Click);
//
// btnDivide
//
this.btnDivide.Location = new System.Drawing.Point(236, 114);
this.btnDivide.Name = "btnDivide";
this.btnDivide.Size = new System.Drawing.Size(54, 23);
this.btnDivide.TabIndex = 4;
this.btnDivide.Text = "/";
this.btnDivide.UseVisualStyleBackColor = true;
this.btnDivide.Click += new System.EventHandler(this.operator_click);
//
// btnNine
//
this.btnNine.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnNine.Location = new System.Drawing.Point(165, 113);
this.btnNine.Name = "btnNine";
this.btnNine.Size = new System.Drawing.Size(54, 23);
this.btnNine.TabIndex = 5;
this.btnNine.Text = "9";
this.btnNine.UseVisualStyleBackColor = true;
this.btnNine.Click += new System.EventHandler(this.button_Click);
//
// btnEight
//
this.btnEight.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnEight.Location = new System.Drawing.Point(88, 113);
this.btnEight.Name = "btnEight";
this.btnEight.Size = new System.Drawing.Size(54, 23);
this.btnEight.TabIndex = 6;
this.btnEight.Text = "8";
this.btnEight.UseVisualStyleBackColor = true;
this.btnEight.Click += new System.EventHandler(this.button_Click);
//
// btnTimes
//
this.btnTimes.Location = new System.Drawing.Point(236, 159);
this.btnTimes.Name = "btnTimes";
this.btnTimes.Size = new System.Drawing.Size(54, 23);
this.btnTimes.TabIndex = 9;
this.btnTimes.Text = "*";
this.btnTimes.UseVisualStyleBackColor = true;
this.btnTimes.Click += new System.EventHandler(this.operator_click);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(313, 159);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(54, 23);
this.btnClear.TabIndex = 8;
this.btnClear.Text = "C";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btn6
//
this.btn6.Location = new System.Drawing.Point(165, 159);
this.btn6.Name = "btn6";
this.btn6.Size = new System.Drawing.Size(54, 23);
this.btn6.TabIndex = 7;
this.btn6.Text = "6";
this.btn6.UseVisualStyleBackColor = true;
this.btn6.Click += new System.EventHandler(this.button_Click);
//
// btnTwo
//
this.btnTwo.Location = new System.Drawing.Point(88, 202);
this.btnTwo.Name = "btnTwo";
this.btnTwo.Size = new System.Drawing.Size(54, 23);
this.btnTwo.TabIndex = 12;
this.btnTwo.Text = "2";
this.btnTwo.UseVisualStyleBackColor = true;
this.btnTwo.Click += new System.EventHandler(this.button_Click);
//
// btnThree
//
this.btnThree.Location = new System.Drawing.Point(165, 202);
this.btnThree.Name = "btnThree";
this.btnThree.Size = new System.Drawing.Size(54, 23);
this.btnThree.TabIndex = 11;
this.btnThree.Text = "3";
this.btnThree.UseVisualStyleBackColor = true;
this.btnThree.Click += new System.EventHandler(this.button_Click);
//
// btnOne
//
this.btnOne.Location = new System.Drawing.Point(24, 202);
this.btnOne.Name = "btnOne";
this.btnOne.Size = new System.Drawing.Size(54, 23);
this.btnOne.TabIndex = 10;
this.btnOne.Text = "1";
this.btnOne.UseVisualStyleBackColor = true;
this.btnOne.Click += new System.EventHandler(this.button_Click);
//
// btnZero
//
this.btnZero.Location = new System.Drawing.Point(88, 251);
this.btnZero.Name = "btnZero";
this.btnZero.Size = new System.Drawing.Size(54, 23);
this.btnZero.TabIndex = 15;
this.btnZero.Text = "0";
this.btnZero.UseVisualStyleBackColor = true;
this.btnZero.Click += new System.EventHandler(this.button_Click);
//
// button14
//
this.button14.Location = new System.Drawing.Point(165, 251);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(54, 23);
this.button14.TabIndex = 14;
this.button14.Text = "button14";
this.button14.UseVisualStyleBackColor = true;
//
// btnDecimal
//
this.btnDecimal.Location = new System.Drawing.Point(24, 251);
this.btnDecimal.Name = "btnDecimal";
this.btnDecimal.Size = new System.Drawing.Size(54, 23);
this.btnDecimal.TabIndex = 13;
this.btnDecimal.Text = ".";
this.btnDecimal.UseVisualStyleBackColor = true;
this.btnDecimal.Click += new System.EventHandler(this.button_Click);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(236, 251);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(54, 23);
this.btnAdd.TabIndex = 20;
this.btnAdd.Text = "+";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.operator_click);
//
// btnSubstract
//
this.btnSubstract.Location = new System.Drawing.Point(236, 209);
this.btnSubstract.Name = "btnSubstract";
this.btnSubstract.Size = new System.Drawing.Size(54, 23);
this.btnSubstract.TabIndex = 21;
this.btnSubstract.Text = "-";
this.btnSubstract.UseVisualStyleBackColor = true;
this.btnSubstract.Click += new System.EventHandler(this.operator_click);
//
// btnFour
//
this.btnFour.Location = new System.Drawing.Point(24, 159);
this.btnFour.Name = "btnFour";
this.btnFour.Size = new System.Drawing.Size(54, 23);
this.btnFour.TabIndex = 22;
this.btnFour.Text = "4";
this.btnFour.UseVisualStyleBackColor = true;
this.btnFour.Click += new System.EventHandler(this.button_Click);
//
// btnFive
//
this.btnFive.Location = new System.Drawing.Point(88, 159);
this.btnFive.Name = "btnFive";
this.btnFive.Size = new System.Drawing.Size(54, 23);
this.btnFive.TabIndex = 23;
this.btnFive.Text = "5";
this.btnFive.UseVisualStyleBackColor = true;
this.btnFive.Click += new System.EventHandler(this.button_Click);
//
// lblOperation
//
this.lblOperation.AutoSize = true;
this.lblOperation.Location = new System.Drawing.Point(33, 46);
this.lblOperation.Name = "lblOperation";
this.lblOperation.Size = new System.Drawing.Size(0, 16);
this.lblOperation.TabIndex = 24;
//
// btnEqual
//
this.btnEqual.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnEqual.Location = new System.Drawing.Point(313, 209);
this.btnEqual.Name = "btnEqual";
this.btnEqual.Size = new System.Drawing.Size(54, 65);
this.btnEqual.TabIndex = 19;
this.btnEqual.Text = "=";
this.btnEqual.UseVisualStyleBackColor = true;
this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(379, 322);
this.Controls.Add(this.lblOperation);
this.Controls.Add(this.btnFive);
this.Controls.Add(this.btnFour);
this.Controls.Add(this.btnSubstract);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnEqual);
this.Controls.Add(this.btnZero);
this.Controls.Add(this.button14);
this.Controls.Add(this.btnDecimal);
this.Controls.Add(this.btnTwo);
this.Controls.Add(this.btnThree);
this.Controls.Add(this.btnOne);
this.Controls.Add(this.btnTimes);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btn6);
this.Controls.Add(this.btnEight);
this.Controls.Add(this.btnNine);
this.Controls.Add(this.btnDivide);
this.Controls.Add(this.btnClearEntry);
this.Controls.Add(this.button2);
this.Controls.Add(this.btnSeven);
this.Controls.Add(this.txtResult);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Margin = new System.Windows.Forms.Padding(4);
this.MaximumSize = new System.Drawing.Size(395, 360);
this.MinimumSize = new System.Drawing.Size(395, 360);
this.Name = "Form1";
this.ShowIcon = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Calculator";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.Button btnSeven;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button btnClearEntry;
private System.Windows.Forms.Button btnDivide;
private System.Windows.Forms.Button btnNine;
private System.Windows.Forms.Button btnEight;
private System.Windows.Forms.Button btnTimes;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btn6;
private System.Windows.Forms.Button btnTwo;
private System.Windows.Forms.Button btnThree;
private System.Windows.Forms.Button btnOne;
private System.Windows.Forms.Button btnZero;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button btnDecimal;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnSubstract;
private System.Windows.Forms.Button btnFour;
private System.Windows.Forms.Button btnFive;
private System.Windows.Forms.Label lblOperation;
private System.Windows.Forms.Button btnEqual;
}
}
Exception Class
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Calculator2
{
class DivideException : Exception
{
public DivideException(): base() { }
public DivideException(string message) : base(message)
{
}
}
}
Calculator
Explanation / Answer
Form1.Cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator2
{
public partial class Form1: Form
{
string operation = string.Empty;
bool DivideByZero = false;
bool ModuludOfZero = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn0_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + "0";
}
private void btn1_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + "1";
}
private void btn2_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + "2";
}
private void btn3_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + "3";
}
private void btn6_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + "6";
}
private void btn5_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + "5";
}
private void btn4_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + "4";
}
private void btn9_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + "9";
}
private void btn8_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + "8";
}
private void btn7_Click(object sender, EventArgs e)
{
txtResult.Text = txtResult.Text + "7";
}
private void btnModules_Click(object sender, EventArgs e)
{
string bkp = string.Empty;
if (txtResult.Text != "")
{
Calculations();
if (txtResult.Text[txtResult.Text.Length - 1] != '%')
{
if (txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("*") || txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("-") ||
txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("/") || txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("+"))
{
bkp = RemoveLastChar(txtResult.Text);
txtResult.Text = bkp + "%";
}
else
{
if (!DivideByZero && !ModuludOfZero)
txtResult.Text = txtResult.Text + "%";
//txtExpression.Text = txtExpression.Text + txtResult.Text;
}
}
}
operation = "modulus";
}
private void btnPoint_Click(object sender, EventArgs e)
{
int counter = 0;
if (txtResult.Text != "")
{
//Calculations();
if (txtResult.Text[txtResult.Text.Length - 1] != '.')
{
char[] signs = { '/', '*', '-', '+', '%' };
foreach (char c in signs)
{
if (txtResult.Text.Contains(c))
{
//int index = txtResult.Text.LastIndexOf(c);
string[] SignSplitted = txtResult.Text.Split(c);
//if (!txtResult.Text.Substring(index, txtResult.Text.Length - 1).Contains('.'))
//{
counter++;
if (!SignSplitted[1].Contains('.'))
txtResult.Text = txtResult.Text + ".";
//}
}
}
if (counter == 0)
{
if (!txtResult.Text.Contains('.'))
txtResult.Text = txtResult.Text + ".";
}
}
}
else
{
txtResult.Text = txtResult.Text + "0.";
}
}
private void btnPlus_Click(object sender, EventArgs e)
{
string bkp = string.Empty;
if (txtResult.Text != string.Empty)
{
if (txtResult.Text[txtResult.Text.Length - 1] != Convert.ToChar("+"))
{
Calculations();
if (txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("*") || txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("-") ||
txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("/") || txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("%"))
{
bkp = RemoveLastChar(txtResult.Text);
txtResult.Text = bkp + "+";
}
else
{
if (!DivideByZero && !ModuludOfZero)
txtResult.Text = txtResult.Text + "+";
}
}
}
operation = "plus";
}
private void btnMinus_Click(object sender, EventArgs e)
{
string bkp = string.Empty;
if (txtResult.Text != string.Empty)
{
// num1 = float.Parse(txtResult.Text);
if (txtResult.Text[txtResult.Text.Length - 1] != Convert.ToChar("-"))
{
Calculations();
if (txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("*") || txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("+") ||
txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("/") || txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("%"))
{
bkp = RemoveLastChar(txtResult.Text);
txtResult.Text = bkp + "-";
}
else
{
if (!DivideByZero && !ModuludOfZero)
txtResult.Text = txtResult.Text + "-";
//txtExpression.Text = txtExpression.Text + txtResult.Text;
}
}
operation = "minus";
}
}
private void btnMultiply_Click(object sender, EventArgs e)
{
string bkp = string.Empty;
if (txtResult.Text != string.Empty)
{
Calculations();
if (txtResult.Text[txtResult.Text.Length - 1] != Convert.ToChar("*"))
{
if (txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("-") || txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("+") ||
txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("/") || txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("%"))
{
bkp = RemoveLastChar(txtResult.Text);
txtResult.Text = bkp + "*";
}
else
{
if (!DivideByZero && !ModuludOfZero)
txtResult.Text = txtResult.Text + "*";
//txtExpression.Text = txtExpression.Text + txtResult.Text;
}
}
}
operation = "multiply";
}
private void btnDivide_Click(object sender, EventArgs e)
{
string bkp = string.Empty;
if (txtResult.Text != string.Empty)
{
Calculations();
if (txtResult.Text[txtResult.Text.Length - 1] != Convert.ToChar("/"))
{
if (txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("*") || txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("+") ||
txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("/") || txtResult.Text[txtResult.Text.Length - 1] == Convert.ToChar("%"))
{
bkp = RemoveLastChar(txtResult.Text);
txtResult.Text = bkp + "/";
}
else
{
if (!DivideByZero && !ModuludOfZero)
txtResult.Text = txtResult.Text + "/";
//txtExpression.Text = txtExpression.Text + txtResult.Text;
}
}
}
operation = "divide";
}
private void btnEquals_Click(object sender, EventArgs e)
{
Calculations();
}
private void btnC_Click(object sender, EventArgs e)
{
txtResult.Text = string.Empty;
}
private void btnBkpSpace_Click(object sender, EventArgs e)
{
string bkp = RemoveLastChar(txtResult.Text);
txtResult.Text = bkp;
}
public string RemoveLastChar(string fulltext)
{
string bkp = string.Empty;
char[] text = fulltext.ToCharArray();
for (int i = 0; i < text.Length - 1; i++)
{
bkp += text[i];
}
return bkp;
}
public long CalculateIntegerResult(string operation, long num1, long num2)
{
long resut = 0;
try
{
if (txtResult.Text.Contains("+") || txtResult.Text.Contains("-") || txtResult.Text.Contains("*") || txtResult.Text.Contains("/") ||
txtResult.Text.Contains("%"))
{
switch (operation)
{
case "plus":
resut = num1 + num2;
break;
case "minus":
resut = num1 - num2;
break;
case "multiply":
resut = num1 * num2;
break;
case "divide":
resut = num1 / num2;
break;
case "modulus":
resut = num1 % num2;
break;
default:
break;
}
}
return resut;
}
catch
{
return resut;
}
}
public float CalculateFloatResult(string operation, float num1, float num2)
{
float resut = 0;
try
{
if (txtResult.Text.Contains("+") || txtResult.Text.Contains("-") || txtResult.Text.Contains("*") || txtResult.Text.Contains("/") ||
txtResult.Text.Contains("%"))
{
switch (operation)
{
case "plus":
resut = num1 + num2;
break;
case "minus":
resut = num1 - num2;
break;
case "multiply":
resut = num1 * num2;
break;
case "divide":
resut = num1 / num2;
break;
case "modulus":
if (num2 != 0.0)
resut = num1 % num2;
else
MessageBox.Show("Cannot find Modulus of Zero.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
default:
break;
}
}
return resut;
}
catch
{
return resut;
}
}
public void Calculations()
{
try
{
if (txtResult.Text.Contains('+'))
{
string[] splitted = txtResult.Text.Split('+');
if (splitted[1] != "")
{
if (splitted[0].Contains('.') || splitted[1].Contains('.'))
{
float floatResult = CalculateFloatResult("plus", float.Parse(splitted[0]), float.Parse(splitted[1]));
txtResult.Text = Convert.ToString(floatResult);
}
else
{
long rr = CalculateIntegerResult("plus", long.Parse(splitted[0]), long.Parse(splitted[1]));
txtResult.Text = Convert.ToString(rr);
}
}
}
if (txtResult.Text.Contains('-'))
{
string[] splitted = txtResult.Text.Split('-');
if (splitted[1] != "")
{
if (splitted[0].Contains('.') || splitted[1].Contains('.'))
{
float floatResult = CalculateFloatResult("minus", float.Parse(splitted[0]), float.Parse(splitted[1]));
txtResult.Text = Convert.ToString(floatResult);
}
else
{
long rr = CalculateIntegerResult("minus", long.Parse(splitted[0]), long.Parse(splitted[1]));
txtResult.Text = Convert.ToString(rr);
}
}
}
if (txtResult.Text.Contains('*'))
{
string[] splitted = txtResult.Text.Split('*');
if (splitted[1] != "")
{
if (splitted[0].Contains('.') || splitted[1].Contains('.'))
{
float floatResult = CalculateFloatResult("multiply", float.Parse(splitted[0]), float.Parse(splitted[1]));
txtResult.Text = Convert.ToString(floatResult);
}
else
{
long rr = CalculateIntegerResult("multiply", long.Parse(splitted[0]), long.Parse(splitted[1]));
txtResult.Text = Convert.ToString(rr);
}
}
}
if (txtResult.Text.Contains('/'))
{
string[] splitted = txtResult.Text.Split('/');
if (splitted[1] != "")
{
if (!splitted[1].Contains('.'))
{
if (Convert.ToInt32(splitted[1]) != 0)
{
if (splitted[0].Contains('.') || splitted[1].Contains('.'))
{
float floatResult = CalculateFloatResult("divide", float.Parse(splitted[0]), float.Parse(splitted[1]));
txtResult.Text = Convert.ToString(floatResult);
}
else
{
long rr = CalculateIntegerResult("divide", long.Parse(splitted[0]), long.Parse(splitted[1]));
txtResult.Text = Convert.ToString(rr);
}
}
else
{
MessageBox.Show("Cannot divide by Zero.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
DivideByZero = true;
}
}
else
{
if (float.Parse(splitted[1]) != 0)
{
if (splitted[0].Contains('.') || splitted[1].Contains('.'))
{
float floatResult = CalculateFloatResult("divide", float.Parse(splitted[0]), float.Parse(splitted[1]));
txtResult.Text = Convert.ToString(floatResult);
}
else
{
long rr = CalculateIntegerResult("divide", long.Parse(splitted[0]), long.Parse(splitted[1]));
txtResult.Text = Convert.ToString(rr);
}
}
else
{
MessageBox.Show("Cannot divide by Zero.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
DivideByZero = true;
}
}
}
}
if (txtResult.Text.Contains('%'))
{
string[] splitted = txtResult.Text.Split('%');
if (splitted[1] != "")
{
if (splitted[0].Contains('.') || splitted[1].Contains('.'))
{
float floatResult = CalculateFloatResult("modulus", float.Parse(splitted[0]), float.Parse(splitted[1]));
txtResult.Text = Convert.ToString(floatResult);
}
else
{
if (Convert.ToInt32(splitted[1]) == 0)
{
MessageBox.Show("Cannot find Modulus of Zero.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
ModuludOfZero = true;
}
else
{
long rr = CalculateIntegerResult("modulus", long.Parse(splitted[0]), long.Parse(splitted[1]));
txtResult.Text = Convert.ToString(rr);
}
}
}
}
}
catch
{
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.