Skip to content

Commit a122d84

Browse files
committed
Creating a calculator class, user interface, and writing tests for the calculator class
1 parent 8bf417e commit a122d84

9 files changed

Lines changed: 919 additions & 0 deletions

File tree

Calculator/Calculator.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32328.378
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calculator", "Calculator\Calculator.csproj", "{AA661CFD-17A7-407A-A279-8E003F5DB812}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestCalculator", "TestCalculator\TestCalculator.csproj", "{D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{AA661CFD-17A7-407A-A279-8E003F5DB812}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{AA661CFD-17A7-407A-A279-8E003F5DB812}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{AA661CFD-17A7-407A-A279-8E003F5DB812}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{AA661CFD-17A7-407A-A279-8E003F5DB812}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {6B5AF291-04A2-4889-A9EF-EA812796302C}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
namespace Calculator;
2+
3+
public class Calculator
4+
{
5+
private string? FirstOperand { get; set; }
6+
7+
private string? SecondOperand { get; set; }
8+
9+
private string? Operator { get; set; }
10+
11+
private string? SecondOperator { get; set; }
12+
13+
public string? Text { get; private set; }
14+
15+
public static bool IsOperator(string text) => text == "/" || text == "*" || text == "-" || text == "+" || text == "±" || text == "=" || text == "√" || text == "C" || text == "⌫";
16+
17+
public string? CalculateExpression(string[] expression)
18+
{
19+
for (int i = 0; i < expression.Length; i++)
20+
{
21+
if(!AddOperatorOrOperand(expression[i]))
22+
{
23+
GetValue();
24+
}
25+
}
26+
return FirstOperand;
27+
}
28+
29+
public string? GetValue()
30+
{
31+
if (FirstOperand == null || SecondOperand == null)
32+
{
33+
return FirstOperand;
34+
}
35+
36+
float firstNumber = float.Parse(FirstOperand);
37+
float secondNumber = float.Parse(SecondOperand);
38+
switch (Operator)
39+
{
40+
case "+":
41+
{
42+
FirstOperand = (firstNumber + secondNumber).ToString();
43+
break;
44+
}
45+
case "-":
46+
{
47+
FirstOperand = (firstNumber - secondNumber).ToString();
48+
break;
49+
}
50+
case "*":
51+
{
52+
FirstOperand = (firstNumber * secondNumber).ToString();
53+
break;
54+
}
55+
case "/":
56+
{
57+
if (Math.Abs(secondNumber - 0) < 0.000000000000000000000000000001)
58+
{
59+
FirstOperand = SecondOperand = Operator = SecondOperand = null;
60+
}
61+
break;
62+
}
63+
default:
64+
{
65+
return null;
66+
}
67+
}
68+
69+
SecondOperand = null;
70+
Text = $"{FirstOperand} {SecondOperator} ";
71+
Operator = SecondOperator;
72+
return FirstOperand;
73+
}
74+
75+
public bool AddOperatorOrOperand(string text)
76+
{
77+
if (text == "")
78+
{
79+
return true;
80+
}
81+
82+
if (!IsOperator(text))
83+
{
84+
if (text == ",")
85+
{
86+
if (Text!= null && Text.Contains(',') || FirstOperand == null)
87+
{
88+
return true;
89+
}
90+
}
91+
if (Operator == null)
92+
{
93+
FirstOperand += text;
94+
}
95+
else
96+
{
97+
SecondOperand += text;
98+
}
99+
100+
Text += text;
101+
return true;
102+
}
103+
104+
if (FirstOperand == null)
105+
{
106+
return true;
107+
}
108+
109+
switch (text)
110+
{
111+
case "±":
112+
{
113+
Text = GetValue();
114+
if (FirstOperand != null)
115+
{
116+
FirstOperand = FirstOperand[0] == '-' ? FirstOperand[1..FirstOperand.Length] : $"-{FirstOperand[0..FirstOperand.Length]}";
117+
}
118+
119+
Operator = null;
120+
Text = FirstOperand;
121+
return true;
122+
}
123+
124+
case "√":
125+
{
126+
Text = GetValue();
127+
if (FirstOperand != null)
128+
{
129+
FirstOperand = FirstOperand[0] == '-' ? null : Math.Sqrt(double.Parse(FirstOperand)).ToString();
130+
}
131+
132+
Text = FirstOperand;
133+
return true;
134+
}
135+
136+
case "⌫":
137+
{
138+
if (SecondOperand != null)
139+
{
140+
if (SecondOperand.Length == 1)
141+
{
142+
SecondOperand = null;
143+
Text = $"{FirstOperand} {Operator} ";
144+
}
145+
146+
else
147+
{
148+
SecondOperand = SecondOperand[0..(SecondOperand.Length - 1)];
149+
Text = $"{FirstOperand} {Operator} {SecondOperand}";
150+
}
151+
}
152+
153+
else if (Operator != null)
154+
{
155+
Operator = null;
156+
Text = $"{FirstOperand} ";
157+
}
158+
159+
else if (FirstOperand != null)
160+
{
161+
if (FirstOperand.Length == 1)
162+
{
163+
FirstOperand = null;
164+
Text = "";
165+
}
166+
167+
else
168+
{
169+
FirstOperand = FirstOperand[0..(FirstOperand.Length - 1)];
170+
Text = $"{FirstOperand}";
171+
}
172+
}
173+
174+
return true;
175+
}
176+
177+
case "C":
178+
{
179+
Text = FirstOperand = SecondOperand = Operator = SecondOperator = null;
180+
return false;
181+
}
182+
183+
case "=":
184+
{
185+
Text = FirstOperand = GetValue();
186+
SecondOperand = Operator = SecondOperator = null;
187+
return true;
188+
}
189+
190+
default:
191+
{
192+
Text += " " + text + " ";
193+
if (Operator != null)
194+
{
195+
if (SecondOperand == null)
196+
{
197+
Operator = text;
198+
}
199+
SecondOperator = text;
200+
return false;
201+
}
202+
Operator = text;
203+
return false;
204+
}
205+
}
206+
}
207+
}
208+
209+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net6.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<UseWindowsForms>true</UseWindowsForms>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
</PropertyGroup>
10+
11+
</Project>

0 commit comments

Comments
 (0)