crapcalc (listed) 08.02.2022 12:24:21 (145 lines) [Edit] [Raw] [View]

namespace Main { class Program { static void Main() {

        Console.WriteLine("Enter Expression:");
        string input = Console.ReadLine() + " "; // the extra space is a hacky trick to prevent the analyzer from going out of bounds
        int i = 0;
        List expressionList = new List();
        List operatorList = new List();
        List operandList = new List();

        //analyzer

        while ( i < input.Length )
        {
            Console.Write(input[i]);
            if (Char.IsDigit(input[i]))
            {
                Console.WriteLine(" - Number");
                string numberString = Convert.ToString(input[i]);
                while (Char.IsDigit(input[i + 1]) || input[i + 1] == '.' || input[i + 1] == ',')
                {
                    i++;
                    numberString += input[i];
                }
                Console.WriteLine(numberString +" - Fully Parsed Number");
                Operand newOp = new Operand(numberString);
                expressionList.Add(newOp);
                operandList.Add(newOp);
            }
            else if (input[i] == '+' || input[i] == '-' || input[i] == '/' || input[i] == '*' || input[i] == '%' || input[i] == '^') 
            {
                Console.WriteLine(" - Operator");
                Operator newOp = new Operator(input[i]);
                expressionList.Add(newOp);
                operatorList.Add(newOp);
            } 
            else
            {
                Console.WriteLine(" - ?");
            }
            i++;

        }

        //evaluator

        double output = 0;

        //exponentiation

        Operator opSearch = operatorList.Find(x => x.value == '^');

        while (opSearch != null)
        {
            int searchIndOl = operatorList.IndexOf(opSearch);
            int searchIndEl = expressionList.IndexOf(opSearch);

            if (Convert.ToString(expressionList[searchIndEl - 1].GetType()) == "Main.Operand" && Convert.ToString(expressionList[searchIndEl + 1].GetType()) == "Main.Operand")
            {
                Operand op1 = operandList.Find(x => x == expressionList[searchIndEl - 1]);
                Operand op2 = operandList.Find(x => x == expressionList[searchIndEl + 1]);
                Console.WriteLine(op1.value + " " + opSearch.value + " " + op2.value);

                op1.value = Math.Pow(op1.value, op2.value);
                expressionList.RemoveAt(searchIndEl + 1);
            }

            operatorList.RemoveAt(searchIndOl);
            expressionList.RemoveAt(searchIndEl);
            opSearch = operatorList.Find(x => x.value == '^');
        }

        //multiplication and divison

        opSearch = operatorList.Find(x => x.value == '*' || x.value == '/');

        while (opSearch != null)
        {
            int searchIndOl = operatorList.IndexOf(opSearch);
            int searchIndEl = expressionList.IndexOf(opSearch);

            if (Convert.ToString(expressionList[searchIndEl - 1].GetType()) == "Main.Operand" && Convert.ToString(expressionList[searchIndEl + 1].GetType()) == "Main.Operand")
            {
                Operand op1 = operandList.Find(x => x == expressionList[searchIndEl - 1]);
                Operand op2 = operandList.Find(x => x == expressionList[searchIndEl + 1]);
                Console.WriteLine(op1.value + " " + opSearch.value + " " + op2.value);

                op1.value = (opSearch.value == '*') ? op1.value * op2.value : op1.value / op2.value;
                expressionList.RemoveAt(searchIndEl + 1);
            }

            operatorList.RemoveAt(searchIndOl);
            expressionList.RemoveAt(searchIndEl);
            opSearch = operatorList.Find(x => x.value == '*' || x.value == '/');
        }

        //plus and minus

        opSearch = operatorList.Find(x => x.value == '+' || x.value == '-');

        while (opSearch != null)
        {
            int searchIndOl = operatorList.IndexOf(opSearch);
            int searchIndEl = expressionList.IndexOf(opSearch);

            if (Convert.ToString(expressionList[searchIndEl - 1].GetType()) == "Main.Operand" && Convert.ToString(expressionList[searchIndEl + 1].GetType()) == "Main.Operand") 
            {
                Operand op1 = operandList.Find(x => x == expressionList[searchIndEl - 1]);
                Operand op2 = operandList.Find(x => x == expressionList[searchIndEl + 1]);
                Console.WriteLine( op1.value + " " + opSearch.value + " " + op2.value);

                op1.value = (opSearch.value == '+') ? op1.value + op2.value : op1.value - op2.value;
                expressionList.RemoveAt(searchIndEl + 1);
            }

            operatorList.RemoveAt(searchIndOl);
            expressionList.RemoveAt(searchIndEl);
            opSearch = operatorList.Find(x => x.value == '+' || x.value == '-');
        }

        Console.WriteLine("Result: "+operandList[0].value);
    }
}
public abstract class ExpressionObj { }


public class Operator : ExpressionObj
{
    char type = '+';
    public char value;
    public Operator(char arg) { value = arg; }

}
public class Operand : ExpressionObj
{
    char type = '*';
    public double value;

    public Operand(string arg) { value = Convert.ToDouble(arg); }
}

}