Advertisement
Guest User

Shell

a guest
Apr 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ShellTest
  8. {
  9.  
  10.     class ExitCommand : ShellDLL.Command
  11.     {
  12.         CalculatorShell calcShell;
  13.  
  14.         public ExitCommand(CalculatorShell calcShell) :base("exit")
  15.         {
  16.             this.calcShell = calcShell;
  17.         }
  18.  
  19.         public override bool Execute(params string[] args)
  20.         {
  21.             if (args.Length != 0)
  22.             {
  23.                 return false;
  24.             }
  25.  
  26.             calcShell.Exit(0);
  27.             return true;
  28.         }
  29.     }
  30.  
  31.     class PlusCommand : ShellDLL.Command
  32.     {
  33.         CalculatorShell calcShell;
  34.  
  35.         public PlusCommand(CalculatorShell calcShell) : base("+")
  36.         {
  37.             this.calcShell = calcShell;
  38.         }
  39.  
  40.         public override bool Execute(params string[] args)
  41.         {
  42.             if (args.Length != 0)
  43.             {
  44.                 return false;
  45.             }
  46.             double number;
  47.             if (!double.TryParse(args[0], out number))
  48.             {
  49.                 return false;
  50.             }
  51.             calcShell.Result += number;
  52.             calcShell.Format(calcShell.ToString());
  53.             return true;
  54.         }
  55.  
  56.     }
  57.  
  58.     class CalculatorShell : ShellDLL.Shell
  59.     {
  60.         public double Result { get; set; }
  61.  
  62.         public override string ToString()
  63.         {
  64.             return string.Format($"{Result:F2}");
  65.         }
  66.  
  67.         protected override void Init()
  68.         {
  69.             base.Init();
  70.             Result = 0.0;
  71.  
  72.             Format($"{Result:F2}");
  73.         }
  74.  
  75.         public CalculatorShell()
  76.         {
  77.             AddCommand(new ExitCommand(this));
  78.             AddCommand(new PlusCommand(this));
  79.         }
  80.     }
  81.  
  82.     public class Program
  83.     {
  84.         public static void Main(string[] args)
  85.         {
  86.             ShellDLL.Shell sh = new CalculatorShell();
  87.             sh.ReadEvalPrint();
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement