latest fixes from Fattakh
This commit is contained in:
parent
cfe68e39cc
commit
e8959b9498
@ -36,9 +36,17 @@
|
|||||||
Background="Gray"
|
Background="Gray"
|
||||||
Click="ClearButton_OnClick"
|
Click="ClearButton_OnClick"
|
||||||
Content="AC"
|
Content="AC"
|
||||||
|
CornerRadius="30"
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
Grid.Row="2"
|
Grid.Row="2"
|
||||||
Name="ClearButton"/>
|
Height="60"
|
||||||
|
HorizontalAlignment="Center"
|
||||||
|
HorizontalContentAlignment="Center"
|
||||||
|
Margin="5"
|
||||||
|
Name="ClearButton"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
VerticalContentAlignment="Center"
|
||||||
|
Width="60" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
Background="Gray"
|
Background="Gray"
|
||||||
|
|||||||
@ -1,17 +1,14 @@
|
|||||||
// This implementation of the calculator using
|
// This implementation of the calculator using
|
||||||
// multi-translating programming language Fusion.
|
// multi-translating programming language Fusion.
|
||||||
// The main logic was written on the Fusion,
|
// The main logic was written on the Fusion,
|
||||||
// then retranslated into c#, 'out' module in FusionCalculator
|
// then translated into c#, 'out' module in FusionCalculator
|
||||||
// is a translated c# code, that used by SharpCalculator module.
|
// is a translated c# code, that used by SharpCalculator module.
|
||||||
// If you want to dive into the logic of the calculator,
|
// If you want to dive into the logic of the calculator,
|
||||||
// you need to check 'out' folder in FusionCalculator module.
|
// you need to check 'out' folder in FusionCalculator module.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Interactivity;
|
using Avalonia.Interactivity;
|
||||||
using FusionCalculator;
|
using FusionCalculator;
|
||||||
@ -22,23 +19,47 @@ namespace SharpCalculator.Avalonia;
|
|||||||
|
|
||||||
public partial class MainWindow : Window
|
public partial class MainWindow : Window
|
||||||
{
|
{
|
||||||
static Random _random = new Random(); // Random object for rand() function in calculator
|
private static readonly Random Random = new Random(); // Random object for rand() function in calculator
|
||||||
double _randomNumber = _random.NextDouble(); // Random variable is between 0 to 1
|
private readonly double _randomNumber = Random.NextDouble(); // Random variable is between 0 to 1
|
||||||
private int _currentIndex = 0; // Current index for checking the length of input text, accordingly for clear button
|
private readonly List<string> _history = new List<string>(); // History list to append expressions
|
||||||
|
|
||||||
private List<string> _history = new List<string>();
|
|
||||||
|
|
||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void AppendToHistory(string calculation) //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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void MathButton_OnClick(object? sender, RoutedEventArgs e)
|
private void MathButton_OnClick(object? sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
if (sender is not Button button) // Checking if button exist, if not throw Exception
|
if (sender is not Button button) // Checking if button exist, if not throw Exception
|
||||||
throw new Exception();
|
throw new Exception();
|
||||||
|
|
||||||
string text; // declaring text for using it in switch case
|
|
||||||
|
|
||||||
ClearButton.Content = "AC"; // clear button content changing to Accurate Clear mode
|
ClearButton.Content = "AC"; // clear button content changing to Accurate Clear mode
|
||||||
|
|
||||||
@ -65,31 +86,29 @@ public partial class MainWindow : Window
|
|||||||
Input.Text += "1/";
|
Input.Text += "1/";
|
||||||
break;
|
break;
|
||||||
default: // if there is no function input, then just input Button content
|
default: // if there is no function input, then just input Button content
|
||||||
text = button.Content!.ToString()!;
|
string text = button.Content!.ToString()!; // declaring text for using it in switch case
|
||||||
Input.Text += text;
|
Input.Text += text;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void DeleteLastDigit() // function of deleting the digits and math operators one by one from right, like in real calculators
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(Input.Text))
|
||||||
|
{
|
||||||
|
Input.Text = Input.Text.Substring(0, Input.Text.Length - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void ClearButton_OnClick(object? sender, RoutedEventArgs e)
|
private void ClearButton_OnClick(object? sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
// Checking the indexes and the emptiness of the input for clearing one by one
|
if ((string)ClearButton.Content! == "AC") // if clear button content equal to AC, clear one by one
|
||||||
if (Input.Text != "" && _currentIndex < Input.Text!.Length)
|
|
||||||
{
|
{
|
||||||
if ((string)ClearButton.Content! == "AC") // if clear button content equal to AC, clear one by one
|
DeleteLastDigit();
|
||||||
{
|
|
||||||
Input.Text = Input.Text.Remove(_currentIndex, 1);
|
|
||||||
_currentIndex++;
|
|
||||||
}
|
|
||||||
else if ((string)ClearButton.Content! == "C") // if clear button content equal to C, clear all
|
|
||||||
{
|
|
||||||
Input.Text = "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else if ((string)ClearButton.Content! == "C") // if clear button content equal to C, clear all
|
||||||
{
|
{
|
||||||
// Handle the case when there's no text or all characters are cleared
|
Input.Text = "";
|
||||||
_currentIndex = 0; // Reset the index for the next round
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,31 +118,26 @@ public partial class MainWindow : Window
|
|||||||
History2.Foreground = new SolidColorBrush(Colors.Gray);
|
History2.Foreground = new SolidColorBrush(Colors.Gray);
|
||||||
History3.Foreground = new SolidColorBrush(Colors.Gray);
|
History3.Foreground = new SolidColorBrush(Colors.Gray);
|
||||||
|
|
||||||
|
string inputHistory = Input.Text!;
|
||||||
|
|
||||||
if(Input.Text == null) // checking Input for nullability
|
if(Input.Text == null) // checking Input for nullability
|
||||||
return;
|
return;
|
||||||
string exprStr = Input.Text; // expression variable, for print result
|
string exprStr = Input.Text; // expression variable, for print result
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
double rezult = Calculator.Calculate(exprStr); // result being processed by calculator object that implemented in FusionCalculator Module
|
double result = Calculator.Calculate(exprStr); // result being processed by calculator object that implemented in FusionCalculator Module
|
||||||
if (!double.IsNaN(rezult))
|
if (!double.IsNaN(result))
|
||||||
{
|
{
|
||||||
Input.Text = rezult.ToString(CultureInfo.InvariantCulture);
|
Input.Text = result.ToString(CultureInfo.InvariantCulture);
|
||||||
_history.Add(rezult.ToString(CultureInfo.InvariantCulture));
|
AppendToHistory($"{inputHistory} = {result}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
Input.Text = "Error: "+exception.Message;
|
Input.Text = "Error";
|
||||||
|
AppendToHistory($"Error: {exception.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display the calculation history in the three TextBlocks
|
|
||||||
if (_history.Count > 0)
|
|
||||||
History1.Text = _history[0];
|
|
||||||
if (_history.Count > 1)
|
|
||||||
History2.Text = _history[1];
|
|
||||||
if (_history.Count > 2)
|
|
||||||
History3.Text = _history[2];
|
|
||||||
|
|
||||||
ClearButton.Content = "C"; // if the expression is printed, then change clear button content ot C, for clearing expression totally
|
ClearButton.Content = "C"; // if the expression is printed, then change clear button content ot C, for clearing expression totally
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user