Advertisement
Guest User

OOBL kalkulator

a guest
Dec 18th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace PrvaDomacaZadaca_Kalkulator
  7. {
  8.     public class Factory
  9.     {
  10.         public static ICalculator CreateCalculator()
  11.         {
  12.             Kalkulator.initialize();
  13.             // vratiti kalkulator
  14.             return new Kalkulator();
  15.         }
  16.     }
  17.  
  18.     public class Kalkulator : ICalculator
  19.     {
  20.         private delegate double binaryOperationDelegate(double x, double y);
  21.         private delegate double unaryOperationDelegate(double x);
  22.         private static binaryOperationDelegate binaryOperation;
  23.         private static binaryOperationDelegate lastOperation;
  24.         private unaryOperationDelegate unaryOperation = null;
  25.  
  26.         private static string displayState;
  27.         private static bool isPressed;
  28.         private static double lastDigit;
  29.         private static double memory;
  30.         private double result;
  31.  
  32.        
  33.         public static void initialize()
  34.         {
  35.             displayState = "0";
  36.             isPressed = false;
  37.             lastDigit = 0;
  38.             memory = 0;      
  39.             binaryOperation = null;
  40.             lastOperation = null;
  41.          }
  42.        
  43.  
  44.         Dictionary<char, binaryOperationDelegate> binaryOperations = new Dictionary<char, binaryOperationDelegate>()
  45.         {
  46.         { '+', new binaryOperationDelegate(Add) },
  47.         { '-', new binaryOperationDelegate(Substract) },
  48.         { '/', new binaryOperationDelegate(Divide) },
  49.         { '*', new binaryOperationDelegate(Multiply) }
  50.     };
  51.         Dictionary<char, unaryOperationDelegate> unaryOperations = new Dictionary<char, unaryOperationDelegate>()
  52.         {
  53.         { 'S', new unaryOperationDelegate(Sinus) },
  54.         { 'K', new unaryOperationDelegate(Kosinus) },
  55.         { 'T', new unaryOperationDelegate(Tangens) },
  56.         { 'Q', new unaryOperationDelegate(Quadrat)},
  57.         { 'R', new unaryOperationDelegate(Root)},
  58.         { 'I', new unaryOperationDelegate(Invers)},
  59.         { 'P', new unaryOperationDelegate(Put)},
  60.         { 'G', new unaryOperationDelegate(Get)},
  61.         { 'C', new unaryOperationDelegate(Clear)},
  62.         { 'O', new unaryOperationDelegate(OffOn)},
  63.         { 'M', new unaryOperationDelegate(Minus)},
  64.         { '=', new unaryOperationDelegate(Equal)}
  65.     };
  66.  
  67.         private static double Equal(double x)
  68.         {
  69.             double val = 0;
  70.             try
  71.             {
  72.                 val = binaryOperation(Convert.ToDouble(displayState), lastDigit);
  73.             }
  74.             catch (NullReferenceException)
  75.             {
  76.                 val = Convert.ToDouble(displayState);
  77.             }
  78.                  
  79.             binaryOperation = null;
  80.             isPressed = true;
  81.             return val;
  82.         }
  83.  
  84.  
  85.         public void Press(char inPressedDigit)
  86.         {
  87.             if (inPressedDigit == ',')
  88.             {
  89.                 if (displayState.IndexOf(',') == -1 && displayState.Count() <= 10)
  90.                 {
  91.                     displayState += inPressedDigit.ToString();
  92.                 }
  93.             }
  94.                 else {
  95.             try
  96.             {
  97.                 try
  98.                 {
  99.                     if (!unaryOperations.ContainsKey(inPressedDigit) && !Char.IsDigit(inPressedDigit)) {
  100.                     lastDigit = lastOperation(Convert.ToDouble(displayState), lastDigit);
  101.                     displayState = lastDigit.ToString("#########0.#########");
  102.                     lastOperation = null;
  103.                     }
  104.                 }
  105.                 catch (NullReferenceException) { }
  106.                 binaryOperation = binaryOperations[inPressedDigit];
  107.                 isPressed = true;
  108.                 lastDigit = Convert.ToDouble(displayState);
  109.                 displayState = lastDigit.ToString("#########0.#########");
  110.  
  111.             }
  112.             catch (KeyNotFoundException)
  113.             {
  114.                 try
  115.                 {
  116.                     unaryOperation = unaryOperations[inPressedDigit];
  117.                     result = unaryOperation(Convert.ToDouble(displayState));
  118.                     if (Double.IsInfinity(result))
  119.                     {
  120.                         displayState = "-E-";
  121.                     }
  122.                     else if (Math.Abs(result) >= Math.Pow(10, 11))
  123.                     {
  124.                         displayState = "-E-";
  125.                     }
  126.                     else if (result.ToString() == "NaN")
  127.                     {
  128.                         displayState = "-E-";
  129.                     }
  130.                     else
  131.                     {
  132.                         displayState = result.ToString("#########0.#########");
  133.                     }
  134.  
  135.                 }
  136.                 catch (KeyNotFoundException)
  137.                 {
  138.  
  139.                 }
  140.             }
  141.             if (Char.IsDigit(inPressedDigit))
  142.             {
  143.                 lastOperation = binaryOperation;
  144.                 if (isPressed)
  145.                 {
  146.                     displayState = inPressedDigit.ToString();
  147.                     isPressed = false;
  148.                 }
  149.                 else
  150.                 {
  151.                     SetCurrentDisplayState(char.GetNumericValue(inPressedDigit).ToString());
  152.                 }
  153.                
  154.             }    
  155.         }
  156.         }
  157.  
  158.         public string GetCurrentDisplayState()
  159.         {
  160.             return displayState;
  161.         }
  162.  
  163.         public void SetCurrentDisplayState(String inPressedDigit)
  164.         {
  165.             if (inPressedDigit == "0" && displayState == "0")
  166.             {
  167.                 displayState = inPressedDigit.ToString();
  168.             }
  169.             else if (displayState == "0")
  170.             {
  171.                 displayState = inPressedDigit.ToString();
  172.             }
  173.             else if (displayState.IndexOf(',') != -1 && displayState.Count() < 11)
  174.             {
  175.                 displayState += inPressedDigit.ToString();
  176.             }
  177.             else if (displayState.Count() >= 10)            {          }
  178.             else
  179.             {
  180.                 displayState += inPressedDigit.ToString();
  181.             }
  182.         }
  183.  
  184.         private static double Add(double x, double y)
  185.         {
  186.             return x + y;
  187.         }
  188.  
  189.         private static double Substract(double x, double y)
  190.         {
  191.             return y - x;
  192.         }
  193.  
  194.         private static double Divide(double x, double y)
  195.         {
  196.             return y / x;
  197.         }
  198.  
  199.         private static double Multiply(double x, double y)
  200.         {
  201.             return x * y;
  202.         }
  203.  
  204.         private static double Invers(double x)
  205.         {
  206.             return 1 / x;
  207.         }
  208.  
  209.         private static double Root(double x)
  210.         {
  211.             return Math.Pow(x, 0.5);
  212.         }
  213.  
  214.         private static double Quadrat(double x)
  215.         {
  216.             return Math.Pow(x, 2);
  217.         }
  218.  
  219.         private static double Tangens(double x)
  220.         {
  221.             return Math.Tan(x);
  222.         }
  223.  
  224.         private static double Kosinus(double x)
  225.         {
  226.             return Math.Cos(x);
  227.         }
  228.  
  229.         private static double Sinus(double x)
  230.         {
  231.             return Math.Sin(x);
  232.         }
  233.  
  234.         private static double Minus(double x)
  235.         {
  236.  
  237.             return -x;
  238.         }
  239.  
  240.         private static double OffOn(double x)
  241.         {
  242.             memory = 0;
  243.             lastDigit = 0;
  244.             return 0;
  245.         }
  246.  
  247.         private static double Clear(double x)
  248.         {
  249.             lastOperation = null;
  250.             return 0;
  251.         }
  252.  
  253.         private static double Get(double x)
  254.         {
  255.             return memory;
  256.         }
  257.  
  258.         private static double Put(double x)
  259.         {
  260.             memory = x;        
  261.             return x;
  262.         }
  263.  
  264.  
  265.     }
  266.  
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement