latest fixes from Fattakh

This commit is contained in:
Timerix22 2023-12-25 18:19:04 +06:00
parent cfe68e39cc
commit e8959b9498
2 changed files with 62 additions and 40 deletions

View File

@ -36,9 +36,17 @@
Background="Gray"
Click="ClearButton_OnClick"
Content="AC"
CornerRadius="30"
Grid.Column="0"
Grid.Row="2"
Name="ClearButton"/>
Height="60"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
Margin="5"
Name="ClearButton"
VerticalAlignment="Center"
VerticalContentAlignment="Center"
Width="60" />
<Button
Background="Gray"

View File

@ -1,17 +1,14 @@
// This implementation of the calculator using
// multi-translating programming language 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.
// If you want to dive into the logic of the calculator,
// you need to check 'out' folder in FusionCalculator module.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Interactivity;
using FusionCalculator;
@ -22,23 +19,47 @@ namespace SharpCalculator.Avalonia;
public partial class MainWindow : Window
{
static Random _random = new Random(); // Random object for rand() function in calculator
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 List<string> _history = new List<string>();
private static readonly Random Random = new Random(); // Random object for rand() function in calculator
private readonly double _randomNumber = Random.NextDouble(); // Random variable is between 0 to 1
private readonly List<string> _history = new List<string>(); // History list to append expressions
public MainWindow()
{
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)
{
if (sender is not Button button) // Checking if button exist, if not throw 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
@ -65,33 +86,31 @@ public partial class MainWindow : Window
Input.Text += "1/";
break;
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;
break;
}
}
private void ClearButton_OnClick(object? sender, RoutedEventArgs e)
private void DeleteLastDigit() // function of deleting the digits and math operators one by one from right, like in real calculators
{
// Checking the indexes and the emptiness of the input for clearing one by one
if (Input.Text != "" && _currentIndex < Input.Text!.Length)
if (!string.IsNullOrEmpty(Input.Text))
{
Input.Text = Input.Text.Substring(0, Input.Text.Length - 1);
}
}
private void ClearButton_OnClick(object? sender, RoutedEventArgs e)
{
if ((string)ClearButton.Content! == "AC") // if clear button content equal to AC, clear one by one
{
Input.Text = Input.Text.Remove(_currentIndex, 1);
_currentIndex++;
DeleteLastDigit();
}
else if ((string)ClearButton.Content! == "C") // if clear button content equal to C, clear all
{
Input.Text = "";
}
}
else
{
// Handle the case when there's no text or all characters are cleared
_currentIndex = 0; // Reset the index for the next round
}
}
private void ResultButton_OnClick(object? sender, RoutedEventArgs e)
{
@ -99,31 +118,26 @@ public partial class MainWindow : Window
History2.Foreground = new SolidColorBrush(Colors.Gray);
History3.Foreground = new SolidColorBrush(Colors.Gray);
string inputHistory = Input.Text!;
if(Input.Text == null) // checking Input for nullability
return;
string exprStr = Input.Text; // expression variable, for print result
try
{
double rezult = Calculator.Calculate(exprStr); // result being processed by calculator object that implemented in FusionCalculator Module
if (!double.IsNaN(rezult))
double result = Calculator.Calculate(exprStr); // result being processed by calculator object that implemented in FusionCalculator Module
if (!double.IsNaN(result))
{
Input.Text = rezult.ToString(CultureInfo.InvariantCulture);
_history.Add(rezult.ToString(CultureInfo.InvariantCulture));
Input.Text = result.ToString(CultureInfo.InvariantCulture);
AppendToHistory($"{inputHistory} = {result}");
}
}
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
}
}