無聊做個小程序.... (計算器,不完整版) - Java - 程式設計 - 頂客論壇 ,免費遊戲,免費交友,免費空間,免費部落格,免費相簿,免費開店 - 頂客社區 dk101.com 最用心的華人社群網站
發新話題

無聊做個小程序.... (計算器,不完整版)

無聊做個小程序.... (計算器,不完整版)

沒有界面,沒有判斷是否是合理的輸入...
複製內容到剪貼板
代碼:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Compute {

//store numbers and symbols such + { ^
public static ArrayList val = new ArrayList();

public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader stdin = new BufferedReader(isr);
String input = null;
while(true) {

System.out.println("============ Small Calculator ============");
System.out.println(" Q or q for quit.");
System.out.println(" Please input: ");


try {
input = stdin.readLine();
val = new ArrayList();
if (input.trim().equals("Q") || input.trim().equals("q"))
break;
else
startCalculate(input);
} catch (IOException e) {
e.printStackTrace();
}
}

}

private static void startCalculate(String line) {
char[] a = line.toCharArray();
String var = "";

for (int i = 0; i < a.length; i++) {
switch (a[i]) {
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
if (!var.equals("") && !var.equals(null))
{
val.add(var);
var = "";
}
val.add(String.valueOf(a[i]));
break;
default:
var += a[i];
if (i == a.length-1)
val.add(var);
break;
}
}

while (val.size()>1) {
calculate();
}

System.out.println(val.get(0));
}

// Check weather these is bracket or not
// if bracket exists、then calculate the value
// within the first pair of bracket
private static void calculate() {
int leftbracket = 0;
int rightbracket = 0;
for (int i = 0; i < val.size(); i++ ){
if (val.get(i).equals("("))
leftbracket = i;
else if (val.get(i).equals(")")) {
rightbracket = i;
//remove bracket and calculate the value within them
val.remove(rightbracket);
val.remove(leftbracket);
CalPow(leftbracket,rightbracket-2);
return;
}
}

//no bracket calculation.
CalPow(0、val.size()-1);
}




//Calculate the highest PRI which is power (^) from right side.
private static void CalPow(int startPos、int endPos) {
for (int i = endPos; i >= startPos; i--) {
if (val.get(i).equals("^"))
{
//calculate the power and store it into the left value
//remove ^ and the right value
val.set(i-1、String.valueOf(Math.pow(Double.parseDouble(val.get(i-1)),Double.parseDouble(val.get(i+1)))));
val.remove(i+1);
val.remove(i--);
endPos-=2;
}
}

//No Power Symbol
CalSimple(startPos、endPos);
}


//No Bracket No Power Symbol
private static void CalSimple(int startPos、int endPos) {


double result = 0.0; //return
double temp = 0.0; //store the value which is used for low PRI calculation
String symbol = "";

for (int i = startPos; i<= endPos; i++) {
if (val.get(i).equals("+")) {

if (temp != 0.0) {
if (symbol.equals("+")) {
result += temp;
}
else if (symbol.equals("-")) {
result -= temp;
}
}
temp = 0.0;
symbol = "+";
} else if (val.get(i).equals("-")) {
if (temp != 0.0) {
if (symbol.equals("+")) {
result += temp;
}
else if (symbol.equals("-")) {
result -= temp;
}
}
temp = 0.0;
symbol = "-";
} else if (val.get(i).equals("*")) {
if (temp != 0.0) {
temp *= Double.parseDouble(val.get(++i));
} else {
result *= Double.parseDouble(val.get(++i));
}
} else if (val.get(i).equals("/")) {
if (temp != 0.0) {
temp /= Double.parseDouble(val.get(++i));
} else {
result /= Double.parseDouble(val.get(++i));
}
} else {
if (symbol.equals(""))
result = Double.parseDouble(val.get(i));
else temp = Double.parseDouble(val.get(i));
}
}

if (symbol.equals("+")) {
result += temp;
}
else if (symbol.equals("-")) {
result -= temp;
}



val.set(startPos、String.valueOf(result));

for (int i =endPos ; i> startPos; i--)
val.remove(i);

}


}
儲存為 Compute.java 檔案
進入Dos 模式,這個檔案的目錄下

javac Compute.java //編譯
java Compute //運行


打開java編程工具,把代碼複製進去,然後運行

本帖最近評分記錄

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。