简易四则运算计算器

//created by Y.DanielLiang
public class Calculator {
	public static void main(String[] args) {
    // Check number of strings passed
    if (args.length != 1) {
      System.out.println(
        "Usage: java Calculator \"operand1 operator operand2\"");
      System.exit(1);
    }

    // The result of the operation
    int result = 0;

    // The result of the operation
    String s = insertBlanks(args[0]);
    String[] tokens = s.split("\\s+"); // See Supplement III.H for regular expressions

    // Determine the operator
    switch (tokens[1].charAt(0)) {
      case '+': result = Integer.parseInt(tokens[0]) +
                         Integer.parseInt(tokens[2]);
                break;
      case '-': result = Integer.parseInt(tokens[0]) -
                         Integer.parseInt(tokens[2]);
                break;
      case '*': result = Integer.parseInt(tokens[0]) *
                         Integer.parseInt(tokens[2]);
                break;
      case '/': result = Integer.parseInt(tokens[0]) /
                         Integer.parseInt(tokens[2]);
    }

    // Display result
    System.out.println(tokens[0] + ' ' + tokens[1] + ' ' 
      + tokens[2] + " = " + result);
  }
  
  public static String insertBlanks(String s) {
    String result = "";
    
    for (int i = 0; i < s.length(); i++) {
      if (s.charAt(i) == '+' || s.charAt(i) == '-' ||
          s.charAt(i) == '*' || s.charAt(i) == '/')
        result += " " + s.charAt(i) + " ";
      else
        result += s.charAt(i);
    } 
    return result;
  }
}
//忽略空格的简易四则运算器
import java.util.*;

import javax.xml.stream.events.Characters;
public class MyCalculator {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter an expression.");
		String s1 = input.next();
		String[] tokens = new String[3];
		for ( int i = 0; i < tokens.length; ) {
			for ( int j = 0; j < s1.length() ; j ++ ) {
				if ( s1.charAt(j)>='0'&&s1.charAt(j)<='9')
					tokens[i++] = s1.charAt(j)+"";
				if ( s1.charAt(j)=='*'||s1.charAt(j)=='/'||s1.charAt(j)=='+'||s1.charAt(j)=='i')
					tokens[i++] = s1.charAt(j)+"";
			}
		}
		
		if( isRightOrder(tokens)) {
			System.out.print(tokens[0]+" "+ tokens[1]+" "+tokens[2]+ " "+"="+" "+getResult(tokens));
		}
		else if ( !isRightOrder(tokens)) {
			System.out.println("Please enter again.");
			System.exit(1);
		}
	}

	private static boolean isRightOrder(String[] tokens) {
		for ( int i = 0; i < tokens.length ; i ++ ) {
			if ( i % 2== 0 ) {
				if (tokens[i].charAt(0)<='0'||tokens[i].charAt(0)>='9')
					return false;
			}
			else {
				if ( tokens[i].charAt(0)!='+'&&tokens[i].charAt(0)!='-'&&tokens[i].charAt(0)!='*'&&tokens[i].charAt(i)!='/')
					return false;
			}
		}
		return true;
	}

	private static int getResult(String[] tokens) {
		int num1 = Integer.parseInt(tokens[0]);
		int num2 = Integer.parseInt(tokens[2]);
		switch ( tokens[1]) {
		case "+": return num1 + num2;
		case "-": return num1 - num2;
		case "*": return num1 * num2;
		case "/": return num1/num2;
		}
		return -9999;
	}

}


你可能感兴趣的:(简易四则运算计算器)