Simple Calculator

 avatar
unknown
plain_text
3 years ago
4.9 kB
9
Indexable
using System;
using System.Collections.Generic;

namespace Simple_Calculator
{
    class FourOperatorsCalculator
    {
        //Description:
        // - Enter an expression, example: 8.23 + 8.12
        // - Check the expression's validity
        // - Return the result
        static void Main(string[] args)
        {
            //Causes Of Invalidity:
            //- Invalid Operator Count
            //- Invalid Types
            string quote = "Please enter a simple mathematic expression.";
            char[] operators = new char[4]
            {
                    '+', '-', '*', '/'
            };
            double[] numbers;
            char operatorUsed;
            while (true)
            {
                Console.WriteLine(quote);
                string s = Console.ReadLine();
                Tools.RemoveCharsFromString(ref s, ' ');
                if (!CheckOperatorCountValidity(s, out bool[] operatorsInString, operators))
                {
                    quote = "Please try again. Problem caused by: Invalid Operator Count.\nPlease enter a simple mathematic expression.";
                    continue;
                }
                if (!CheckTypesValidity(s, operatorsInString, operators, out numbers, out operatorUsed))
                {
                    quote = "Please try again. Problem caused by: Invalid Types. \nPlease enter a simple mathematic expression.";
                    continue;
                }
                break;
            }
            double result;
            switch (operatorUsed)
            {
                case '+':
                    result = numbers[0] + numbers[1];
                    break;
                case '-':
                    result = numbers[0] - numbers[1];
                    break;
                case '*':
                    result = numbers[0] * numbers[1];
                    break;
                case '/':
                    result = numbers[0] / numbers[1];
                    break;
                default:
                    Console.WriteLine("An unknown error has occured.");
                    result = 0;
                    break;
            }
            Console.WriteLine(result);
        }

        private static bool CheckOperatorCountValidity(string s, out bool[] operatorsInString, char[] operators)
        {
            operatorsInString = Tools.CheckIfStringContainsChar(s, out int operatorCount, operators);
            return (operatorCount == 1);
        }

        private static bool CheckTypesValidity(string s, bool[] operatorsInString, char[] operators, out double[] numbers, out char operatorUsed)
        {
            operatorUsed = operators[Tools.FindFirstTrueInBoolArray(operatorsInString)];
            string[] numberStrings = s.Split(operatorUsed);
            numbers = new double[numberStrings.Length];
            for (int i = 0; i < numberStrings.Length; i++)
            {
                bool isDouble = Double.TryParse(numberStrings[i], out numbers[i]);
                if (!isDouble)
                {
                    return false;
                }
            }
            return true;
        }
    }
    static class Tools
    {
        public static void RemoveCharsFromString(ref string s, params char[] chars)
        {
            string result = "";
            foreach (char charOfString in s)
            {
                if (!chars.ArrayContains<char>(charOfString))
                {
                    result += charOfString;
                }
            }
            s = result;
        }

        private static bool ArrayContains<T>(this T[] container, T contained)
        {
            foreach (T containerMember in container)
            {
                if (EqualityComparer<T>.Default.Equals(containerMember, contained))
                {
                    return true;
                }
            }
            return false;
        }

        public static bool[] CheckIfStringContainsChar(string s, out int count, char[] chars)
        {
            count = 0;
            bool[] result = new bool[chars.Length];
            for (int i = 0; i < chars.Length; i++)
            {
                result[i] = s.Contains(chars[i]);
            }
            for (int i = 0; i < s.Length; i++)
            {
                if (chars.ArrayContains<char>(s[i]))
                {
                    count++;
                }
            }
            return result;
        }

        public static int FindFirstTrueInBoolArray(bool[] boolArray)
        {
            for (int i = 0; i < boolArray.Length; i++)
            {
                if (boolArray[i] == true) return i;
            }
            return -1;
        }

    }
}
Editor is loading...