Files
MathApp/Core/Operations.cs
T
2026-09-08 15:27:34 -05:00

101 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Core
{
public class Operations
{
public Dictionary<string, float> Variables = new Dictionary<string, float>();
private float ResolveValue(string value)
{
// Checks if the value contains any letters
if (!value.Any(char.IsLetter))
{
float.TryParse(value, out float result);
return result;
}
else
{
Variables.TryGetValue(value, out float result);
return result;
}
}
public float ExecuteCommand(ComponentType type, string val1, string val2)
{
switch (type)
{
default: return 2;
case ComponentType.EQ_ADD:
float a = ResolveValue(val1);
float b = ResolveValue(val2);
return a + b;
case ComponentType.EQ_SUB:
float aS = ResolveValue(val1);
float bS = ResolveValue(val2);
return aS - bS;
case ComponentType.EQ_MUL:
float aM = ResolveValue(val1);
float bM = ResolveValue(val2);
return aM * bM;
case ComponentType.EQ_DIV:
float aD = ResolveValue(val1);
float bD = ResolveValue(val2);
return aD / bD;
case ComponentType.QZ_VAR:
float val = 0.0f;
float.TryParse(val2, out val);
if (Variables.ContainsKey(val1))
{
Variables[val1] = val;
}
else
{
Variables.Add(val1, val);
}
return val;
}
}
public float EquParserLoop(string equ)
{
if (equ != "")
{
string[] tokens = equ.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length >= 3)
{
string command = tokens[0]; // "ADD" or "VAR"
string val1 = tokens[1]; // "X"
string val2 = tokens[2]; // "0" or "10"
switch (command)
{
default: return 0;
case "ADD":
Console.WriteLine("[CommandParser] Executed command ADD");
return ExecuteCommand(ComponentType.EQ_ADD, val1, val2);
case "SUB":
Console.WriteLine("[CommandParser] Executed command ADD");
return ExecuteCommand(ComponentType.EQ_SUB, val1, val2);
case "MUL":
Console.WriteLine("[CommandParser] Executed command ADD");
return ExecuteCommand(ComponentType.EQ_MUL, val1, val2);
case "DIV":
Console.WriteLine("[CommandParser] Executed command ADD");
return ExecuteCommand(ComponentType.EQ_DIV, val1, val2);
case "VAR":
Console.WriteLine("[CommandParser] Executed command ADD");
return ExecuteCommand(ComponentType.QZ_VAR, val1, val2);
}
}
else { return 0; }
}
else { return 0; }
}
}
}