-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParsing.cs
112 lines (101 loc) · 2.63 KB
/
Parsing.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.Text.RegularExpressions;
namespace Computorv1
{
internal static class Parsing
{
static Dictionary<int, double> ParseSide(string arg)
{
Regex allRegex = new Regex(@"^(?<coefficient>[+-]?[\d+]?.?[\d+]?\*?)?(?<x>X)(?<power>\^[+-]?[\d+])?$", RegexOptions.IgnoreCase);
Regex coeffOnlyRegex = new Regex(@"^([+-]?[\d+]?.?[\d+]?)$", RegexOptions.IgnoreCase);
Dictionary<int, double> dict = new()
{
{0, 0},
{1, 0},
{2, 0}
};
string[] parts = Regex.Split(arg, @"(?=[+-])");
foreach (string part in parts)
{
if (parts.Length > 1 && string.IsNullOrEmpty(part))
{
continue;
}
Match allMatch = allRegex.Match(part);
Match coeffOnlyMatch = coeffOnlyRegex.Match(part);
if (allMatch.Success)
{
double coefficient;
int power;
string coefficientString = allMatch.Groups["coefficient"].Value;
string powerString = allMatch.Groups["power"].Value;
if (!string.IsNullOrEmpty(powerString) && powerString[0] == '^')
{
powerString = powerString.Substring(1, powerString.Length - 1);
power = int.Parse(powerString);
}
else
{
power = 0;
}
if (!string.IsNullOrEmpty(coefficientString))
{
if (coefficientString is "+" or "-")
{
coefficientString = string.Concat(coefficientString, "1");
}
if (coefficientString[^1] == '*')
{
coefficientString = coefficientString.Substring(0, coefficientString.Length - 1);
}
coefficient = double.Parse(coefficientString);
}
else
{
coefficient = 1;
}
if (!dict.ContainsKey(power))
dict[power] = 0;
dict[power] += coefficient;
}
else if (coeffOnlyMatch.Success)
{
int coefficient = int.Parse(part);
const int power = 0;
dict[power] += coefficient;
}
else
{
throw new ArgumentException("cant match part");
}
}
return dict;
}
public static Dictionary<int, double> Parse(string arg)
{
string arg2 = Regex.Replace(arg, @"\s+", "");
string[] splitted = arg2.Split('=');
Dictionary<int, double> lhs = ParseSide(splitted[0]);
Dictionary<int, double> rhs = ParseSide(splitted[1]);
if (string.IsNullOrWhiteSpace(splitted[0]) || string.IsNullOrWhiteSpace(splitted[1]))
{
throw new ArgumentException("Empty string on either side of =sign");
}
foreach ((int key, double value) in rhs)
{
if (!lhs.ContainsKey(key))
{
lhs[key] = 0;
}
lhs[key] -= value;
}
foreach ((int key, var _) in lhs.Where(kvp => kvp.Value == 0).ToList())
{
if (key != 0)
{
lhs.Remove(key);
}
}
return (lhs);
}
}
}