Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace PrvaDomacaZadaca_Kalkulator
- {
- public class Factory
- {
- public static ICalculator CreateCalculator()
- {
- Kalkulator.initialize();
- // vratiti kalkulator
- return new Kalkulator();
- }
- }
- public class Kalkulator : ICalculator
- {
- private delegate double binaryOperationDelegate(double x, double y);
- private delegate double unaryOperationDelegate(double x);
- private static binaryOperationDelegate binaryOperation;
- private static binaryOperationDelegate lastOperation;
- private unaryOperationDelegate unaryOperation = null;
- private static string displayState;
- private static bool isPressed;
- private static double lastDigit;
- private static double memory;
- private double result;
- public static void initialize()
- {
- displayState = "0";
- isPressed = false;
- lastDigit = 0;
- memory = 0;
- binaryOperation = null;
- lastOperation = null;
- }
- Dictionary<char, binaryOperationDelegate> binaryOperations = new Dictionary<char, binaryOperationDelegate>()
- {
- { '+', new binaryOperationDelegate(Add) },
- { '-', new binaryOperationDelegate(Substract) },
- { '/', new binaryOperationDelegate(Divide) },
- { '*', new binaryOperationDelegate(Multiply) }
- };
- Dictionary<char, unaryOperationDelegate> unaryOperations = new Dictionary<char, unaryOperationDelegate>()
- {
- { 'S', new unaryOperationDelegate(Sinus) },
- { 'K', new unaryOperationDelegate(Kosinus) },
- { 'T', new unaryOperationDelegate(Tangens) },
- { 'Q', new unaryOperationDelegate(Quadrat)},
- { 'R', new unaryOperationDelegate(Root)},
- { 'I', new unaryOperationDelegate(Invers)},
- { 'P', new unaryOperationDelegate(Put)},
- { 'G', new unaryOperationDelegate(Get)},
- { 'C', new unaryOperationDelegate(Clear)},
- { 'O', new unaryOperationDelegate(OffOn)},
- { 'M', new unaryOperationDelegate(Minus)},
- { '=', new unaryOperationDelegate(Equal)}
- };
- private static double Equal(double x)
- {
- double val = 0;
- try
- {
- val = binaryOperation(Convert.ToDouble(displayState), lastDigit);
- }
- catch (NullReferenceException)
- {
- val = Convert.ToDouble(displayState);
- }
- binaryOperation = null;
- isPressed = true;
- return val;
- }
- public void Press(char inPressedDigit)
- {
- if (inPressedDigit == ',')
- {
- if (displayState.IndexOf(',') == -1 && displayState.Count() <= 10)
- {
- displayState += inPressedDigit.ToString();
- }
- }
- else {
- try
- {
- try
- {
- if (!unaryOperations.ContainsKey(inPressedDigit) && !Char.IsDigit(inPressedDigit)) {
- lastDigit = lastOperation(Convert.ToDouble(displayState), lastDigit);
- displayState = lastDigit.ToString("#########0.#########");
- lastOperation = null;
- }
- }
- catch (NullReferenceException) { }
- binaryOperation = binaryOperations[inPressedDigit];
- isPressed = true;
- lastDigit = Convert.ToDouble(displayState);
- displayState = lastDigit.ToString("#########0.#########");
- }
- catch (KeyNotFoundException)
- {
- try
- {
- unaryOperation = unaryOperations[inPressedDigit];
- result = unaryOperation(Convert.ToDouble(displayState));
- if (Double.IsInfinity(result))
- {
- displayState = "-E-";
- }
- else if (Math.Abs(result) >= Math.Pow(10, 11))
- {
- displayState = "-E-";
- }
- else if (result.ToString() == "NaN")
- {
- displayState = "-E-";
- }
- else
- {
- displayState = result.ToString("#########0.#########");
- }
- }
- catch (KeyNotFoundException)
- {
- }
- }
- if (Char.IsDigit(inPressedDigit))
- {
- lastOperation = binaryOperation;
- if (isPressed)
- {
- displayState = inPressedDigit.ToString();
- isPressed = false;
- }
- else
- {
- SetCurrentDisplayState(char.GetNumericValue(inPressedDigit).ToString());
- }
- }
- }
- }
- public string GetCurrentDisplayState()
- {
- return displayState;
- }
- public void SetCurrentDisplayState(String inPressedDigit)
- {
- if (inPressedDigit == "0" && displayState == "0")
- {
- displayState = inPressedDigit.ToString();
- }
- else if (displayState == "0")
- {
- displayState = inPressedDigit.ToString();
- }
- else if (displayState.IndexOf(',') != -1 && displayState.Count() < 11)
- {
- displayState += inPressedDigit.ToString();
- }
- else if (displayState.Count() >= 10) { }
- else
- {
- displayState += inPressedDigit.ToString();
- }
- }
- private static double Add(double x, double y)
- {
- return x + y;
- }
- private static double Substract(double x, double y)
- {
- return y - x;
- }
- private static double Divide(double x, double y)
- {
- return y / x;
- }
- private static double Multiply(double x, double y)
- {
- return x * y;
- }
- private static double Invers(double x)
- {
- return 1 / x;
- }
- private static double Root(double x)
- {
- return Math.Pow(x, 0.5);
- }
- private static double Quadrat(double x)
- {
- return Math.Pow(x, 2);
- }
- private static double Tangens(double x)
- {
- return Math.Tan(x);
- }
- private static double Kosinus(double x)
- {
- return Math.Cos(x);
- }
- private static double Sinus(double x)
- {
- return Math.Sin(x);
- }
- private static double Minus(double x)
- {
- return -x;
- }
- private static double OffOn(double x)
- {
- memory = 0;
- lastDigit = 0;
- return 0;
- }
- private static double Clear(double x)
- {
- lastOperation = null;
- return 0;
- }
- private static double Get(double x)
- {
- return memory;
- }
- private static double Put(double x)
- {
- memory = x;
- return x;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement