Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace calculator {
- class Calculator {
- public Calculator() { }
- public void Run() {
- Queue<string> queue = new Queue<string>();
- while (true) {
- string input = Console.ReadLine();
- if (input == "=") {
- break;
- } else {
- queue.Enqueue(input);
- }
- }
- Console.WriteLine("Calculating!!!");
- double res = 0.0;
- string operatorMode = "+";
- while (queue.Count != 0) {
- string current = queue.Dequeue();
- switch (current) {
- case "+":
- case "-":
- case "*":
- case "/":
- operatorMode = current;
- break;
- default:
- res = Operate(res, current, operatorMode);
- break;
- }
- }
- Console.WriteLine(res);
- }
- static double Operate(double res, string current, string operatorMode) {
- try {
- double num = double.Parse(current);
- switch (operatorMode) {
- case "+":
- res += num;
- break;
- case "-":
- res -= num;
- break;
- case "*":
- res *= num;
- break;
- case "/":
- res /= num;
- break;
- }
- } catch (Exception ex) {
- Console.WriteLine("Unknown char: " + current);
- }
- return res;
- }
- }
- class MainClass {
- public static void Main(string[] args) {
- Calculator calc = new Calculator();
- calc.Run();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement