将一个加,减,乘,除的算式,打印成竖式的样子 public class VerticalForm {private final static String[] Operations = {"+","-","*","/"};public static void main(String[] args) {String ss = "2020 * 34234";VerticalForm.showE
public class VerticalForm {
private final static String[] Operations = {"+","-","*","/"};
public static void main(String[] args) {
String ss = "2020 * 34234";
VerticalForm.showElement(ss);
ss = "2020 + 34234";
VerticalForm.showElement(ss);
ss = "2020 - 34234";
VerticalForm.showElement(ss);
ss = "2020 / 5";
VerticalForm.showElement(ss);
}
/**
* 显示算式
* @param equation
*/
public static void showElement(String equation) {
String operation = getOperation(equation);
if (isEmpty(operation)) {
System.out.println("未找到运算符");
return;
}
String[] nums = getNum(equation, operation);
if (null == nums || isEmpty(nums[0]) || isEmpty(nums[1])) {
System.out.println("数字未找到");
return;
}
String result = getResult(nums[0], operation, nums[1]);
int len = nums[0].length() + nums[1].length() + 1;
System.out.println(addEmptyBefor(nums[0], len));
System.out.println(addEmptyIn(operation , nums[1], len));
System.out.println(showLine(len));
System.out.println(addEmptyBefor(result, len));
}
/**
* 显示横线
* @param len
* @return
*/
private static String showLine(int len) {
StringBuffer num = new StringBuffer();
for(int i = 0;i < len; i++) {
num.append("-");
}
return num.toString();
}
/**
* 添加中间的空格
* @param operation
* @param str
* @param len
* @return
*/
private static String addEmptyIn(String operation, String str, int len) {
StringBuffer num = new StringBuffer();
num.append(operation);
for(int i = 0;i < (len - str.length() - operation.length()); i++) {
num.append(" ");
}
return num.append(str).toString();
}
/**
* 添加前面的空格
* @param str
* @param len
* @return
*/
private static String addEmptyBefor(String str, int len) {
StringBuffer num = new StringBuffer();
for(int i = 0;i < (len - str.length()); i++) {
num.append(" ");
}
return num.append(str).toString();
}
/**
* 计算结果
* @param first
* @param operation
* @param last
* @return
*/
private static String getResult(String first, String operation,String last) {
float firstNum = Float.parseFloat(first);
float lastNum = Float.parseFloat(last);
float result = 0l;
if(Operations[0].equals(operation)) {
result = firstNum + lastNum;
}else if(Operations[1].equals(operation)) {
result = firstNum - lastNum;
}else if(Operations[2].equals(operation)) {
result = firstNum * lastNum;
}else if(Operations[3].equals(operation)) {
result = firstNum / lastNum;
} else {
return null;
}
return String.valueOf(result);
}
/**
* 获取算式里的数字
* @param equation
* @param operation
* @return
*/
private static String[] getNum(String equation, String operation) {
String[] nums = new String[2];
String firstNum = "";
String lastNum = "";
firstNum = equation.substring(0, equation.indexOf(operation)).trim();
lastNum = equation.substring(equation.indexOf(operation) + 1, equation.length()).trim();
try {
float first = Float.parseFloat(firstNum);
float last = Float.parseFloat(lastNum);
nums[0] = firstNum;
nums[1] = lastNum;
return nums;
} catch(Exception e){
return null;
}
}
/**
* 获取算式里的运算符
* @param equation
* @return
*/
private static String getOperation(String equation) {
for(String op:Operations) {
if(equation.indexOf(op) >= 0) {
return op;
}
}
return null;
}
/**
* 是否为空
* @param str
* @return
*/
private static boolean isEmpty(String str) {
if(str == null || str.length() == 0) {
return true;
}
return false;
}
}
结果.png
