Advertisement
Gillito

Untitled

Dec 9th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 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 ConsoleCalculator
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Calc calc = new Calc();
  14.             calc.ShowInstruction();
  15.  
  16.             do
  17.             {
  18.                 try
  19.                 {
  20.                     calc.GetChoise();
  21.                     calc.Calculate();
  22.                 }
  23.                 catch
  24.                 {
  25.                     Console.WriteLine("Некорректный ввод");
  26.                 }
  27.             }
  28.             while (calc.UserChoise != 'q');
  29.         }
  30.     }
  31.  
  32.     class Calc
  33.     {
  34.         private double first, second;
  35.         private char usrChoise;
  36.  
  37.         public char UserChoise
  38.         {
  39.             get
  40.             {
  41.                 return usrChoise;
  42.             }
  43.         }
  44.  
  45.         public void ShowInstruction()
  46.         {
  47.             Console.WriteLine("Введите требуемую операцию одиночным симовлом");
  48.             Console.WriteLine("'+' - Сложение");
  49.             Console.WriteLine("'-' - Вычитание");
  50.             Console.WriteLine("'*' - Умножение");
  51.             Console.WriteLine("'/' - Деление");
  52.             Console.WriteLine("'^' - Возведение в степень");
  53.             Console.WriteLine("'q' - Выход из программы" + Environment.NewLine);
  54.         }
  55.  
  56.         public void GetChoise()
  57.         {
  58.                 Console.Write("Введите первое число: ");
  59.                 first = Int32.Parse(Console.ReadLine());
  60.  
  61.                 Console.Write("Введите второе число: ");
  62.                 second = Int32.Parse(Console.ReadLine());
  63.  
  64.                 Console.Write("Введите требуемую операцию: ");
  65.                 usrChoise = Char.Parse(Console.ReadLine());
  66.         }
  67.  
  68.         public void Calculate()
  69.         {
  70.                 switch (usrChoise)
  71.                 {
  72.                     case '+': Console.WriteLine("{0} + {1} = {2}", first, second, (first + second));
  73.                         break;
  74.                     case '-': Console.WriteLine("{0} - {1} = {2}", first, second, (first - second));
  75.                         break;
  76.                     case '*': Console.WriteLine("{0} * {1} = {2}", first, second, (first * second));
  77.                         break;
  78.                     case '/': Console.WriteLine("{0} / {1} = {2}", first, second, (first / second));
  79.                         break;
  80.                     case '^': Console.WriteLine("{0} ^ {1} = {2}", first, second, (Math.Pow(first, second)));
  81.                         break;
  82.                     default:
  83.                         if (usrChoise != 'q')
  84.                             ShowInstruction();
  85.                         break;
  86.                 }
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement