-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.cs
107 lines (98 loc) · 2 KB
/
Cell.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
namespace test;
public class Cell
{
public double Value{get; set;}
public string Expression{get; set;}
public int CoordinateX{get; set;}
public int CoordinateY{get; set;}
public string Name {get; set;}
public int ID{get;}
public static int Count { get => count; set => count = value; }
private static int count = 0;
public Cell(double val, string exp, int coordX, int coordY, string jname, int id)
{
Value = val;
Expression = exp;
CoordinateX = coordX;
CoordinateY = coordY;
Name = jname;
ID = id;
Cell.count = Math.Max(Cell.count, ID+1);
if(Calculator.GlobalScope.ContainsKey(this.Name))
{
Calculator.GlobalScope[Name] = Value;
} else
{
Calculator.GlobalScope.Add(Name, Value);
}
}
public string GetExpression()
{
return Expression;
}
public Tuple<int, int> GetCoordinates()
{
Tuple<int, int> p = new Tuple<int, int>(this.CoordinateX, this.CoordinateY);
return p;
}
public string GetName()
{
return this.Name;
}
public string CoordinatesToName()
{
string ans = string.Empty;
int x = CoordinateX;
while(x>0)
{
ans=Convert.ToChar((x-1)%26+65)+ans;
x/=26;
}
ans=ans+Convert.ToString(CoordinateY);
return ans;
}
public Cell(int x, int y)
{
CoordinateX = x;
CoordinateY = y;
Value = 0;
Expression = string.Empty;
Name = CoordinatesToName();
ReCalculate();
ID = Cell.count;
Cell.count++;
}
private void ReCalculate()
{
Value = 0;
if(Expression!="")
{
Value = Calculator.Evaluate(this.Expression);
}
if(Calculator.GlobalScope.ContainsKey(this.Name))
{
Calculator.GlobalScope.Remove(this.Name);
}
Calculator.GlobalScope.Add(this.Name, this.Value);
}
public void ChangeExpression(string exp)
{
Expression = exp;
ReCalculate();
}
public Cell(int x, int y, string exp)
{
CoordinateX = x;
CoordinateY = y;
Expression = exp;
Name = CoordinatesToName();
ID = Cell.count;
Cell.count++;
ReCalculate();
}
public void Delete()
{
Expression = "0";
Calculator.GlobalScope.Remove(this.Name);
}
}