Advertisement
brilliant_moves

EasyCalc.java

Oct 1st, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.33 KB | None | 0 0
  1. import java.util.*;     // for StringTokenizer, Scanner
  2.  
  3. public class EasyCalc {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         /**
  8.         *   Program:    EasyCalc.java
  9.         *   Creator:    Chris Clarke
  10.         *   Created:    11.01.2011
  11.         *   Purpose:    Perform simple calculation by evaluating string.
  12.         *   Notes:      The text entered can include spaces.
  13.         *           For "times", you can use "*" or "x".
  14.         */
  15.  
  16.         Scanner scan = new Scanner(System.in);
  17.         // declare variable 'theString' to be of type String
  18.         String theString = "";
  19.         // 'length' is an Integer variable
  20.         int length;
  21.         // 'temp' is a String variable
  22.         String temp;
  23.         // num1, num2 and answer are all declared as type double
  24.         double num1 = 0;
  25.         double num2 = 0;
  26.         double answer = 0;
  27.         // 'first', 'operand' and 'second' are declared to be of type String.
  28.         // 'operand' is given an arbitrary value
  29.         String first = "";
  30.         String operand = "&";
  31.         String second = "";
  32.         // declare Boolean flag 'error'
  33.         boolean error;
  34.         do {
  35.             // 'theString' is the text the user types in
  36.             System.out.print("Type your sum: ");
  37.             theString = scan.nextLine();
  38.             if (theString.equals("" )) {
  39.                 // user didn't type anything so quit the program
  40.                 System.exit(0);
  41.             }
  42.             // assign the variable 'length' the length of string 'theString'
  43.             length = theString.length();
  44.             // get last character of theString and assign it to temp
  45.             temp = theString.substring(length-1, length);
  46.             if (temp.equals("=" )) {
  47.                 // remove equals sign at the end, if there was one.
  48.                 theString = theString.substring(0, length-1);
  49.             }
  50.             StringTokenizer t = new StringTokenizer(theString, "+-*x/");
  51.             // separate theString into two numbers, separated by the operand
  52.             first = t.nextToken();
  53.             try {
  54.                 second = t.nextToken();
  55.                 error = false;
  56.             }
  57.             catch (Exception e) {
  58.                 // there wasn't a second token because it didn't find...
  59.                 // ...an operand "+, -, * or x, /"
  60.                 System.out.print("Operand error! Use +, -, * or x, and /");
  61.                 System.out.println();
  62.                 error = true;
  63.             }
  64.             // end of try/catch statement
  65.         }
  66.         while (error);
  67.         // convert strings to doubles, i.e. floating point numbers
  68.         try {
  69.             num1 = Double.parseDouble(first);
  70.             num2 = Double.parseDouble(second);
  71.         } catch (NumberFormatException e) {
  72.             System.out.print("Number error!");
  73.             System.exit(1);
  74.         }
  75.         // end of try/catch statement
  76.  
  77.         // is the first letter a minus sign?
  78.         String firstLetter;
  79.         firstLetter = theString.substring(0, 1);
  80.         // is the first number negative?
  81.         if (firstLetter.equals("-" )) {
  82.             num1 *= -1;
  83.         }
  84.         // find which operand was used
  85.         for (int i = 1; i < length; i++) {
  86.             operand = theString.substring(i, i+1);
  87.             if (operand.equals("+" ) || operand.equals("-" )
  88.              || operand.equals("*" ) || operand.equals("x" )
  89.              || operand.equals("/" )) {
  90.                 break;
  91.             }
  92.             // end if
  93.         }
  94.         // end for
  95.         // compute answer
  96.         if (operand.equals("+" )) {
  97.             answer = num1+num2;
  98.         }
  99.         else if (operand.equals("-")) {
  100.             answer = num1-num2;
  101.         }
  102.         else if (operand.equals("*") || operand.equals("x")) {
  103.             answer = num1*num2;
  104.         }
  105.         else if (operand.equals("/")) {
  106.             if (num2 == 0) {
  107.                 System.out.print("Division by zero error!");
  108.                 System.exit(0);
  109.             }
  110.             answer = num1/num2;
  111.         }
  112.         // end if
  113.  
  114.         // display original sum and the answer
  115.         System.out.println(num1+" "+operand+" "+num2+" = "+answer);
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement