Advertisement
Batisk_AFF

C# - lr1

Oct 4th, 2022
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 KB | Source Code | 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 lr1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             float num1 = 0, num2 = 0, res = 0;
  14.             string op = "";
  15.             bool show_res;
  16.  
  17.             do
  18.             {
  19.                 show_res = true;
  20.                 do // check if data is correct
  21.                 {
  22.                     Console.Write("Enter 1st number: ");
  23.                 } while (!float.TryParse(Console.ReadLine(), out num1));
  24.  
  25.                 do // check if data is correct
  26.                 {
  27.                     Console.Write("Enter 2nd number: ");
  28.                 } while (!float.TryParse(Console.ReadLine(), out num2));
  29.  
  30.                 Console.Write("Choose operation from {/, *, -, +}: ");
  31.                 op = Console.ReadLine();
  32.                 switch (op)
  33.                 {
  34.                     case "+":
  35.                         res = num1 + num2;
  36.                         break;
  37.                     case "-":
  38.                         res = num1 - num2;
  39.                         break;
  40.                     case "*":
  41.                         res = num1 * num2;
  42.                         break;
  43.                     case "/":
  44.                         if (num2 != 0)
  45.                             res = num1 / num2;
  46.                         else
  47.                         {
  48.                             Console.WriteLine("Error: Division by zero!");
  49.                             show_res = false;
  50.                         }
  51.                         break;
  52.                     default:  // if data is incorrect
  53.                         Console.WriteLine("Non-existent option chosen, try again!");
  54.                         show_res = false;
  55.                         break;
  56.                 }
  57.  
  58.                 if (show_res)
  59.                     Console.WriteLine(String.Format("{0}{1}{2} = {3}", num1, op, num2, res));
  60.  
  61.                 Console.Write("\nEnter 0 to exit or nothing to continue: ");
  62.             } while (Console.ReadLine() != "0");
  63.         }
  64.     }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement