Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace calculator {
  5.     class Calculator {
  6.         public Calculator() { }
  7.  
  8.         public void Run() {
  9.             Queue<string> queue = new Queue<string>();
  10.  
  11.             while (true) {
  12.                 string input = Console.ReadLine();
  13.  
  14.                 if (input == "=") {
  15.                     break;
  16.                 } else {
  17.                     queue.Enqueue(input);
  18.                 }
  19.             }
  20.  
  21.             Console.WriteLine("Calculating!!!");
  22.  
  23.             double res = 0.0;
  24.             string operatorMode = "+";
  25.  
  26.             while (queue.Count != 0) {
  27.                 string current = queue.Dequeue();
  28.  
  29.                 switch (current) {
  30.                     case "+":
  31.                     case "-":
  32.                     case "*":
  33.                     case "/":
  34.                         operatorMode = current;
  35.                         break;
  36.                     default:
  37.                         res = Operate(res, current, operatorMode);
  38.                         break;
  39.                 }
  40.             }
  41.  
  42.             Console.WriteLine(res);
  43.         }
  44.  
  45.         static double Operate(double res, string current, string operatorMode) {
  46.             try {
  47.                 double num = double.Parse(current);
  48.                 switch (operatorMode) {
  49.                     case "+":
  50.                         res += num;
  51.                         break;
  52.                     case "-":
  53.                         res -= num;
  54.                         break;
  55.                     case "*":
  56.                         res *= num;
  57.                         break;
  58.                     case "/":
  59.                         res /= num;
  60.                         break;
  61.                 }
  62.             } catch (Exception ex) {
  63.                 Console.WriteLine("Unknown char: " + current);
  64.             }
  65.  
  66.             return res;
  67.         }
  68.     }
  69.  
  70.     class MainClass {
  71.         public static void Main(string[] args) {
  72.             Calculator calc = new Calculator();
  73.             calc.Run();
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement