7 Commits

6 changed files with 96 additions and 106 deletions

1
.gitignore vendored
View File

@@ -398,3 +398,4 @@ FodyWeavers.xsd
# JetBrains Rider # JetBrains Rider
*.sln.iml *.sln.iml
.idea/

View File

@@ -5,6 +5,12 @@ VisualStudioVersion = 17.13.35931.197 d17.13
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Evil Calculator", "Evil Calculator\Evil Calculator.csproj", "{942928AE-D155-4E97-A49D-174BFD563AF2}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Evil Calculator", "Evil Calculator\Evil Calculator.csproj", "{942928AE-D155-4E97-A49D-174BFD563AF2}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionFolder", "SolutionFolder", "{5F98C4DD-6BD6-4E12-94A8-87FB896463CD}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
README.md = README.md
EndProjectSection
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU

View File

@@ -5,21 +5,21 @@
private bool mouseDown; private bool mouseDown;
private bool onTop; private bool onTop;
private Point lastLocation; private Point lastLocation;
private static Color LunarGray = System.Drawing.ColorTranslator.FromHtml("#2b292d"); private static Color LunarGray = ColorTranslator.FromHtml("#2b292d");
private static Color LunarOrange = System.Drawing.ColorTranslator.FromHtml("#ff8700"); private static Color LunarOrange = ColorTranslator.FromHtml("#ff8700");
private Form CalculatorPanel = new Form(); private Form CalculatorPanel = new();
private EquationManager equationManager = new EquationManager(); private EquationManager equationManager = new();
public CalculatorBody() public CalculatorBody()
{ {
InitializeComponent(); InitializeComponent();
this.BackColor = LunarGray; BackColor = LunarGray;
this.Opacity = 0.75; Opacity = 0.9;
this.FormBorderStyle = FormBorderStyle.None; FormBorderStyle = FormBorderStyle.None;
this.ClientSize = new Size(300, 500); ClientSize = new Size(300, 500);
this.Text = "Calculator"; Text = "Calculator";
MyTitleBar(); MyTitleBar();
} }
@@ -54,7 +54,7 @@
TitleBar.MouseDown += TitleBar_MouseDown; TitleBar.MouseDown += TitleBar_MouseDown;
TitleBar.MouseMove += TitleBar_MouseMove; TitleBar.MouseMove += TitleBar_MouseMove;
TitleBar.MouseUp += TitleBar_MouseUp; TitleBar.MouseUp += TitleBar_MouseUp;
this.Controls.Add(TitleBar); Controls.Add(TitleBar);
Button ButtonMinimize = new Button Button ButtonMinimize = new Button
{ {
@@ -67,7 +67,7 @@
}; };
ButtonMinimize.FlatAppearance.BorderSize = 0; ButtonMinimize.FlatAppearance.BorderSize = 0;
ButtonMinimize.FlatAppearance.BorderColor = LunarGray; ButtonMinimize.FlatAppearance.BorderColor = LunarGray;
ButtonMinimize.Click += (s, e) => this.WindowState = FormWindowState.Minimized; ButtonMinimize.Click += (s, e) => WindowState = FormWindowState.Minimized;
TitleBar.Controls.Add(ButtonMinimize); TitleBar.Controls.Add(ButtonMinimize);
Button ButtonMaximize = new Button Button ButtonMaximize = new Button
@@ -83,7 +83,7 @@
ButtonMaximize.FlatAppearance.BorderColor = LunarGray; ButtonMaximize.FlatAppearance.BorderColor = LunarGray;
ButtonMaximize.Click += (s, e) => ButtonMaximize.Click += (s, e) =>
{ {
this.WindowState = (this.WindowState == FormWindowState.Normal) ? FormWindowState.Maximized : FormWindowState.Normal; WindowState = (WindowState == FormWindowState.Normal) ? FormWindowState.Maximized : FormWindowState.Normal;
}; };
TitleBar.Controls.Add(ButtonMaximize); TitleBar.Controls.Add(ButtonMaximize);
@@ -98,12 +98,12 @@
}; };
ButtonClose.FlatAppearance.BorderSize = 0; ButtonClose.FlatAppearance.BorderSize = 0;
ButtonClose.FlatAppearance.BorderColor = LunarGray; ButtonClose.FlatAppearance.BorderColor = LunarGray;
ButtonClose.Click += (s, e) => this.Close(); ButtonClose.Click += (s, e) => Close();
TitleBar.Controls.Add(ButtonClose); TitleBar.Controls.Add(ButtonClose);
Label titleLabel = new Label Label titleLabel = new Label
{ {
Text = this.Text, Text = Text,
Location = new Point(8, 7), Location = new Point(8, 7),
AutoSize = true, AutoSize = true,
ForeColor = Color.White ForeColor = Color.White
@@ -120,10 +120,10 @@
{ {
if (mouseDown) if (mouseDown)
{ {
this.Location = new Point( Location = new Point(
(this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y); (Location.X - lastLocation.X) + e.X, (Location.Y - lastLocation.Y) + e.Y);
this.Update(); Update();
} }
} }
@@ -134,9 +134,9 @@
void Titlebar_BaseTextChanged(object? sender, EventArgs e) void Titlebar_BaseTextChanged(object? sender, EventArgs e)
{ {
titleLabel.Text = this.Text; titleLabel.Text = Text;
} }
this.TextChanged += Titlebar_BaseTextChanged; TextChanged += Titlebar_BaseTextChanged;
} }
private void CalculatorPanelInit() private void CalculatorPanelInit()
@@ -144,10 +144,9 @@
CalculatorPanel.FormBorderStyle = FormBorderStyle.None; CalculatorPanel.FormBorderStyle = FormBorderStyle.None;
CalculatorPanel.BackColor = Color.Gray; CalculatorPanel.BackColor = Color.Gray;
CalculatorPanel.TransparencyKey = CalculatorPanel.BackColor; CalculatorPanel.TransparencyKey = CalculatorPanel.BackColor;
CalculatorPanel.Opacity = 0.90;
CalculatorPanel.StartPosition = FormStartPosition.Manual; CalculatorPanel.StartPosition = FormStartPosition.Manual;
CalculatorPanel.DesktopLocation = new Point(this.Location.X, this.Location.Y + 30); CalculatorPanel.DesktopLocation = new Point(Location.X, Location.Y + 30);
CalculatorPanel.ClientSize = new Size(this.Size.Width, this.Size.Height - 30); CalculatorPanel.ClientSize = new Size(Size.Width, Size.Height - 30);
CalculatorPanel.Owner = this; CalculatorPanel.Owner = this;
CalculatorPanel.ShowInTaskbar = false; CalculatorPanel.ShowInTaskbar = false;
@@ -200,15 +199,15 @@
currentOperandTextBox.ForeColor = Color.White; currentOperandTextBox.ForeColor = Color.White;
CalculatorPanel.Controls.Add(currentOperandTextBox); CalculatorPanel.Controls.Add(currentOperandTextBox);
equationManager.AcquireOutputLabels(ref equationTextBox, ref currentOperandTextBox); equationManager.AcquireOutputLabels(equationTextBox, currentOperandTextBox);
CalculatorPanel.Show(); CalculatorPanel.Show();
void ToggleTopButton_Click(object? sender, EventArgs e) void ToggleTopButton_Click(object? sender, EventArgs e)
{ {
this.Text = this.onTop ? "Calculator" : "Calculator [pinned]"; Text = onTop ? "Calculator" : "Calculator [pinned]";
this.onTop = !this.onTop; onTop = !onTop;
this.TopMost = !this.TopMost; TopMost = !TopMost;
CalculatorPanel.TopMost = !CalculatorPanel.TopMost; CalculatorPanel.TopMost = !CalculatorPanel.TopMost;
} }
@@ -220,16 +219,16 @@
public CalculatorButton(string Text, Point Location, Form Panel, EquationManager equationManager) public CalculatorButton(string Text, Point Location, Form Panel, EquationManager equationManager)
{ {
this.Text = Text; this.Text = Text;
this.Font = new Font("Arial", 12); Font = new Font("Arial", 12);
this.Location = Location; this.Location = Location;
this.Width = 50; Width = 50;
this.Height = 40; Height = 40;
this.FlatStyle = FlatStyle.Flat; FlatStyle = FlatStyle.Flat;
this.BackColor = LunarGray; BackColor = LunarGray;
this.FlatAppearance.BorderSize = 0; FlatAppearance.BorderSize = 0;
this.FlatAppearance.BorderColor = LunarGray; FlatAppearance.BorderColor = LunarGray;
ForeColor = Color.White; ForeColor = Color.White;
this.Click += equationManager.ProcessButtonInput; Click += equationManager.ProcessButtonInput;
Panel.Controls.Add(this); Panel.Controls.Add(this);
} }
} }
@@ -238,17 +237,17 @@
{ {
public EqualsButton(Point Location, Form Panel, EquationManager equationManager) public EqualsButton(Point Location, Form Panel, EquationManager equationManager)
{ {
this.Text = "="; Text = "=";
this.Font = new Font("Arial", 12); Font = new Font("Arial", 12);
this.Location = Location; this.Location = Location;
this.Width = 50; Width = 50;
this.Height = 40; Height = 40;
this.FlatStyle = FlatStyle.Flat; FlatStyle = FlatStyle.Flat;
this.BackColor = LunarOrange; BackColor = LunarOrange;
this.FlatAppearance.BorderSize = 0; FlatAppearance.BorderSize = 0;
this.FlatAppearance.BorderColor = LunarOrange; FlatAppearance.BorderColor = LunarOrange;
ForeColor = Color.White; ForeColor = Color.White;
this.Click += equationManager.EqualsButtonPressed; Click += equationManager.EqualsButtonPressed;
Panel.Controls.Add(this); Panel.Controls.Add(this);
} }
} }
@@ -257,15 +256,15 @@
{ {
public ToggleTopButton(Point Location, Form Panel) public ToggleTopButton(Point Location, Form Panel)
{ {
this.Text = "Toggle\ntop"; Text = "Toggle\ntop";
this.Font = new Font("Arial", 7); Font = new Font("Arial", 7);
this.Location = Location; this.Location = Location;
this.Width = 50; Width = 50;
this.Height = 40; Height = 40;
this.FlatStyle = FlatStyle.Flat; FlatStyle = FlatStyle.Flat;
this.BackColor = LunarGray; BackColor = LunarGray;
this.FlatAppearance.BorderSize = 0; FlatAppearance.BorderSize = 0;
this.FlatAppearance.BorderColor = LunarGray; FlatAppearance.BorderColor = LunarGray;
ForeColor = Color.White; ForeColor = Color.White;
Panel.Controls.Add(this); Panel.Controls.Add(this);
} }

View File

@@ -2,9 +2,9 @@
{ {
class EquationManager class EquationManager
{ {
private ReverseNotationSolver reverseNotationSolver = new ReverseNotationSolver(); private static readonly HashSet<string> _operators = new() { "+", "-", "*", "/", "^", "√" };
List<Token> equation = new List<Token>(); List<Token> equation = new();
string currentOperand = ""; string currentOperand = "";
Label? outerEquationLabel; Label? outerEquationLabel;
Label? outerOperandLabel; Label? outerOperandLabel;
@@ -24,11 +24,8 @@
public void ProcessButtonInput(object? sender, EventArgs e) public void ProcessButtonInput(object? sender, EventArgs e)
{ {
Button? buttonPressed = sender as Button; if (sender is Button buttonPressed)
if (buttonPressed != null)
{ {
switch (buttonPressed.Text) switch (buttonPressed.Text)
{ {
//CLEAR //CLEAR
@@ -136,26 +133,26 @@
} }
break; break;
} }
UpdateLabels(); UpdateLabels();
} }
} }
public void EqualsButtonPressed(object? sender, EventArgs e) public void EqualsButtonPressed(object? sender, EventArgs e)
{ {
Button? buttonEquals = sender as Button; if (sender is Button buttonEquals)
if (buttonEquals != null)
{ {
if (currentOperand != "") equation.Add(new Token(Token.TokenType.Operand, currentOperand)); if (currentOperand != "") equation.Add(new Token(Token.TokenType.Operand, currentOperand));
buttonEquals.Text = "#"; buttonEquals.Text = "#";
this.currentOperand = reverseNotationSolver.Solve(ref equation); currentOperand = ReversePolishNotationSolver.Solve(equation);
UpdateLabels(); UpdateLabels();
} }
} }
public void AcquireOutputLabels(ref Label equationTextBox, ref Label currentOperandTextBox) public void AcquireOutputLabels(Label equationTextBox, Label currentOperandTextBox)
{ {
this.outerEquationLabel = equationTextBox; outerEquationLabel = equationTextBox;
this.outerOperandLabel = currentOperandTextBox; outerOperandLabel = currentOperandTextBox;
} }
public string equationToString() public string equationToString()
@@ -163,7 +160,7 @@
string equationString = ""; string equationString = "";
foreach (Token token in equation) foreach (Token token in equation)
{ {
equationString += token.value.ToString() + " "; equationString += token.value + " ";
} }
return equationString; return equationString;
} }
@@ -176,11 +173,11 @@
outerOperandLabel.Text = currentOperand == "" ? "0" : currentOperand; outerOperandLabel.Text = currentOperand == "" ? "0" : currentOperand;
} }
} }
private bool IsOperator() private bool IsOperator()
{ {
if (equation.Count == 0) return false; if (equation.Count == 0) return false;
return new[] { "+", "-", "*", "/", "^", "√" }.Contains(equation.Last().value); return _operators.Contains(equation.Last().value);
} }
private bool IsOpen() private bool IsOpen()

View File

@@ -1,58 +1,55 @@
namespace Lunar.Evil_Calculator namespace Lunar.Evil_Calculator
{ {
internal class ReverseNotationSolver internal static class ReversePolishNotationSolver
{ {
List<Token> equation = new List<Token>(); private static List<Token> ToReversePolishNotation(List<Token> equation)
public string? ToReverseNotation(ref List<Token> equation)
{ {
List<Token> TEMP = new List<Token>(); List<Token> reversedEquation = new List<Token>();
Stack<Token> OPERATORS = new Stack<Token>(); Stack<Token> operators = new Stack<Token>();
foreach (Token token in equation) foreach (Token token in equation)
{ {
if (token.tokenType == Token.TokenType.Operand) if (token.tokenType == Token.TokenType.Operand)
{ {
TEMP.Add(token); reversedEquation.Add(token);
} }
if (token.tokenType == Token.TokenType.Operator) if (token.tokenType == Token.TokenType.Operator)
{ {
if (token.value == "(") if (token.value == "(")
{ {
OPERATORS.Push(token); operators.Push(token);
} }
else if (token.value == ")") else if (token.value == ")")
{ {
while (OPERATORS.Peek().value != "(") while (operators.Peek().value != "(")
{ {
TEMP.Add(OPERATORS.Pop()); reversedEquation.Add(operators.Pop());
} }
OPERATORS.Pop(); operators.Pop();
} }
else else
{ {
while (OPERATORS.Count != 0 ? OPERATORS.Peek().priority >= token.priority : false) while (operators.Count != 0 && operators.Peek().priority >= token.priority)
{ {
TEMP.Add(OPERATORS.Pop()); reversedEquation.Add(operators.Pop());
} }
OPERATORS.Push(token); operators.Push(token);
} }
} }
} }
while (OPERATORS.Count != 0) while (operators.Count != 0)
{ {
TEMP.Add(OPERATORS.Pop()); reversedEquation.Add(operators.Pop());
} }
this.equation = TEMP;
return equationToString(); return reversedEquation;
} }
public static string Solve(List<Token> originalEquation)
public string? Solve(ref List<Token> equation)
{ {
ToReverseNotation(ref equation); List<Token> reversedEquation = ToReversePolishNotation(originalEquation);
Stack<Token> stack = new Stack<Token>(); Stack<Token> stack = new Stack<Token>();
foreach (Token token in this.equation) foreach (Token token in reversedEquation)
{ {
double result, left, right; double result, left, right;
if (token.tokenType == Token.TokenType.Operand) if (token.tokenType == Token.TokenType.Operand)
@@ -98,19 +95,5 @@
} }
return stack.Pop().value; return stack.Pop().value;
} }
public string equationToString()
{
string equationString = "";
foreach (Token token in this.equation)
{
equationString += token.value.ToString() + " ";
}
return equationString;
}
} }
} }

View File

@@ -2,12 +2,16 @@
{ {
internal class Token internal class Token
{ {
public enum TokenType { Operator, Operand }; public enum TokenType
{
Operator,
Operand,
}
public TokenType tokenType; public TokenType tokenType;
public string value = ""; public string value;
public int? priority; public int? priority;
public Token() { }
public Token(TokenType tokenType, string value) public Token(TokenType tokenType, string value)
{ {