app logic restored
This commit is contained in:
parent
bf2c734baf
commit
04d258c5a3
@ -1 +1 @@
|
||||
Subproject commit 2a4f92fe351882a489c330ba82504d319df14c73
|
||||
Subproject commit 7dab069aa6c58ea4a5270e8983663076eb35dbf2
|
||||
@ -15,7 +15,6 @@
|
||||
<Setter Property="Height" Value="60"/>
|
||||
<Setter Property="Width" Value="60"/>
|
||||
<Setter Property="CornerRadius" Value="30"/>
|
||||
<Setter Property="Margin" Value="5"/>
|
||||
<Setter Property="HorizontalAlignment" Value="Center"/>
|
||||
<Setter Property="VerticalAlignment" Value="Center"/>
|
||||
<Setter Property="HorizontalContentAlignment" Value="Center"/>
|
||||
|
||||
@ -4,148 +4,158 @@
|
||||
x:Class="SharpCalculator.Avalonia.MainWindow"
|
||||
Title="SharpCalculator"
|
||||
FontFamily="Consolas" FontSize="24"
|
||||
Width="600" Height="750"
|
||||
MinWidth="600" MinHeight="750">
|
||||
Width="460" Height="570">
|
||||
|
||||
<Grid Classes="center" Margin="20">
|
||||
<Grid.ColumnDefinitions>* * * * * * *</Grid.ColumnDefinitions>
|
||||
<Grid Margin="10">
|
||||
<Grid.RowDefinitions>1* 40 1* 330</Grid.RowDefinitions>
|
||||
|
||||
<Grid.RowDefinitions>* * * * * * * * * *</Grid.RowDefinitions>
|
||||
<TextBox Name="Input" Grid.Row="0"
|
||||
TextChanged="Input_OnTextChanged"
|
||||
BorderThickness="2"
|
||||
BorderBrush="#AAAAAA"
|
||||
TextWrapping="Wrap"/>
|
||||
|
||||
<!-- 1st row -->
|
||||
<TextBlock Grid.Row="1"
|
||||
VerticalAlignment="Center"
|
||||
Foreground="#AAAAAA"
|
||||
Text="="/>
|
||||
<TextBox Name="Output" Grid.Row="1"
|
||||
Margin="16 0 0 0"
|
||||
VerticalAlignment="Center"
|
||||
VerticalContentAlignment="Center"
|
||||
IsReadOnly="True"
|
||||
BorderThickness="0"
|
||||
TextWrapping="NoWrap"/>
|
||||
|
||||
<TextBox Name="Input" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="7"
|
||||
HorizontalAlignment="Right"
|
||||
Text=""/>
|
||||
<TextBox Name="History" Grid.Row="2"
|
||||
Foreground="Gray"
|
||||
BorderThickness="1"
|
||||
BorderBrush="Gray"
|
||||
IsReadOnly="True"
|
||||
TextWrapping="NoWrap"
|
||||
FontSize="18"/>
|
||||
|
||||
<!-- 2nd row -->
|
||||
<Grid Grid.Row="3" VerticalAlignment="Bottom"
|
||||
MinWidth="440" MaxWidth="440" MinHeight="320" MaxHeight="320">
|
||||
<Grid.ColumnDefinitions>* * * * * * *</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>* * * * *</Grid.RowDefinitions>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="0" Grid.Row="2"
|
||||
Click="ClearButton_OnClick" Content="AC"/>
|
||||
<!-- 1st row -->
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="1" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content="("/>
|
||||
<Button Classes="cirlce gray" Grid.Column="0" Grid.Row="0"
|
||||
Click="ClearButton_OnClick" Content="AC"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="2" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content=")" />
|
||||
<Button Classes="cirlce gray" Grid.Column="1" Grid.Row="0"
|
||||
Click="MathButton_OnClick" Content="("/>
|
||||
|
||||
<Button Classes="cirlce orange" Grid.Column="3" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content="/"/>
|
||||
<Button Classes="cirlce gray" Grid.Column="2" Grid.Row="0"
|
||||
Click="MathButton_OnClick" Content=")" />
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="4" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content="^"/>
|
||||
<Button Classes="cirlce orange" Grid.Column="3" Grid.Row="0"
|
||||
Click="MathButton_OnClick" Content="/"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="5" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content="sin"/>
|
||||
<Button Classes="cirlce gray" Grid.Column="4" Grid.Row="0"
|
||||
Click="MathButton_OnClick" Content="1/x"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="6" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content="asin"/>
|
||||
<Button Classes="cirlce gray" Grid.Column="5" Grid.Row="0"
|
||||
Click="MathButton_OnClick" Content="sin"/>
|
||||
|
||||
<!-- 3rd row -->
|
||||
<Button Classes="cirlce gray" Grid.Column="6" Grid.Row="0"
|
||||
Click="MathButton_OnClick" Content="asin"/>
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="0" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="7"/>
|
||||
<!-- 2nd row -->
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="1" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="8"/>
|
||||
<Button Classes="cirlce" Grid.Column="0" Grid.Row="1"
|
||||
Click="MathButton_OnClick" Content="7"/>
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="2" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="9"/>
|
||||
<Button Classes="cirlce" Grid.Column="1" Grid.Row="1"
|
||||
Click="MathButton_OnClick" Content="8"/>
|
||||
|
||||
<Button Classes="cirlce orange" Grid.Column="3" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="*"/>
|
||||
<Button Classes="cirlce" Grid.Column="2" Grid.Row="1"
|
||||
Click="MathButton_OnClick" Content="9"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="4" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="e"/>
|
||||
<Button Classes="cirlce orange" Grid.Column="3" Grid.Row="1"
|
||||
Click="MathButton_OnClick" Content="*"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="5" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="cos"/>
|
||||
<Button Classes="cirlce gray" Grid.Column="4" Grid.Row="1"
|
||||
Click="MathButton_OnClick" Content="^"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="6" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="acos"/>
|
||||
<Button Classes="cirlce gray" Grid.Column="5" Grid.Row="1"
|
||||
Click="MathButton_OnClick" Content="cos"/>
|
||||
|
||||
<!-- 4th row -->
|
||||
<Button Classes="cirlce gray" Grid.Column="6" Grid.Row="1"
|
||||
Click="MathButton_OnClick" Content="acos"/>
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="0" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="4"/>
|
||||
<!-- 3th row -->
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="1" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="5"/>
|
||||
<Button Classes="cirlce" Grid.Column="0" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content="4"/>
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="2" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="6"/>
|
||||
<Button Classes="cirlce" Grid.Column="1" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content="5"/>
|
||||
|
||||
<Button Classes="cirlce orange" Grid.Column="3" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="-"/>
|
||||
<Button Classes="cirlce" Grid.Column="2" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content="6"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="4" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="10^x"/>
|
||||
<Button Classes="cirlce orange" Grid.Column="3" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content="-"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="5" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="tan"/>
|
||||
<Button Classes="cirlce gray" Grid.Column="4" Grid.Row="2"
|
||||
FontSize="22"
|
||||
Click="MathButton_OnClick" Content="10^x"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="6" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="atan"/>
|
||||
<Button Classes="cirlce gray" Grid.Column="5" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content="tg"/>
|
||||
|
||||
<!-- 5th row -->
|
||||
<Button Classes="cirlce gray" Grid.Column="6" Grid.Row="2"
|
||||
Click="MathButton_OnClick" Content="atg"/>
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="0" Grid.Row="5"
|
||||
Click="MathButton_OnClick" Content="1"/>
|
||||
<!-- 4th row -->
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="1" Grid.Row="5"
|
||||
Click="MathButton_OnClick" Content="2"/>
|
||||
<Button Classes="cirlce" Grid.Column="0" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="1"/>
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="2" Grid.Row="5"
|
||||
Click="MathButton_OnClick" Content="3"/>
|
||||
<Button Classes="cirlce" Grid.Column="1" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="2"/>
|
||||
|
||||
<Button Classes="cirlce orange" Grid.Column="3" Grid.Row="5"
|
||||
Click="MathButton_OnClick" Content="+"/>
|
||||
<Button Classes="cirlce" Grid.Column="2" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="3"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="4" Grid.Row="5"
|
||||
Click="MathButton_OnClick" Content="rand"/>
|
||||
<Button Classes="cirlce orange" Grid.Column="3" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="+"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="5" Grid.Row="5"
|
||||
Click="MathButton_OnClick" Content="ctg"/>
|
||||
<Button Classes="cirlce gray" Grid.Column="4" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="e"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="6" Grid.Row="5"
|
||||
Click="MathButton_OnClick" Content="actg"/>
|
||||
<Button Classes="cirlce gray" Grid.Column="5" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="ctg"/>
|
||||
|
||||
<!-- 6th row -->
|
||||
<Button Classes="cirlce gray" Grid.Column="6" Grid.Row="3"
|
||||
Click="MathButton_OnClick" Content="actg"/>
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="0" Grid.Row="6"
|
||||
Click="MathButton_OnClick" Content="0"/>
|
||||
<!-- 5th row -->
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="1" Grid.Row="6"
|
||||
Click="MathButton_OnClick" Content="%"/>
|
||||
<Button Classes="cirlce" Grid.Column="0" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="0"/>
|
||||
|
||||
<Button Classes="cirlce" Grid.Column="2" Grid.Row="6"
|
||||
Click="MathButton_OnClick" Content="."/>
|
||||
<Button Classes="cirlce" Grid.Column="1" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="."/>
|
||||
|
||||
<Button Classes="cirlce orange" Grid.Column="3" Grid.Row="6"
|
||||
Click="ResultButton_OnClick" Content="="/>
|
||||
<Button Classes="cirlce" Grid.Column="2" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="%"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="4" Grid.Row="6"
|
||||
Click="MathButton_OnClick" Content="π"/>
|
||||
<Button Classes="cirlce orange" Grid.Column="3" Grid.Row="4"
|
||||
Click="ResultButton_OnClick" Content="="/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="5" Grid.Row="6"
|
||||
Click="MathButton_OnClick" Content="1/x"/>
|
||||
<Button Classes="cirlce gray" Grid.Column="4" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="π"/>
|
||||
|
||||
<Button Classes="cirlce gray" Grid.Column="6" Grid.Row="6"
|
||||
Click="MathButton_OnClick" Content="log"/>
|
||||
<Button Classes="cirlce gray" Grid.Column="5" Grid.Row="4"
|
||||
FontSize="22"
|
||||
Click="MathButton_OnClick" Content="rand"/>
|
||||
|
||||
<!-- 7th row -->
|
||||
|
||||
<TextBlock Grid.Column="0" Grid.Row="7" Grid.ColumnSpan="7" Name="History1"
|
||||
Text=""/>
|
||||
|
||||
<!-- 8th row -->
|
||||
|
||||
<TextBlock Name="History2" Grid.Column="0" Grid.Row="8" Grid.ColumnSpan="7"
|
||||
Text=""/>
|
||||
|
||||
<!-- 9th row -->
|
||||
|
||||
<TextBlock Name="History3" Grid.Column="0" Grid.Row="9" Grid.ColumnSpan="7"
|
||||
Text=""/>
|
||||
<Button Classes="cirlce gray" Grid.Column="6" Grid.Row="4"
|
||||
Click="MathButton_OnClick" Content="ln"/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Window>
|
||||
@ -4,29 +4,24 @@ namespace SharpCalculator.Avalonia;
|
||||
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
private static readonly Random Random = new();
|
||||
private readonly List<string> _history = new(); // History list to append expressions
|
||||
private readonly Random Random = new();
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void AppendToHistory(string calculation) //function of appending expression to the history list
|
||||
private void AppendToHistory(string line) //function of appending expression to the history list
|
||||
{
|
||||
_history.Add(calculation);
|
||||
UpdateHistoryText();
|
||||
}
|
||||
|
||||
private void UpdateHistoryText() //function to update history list
|
||||
{
|
||||
int lastIndex = _history.Count - 1;
|
||||
if (lastIndex >= 0)
|
||||
History1.Text = _history[lastIndex];
|
||||
if (lastIndex - 1 >= 0)
|
||||
History2.Text = _history[lastIndex - 1];
|
||||
if (lastIndex - 2 >= 0)
|
||||
History3.Text = _history[lastIndex - 2];
|
||||
// begin new line if not empty
|
||||
if (!string.IsNullOrEmpty(History.Text))
|
||||
History.Text += '\n';
|
||||
/// add line without space characters
|
||||
History.Text += line
|
||||
.Replace("\t", " ")
|
||||
.Replace("\n", " ")
|
||||
.Replace("\r", " ")
|
||||
.Replace(" ", " ");
|
||||
}
|
||||
|
||||
private void MathButton_OnClick(object? sender, RoutedEventArgs e)
|
||||
@ -34,39 +29,30 @@ public partial class MainWindow : Window
|
||||
if (sender is not Button button)
|
||||
throw new Exception();
|
||||
|
||||
switch (Input.Text) // Switch case for checking the inputting functions
|
||||
string buttonText = button.Content!.ToString()!; // declaring text for using it in switch case
|
||||
Input.Text += buttonText switch
|
||||
{
|
||||
case "rand": // rand function add random variable from 0 to 1 into Input
|
||||
Input.Text = "";
|
||||
Input.Text += Random.NextDouble().ToString(CultureInfo.InvariantCulture);
|
||||
break;
|
||||
case "π": // Pi function add pi value into Input
|
||||
Input.Text = "";
|
||||
Input.Text += "3.1415";
|
||||
break;
|
||||
case "e": // euler function add e value into Input
|
||||
Input.Text = "";
|
||||
Input.Text += "2.71828";
|
||||
break;
|
||||
case "10^x": // ten in power of x function
|
||||
Input.Text = "";
|
||||
Input.Text += "10^";
|
||||
break;
|
||||
case "1/x": // one over x function
|
||||
Input.Text = "";
|
||||
Input.Text += "1/";
|
||||
break;
|
||||
default: // if there is no function input, then just input Button content
|
||||
string text = button.Content!.ToString()!; // declaring text for using it in switch case
|
||||
Input.Text += text;
|
||||
break;
|
||||
}
|
||||
"rand" => // random number from 0 to 1 into Input
|
||||
Random.NextDouble().ToString("0.#####", CultureInfo.InvariantCulture),
|
||||
"π" => // Pi constant
|
||||
"3.14159",
|
||||
"e" => // E constant
|
||||
"2.71828",
|
||||
"10^x" => // ten in power of x function
|
||||
"10^",
|
||||
"1/x" => // one over x function
|
||||
"1/",
|
||||
"sin" or "cos" or "tg" or "ctg"
|
||||
or "asin" or "acos" or "atg" or "actg"
|
||||
or "ln" => buttonText+'(',
|
||||
_ => buttonText
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
private void ClearButton_OnClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
Input.Text = "";
|
||||
Output.Text = "";
|
||||
}
|
||||
|
||||
private void UndoButton_OnClick(object? sender, RoutedEventArgs e)
|
||||
@ -78,6 +64,16 @@ public partial class MainWindow : Window
|
||||
}
|
||||
|
||||
private void ResultButton_OnClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
if(Input.Text == null || Output.Text == null || Output.Text.StartsWith("Error"))
|
||||
return;
|
||||
string expression = Input.Text;
|
||||
string result = Output.Text;
|
||||
AppendToHistory($"{expression}={result}");
|
||||
Input.Text = result;
|
||||
}
|
||||
|
||||
private void Input_OnTextChanged(object? sender, TextChangedEventArgs e)
|
||||
{
|
||||
if(Input.Text == null)
|
||||
return;
|
||||
@ -85,16 +81,19 @@ public partial class MainWindow : Window
|
||||
string exprStr = Input.Text; // mathematical expression
|
||||
try
|
||||
{
|
||||
double result = Calculator.Calculate(exprStr); // result being processed by calculator object that implemented in FusionCalculator Module
|
||||
if (!double.IsNaN(result))
|
||||
{
|
||||
AppendToHistory($"{exprStr} = {result}");
|
||||
Input.Text = result.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
// expression is being processed by Calculator class that implemented in FusionCalculator Module
|
||||
double result = Calculator.Calculate(exprStr);
|
||||
if (double.IsNaN(result))
|
||||
Output.Text = "";
|
||||
// scientific notation with 5-digit precision for big and small numbers
|
||||
else if (result > 10e+15 || result < 10e-5)
|
||||
Output.Text = result.ToString("0.#####E0", CultureInfo.InvariantCulture);
|
||||
// decimal number with 5-digit precision for regular numbers
|
||||
else Output.Text = result.ToString("0.#####", CultureInfo.InvariantCulture);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
AppendToHistory($"Error: {exception.Message}");
|
||||
Output.Text = $"Error: {exception.Message}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user