Professional Documents
Culture Documents
Calculator Assignment
Calculator Assignment
Calculator Assignment
Assignment
2024
Assignment supervisor:
Dr. Noha
Preformed By :
Abanoub Nashaat Sobhy
Class 2 (12200022)
3/3/24, 11:25 PM Form1.cs
Form1.cs
1 /*
2 Author: Abanoub Nashaat
3 Project Name: Calculator
4 Class : 2
5 Supervision : Dr. Noha
6 */
7 using System;
8 using System.Collections.Generic;
9 using System.ComponentModel;
10 using System.Data;
11 using System.Drawing;
12 using System.Linq;
13 using System.Text;
14 using System.Threading.Tasks;
15 using System.Windows.Forms;
16
17 namespace Calculator
18 {
19 public partial class Form1 : Form
20 {
21 //Fields
22 Double result = 0;
23 string operation = string.Empty;
24 string fstNum, secNum;
25 bool enterValue = false;
26
27 public Form1()
28 {
29 InitializeComponent();
30 }
31
32 private void BtnC_Click(object sender, EventArgs e)
33 {
34 TxtDisplay1.Text = "0";
35 TxtDisplay2.Text= string.Empty;
36 result = 0;
37 }
38
39 private void BtnMathOperation_Click(object sender, EventArgs e)
40 {
41 if (result != 0) BtnEql.PerformClick();
42 else result = Double.Parse(TxtDisplay1.Text);
43
44 Button button = (Button)sender;
45 operation = button.Text;
46 enterValue = true;
47 if (TxtDisplay1.Text != "0")
48 {
49 TxtDisplay2.Text = fstNum = $"{result}{operation}";
50 TxtDisplay1.Text = string.Empty;
51 }
52 }
53
54 private void BtnEql_Click(object sender, EventArgs e)
localhost:51207/4e1c3aab-8b1e-493e-9215-e7adab57cd76/ 1/3
3/3/24, 11:25 PM Form1.cs
55 {
56 secNum = TxtDisplay1.Text;
57 TxtDisplay2.Text = $"{TxtDisplay2.Text} {TxtDisplay1.Text}=";
58 if (TxtDisplay1.Text != string.Empty)
59 {
60 if (TxtDisplay1.Text == "0") TxtDisplay2.Text = string.Empty;
61 switch (operation)
62 {
63 case "+":
64 TxtDisplay1.Text = (result + Double.Parse(TxtDisplay1.Text))
.ToString();
65 break;
66 case "−":
67 TxtDisplay1.Text = (result - Double.Parse(TxtDisplay1.Text))
.ToString();
68 break;
69 case "×":
70 TxtDisplay1.Text = (result * Double.Parse(TxtDisplay1.Text))
.ToString();
71 break;
72 case "÷":
73 TxtDisplay1.Text = (result / Double.Parse(TxtDisplay1.Text))
.ToString();
74 break;
75 default: TxtDisplay2.Text = $"{TxtDisplay1.Text}=";
76 break;
77 }
78 result = Double.Parse(TxtDisplay1.Text);
79 operation = string.Empty;
80
81 }
82 }
83
84 private void BtnBackSpace_Click(object sender, EventArgs e)
85 {
86 if (TxtDisplay1.Text.Length > 0)
87 TxtDisplay1.Text = TxtDisplay1.Text.Remove(TxtDisplay1.TextLength - 1,
1);
88 if (TxtDisplay1.Text == string.Empty) TxtDisplay1.Text = "0";
89 }
90
91 private void BtnCE_Click(object sender, EventArgs e)
92 {
93
94 }
95
96 private void BtnOperation_Click(object sender, EventArgs e)
97 {
98 Button button = (Button)sender;
99 operation = button.Text;
100 switch (operation)
101 {
102 case "√x":
103 TxtDisplay2.Text = $"√({TxtDisplay1.Text})";
104 TxtDisplay1.Text =
Convert.ToString(Math.Sqrt(Double.Parse(TxtDisplay1.Text)));
105 break;
106 case "^2":
localhost:51207/4e1c3aab-8b1e-493e-9215-e7adab57cd76/ 2/3
3/3/24, 11:25 PM Form1.cs
localhost:51207/4e1c3aab-8b1e-493e-9215-e7adab57cd76/ 3/3
Results Example