atm959

Text-Based Calculator Made in C#.

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