I have a project that has a lot of mathematical formulas and I need to show to users:
I already have a class to solve basic math problems, like sum, subtraction, division, multiplication, etc, and with that, I created a dictionary to change the operator, so it's good.
But I have another class that I do formulas, about 20 formulas, such as Bhaskara, logarithms, geometric sequences, etc. So I'm trying to reuse that entries and labels of these formulas, because if I create formula per formula, it will consume a lot of classes, or one class with a lot of lines.
What's the best way to reuse it? Considering:
- Space in the screen of entries and labels (they change depending of the formulas)
- Quantity of entries and labels to show, e.g: Bhaskara have 3 entries (for B², A and C), but the volume of a cube is 1 entry (V = s³), so few entries/labels will not appear.
- Each formula will appear if I click to it appears, they are already doing this, e.g: Solve Bhaskara button will inflate a page with Bhaskara to solve, solve Trigonometry button will inflate the same page, but with Trigonometry to solve, etc, the picture above shows an example.
This is the picture of how Bhaskara appears on mobile (it only appears the first line, so if I click in the button to solve, it will solve and appear this colorful lines solving Bhaskara), and the code of Bhaskara:
void Bhaskara()
{
StandardImgExample();
Label lblDelta = new Label() { Text = delta, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
gridInputs.Children.Add(lblDelta, 0, 0);
Label lblEqual = new Label() { Text = equal, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
gridInputs.Children.Add(lblEqual, 1, 0);
firstEntry = new Entry() { IsVisible = true, TextColor = Color.Black, Keyboard = Keyboard.Numeric, PlaceholderColor = Color.DarkGray, Placeholder = "\t b\u00b2 \t", HorizontalTextAlignment = TextAlignment.Center, HorizontalOptions = LayoutOptions.FillAndExpand };
firstEntry.Completed += (s, e) =>
{
if (!firstEntry.Text.Contains("\u00b2"))
{
firstEntry.Text += "\u00b2";
}
};
firstEntry.Focus();
gridInputs.Children.Add(firstEntry, 2, 0);
Label lblMinus = new Label() { Text = minus, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
gridInputs.Children.Add(lblMinus, 3, 0);
Label lblFour = new Label() { Text = "4", HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
gridInputs.Children.Add(lblFour, 4, 0);
Label lblMult = new Label() { Text = "*", TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Center };
gridInputs.Children.Add(lblMult, 5, 0);
midEntry = new Entry() { IsVisible = true, MaxLength = 9999999, TextColor = Color.Black, Keyboard = Keyboard.Numeric, PlaceholderColor = Color.DarkGray, Placeholder = "\t a \t", HorizontalTextAlignment = TextAlignment.Center };
gridInputs.Children.Add(midEntry, 6, 0);
Label lblMult2 = new Label() { Text = "*", TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Center };
gridInputs.Children.Add(lblMult2, 7, 0);
lastEntry = new Entry() { IsVisible = true, MaxLength = 9999999, TextColor = Color.Black, Keyboard = Keyboard.Numeric, PlaceholderColor = Color.DarkGray, Placeholder = "\t c \t", HorizontalTextAlignment = TextAlignment.Center };
gridInputs.Children.Add(lastEntry, 8, 0);
firstEntry.Completed += (s, e) => { midEntry.Focus(); };
midEntry.Completed += (s, e) => { lastEntry.Focus(); };
Button btnSolve = new Button()
{
VerticalOptions = LayoutOptions.EndAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Text = "Solve",
};
btnSolve.Clicked += async (s, e) =>
{
if (String.IsNullOrEmpty(firstEntry.Text))
{
firstEntry.PlaceholderColor = Color.Red;
return;
}
else if (String.IsNullOrEmpty(midEntry.Text))
{
midEntry.PlaceholderColor = Color.Red;
return;
}
else if (String.IsNullOrEmpty(lastEntry.Text))
{
lastEntry.PlaceholderColor = Color.Red;
return;
}
frameFormula.IsVisible = true;
frameFormula.IsEnabled = true;
if(!isFirst)
await frameFormula.FadeTo(0, 400);
gridFrame.Children.Clear();
DisplayFirstLineResult();
DisplaySecondLineResult();
DisplayThirdLineResult();
DisplayDeltaResult();
isFirst = false;
await frameFormula.FadeTo(100, 200);
};
stackLayoutMain.Children.Add(btnSolve);
}
void DisplayFirstLineResult()
{
Label lblDelta = new Label() { Text = delta, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Blue, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblDelta, 0, 1);
Label lblEqual = new Label() { Text = equal, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblEqual, 1, 1);
firstEntryResult = StringToNumeric(RemovePow(firstEntry.Text));
firstEntryResult = Math.Pow(firstEntryResult, 2);
Label lblFirstEntry = new Label() { Text = firstEntryResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Green, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblFirstEntry, 2, 1);
Label lblMinus = new Label() { Text = minus, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblMinus, 3, 1);
Label lblFour = new Label() { Text = "4", HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Purple, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblFour, 4, 1);
Label lblMult = new Label() { Text = "*", TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Center };
gridFrame.Children.Add(lblMult, 5, 1);
Label lblMidEntry = new Label() { Text = midEntry.Text, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Purple, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblMidEntry, 6, 1);
Label lblMult2 = new Label() { Text = "*", TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Center };
gridFrame.Children.Add(lblMult2, 7, 1);
Label lblLastEntry = new Label() { Text = lastEntry.Text, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Orange, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblLastEntry, 8, 1);
}
void DisplaySecondLineResult()
{
Label lblDelta = new Label() { Text = delta, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Blue, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblDelta, 0, 2);
Label lblEqual = new Label() { Text = equal, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblEqual, 1, 2);
Label lblFirstEntry = new Label() { Text = firstEntryResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Green, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblFirstEntry, 2, 2);
Label lblMinus = new Label() { Text = minus, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblMinus, 3, 2);
midEntryResult = StringToNumeric(midEntry.Text);
midEntryResult = 4 * midEntryResult;
Label lblMidEntry = new Label() { Text = midEntryResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Purple, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblMidEntry, 4, 2);
Label lblMult2 = new Label() { Text = "*", TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Center };
gridFrame.Children.Add(lblMult2, 5, 2);
Label lblLastEntry = new Label() { Text = lastEntry.Text, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Orange, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblLastEntry, 6, 2);
}
void DisplayThirdLineResult()
{
Label lblDelta = new Label() { Text = delta, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Blue, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblDelta, 0, 3);
Label lblEqual = new Label() { Text = equal, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblEqual, 1, 3);
Label lblFirstEntry = new Label() { Text = firstEntryResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Green, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblFirstEntry, 2, 3);
Label lblMinus = new Label() { Text = minus, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblMinus, 3, 3);
lastEntryResult = StringToNumeric(lastEntry.Text);
midAndlastEntryResult = midEntryResult * lastEntryResult;
Label lblMidEntry = new Label() { Text = midAndlastEntryResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Purple, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblMidEntry, 4, 3);
}
void DisplayDeltaResult()
{
Label lblDelta = new Label() { Text = delta, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Blue, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblDelta, 0, 4);
Label lblEqual = new Label() { Text = equal, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblEqual, 1, 4);
deltaResult = firstEntryResult - midAndlastEntryResult;
Label lblFirstEntry = new Label() { Text = deltaResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Green, HorizontalOptions = LayoutOptions.Center };
gridFrame.Children.Add(lblFirstEntry, 2, 4);
}