-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.cs
89 lines (77 loc) · 3.2 KB
/
Main.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
using Xamarin.Forms;
using System;
using System.Linq;
using System.Data;
namespace FormsTinyCalculator {
public partial class App : Application {
public App() { MainPage = new MainPage();}
}
public enum ButtonType { Digit, Operand, Cancel, Total};
public class GridButton: Button {
private Color[] Colors = new[] { Color.White, Color.Orange, Color.Gray, Color.Orange };
public GridButton(string text, ButtonType buttonType, int row, int col, int colspan){
Text = text;
FontSize = 36;
TextColor = Color.Black;
BackgroundColor = Colors[(int)buttonType];
ButtonType = buttonType;
SetValue(Grid.RowProperty, row);
SetValue(Grid.ColumnProperty, col);
SetValue(Grid.ColumnSpanProperty, colspan);
}
public ButtonType ButtonType { get; }
}
public class DisplayLabel : Label {
public DisplayLabel() {
Text = "0";
FontSize = 36;
HorizontalOptions = LayoutOptions.End;
VerticalOptions = LayoutOptions.Center;
BackgroundColor = Color.Black; TextColor = Color.White;
SetValue(Grid.ColumnSpanProperty, 4);
}
}
public class MainPage : ContentPage {
private Label _displayLabel = new DisplayLabel();
private string _expression, _digits;
private (string FirstName, ButtonType ButtonType, int Row, int Col, int Span)[] _buttonStruct = new[] {
("7", ButtonType.Digit, 1, 0, 1), ("8", ButtonType.Digit, 1, 1, 1), ("9", ButtonType.Digit, 1, 2, 1), ("/", ButtonType.Operand, 1, 3, 1),
("4", ButtonType.Digit, 2, 0, 1), ("5", ButtonType.Digit, 2, 1, 1), ("6", ButtonType.Digit, 2, 2, 1), ("*", ButtonType.Operand, 2, 3, 1),
("1", ButtonType.Digit, 3, 0, 1), ("2", ButtonType.Digit, 3, 1, 1), ("3", ButtonType.Digit, 3, 2, 1), ("-", ButtonType.Operand, 3, 3, 1),
("0", ButtonType.Digit, 4, 0, 3), ("+", ButtonType.Operand, 4, 3, 1),
("C", ButtonType.Cancel, 5, 0, 1), ("=", ButtonType.Total, 5, 1, 3)
};
public MainPage() {
var grid = new Grid() { BackgroundColor = Color.Black, RowSpacing = 1, ColumnSpacing = 1 };
grid.Children.Add(_displayLabel);
foreach(var b in _buttonStruct) { grid.Children.Add(new GridButton(b.FirstName, b.ButtonType, b.Row, b.Col, b.Span));}
Content = grid;
foreach(GridButton gridButton in grid.Children.Where(x => x is GridButton)) {
switch (gridButton.ButtonType){
case ButtonType.Digit: gridButton.Clicked += DigitClicked; break;
case ButtonType.Operand: gridButton.Clicked += OperandClicked; break;
case ButtonType.Cancel: gridButton.Clicked += CancelClicked; break;
case ButtonType.Total : gridButton.Clicked += TotalClicked; break;
}
}
}
private void CancelClicked(object sender, EventArgs e) {
_displayLabel.Text = "0";
_expression = _digits = null;
}
private void TotalClicked(object sender, EventArgs e) {
_expression = Convert.ToDouble(new DataTable().Compute(_expression, null)).ToString();
_displayLabel.Text = _expression;
_digits = null;
}
private void OperandClicked(object sender, EventArgs e) {
_expression += (sender as Button)?.Text;
_digits = null;
}
private void DigitClicked(object sender, EventArgs e) {
_expression += (sender as Button)?.Text;
_digits += (sender as Button)?.Text;
_displayLabel.Text = _digits;
}
}
}