-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.cs
81 lines (69 loc) · 2.24 KB
/
Calculator.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
namespace CalculatorConsole
{
using System;
using System.Data;
using static Program;
public class Calculator
{
public static decimal result;
public static object ComputeTable;
public static decimal ConvertTable;
public static void SmallMath()
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine("Enter the equation: ");
Console.WriteLine(" ");
result = CalculateDecimalString(Console.ReadLine());
if (isError == false)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" ");
Console.WriteLine("-------------------");
Console.WriteLine("Result: " + result);
Console.WriteLine("-------------------");
Console.WriteLine(" ");
Console.WriteLine(" ");
}
Calculate();
}
public static decimal CalculateDecimalString(string input)
{
var table = new DataTable();
if (input.Contains("/") || input.Contains("*") || input.Contains("+") || input.Contains("-"))
{
try
{
ComputeTable = table.Compute(input, string.Empty);
}
catch
{
Console.WriteLine("Input equation was too large or too small.");
Console.WriteLine(" ");
}
}
else
{
Console.WriteLine(" ");
Console.WriteLine("Not a computable equation.");
SmallMath();
}
try
{
if (ComputeTable != null)
{
ConvertTable = Convert.ToDecimal(ComputeTable);
}
}
catch
{
Console.WriteLine(" ");
Console.WriteLine("Input equation was too large or too small.");
Console.WriteLine(" ");
isError = true;
}
return ConvertTable;
}
}
}