Advertisement
ZeronSix

calc4gcup

Aug 21st, 2013
1,265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System;
  2.  
  3. namespace testcalc
  4. {
  5.     public class Program
  6.     {
  7.         delegate double MathAction(double a, double b);
  8.  
  9.         static void Main() {
  10.             MathAction action;
  11.             double a = 0, b = 0, result = 0;
  12.  
  13.             do {
  14.                 Console.Clear();
  15.                 Console.WriteLine("CALC");
  16.                 Console.WriteLine("Введите первое число, потом знак, потом второе число.\nВведите exit для выхода");
  17.  
  18.                 if (!GetNumber(out a))
  19.                     continue;
  20.  
  21.                 string actionText = Console.ReadLine();
  22.  
  23.                 if (!GetNumber(out b))
  24.                     continue;
  25.  
  26.                 try {
  27.                     switch (actionText) {
  28.                         case "+":
  29.                             action = (double x, double y) => {
  30.                                 return a + b;
  31.                             };
  32.                             break;
  33.                         case "-":
  34.                             action = (double x, double y) => {
  35.                                 return a - b;
  36.                             };
  37.                             break;
  38.                         case "/":
  39.                             action = (double x, double y) => {
  40.                                 if (y == 0)
  41.                                     throw new DivideByZeroException("ERROR: деление на ноль!");
  42.                                 return x / y;
  43.                             };
  44.                             break;
  45.                         case "*":
  46.                             action = (double x, double y) => {
  47.                                 return a * b;
  48.                             };
  49.                             break;
  50.                         case "%":
  51.                             action = (double x, double y) => {
  52.                 if (y == 0)
  53.                                     throw new DivideByZeroException("ERROR: деление на ноль!");
  54.                                 return x % y;
  55.                             };
  56.                             break;
  57.                         default:
  58.                             throw new ArgumentException("ERROR: неподдерживаемый оператор!");
  59.                     }
  60.                 }
  61.                 catch (ArgumentException ex) {
  62.                     Console.WriteLine(ex.Message);
  63.                     continue;
  64.                 }
  65.  
  66.                 try {
  67.                     result = action(a, b);
  68.                 }
  69.                 catch (DivideByZeroException ex) {
  70.                     Console.WriteLine(ex.Message);
  71.                     continue;
  72.                 }
  73.                 Console.WriteLine("Результат: {0}", result);
  74.             } while (Console.ReadLine() != "exit");
  75.         }
  76.  
  77.         static bool GetNumber(out double a) {
  78.             if (!double.TryParse(Console.ReadLine(), out a)) {
  79.                 Console.WriteLine("ERROR: Ошибка ввода!");
  80.                 return false;
  81.             }
  82.             return true;
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement