Ejemplo n.º 1
0
Archivo: Div.cs Proyecto: xfree123/cal2
        /// <summary>
        /// Execute divison operator and put result into operands stack
        /// </summary>
        /// <param name="operands">stack with operands</param>
        /// <param name="mode">mode to operate in</param>
        /// <returns>true/false if execution went well</returns>
        public override bool Execute(Stack <Operand> operands, CalculatorMode mode)
        {
            try
            {
                // b / a
                Operand a = operands.Pop();
                Operand b = operands.Pop();
                Operand c; // result

                // if we are in programming mode and both operands are integers
                // do a integer division
                if (mode == CalculatorMode.Programming &&
                    a.Value == (Int32)a.Value &&
                    b.Value == (Int32)b.Value)
                {
                    c = new Operand((Int32)b.Value / (Int32)a.Value);
                }
                else
                {
                    c = new Operand(b.Value / a.Value);
                }
                operands.Push(c);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Execute divison operator and put result into operands stack
        /// </summary>
        /// <param name="operands">stack with operands</param>
        /// <param name="mode">mode to operate in</param>
        /// <returns>true/false if execution went well</returns>
        public override bool Execute(Stack<Operand> operands, CalculatorMode mode)
        {
            try
            {
                // b / a
                Operand a = operands.Pop();
                Operand b = operands.Pop();
                Operand c; // result

                // if we are in programming mode and both operands are integers
                // do a integer division
                if (mode == CalculatorMode.Programming &&
                    a.Value == (Int32)a.Value &&
                    b.Value == (Int32)b.Value)
                {
                    c = new Operand((Int32)b.Value / (Int32)a.Value);
                }
                else
                {
                    c = new Operand(b.Value / a.Value);
                }
                operands.Push(c);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handle input of commands
        /// </summary>
        /// <param name="sender">calling object</param>
        /// <param name="e">event arguments</param>
        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
            {
                bool handled = false;
                switch (txtInput.Text)
                {
                // Set to mathematics mode
                case "m":
                    this.Mode = CalculatorMode.Mathematics;
                    handled   = true;
                    break;

                // Set to programming mode
                case "p":
                    this.Mode = CalculatorMode.Programming;
                    handled   = true;
                    break;

                // Enter "save/store" mode
                default:
                    txtInput.Enabled = false;
                    txtStore.Visible = true;
                    txtStore.Focus();
                    break;
                }
                if (handled)
                {
                    txtInput.Clear();
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handle input of commands
        /// </summary>
        /// <param name="sender">calling object</param>
        /// <param name="e">event arguments</param>
        private void txtInput_KeyDown(object sender, ConsoleKeyInfo e)
        {
            if (e.Key == ConsoleKey.Enter)
            {
                bool handled = false;
                switch (emulator.txtInput)
                {
                // Set to mathematics mode
                case "m":
                    this.Mode = CalculatorMode.Mathematics;
                    handled   = true;
                    break;

                // Set to programming mode
                case "p":
                    this.Mode = CalculatorMode.Programming;
                    handled   = true;
                    break;

                // Enter "save/store" mode
                default:
                    break;
                }
                if (handled)
                {
                    emulator.txtInput = "";
                }
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Execute right parenthesis operator
 /// </summary>
 /// <param name="operands">stack with operands</param>
 /// <param name="mode">mode to operate in</param>
 /// <returns>true/false if execution went well (always true)</returns>
 public override bool Execute(Stack <Operand> operands, CalculatorMode mode)
 {
     try
     {
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Execute right parenthesis operator
 /// </summary>
 /// <param name="operands">stack with operands</param>
 /// <param name="mode">mode to operate in</param>
 /// <returns>true/false if execution went well (always true)</returns>
 public override bool Execute(Stack<Operand> operands, CalculatorMode mode)
 {
     try
     {
         return true;
     }
     catch (Exception)
     {
         return false;
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Execute plus operator and push answer onto operands stack
 /// </summary>
 /// <param name="operands">stack with operands</param>
 /// <param name="mode">mode to operate in</param>
 /// <returns>true/false if execution went well</returns>
 public override bool Execute(Stack <Operand> operands, CalculatorMode mode)
 {
     try
     {
         Operand a = operands.Pop();
         Operand b = operands.Pop();
         Operand c = new Operand(a.Value + b.Value);
         operands.Push(c);
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Execute not operator and push answer onto operands stack
 /// </summary>
 /// <param name="operands">stack with operands</param>
 /// <param name="mode">mode to operate in</param>
 /// <returns>true/false if execution went well</returns>
 public override bool Execute(Stack<Operand> operands, CalculatorMode mode)
 {
     try
     {
         // ~a
         Operand a = operands.Pop();
         Operand b = new Operand(~(Int64)a.Value);
         operands.Push(b);
         return true;
     }
     catch (Exception)
     {
         return false;
     }
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Execute not operator and push answer onto operands stack
 /// </summary>
 /// <param name="operands">stack with operands</param>
 /// <param name="mode">mode to operate in</param>
 /// <returns>true/false if execution went well</returns>
 public override bool Execute(Stack <Operand> operands, CalculatorMode mode)
 {
     try
     {
         // ~a
         Operand a = operands.Pop();
         Operand b = new Operand(~(Int64)a.Value);
         operands.Push(b);
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Execute plus operator and push answer onto operands stack
 /// </summary>
 /// <param name="operands">stack with operands</param>
 /// <param name="mode">mode to operate in</param>
 /// <returns>true/false if execution went well</returns>
 public override bool Execute(Stack<Operand> operands, CalculatorMode mode)
 {
     try
     {
         Operand a = operands.Pop();
         Operand b = operands.Pop();
         Operand c = new Operand(a.Value + b.Value);
         operands.Push(c);
         return true;
     }
     catch (Exception)
     {
         return false;
     }
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Execute right shift operator and push answer onto operands stack
        /// </summary>
        /// <param name="operands">stac with operandsk</param>
        /// <param name="mode">mode to operate in</param>
        /// <returns>true/false if execution went well</returns>
        public override bool Execute(Stack <Operand> operands, CalculatorMode mode)
        {
            try
            {
                // b % a
                Operand a = operands.Pop();
                Operand b = operands.Pop();
                if (!a.IsInteger || !b.IsInteger)
                {
                    return(false);
                }

                Operand c = new Operand((Int32)b.Value >> (Int32)a.Value);
                operands.Push(c);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// add a constant value to the variables dictionary
        /// </summary>
        /// <param name="name">variable name</param>
        /// <param name="calculation">variable calculation</param>
        /// <param name="mode">calculator mode for calculation</param>
        /// <returns>True if variable was added, false if it already exists</returns>
        public bool AddOrUpdateVariable(string name, string calculation, CalculatorMode mode)
        {
            try
            {
                if (!this.VariableDict.ContainsKey(name))
                {
                    this.VariableDict.Add(name, new Variable());
                }

                Variable var;
                if (this.VariableDict.TryGetValue(name, out var))
                {
                    var.Calculation = calculation;
                    var.Mode        = mode;
                    return(true);
                }
            }
            catch
            {
                // do nothing
            }
            return(false);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Gets the mode text by calculator mode.
        /// </summary>
        /// <returns>The mode text by calculator mode.</returns>
        /// <param name="aCalculatorMode">A calculator mode.</param>
        public static string GetModeTextByCalculatorMode(CalculatorMode aCalculatorMode)
        {
            string modeText_string;

            switch (aCalculatorMode)
            {
            case CalculatorMode.LinearEquations:
                modeText_string = "LIN";
                break;

            case CalculatorMode.Scientific:
                modeText_string = "SCI";
                break;

            default:
                                #pragma warning disable 0162
                throw new SwitchStatementException(aCalculatorMode.ToString());
                break;
                                #pragma warning restore 0162
            }

            return(modeText_string);
        }
Ejemplo n.º 14
0
        public Calculator(CalculatorMode mode)
        {
            this.mode = mode;

            operators = new Operator[]
            {
                new Operator('^', 4, Operator.Associativity.Right, (args) =>
                {
                    return(System.Math.Pow(args[0], args[1]));
                }),
                new Operator('%', 3, Operator.Associativity.Left, (args) =>
                {
                    return(args[0] % args[1]);
                }),
                new Operator('*', 3, Operator.Associativity.Left, (args) =>
                {
                    return(args[0] * args[1]);
                }),
                new Operator('/', 3, Operator.Associativity.Left, (args) =>
                {
                    return(args[0] / args[1]);
                }),
                new Operator('+', 2, Operator.Associativity.Left, (args) =>
                {
                    return(args[0] + args[1]);
                }),
                new Operator('-', 2, Operator.Associativity.Left, (args) =>
                {
                    return(args[0] - args[1]);
                })
            };

            functions = new Function[]
            {
                new Function("abs", 1, (args) =>
                {
                    return(System.Math.Abs(args[0]));
                }),
                new Function("pi", 0, (args) =>
                {
                    return(System.Math.PI);
                }),
                new Function("e", 0, (args) =>
                {
                    return(System.Math.E);
                }),
                // the inverse trig are checked first,
                // otherwise cos^-1 is found as (function)cos (operator)& (number)-1
                // when (function)cos^-1 is expected
                new Function("cos^-1", 1, (args) =>
                {
                    return(System.Math.Acos(args[0]));
                }),
                new Function("sin^-1", 1, (args) =>
                {
                    return(System.Math.Asin(args[0]));
                }),
                new Function("tan^-1", 1, (args) =>
                {
                    return(System.Math.Atan(args[0]));
                }),
                new Function("cot^-1", 1, (args) =>
                {
                    return(1 / System.Math.Atan(args[0]));
                }),
                new Function("sec^-1", 1, (args) =>
                {
                    return(1 / System.Math.Acos(args[0]));
                }),
                new Function("csc^-1", 1, (args) =>
                {
                    return(1 / System.Math.Asin(args[0]));
                }),
                new Function("cos", 1, (args) =>
                {
                    return(System.Math.Cos(args[0]));
                }),
                new Function("sin", 1, (args) =>
                {
                    return(System.Math.Sin(args[0]));
                }),
                new Function("tan", 1, (args) =>
                {
                    return(System.Math.Tan(args[0]));
                }),
                new Function("sec", 1, (args) =>
                {
                    return(1 / System.Math.Cos(args[0]));
                }),
                new Function("csc", 1, (args) =>
                {
                    return(1 / System.Math.Sin(args[0]));
                }),
                new Function("cot", 1, (args) =>
                {
                    return(1 / System.Math.Tan(args[0]));
                }),
                new Function("sqrt", 1, (args) =>
                {
                    return(System.Math.Sqrt(args[0]));
                }),
                new Function("min", 2, (args) =>
                {
                    return(System.Math.Min(args[0], args[1]));
                }),
                new Function("max", 2, (args) =>
                {
                    return(System.Math.Max(args[0], args[1]));
                }),
                new Function("ceil", 1, (args) =>
                {
                    return(System.Math.Ceiling(args[0]));
                }),
                new Function("jeff", 0, (args) =>
                {
                    return(6969);
                }),
                new Function("floor", 1, (args) =>
                {
                    return(System.Math.Floor(args[0]));
                }),
                new Function("round", 1, (args) =>
                {
                    return(System.Math.Round(args[0]));
                })/*,
                   * new Function("june", 0, (args) =>
                   * {
                   * return 80;
                   * }),
                   * new Function("renzo", 0, (args) =>
                   * {
                   * return 9;
                   * })*/
            };
        }
Ejemplo n.º 15
0
 public MemoryMember(CalculatorMode mode, string mathExeprission)
 {
     Mode            = mode;
     MathExeprission = mathExeprission;
     Result          = MathExeprission.GetNaitveMathExeprission().Calculate();
 }
Ejemplo n.º 16
0
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            string lItemName = (e.Source as MenuItem).Header.ToString();

            switch (lItemName)
            {
            case "_Standard":
                CurrentMode = CalculatorMode.Standard;
                break;

            case "_Scientific":
                CurrentMode = CalculatorMode.Scientific;
                break;

            case "_Binary":
                CurrentMode = CalculatorMode.Binary;
                break;

            case "_Graphs":
                CurrentMode = CalculatorMode.Graphs;
                break;

            case "_Calendar":
                CurrentMode = CalculatorMode.Calendar;
                break;

            case "_Area":
                CurrentMode = CalculatorMode.Area;
                break;

            case "_Electronic":
                CurrentMode = CalculatorMode.Electronic;
                break;

            case "_Energy":
                CurrentMode = CalculatorMode.Energy;
                break;

            case "_Flow":
                CurrentMode = CalculatorMode.Flow;
                break;

            case "_Force":
                CurrentMode = CalculatorMode.Force;
                break;

            case "_Length":
                CurrentMode = CalculatorMode.Length;
                break;

            case "_Power":
                CurrentMode = CalculatorMode.Power;
                break;

            case "_Pressure":
                CurrentMode = CalculatorMode.Pressure;
                break;

            case "_Speed":
                CurrentMode = CalculatorMode.Speed;
                break;

            case "_Temperature":
                CurrentMode = CalculatorMode.Temperature;
                break;

            case "_Time":
                CurrentMode = CalculatorMode.Time;
                break;

            case "_Volume":
                CurrentMode = CalculatorMode.Volume;
                break;

            case "_Weight":
                CurrentMode = CalculatorMode.Weight;
                break;

            case "_Data":
                CurrentMode = CalculatorMode.Data;
                break;

            case "_Angle":
                CurrentMode = CalculatorMode.Angle;
                break;

            case "_Currency":
                CurrentMode = CalculatorMode.Currency;
                break;

            case "_Options":
                CurrentMode = CalculatorMode.Options;
                break;

            case "_Exit":
                Application.Current.Shutdown();
                break;

            default:
                CurrentMode = CalculatorMode.Standard;
                break;
            }

            UIMenuSelected.Content = CurrentMode.ToString();
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Execute operator
 /// </summary>
 /// <param name="operands">stack of operands to execute on</param>
 /// <param name="mode">mode to operate in</param>
 /// <returns>true / false if succeeded</returns>
 public abstract bool Execute(Stack<Operand> operands, CalculatorMode mode);
Ejemplo n.º 18
0
 /// <summary>
 /// Handle input of commands
 /// </summary>
 /// <param name="sender">calling object</param>
 /// <param name="e">event arguments</param>
 private void txtInput_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
     {
         bool handled = false;
         switch (txtInput.Text)
         {
             // Set to mathematics mode
             case "m":
                 this.Mode = CalculatorMode.Mathematics;
                 handled = true;
                 break;
             // Set to programming mode
             case "p":
                 this.Mode = CalculatorMode.Programming;
                 handled = true;
                 break;
         }
         if (handled)
             txtInput.Clear();
     }
 }
Ejemplo n.º 19
0
 public MenuSide_Control()
 {
     InitializeComponent();
     CurrentMode = CalculatorMode.Standard;
 }
Ejemplo n.º 20
0
 /// <summary>
 /// _ons the calculator mode changed signal.
 /// </summary>
 /// <param name="aDisplayText_string">A display text_string.</param>
 private void _onCalculatorModeChangedSignal(CalculatorMode aCalculatorMode)
 {
     viewConcrete.setModeText(Constants.MODE_TEXT_PREFIX + Constants.GetModeTextByCalculatorMode(aCalculatorMode));
 }
Ejemplo n.º 21
0
 public void SetMode(CalculatorMode mode)
 {
     this.mode = mode;
 }
		/// <summary>
		/// _ons the calculator mode changed signal.
		/// </summary>
		/// <param name="aDisplayText_string">A display text_string.</param>
		private void _onCalculatorModeChangedSignal (CalculatorMode aCalculatorMode)
		{
			
			viewConcrete.setModeText (Constants.MODE_TEXT_PREFIX +  Constants.GetModeTextByCalculatorMode(aCalculatorMode));
			
		}
Ejemplo n.º 23
0
        /// <summary>
        /// Execute function operator and push answer onto operands stack
        /// </summary>
        /// <param name="operands">stack of operands</param>
        /// <param name="mode">mode to operate in</param>
        /// <returns>true/false if execution went well</returns>
        public override bool Execute(Stack <Operand> operands, CalculatorMode mode)
        {
            try
            {
                if (this.Type == FunctionTypes.UNKNOWN)
                {
                    return(false);
                }

                Operand argument = operands.Pop();
                Operand answer   = null;

                switch (this.Type)
                {
                case FunctionTypes.ABS:
                    answer = new Operand(Math.Abs(argument.Value));
                    break;

                case FunctionTypes.FLOOR:
                    answer = new Operand(Math.Floor(argument.Value));
                    break;

                case FunctionTypes.CEILING:
                    answer = new Operand(Math.Ceiling(argument.Value));
                    break;

                case FunctionTypes.ROUND:
                    answer = new Operand(Math.Floor(argument.Value + 0.5));
                    break;

                case FunctionTypes.LN:
                    answer = new Operand(Math.Log(argument.Value, 2.718281828));
                    break;

                case FunctionTypes.LOG:
                    answer = new Operand(Math.Log10(argument.Value));
                    break;

                case FunctionTypes.SIN:
                    answer = new Operand(Math.Sin(argument.Value));
                    break;

                case FunctionTypes.TAN:
                    answer = new Operand(Math.Tan(argument.Value));
                    break;

                case FunctionTypes.COS:
                    answer = new Operand(Math.Cos(argument.Value));
                    break;

                case FunctionTypes.SQRT:
                    answer = new Operand(Math.Sqrt(argument.Value));
                    break;
                }

                if (answer != null)
                {
                    operands.Push(answer);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 24
0
 /// <summary>
 /// Constructor, init form
 /// </summary>
 public MainWindow()
 {
     ClearLabels();
     this.Mode = CalculatorMode.Mathematics;
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Constructor, initalize expression
 /// </summary>
 /// <param name="input">expression to evaluate with calculator</param>
 public Calculator(string input)
 {
     _input = input;
     Mode   = CalculatorMode.Mathematics;
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Changes the mode using the navigation menu in the UI
        /// </summary>
        /// <param name="mode">The mode to be changed to</param>
        public void ChangeCalculatorMode(CalculatorMode mode)
        {
            string modeAccessibilityId;

            switch (mode)
            {
            case CalculatorMode.StandardCalculator:
                modeAccessibilityId = "Standard";
                break;

            case CalculatorMode.ScientificCalculator:
                modeAccessibilityId = "Scientific";
                break;

            case CalculatorMode.ProgrammerCalculator:
                modeAccessibilityId = "Programmer";
                break;

            case CalculatorMode.DateCalculator:
                modeAccessibilityId = "Date";
                break;

            case CalculatorMode.Currency:
                modeAccessibilityId = "Currency";
                break;

            case CalculatorMode.Volume:
                modeAccessibilityId = "Volume";
                break;

            case CalculatorMode.Length:
                modeAccessibilityId = "Length";
                break;

            case CalculatorMode.Weight:
                modeAccessibilityId = "Weight";
                break;

            case CalculatorMode.Temperature:
                modeAccessibilityId = "Temperature";
                break;

            case CalculatorMode.Energy:
                modeAccessibilityId = "Energy";
                break;

            case CalculatorMode.Area:
                modeAccessibilityId = "Area";
                break;

            case CalculatorMode.Speed:
                modeAccessibilityId = "Speed";
                break;

            case CalculatorMode.Time:
                modeAccessibilityId = "Time";
                break;

            case CalculatorMode.Power:
                modeAccessibilityId = "Power";
                break;

            case CalculatorMode.Data:
                modeAccessibilityId = "Data";
                break;

            case CalculatorMode.Pressure:
                modeAccessibilityId = "Pressure";
                break;

            case CalculatorMode.Angle:
                modeAccessibilityId = "Angle";
                break;

            default:
                throw (new ArgumentException("The mode is not valid"));
            }

            this.NavigationMenuButton.Click();
            this.NavigationMenuPane.WaitForDisplayed();
            this.session.TryFindElementByAccessibilityId(modeAccessibilityId).Click();
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Constructor, init form
 /// </summary>
 public MainWindow()
 {
     InitializeComponent();
     ClearLabels();
     this.Mode = CalculatorMode.Mathematics;
 }
Ejemplo n.º 28
0
 public MemoryMember(CalculatorMode mode, string mathExeprission, decimal result)
 {
     Mode            = mode;
     MathExeprission = mathExeprission;
     Result          = result;
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Execute operator
 /// </summary>
 /// <param name="operands">stack of operands to execute on</param>
 /// <param name="mode">mode to operate in</param>
 /// <returns>true / false if succeeded</returns>
 public abstract bool Execute(Stack <Operand> operands, CalculatorMode mode);
Ejemplo n.º 30
0
		/// <summary>
		/// Gets the mode text by calculator mode.
		/// </summary>
		/// <returns>The mode text by calculator mode.</returns>
		/// <param name="aCalculatorMode">A calculator mode.</param>
		public static string GetModeTextByCalculatorMode (CalculatorMode aCalculatorMode)
		{
			string modeText_string;

			switch (aCalculatorMode){
			case CalculatorMode.LinearEquations:
				modeText_string = "LIN";
				break;
			case CalculatorMode.Scientific:
				modeText_string = "SCI";
				break;
			default:
				#pragma warning disable 0162
				throw new SwitchStatementException(aCalculatorMode.ToString());
				break;
				#pragma warning restore 0162
			}

			return modeText_string;
		}