-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseCalculator.cs
112 lines (100 loc) · 3.03 KB
/
BaseCalculator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StandardCalculator
{
public abstract class BaseCalculator : IBaseCalculator
{
private char[] Operators { get; set; }
private double answer = 0;
public BaseCalculator(char[] operators)
{
this.Operators = operators;
}
public string EvaluateOperation(string equation, char op)
{
int i = 0;
if (equation.Contains(op))
i = equation.IndexOf(op);
switch (op)
{
case '(':
throw new NotImplementedException();
case ')':
throw new NotImplementedException();
case '/':
answer = EvaluateDivision(Double.Parse(equation[i - 1].ToString()),
Double.Parse(equation[i + 1].ToString()));
break;
case '*':
answer = EvaluateMultiplication(Double.Parse(equation[i - 1].ToString()),
Double.Parse(equation[i + 1].ToString()));
break;
case '+':
answer = EvaluateAddition(Double.Parse(equation[i - 1].ToString()),
Double.Parse(equation[i + 1].ToString()));
break;
case '-':
answer = EvaluateSubtraction(Double.Parse(equation[i - 1].ToString()),
Double.Parse(equation[i + 1].ToString()));
break;
}
// remove the evaluated equation
equation = equation.Remove(i - 1, 3);
// insert the answer
equation = equation.Insert(i - 1, answer.ToString());
return equation;
}
public double EvaluateAddition(double a, double b)
{
try
{
answer = a + b;
}
catch(ArithmeticException ex)
{
throw ex;
}
return answer;
}
public double EvaluateSubtraction(double a, double b)
{
try
{
answer = a - b;
}
catch (ArithmeticException ex)
{
throw ex;
}
return answer;
}
public double EvaluateMultiplication(double a, double b)
{
try
{
answer = a * b;
}
catch (ArithmeticException ex)
{
throw ex;
}
return answer;
}
public double EvaluateDivision(double a, double b)
{
try
{
answer = a / b;
}
catch (ArithmeticException ex)
{
throw ex;
}
return answer;
}
public abstract string EvaluateEquation(string equation);
}
}