Ed94

Eds Simple Calculator in C#

Dec 12th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.74 KB | None | 0 0
  1. //Launch.cs
  2. using System                    ;
  3. using System.Collections.Generic;
  4. using System.Linq               ;
  5. using System.Text               ;
  6. using System.Threading.Tasks    ;
  7.  
  8. namespace SimpleCalculator
  9. {
  10.     class Launch
  11.     {
  12.         private static Calculator calc;
  13.  
  14.         static void Main (string[] args)
  15.         {
  16.             calc = new Calculator();
  17.         }
  18.     }
  19. }
  20.  
  21.  
  22. //Calculator.cs
  23. using System                    ;
  24. using System.Collections.Generic;
  25. using System.Linq               ;
  26. using System.Text               ;
  27. using System.Threading.Tasks    ;
  28.  
  29. namespace SimpleCalculator
  30. {
  31.     class Calculator
  32.     {
  33.         //Variables
  34.         private static double[] userValues = new double[2];
  35.  
  36.         //Constructor
  37.         public Calculator()
  38.         {
  39.             header();
  40.             flow  ();
  41.         }
  42.         //Methods
  43.         //Program Operation
  44.         private static void header()
  45.         {
  46.             Console.Write("====================================================================================================" + "\n"
  47.                          +"Edward R. Gonzalez"                                                                                   + "\n"
  48.                          +"December 12, 2016"                                                                                    + "\n"
  49.                          +"Lynda Class C# Essentials"                                                                            + "\n"
  50.                          +"                                       Simple Calculator                                            " + "\n"
  51.                          +""                                                                                                     + "\n"
  52.                          +""                                                                                                     + "\n"
  53.                          +"            Press enter to begin."                                                                    + "\n");
  54.  
  55.             Console.ReadLine();
  56.         }
  57.  
  58.         private static void flow()
  59.         {
  60.             bool quit = false; while (quit == false)
  61.             {
  62.                 for (int counter = 0;
  63.                          counter < 2;
  64.                          counter  ++)
  65.                 {
  66.                     getValue(counter);
  67.                 }
  68.  
  69.                 printVals      ();
  70.                 decideOperation();
  71.  
  72.                 quit = giveResult();
  73.             }
  74.         }
  75.  
  76.         private static void getValue(int index)
  77.         {
  78.             Console.Write("Please enter a number: ");
  79.  
  80.             double userInput = double.Parse(Console.ReadLine());
  81.  
  82.             userValues[index] = userInput;
  83.         }
  84.  
  85.         private static void printVals()
  86.         {
  87.             Console.WriteLine("\n"
  88.                              +"Your initial values:" + "\n");
  89.  
  90.             foreach (double value in userValues)
  91.             {
  92.                 Console.WriteLine(value);
  93.             }
  94.         }
  95.  
  96.         public static void decideOperation()
  97.         {
  98.             bool complete = false; while (complete == false)
  99.             {
  100.                 Console.Write("\n"
  101.                              +"What operation would you like to complete?"                            + "\n"
  102.                              +"Possible choices: Add, Subtract, Multiply, Divide, or help if needed." + "\n"
  103.                              +": ");
  104.  
  105.                 String userInput = Console.ReadLine();
  106.  
  107.            
  108.                 if      (userInput.Equals("Add"     , StringComparison.InvariantCultureIgnoreCase))
  109.                 {
  110.                     userValues[0] = add     (userValues[0], userValues[1]);
  111.                     complete = true;
  112.                 }
  113.                 else if (userInput.Equals("Subtract", StringComparison.InvariantCultureIgnoreCase))
  114.                 {
  115.                     userValues[0] = subtract(userValues[0], userValues[1]);
  116.                     complete = true;
  117.                 }
  118.                 else if (userInput.Equals("Multiply", StringComparison.InvariantCultureIgnoreCase))
  119.                 {
  120.                     userValues[0] = multiply(userValues[0], userValues[1]);
  121.                     complete = true;
  122.                 }
  123.                 else if (userInput.Equals("divide"  , StringComparison.InvariantCultureIgnoreCase))
  124.                 {
  125.                     userValues[0] = divide  (userValues[0], userValues[1]);
  126.                     complete = true;
  127.                 }
  128.                 else if (userInput.Equals("help"    , StringComparison.InvariantCultureIgnoreCase))
  129.                 {
  130.                     Console.WriteLine("\n"
  131.                                      + "The first two values taken in the program will be put through a mathamatical operation of your choosing from the list aformentioned.");
  132.                 }
  133.                 else
  134.                 {
  135.                     Console.WriteLine("\n"
  136.                                      +"No inputs reconizable to commands known, please enter agian.");
  137.                 }
  138.             }
  139.         }
  140.  
  141.         public static bool giveResult()
  142.         {
  143.             bool quit;
  144.             Console.Write    ("\n"+"Result: ");
  145.             Console.WriteLine(userValues[0]);
  146.  
  147.             Console.Write("\n" + "If you would like to leave type quit, if not press enter to continue."+ "\n:");
  148.             String userInput = Console.ReadLine();
  149.  
  150.             if (userInput.Equals("Quit", StringComparison.CurrentCultureIgnoreCase))
  151.             {
  152.                 quit = true;
  153.             }
  154.             else
  155.             {
  156.                 quit = false;
  157.             }
  158.  
  159.             return quit;
  160.         }
  161.  
  162.         //Arithmetic functions
  163.         public static double add     (double valOne, double valTwo)
  164.         {
  165.             double result;
  166.  
  167.             result = valOne + valTwo;
  168.  
  169.             return result;
  170.         }
  171.  
  172.         public static double subtract(double valOne, double valTwo)
  173.         {
  174.             double result;
  175.  
  176.             result = valOne - valTwo;
  177.  
  178.             return result;
  179.         }
  180.  
  181.         public static double multiply(double valOne, double valTwo)
  182.         {
  183.             double result;
  184.  
  185.             result = valOne * valTwo;
  186.  
  187.             return result;
  188.         }
  189.  
  190.         public static double divide  (double valOne, double valTwo)
  191.         {
  192.             double result;
  193.  
  194.             result = valOne / valTwo;
  195.  
  196.             return result;
  197.         }
  198.     }
  199. }
  200.  
  201. //Readme.txt
  202. Simple Calculator
  203.  
  204. Author: Edward R. Gonzalez
  205. Date  : December 12, 2016
  206.  
  207. Notes: Created for lynda C# Essentials class on the simple calculator assignment to fully understand the c# language.
  208.        Went a bit over what was specified for the program to accomplish.
Add Comment
Please, Sign In to add comment