Guest User

Untitled

a guest
Sep 30th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using CalculatorLibrary;
  2.  
  3. namespace CalculatorProgram
  4. {
  5.  
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             bool endApp = false;
  11.             Console.WriteLine("Console Calculator in C#\r");
  12.             Console.WriteLine("------------------------\n");
  13.  
  14.             Calculator calculator = new Calculator();
  15.             while (!endApp)
  16.             {
  17.                 string numInput1 = "";
  18.                 string numInput2 = "";
  19.                 double result = 0;
  20.  
  21.                 Console.Write("Type a number, and then press Enter: ");
  22.                 numInput1 = Console.ReadLine();
  23.  
  24.                 double cleanNum1 = 0;
  25.                 while (!double.TryParse(numInput1, out cleanNum1))
  26.                 {
  27.                     Console.Write("This is not valid input. Please enter an integer value: ");
  28.                     numInput1 = Console.ReadLine();
  29.                 }
  30.  
  31.                 Console.Write("Type another number, and then press Enter: ");
  32.                 numInput2 = Console.ReadLine();
  33.  
  34.                 double cleanNum2 = 0;
  35.                 while (!double.TryParse(numInput2, out cleanNum2))
  36.                 {
  37.                     Console.Write("This is not valid input. Please enter an integer value: ");
  38.                     numInput2 = Console.ReadLine();
  39.                 }
  40.  
  41.                 Console.WriteLine("Choose an operator from the following list:");
  42.                 Console.WriteLine("\ta - Add");
  43.                 Console.WriteLine("\ts - Subtract");
  44.                 Console.WriteLine("\tm - Multiply");
  45.                 Console.WriteLine("\td - Divide");
  46.                 Console.Write("Your option? ");
  47.  
  48.                 string op = Console.ReadLine();
  49.  
  50.                 try
  51.                 {
  52.                     result = calculator.DoOperation(cleanNum1, cleanNum2, op);
  53.                     if (double.IsNaN(result))
  54.                     {
  55.                         Console.WriteLine("This operation will result in a mathematical error.\n");
  56.                     }
  57.                     else
  58.                     {
  59.                         Console.WriteLine("Your result: {0:0.##}\n", result);
  60.                     }
  61.                 }
  62.                 catch (Exception e)
  63.                 {
  64.                     Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
  65.                 }
  66.  
  67.                 Console.WriteLine("------------------------\n");
  68.  
  69.                 Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
  70.                 if (Console.ReadLine() == "n") endApp = true;
  71.  
  72.                 Console.WriteLine("\n");
  73.             }
  74.  
  75.             calculator.Finish();
  76.             return;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment