表达式按照指定格式输出

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

import java.util.Scanner;

public class Main3 {

	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int num1 = in.nextInt();
		String oper = in.next();
		int num2 = in.nextInt();
		StringBuilder sb = new StringBuilder();
		sb.append(int2String(num1));
		sb.append(" ");
		sb.append(oper);
		sb.append(" ");
		sb.append(int2String(num2));
		sb.append(" = ");
		sb.append(string2String(getResult(num1, num2, oper.charAt(0))));
		String expression = sb.toString();
		for (int i = 0; i < 5; i++) { // five lines
			for (int index = 0, length = expression.length(); index < length; index++) {
				char[][] g = getGByChar(expression.charAt(index));
				for( int j = 0, size = g[i].length; j < size; j++) {
					System.out.print(g[i][j]);
				}
				if(index == length -1) {
					System.out.print("\n");
				}
			}
		}
	}

	static char[][] getGByChar(char ch) {
		switch (ch) {
		case '0': return g0;
		case '1': return g1;
		case '2': return g2;
		case '3': return g3;
		case '4': return g4;
		case '5': return g5;
		case '6': return g6;
		case '7': return g7;
		case '8': return g8;
		case '9': return g9;
		case '+': return gAdd;
		case '-': return gReduce;
		case '*': return gMutil;
		case '/': return gDivode;
		case '.': return gPoint;
		case '=': return gEqual;
		}
		return gBlank;
	}
	/*
	 * 整数位与位之间补充空格
	 */
	static String int2String(int i) {
		String result = i > 0 ? "" : "0";
		while (i > 0) {
			if ("".equals(result))
				result = (i % 10) + result;
			else result = (i % 10) + " " + result;
			i /= 10;
		}
		return result;
	}
	/*
	 * 字符与字符之间补充空格
	 */
	static String string2String(String str) {
		StringBuilder sb = new StringBuilder();
		for (int index = 0, length = str.length(); index < length; index ++) {
			sb.append(str.charAt(index));
			if (index != length -1) {
				sb.append(" ");
			}
		}
		return sb.toString();
	}
	static String getResult(int num1, int num2, char oper) {
		if ('+' == oper) {
			return num1 + num2 + "";
		} else if ('-' == oper) {
			return num1 - num2 + "";
		} else if ('*' == oper) {
			return num1 * num2 + "";
		} else {
			if (num1 % num2 == 0) return num1 / num2 + "";
			return ((double)Math.round((num1 * 1.0 / num2) * 100 ) / 100) + "";
		}
	}
	static char gAdd[][] = {
			{' ',' ',' '},
			{' ','*',' '},
			{'*','*','*'},
			{' ','*',' '},
			{' ',' ',' '}
			};
	static char gReduce[][] = {
			{' ',' ',' '},
			{' ',' ',' '},
			{'*','*','*'},
			{' ',' ',' '},
			{' ',' ',' '}
			};
	static char gMutil[][] = {
			{' ',' ',' '},
			{'*',' ','*'},
			{' ','*',' '},
			{'*',' ','*'},
			{' ',' ',' '}
			};
	static char gDivode[][] = {
			{' ',' ',' '},
			{' ',' ','*'},
			{' ','*',' '},
			{'*',' ',' '},
			{' ',' ',' '}
			};
	static char gEqual[][] = { //5 x 4
			{' ',' ',' ',' '},
			{'*','*','*','*'},
			{' ',' ',' ',' '},
			{'*','*','*','*'},
			{' ',' ',' ',' '}
			};
	static char gPoint[][] = {  //5 x 2
			{' ',' '},
			{' ',' '},
			{' ',' '},
			{'*','*'},
			{'*','*'}
			};
	static char gBlank[][] = {   //5 x 1
			{' '},
			{' '},
			{' '},
			{' '},
			{' '}
			};
	static char g1[][] = {   //5 x 1
			{'*'},
			{'*'},
			{'*'},
			{'*'},
			{'*'}
			};
	static char g2[][] = {
			{'*','*','*'},
			{' ',' ','*'},
			{'*','*','*'},
			{'*',' ',' '},
			{'*','*','*'}
			};
	static char g3[][] = {
			{'*','*','*'},
			{' ',' ','*'},
			{'*','*','*'},
			{' ',' ','*'},
			{'*','*','*'}
			};
	static char g4[][] = {
			{'*',' ','*'},
			{'*',' ','*'},
			{'*','*','*'},
			{' ',' ','*'},
			{' ',' ','*'}
			};
	static char g5[][] = {
			{'*','*','*'},
			{'*',' ',' '},
			{'*','*','*'},
			{' ',' ','*'},
			{'*','*','*'}
			};
	static char g6[][] = {
			{'*','*','*'},
			{'*',' ',' '},
			{'*','*','*'},
			{'*',' ','*'},
			{'*','*','*'}
			};
	static char g7[][] = {
			{'*','*','*'},
			{' ',' ','*'},
			{' ',' ','*'},
			{' ',' ','*'},
			{' ',' ','*'}
			};
	static char g8[][] = {
			{'*','*','*'},
			{'*',' ','*'},
			{'*','*','*'},
			{'*',' ','*'},
			{'*','*','*'}
			};
	static char g9[][] = {
			{'*','*','*'},
			{'*',' ','*'},
			{'*','*','*'},
			{' ',' ','*'},
			{'*','*','*'}
			};
	static char g0[][] = {
			{'*','*','*'},
			{'*',' ','*'},
			{'*',' ','*'},
			{'*',' ','*'},
			{'*','*','*'}
			};
}

截图如图:

表达式按照指定格式输出_第1张图片

转载于:https://my.oschina.net/wtzheng/blog/882505

你可能感兴趣的:(python,java)