Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Calculator{
  4.     class MainClass{
  5.         public static void Main (string[] args){
  6.             while (true) {
  7.                 int choice;
  8.                 string inputNumber;
  9.                 double outputNumber;
  10.                 double sum = 0;
  11.                 bool cont = true;
  12.                 bool firstNumber = true;
  13.  
  14.                 Console.WriteLine ("Welcome to my calculator. \nWhich mathematical arithmatic operation do you want to use?");
  15.                 Console.WriteLine ("1: Addition (+) \n2: Subtraction (-) \n3: Multiplication (*) \n4: Division (/) \n5: Multiple (+, -, *, /)");
  16.                 Console.Write ("\nI want to calculate with: ");
  17.                 choice = Convert.ToInt32 (Console.ReadLine ());
  18.  
  19.                 if (choice == 1) {
  20.                     Console.Clear ();
  21.                     Console.WriteLine ("Input the numbers you want to add. End with '='\n");
  22.  
  23.                     while (cont) {
  24.                         Console.Write ("Input: \t");
  25.                         inputNumber = Console.ReadLine ();
  26.                         cont = Double.TryParse (inputNumber, out outputNumber);
  27.  
  28.                         if (cont) {
  29.                             sum = Convert.ToDouble (inputNumber) + sum;
  30.                             Console.WriteLine ("+");
  31.                         } else {
  32.                             if (inputNumber == "=") {
  33.                                 Console.WriteLine ("Sum = " + sum);
  34.                                 Console.WriteLine ("\nPress a key to continue");
  35.                                 sum = 0;
  36.                             } else {
  37.                                 Console.Clear ();
  38.                                 Console.WriteLine ("Error 150: This is not a valid number. Please contact your administrator.");
  39.                                 Environment.Exit (0);
  40.                             }
  41.                         }
  42.                     }
  43.                 } else if (choice == 2) {//Choice 1 above.
  44.                     Console.Clear ();
  45.                     Console.WriteLine ("Input the numbers you want to substract. End with '='\n");
  46.  
  47.                     while (cont) {
  48.                         Console.Write ("Input: \t");
  49.                         inputNumber = Console.ReadLine ();
  50.                         cont = Double.TryParse (inputNumber, out outputNumber);
  51.                    
  52.                         if (cont) {
  53.                             if (firstNumber) {
  54.                                 sum = Convert.ToDouble (inputNumber);
  55.                                 Console.WriteLine ("-");
  56.                                 firstNumber = false;
  57.                             } else {
  58.                                 sum = sum - Convert.ToDouble (inputNumber);
  59.                                 Console.WriteLine ("-");
  60.                             }
  61.                         } else {
  62.                             if (inputNumber == "=") {
  63.                                 Console.WriteLine ("Sum = " + sum);
  64.                                 Console.WriteLine ("\nPress a key to continue");
  65.                                 sum = 0;
  66.                             } else {
  67.                                 Console.Clear ();
  68.                                 Console.WriteLine ("Error 150: This is not a valid number. Please contact your administrator.");
  69.                                 Environment.Exit (0);
  70.                                 }
  71.                             }
  72.                     }
  73.                 }//Choice 2 above.
  74.  
  75.                 Console.ReadKey ();
  76.                 Console.Clear ();
  77.  
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement