給班裡同學佈置的一道題目……雖然做的人並不算多。
阿新 • • 發佈:2019-02-10
佈置一個課外作業:各位根據自己的能力去完成不同的級別:
LV1:完成一個計算器,跟老師的題目要求一樣;
LV2:計算器的輸入只有一個字串的輸入,如123312+3212回車得到結果
LV3:完成同級別兩個以上的數字的運算,如123123+432432+543254回車得到結果
LV4:完成不同預算級別的數字運算,如123123+43243*432-43243/4324回車得到結果
隱藏級別:包括一些其他的運算子,如(4321341+432432)*43243-543/(432432-543)
目前我自己也只做到了LV4,看之後有沒有機會去改進這些東西了。
NCnum.java
package caculator;
public class NCnum {
public char f[];
public double n[];
}
NewCaculator.java
package caculator;
import java.util.Scanner;
public class NewCaculator {
public double iNum1, iNum2, oNum;
public final String[] arithmetic = { "+", "-", "*", "/" };
public final char[] num = { '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '.' };
public final char[] arith = { '+', '-', '*', '/' };
private String str = "";
private String ari = "", arip = "";
// LV2
public void solve() {
Scanner input = new Scanner(System.in);
System.out.println("請輸入您打算計算的方法,僅支援兩個數的運算:" );
String str = input.nextLine();
for (int i = 0; i < arithmetic.length; i++) {
if (str.contains(arithmetic[i])) {
iNum1 = Double.parseDouble(str.substring(0, str
.indexOf(arithmetic[i])));
iNum2 = Double.parseDouble(str.substring(str
.indexOf(arithmetic[i]) + 1, str.length()));
switch (i) {
case 0:
oNum = iNum1 + iNum2;
break;
case 1:
oNum = iNum1 - iNum2;
break;
case 2:
oNum = iNum1 * iNum2;
break;
case 3:
oNum = iNum1 / iNum2;
break;
}
System.out.println(iNum1 + arithmetic[i] + iNum2 + "=" + oNum);
break;
}
if (i == arithmetic.length - 1) {
System.out.println("運算子輸入有誤");
}
}
}
// 是否為數字
private boolean isnum(char c) {
boolean flag = true;
for (int j = 0; j < num.length; j++) {
if (num[j] == c) {
break;
}
if (j == num.length - 1) {
flag = false;
}
}
return flag;
}
// 運算
private double pcompute(double n1, double n2, char c) {
double x = 0;
switch (c) {
case '+':
x = n1 + n2;
break;
case '-':
x = n1 - n2;
break;
case '*':
x = n1 * n2;
break;
case '/':
x = n1 / n2;
break;
default:
System.out.println("方法使用錯誤");
break;
}
return x;
}
// 運算字串解析成NCnum類的物件
private NCnum analyze() {
NCnum nc = new NCnum();
Scanner input = new Scanner(System.in);
System.out.println("請輸入您打算計算的方法,僅支援多個數的運算(數1+運算子1+數2+運算子2+數3+……回車):");
this.str = input.nextLine();
for (int i = 0; i < str.length(); i++) {
if (!this.isnum(str.charAt(i))) {
this.ari += str.charAt(i) + ":";// String.valueOf(str.charAt(i));
this.arip += i + ":";
}
}
String[] A = this.ari.split(":");// 運算子號陣列
String[] B = this.arip.split(":");// 運算子號位置陣列
int[] p = new int[B.length];
for (int i = 0; i < B.length; i++) {
p[i] = Integer.parseInt(B[i]);
}
nc.f = new char[A.length];// 符號陣列
for (int i = 0; i < nc.f.length; i++) {
nc.f[i] = A[i].charAt(0);
}
nc.n = new double[A.length + 1];// 運算數值陣列
for (int i = 0; i < nc.n.length; i++) {
if (i < nc.n.length - 1 && i > 0) {
nc.n[i] = Double.parseDouble(str.substring(p[i - 1] + 1, p[i]));
} else if (i == 0) {
nc.n[i] = Double.parseDouble(str.substring(0, p[i]));
} else if (i == nc.n.length - 1) {
nc.n[i] = Double.parseDouble(str.substring(p[i - 1] + 1, str
.length()));
}
}
return nc;
}
// 遷移陣列內的double資料陣列
private void movedarr(double x[], int index) {
for (int i = index; i < x.length - 1; i++) {
x[i] = x[i + 1];
}
x[x.length - 1] = 0;
}
// 遷移陣列內的double資料陣列
private void movecarr(char x[], int index) {
for (int i = index; i < x.length - 1; i++) {
x[i] = x[i + 1];
}
x[x.length - 1] = ' ';
}
// 規則
private double opRools(NCnum nc) {
double sum = 0;
for (int i = 0; i < nc.f.length; i++) {
if (nc.f[i] == '*' || nc.f[i] == '/') {
nc.n[i] = pcompute(nc.n[i], nc.n[i + 1], nc.f[i]);
movecarr(nc.f, i);
movedarr(nc.n, i + 1);
i = -1;
}
}
for (int i = 0; i < nc.f.length; i++) {
if (nc.f[i] == '+' || nc.f[i] == '-') {
nc.n[i] = pcompute(nc.n[i], nc.n[i + 1], nc.f[i]);
movecarr(nc.f, i);
movedarr(nc.n, i + 1);
i = -1;
}
}
sum = nc.n[0];
return sum;
}
public void susolve() {
// 測試題目:342121+543254-543254*543245/543254+543245/543254
System.out.println(opRools(this.analyze()));
}
public static void main(String[] args) {
NewCaculator nc = new NewCaculator();
nc.susolve();
}
}
見笑見笑!!!