2018年2月20日 星期二

[JAVA] 簡易計算機

※ 此程式碼並非優化版本,僅供個人學習用途,若有任何問題請告知。

程式碼如下:
import java.util.Scanner;
/*
Integer.parseInt(input); 字串轉數值
Double.parseDouble(input); 字串轉浮點數
Integer.toString(input); 整數轉字串
Double.toString(input); 浮點數轉字串
substring(起始位址,結束位址); 擷取字串中某一段字元
*/
public class count { // 簡易計算機
static Scanner scn = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("請輸入數值串:(Q 離開)");
String str = scn.nextLine();
while(!str.equals("q") && !str.equals("Q")) {
String s[] = new String[str.length()];
for(int si = 0; si < s.length; si++) {s[si] = str.substring(si,si+1);}
f(s); // 循序運算
// f2(s); // 先乘除後加減(未完)
System.out.println("請輸入數值串:");
str = scn.nextLine();
}
}
public static void f(String s[]) {
int i = 0, j = 0, sum = 0;
while(i < s.length) { // 當 j(運算子位置) 不為 0 或是 最後 時
if((s[i].equals("+") || s[i].equals("-") || s[i].equals("*") || s[i].equals("/") || s[i].equals("^")) && j != 0) {
String[] n = new String[i-j-1];
String temp = "";
int u = 0;
for(int ni = j+1; ni < i; ni++) {
n[u] = s[ni];
++u;
}
for(String k : n) {temp += k;}
switch(s[j]) {
case "+":
sum += Integer.parseInt(temp);
break;
case "-":
sum -= Integer.parseInt(temp);
break;
case "*":
sum *= Integer.parseInt(temp);
break;
case "/":
sum /= Integer.parseInt(temp);
break;
case "^":
int num = sum;
if(Integer.parseInt(temp) == 0) {
sum = 1;
}
else if(Integer.parseInt(temp) > 0) {
for(int k = 1; k < Integer.parseInt(temp); k++) {
sum *= num;
}
}
else {
for(int k = -1; k > Integer.parseInt(temp); k--) {
for(int k2 = 1; k2 < Integer.parseInt(temp); k2++) {
sum *= num;
}
sum = 1 / sum;
}
}
break;
default:
break;
}
j = i;
++i;
}
else if((s[i].equals("+") || s[i].equals("-") || s[i].equals("*") || s[i].equals("/") || s[i].equals("^")) && j == 0) { // 當 j 為 0 時
String[] n = new String[i-0];
String temp = "";
for(int ni = 0; ni < n.length; ni++) {n[ni] = s[ni];}
j = i;
for(String k : n) {temp += k;}
sum += Integer.parseInt(temp);
++i;
}
else if(i == s.length-1) { // 當 j 為 最後 時
if(j != 0) {
String[] n = new String[i-j];
String temp = "";
int u = 0;
for(int ni = j+1; ni <= i; ni++) {
n[u] = s[ni];
++u;
}
for(String k : n) {temp += k;}
switch(s[j]) {
case "+":
sum += Integer.parseInt(temp);
break;
case "-":
sum -= Integer.parseInt(temp);
break;
case "*":
sum *= Integer.parseInt(temp);
break;
case "/":
sum /= Integer.parseInt(temp);
break;
case "^":
int num = sum;
if(Integer.parseInt(temp) == 0) {
sum = 1;
}
else if(Integer.parseInt(temp) > 0) {
for(int k = 1; k < Integer.parseInt(temp); k++) {
sum *= num;
}
}
else {
for(int k = -1; k > Integer.parseInt(temp); k--) {
for(int k2 = 1; k2 < Integer.parseInt(temp); k2++) {
sum *= num;
}
sum = 1 / sum;
}
}
break;
default:
break;
}
}
else {
String[] n = new String[i-j+1];
String temp = "";
int u = 0;
for(int ni = j; ni <= i; ni++) {
n[u] = s[ni];
++u;
}
for(String k : n) {temp += k;}
sum = Integer.parseInt(temp);
}
++i;
}
else {++i;}
}
System.out.println("(循序漸進)結果為: "+sum+"\n");
}
static void p(String s[] ) {
for(String k : s) {System.out.print(k);}
System.out.println("");
}
}
view raw count.java hosted with ❤ by GitHub
結果如下:
請輸入數值串:(Q 離開)
16+38*2-40
(循序漸進)結果為: 68

請輸入數值串:
4^2+33*2
(循序漸進)結果為: 98

請輸入數值串:
31/5+8*4
(循序漸進)結果為: 56

請輸入數值串:
q


沒有留言:

張貼留言