atm959

Text-Based Calculator Made in C#.

Feb 11th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Simple_Calculator
  4. {
  5.     class Calc
  6.     {
  7.  
  8.         public static String num1String;
  9.         public static String num2String;
  10.         public static double num1;
  11.         public static double num2;
  12.         public static String oper;
  13.         public static String response;
  14.  
  15.         public static void Main (string[] args)
  16.         {
  17.  
  18.             Console.WriteLine ("Simple Text-Based Calculator by Aaron Murphy");
  19.             Console.Write ("First Number>> ");
  20.             num1String = Console.ReadLine ();
  21.  
  22.             try {
  23.                
  24.                 num1 = Convert.ToInt32 (num1String);
  25.  
  26.             } catch(Exception) {
  27.  
  28.                 Console.WriteLine ("You did not enter a number. Restarting...");
  29.                 Console.WriteLine ("");
  30.                 Main (null);
  31.  
  32.             }
  33.             Console.WriteLine ("");
  34.  
  35.             Console.Write ("Second Number>> ");
  36.             num2String = Console.ReadLine ();
  37.  
  38.             try {
  39.                
  40.                 num2 = Convert.ToInt32 (num2String);
  41.                 Console.WriteLine ("");
  42.  
  43.             } catch(Exception) {
  44.                    
  45.                 Console.WriteLine ("You did not enter a number. Restarting...");
  46.                 Console.WriteLine ("");
  47.                 Main (null);
  48.  
  49.             }
  50.  
  51.             Console.Write ("Operator(+, -, *, /, sin, cos, %, log, exp, and tan)>> ");
  52.             oper = Console.ReadLine ();
  53.             Console.WriteLine ("");
  54.  
  55.             if (oper == "+") {
  56.  
  57.                 Console.WriteLine (num1 + " + " + num2 + " = " + (num1 + num2));
  58.  
  59.             } else if (oper == "-") {
  60.  
  61.                 Console.WriteLine (num1 + " - " + num2 + " = " + (num1 - num2));
  62.  
  63.             } else if (oper == "*") {
  64.  
  65.                 Console.WriteLine (num1 + " * " + num2 + " = " + (num1 * num2));
  66.  
  67.             } else if (oper == "/") {
  68.  
  69.                 Console.WriteLine (num1 + " / " + num2 + " = " + (num1 / num2));
  70.  
  71.             } else if (oper == "sin") {
  72.  
  73.                 Console.WriteLine ("Sin(" + num1 + ") = " + Math.Sin (num1));
  74.  
  75.             } else if (oper == "cos") {
  76.  
  77.                 Console.WriteLine ("Cos(" + num1 + ") = " + Math.Cos(num1));
  78.  
  79.             } else if (oper == "%") {
  80.  
  81.  
  82.  
  83.             } else if (oper == "log") {
  84.  
  85.  
  86.  
  87.             } else if (oper == "exp") {
  88.  
  89.  
  90.  
  91.             } else if (oper == "tan") {
  92.  
  93.  
  94.  
  95.             }
  96.  
  97.             Console.WriteLine ("");
  98.             Console.Write ("Do you want to perform a new calculation(Y/N)?>> ");
  99.             response = Console.ReadLine ();
  100.  
  101.             if (response == "Y" || response == "y") { //It works if the user types "Y" or "y".
  102.  
  103.                 Console.WriteLine ("");
  104.                 Main (null);
  105.  
  106.             }
  107.  
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment