Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Calculator
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- double x;
- double y;
- double res = 0;
- Console.BackgroundColor = ConsoleColor.Black;
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- Console.WriteLine("Please enter a number: \n");
- x = double.Parse(Console.ReadLine());
- for (;;)
- {
- Console.WriteLine("\n Please enter another number: \n");
- y = double.Parse(Console.ReadLine());
- Console.WriteLine();
- Console.WriteLine("\nChoose your operation method (+,-,*,/)\n");
- Boolean eingabe = true;
- switch (Console.ReadLine())
- {
- case "+":
- {
- double sum = Add(x, y);
- Console.WriteLine("\nThe sum of the operation is: " + sum + "\n");
- res = sum;
- break;
- }
- case "-":
- {
- double dif = Sub(x, y);
- Console.WriteLine("\nThe difference of the operation is: " + dif + "\n");
- res = dif;
- break;
- }
- case "*":
- {
- double mult = Mult(x, y);
- Console.WriteLine("\nThe product of the operation is: " + mult + "\n");
- res = mult;
- break;
- }
- case "/":
- {
- double div = Div(x, y);
- Console.WriteLine("\nThe quotient of the operation is: " + div + "\n");
- res = div;
- break;
- }
- default:
- {
- Console.WriteLine("\nYou did not enter a valid operator!\n");
- break;
- }
- }
- Console.WriteLine("\nDo you want to use the previous result in the next operation? Y/N\n");
- while (eingabe)
- {
- string prev = Console.ReadLine();
- if (prev == "Y" || prev == "N")
- {
- if (prev == "Y")
- {
- eingabe = false;
- x = res;
- }
- else
- {
- eingabe = false;
- Console.WriteLine("Please enter a number: \n");
- x = double.Parse(Console.ReadLine());
- }
- }
- }
- }
- } catch (Exception ex)
- {
- Console.WriteLine("\n" + ex.Message + "\n");
- }
- }
- static double Add(double x, double y)
- {
- return x + y;
- }
- static double Sub(double x, double y)
- {
- return x - y;
- }
- static double Mult(double x, double y)
- {
- return x * y;
- }
- static double Div(double x, double y)
- {
- if (y == 0)
- {
- throw new Exception("\nThe quotient of the operation is: infinity (Cannot divide by 0!)\n");
- }
- else
- {
- return x / y;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement