?? datastringdeal.java
字號:
/**
* @(#)DataStringDeal.java
*
*
* @author
* @version 1.00 2007/5/12
*/
//表達式字符串處理類
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class DataStringDeal{
private String[] string;
private int count;
public DataStringDeal() {
}
public DataStringDeal(String[] string,int count){
this.string = string;
this.count = count;
}
public String doOperation(){
int leftNum = 0;
int rightNum = 0;
//以下代碼是用來檢驗表達式的合法性
//以及檢驗該表達式是否具有歧義
//對括號合法性的檢測
for (int i=0;i<=count;i++){
if (string[i]=="(")
leftNum++;
if (string[i]==")")
rightNum++;
if (leftNum<rightNum)
return "The expression is not valid!";
}
if (leftNum!=rightNum)
return "The expression is not valid!";
for (int i=0;i<=count;i++){
//對指數運算的檢查
if (string[i]=="^"){
if (i==0)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (i==count)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="log"||string[i+1]=="n!"||string[i+1]==")"||string[i+1]=="^"||string[i+1]=="+"
||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/"||string[i+1]==")")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if ((string[i+2]=="log"||string[i+2]=="^")&&((i+2)<=count))
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對LOG的檢查
if (string[i]=="log"){
if (i==0)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (i==count)
return "The Expression is not valid or may be misconceived!\rYou must enter the start button to restart!";
if (string[i+1]=="log"||string[i+1]=="n!"||string[i+1]==")"||string[i+1]=="^"||string[i+1]=="+"
||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/"||string[i+1]==")")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if ((string[i+2]=="log"||string[i+2]=="^")&&((i+2)<=count))
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對N!的檢查
if (string[i]=="n!"){
if (i==0)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if ((Double.parseDouble(string[i-1])-(int)(Double.parseDouble(string[i-1])))!=0||Double.parseDouble(string[i-1])<0)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if ((string[i+1]!="+"&&string[i+1]!="-"&&string[i+1]!="*"&&string[i+1]!="/"&&string[i+1]!=")")&&(i<=count-1))
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對LN的檢查
if (string[i]=="ln"){
if (i==count)
return "The Expression is not valid"+ '\n'+"or may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="+"||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="log"||string[i+1]=="^"||string[i+1]==")")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對sqrt的檢查
if (string[i]=="sqrt"){
if (i==count)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]==")"||string[i+1]=="log"||string[i+1]=="^")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="+"||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對sqr的檢查
if (string[i]=="sqr"){
if (i==count)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]==")"||string[i+1]=="log"||string[i+1]=="^")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="+"||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對cos的檢查
if (string[i]=="cos"){
if (i==count)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]==")"||string[i+1]=="log"||string[i+1]=="^")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="+"||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對sin的檢查
if (string[i]=="sin"){
if (i==count)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]==")"||string[i+1]=="log"||string[i+1]=="^")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="+"||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//左括號的檢查
if (string[i]=="("){
if (i==count)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]==")"||string[i+1]=="log"||string[i+1]=="^")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="+"||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對右括號的檢查
if (string[i]==")"){
if (i==1)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (i<count)
if (string[i+1]!="+"&&string[i+1]!="-"&&string[i+1]!="*"&&string[i+1]!="/"&&string[i+1]!=")"
&&string[i+1]!="^"&&string[i+1]!="log")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對加號的檢查
if (string[i]=="+") {
if (i==count)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]==")"||string[i+1]=="log"||string[i+1]=="^")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="+"||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對減號的檢查
if (string[i]=="-") {
if (i==count)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]==")"||string[i+1]=="log"||string[i+1]=="^")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="+"||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對乘號的檢查
if (string[i]=="*") {
if (i==count)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]==")"||string[i+1]=="log"||string[i+1]=="^")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="+"||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
//對除號的檢查
if (string[i]=="/") {
if (i==count)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]==")"||string[i+1]=="log"||string[i+1]=="^")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (string[i+1]=="+"||string[i+1]=="-"||string[i+1]=="*"||string[i+1]=="/")
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
}
}
//以上代碼是用來檢查表達式是否合法
//如果有括號,進行處理的情況
if (leftNum > 0){
int num = 0;
int[] list = new int[10];//記錄左括號的位置
for (int i=0;i<=count;i++){
if (string[i]=="("){
if (num>=list.length){
int[] temp = new int[list.length*2];
for (int k=0;k<list.length;k++)
temp[k]=list[k];
list=temp;
}
list[num]=i;
num++;
}
if(string[i]==")"){
num--;
//下面的FOR語句是計算左右括號內的值
for (int j=list[num]+1;j<i;j++){
if (string[j]=="n!"){
double data = Double.parseDouble(string[j-1]);
if ((data-(int)data)!=0)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (data<0)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
for (double fac=data-1;fac>0;fac--){
data = data*fac;
}
String dataString = String.valueOf(data);
string[j-1]=dataString;
for (int p=j;p<count;p++)
string[p]=string[p+1];
count=count-1;
i=i-1;
}
if (string[j]=="log"){
int p=j+1;//p是用來記錄第一個得到的數字的數組位置
while (string[p]=="sin"||string[p]=="cos"||string[p]=="ln"||string[p]=="sqr"||string[p]=="sqrt")
p++;
double data = Double.parseDouble(string[p]);
if (data<=0)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
if (Double.parseDouble(string[j-1])<=0||Double.parseDouble(string[j-1])==1)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
p--;
int save=p;
for (;p>j;p--){
if (string[p]=="sin")
data=Math.sin(data);
if (string[p]=="cos")
data=Math.cos(data);
if (string[p]=="ln")
data=Math.log(data);
if (string[p]=="sqr")
data=Math.pow(data,2);
if (string[p]=="sqrt")
data=Math.sqrt(data);
}
data = Math.log10(data)/Math.log10(Double.parseDouble(string[j-1]));
String dataString = String.valueOf(data);
string[j-1]=dataString;
for (int m=j;m<count-(save-j+1);m++)
string[m]=string[m+save-j+2];
count=count-(save-j+2);
i=i-(save-j+2);
}
if (string[j]=="^"){
int p=j+1;//p是用來記錄第一個得到的數字的數組位置
while (string[p]=="sin"||string[p]=="cos"||string[p]=="ln"||string[p]=="sqr"||string[p]=="sqrt")
p++;
double data = Double.parseDouble(string[p]);
p--;
int save=p;
for (;p>j;p--){
if (string[p]=="sin")
data=Math.sin(data);
if (string[p]=="cos")
data=Math.cos(data);
if (string[p]=="ln")
data=Math.log(data);
if (string[p]=="sqr")
data=Math.pow(data,2);
if (string[p]=="sqrt")
data=Math.sqrt(data);
}
data = Math.pow(Double.parseDouble(string[j-1]),data);
String dataString = String.valueOf(data);
string[j-1]=dataString;
for (int m=j;m<count-(save-j+1);m++)
string[m]=string[m+save-j+2];
count=count-(save-j+2);
i=i-(save-j+2);
}
if (string[j]=="sin"){
int p=j+1;//p是用來記錄第一個得到的數字的數組位置
while (string[p]=="sin"||string[p]=="cos"||string[p]=="ln"||string[p]=="sqr"||string[p]=="sqrt")
p++;
double data = Double.parseDouble(string[p]);
p--;
int save=p;
for (;p>j;p--){
if (string[p]=="sin")
data=Math.sin(data);
if (string[p]=="cos")
data=Math.cos(data);
if (string[p]=="ln")
data=Math.log(data);
if (string[p]=="sqr")
data=Math.pow(data,2);
if (string[p]=="sqrt")
data=Math.sqrt(data);
}
data = Math.sin(data);
String dataString = String.valueOf(data);
string[j]=dataString;
for (int m=j+1;m<count-(save-j);m++)
string[m]=string[m+save-j+1];
count=count-(save-j+1);
i=i-(save-j+1);
}
if (string[j]=="cos"){
int p=j+1;//p是用來記錄第一個得到的數字的數組位置
while (string[p]=="sin"||string[p]=="cos"||string[p]=="ln"||string[p]=="sqr"||string[p]=="sqrt")
p++;
double data = Double.parseDouble(string[p]);
p--;
int save=p;
for (;p>j;p--){
if (string[p]=="sin")
data=Math.sin(data);
if (string[p]=="cos")
data=Math.cos(data);
if (string[p]=="ln")
data=Math.log(data);
if (string[p]=="sqr")
data=Math.pow(data,2);
if (string[p]=="sqrt")
data=Math.sqrt(data);
}
data = Math.cos(data);
String dataString = String.valueOf(data);
string[j]=dataString;
for (int m=j+1;m<count-(save-j);m++)
string[m]=string[m+save-j+1];
count=count-(save-j+1);
i=i-(save-j+1);
}
if (string[j]=="sqr"){
int p=j+1;//p是用來記錄第一個得到的數字的數組位置
while (string[p]=="sin"||string[p]=="cos"||string[p]=="ln"||string[p]=="sqr"||string[p]=="sqrt")
p++;
double data = Double.parseDouble(string[p]);
p--;
int save=p;
for (;p>j;p--){
if (string[p]=="sin")
data=Math.sin(data);
if (string[p]=="cos")
data=Math.cos(data);
if (string[p]=="ln")
data=Math.log(data);
if (string[p]=="sqr")
data=Math.pow(data,2);
if (string[p]=="sqrt")
data=Math.sqrt(data);
}
data = Math.pow(data,2);
String dataString = String.valueOf(data);
string[j]=dataString;
for (int m=j+1;m<count-(save-j);m++)
string[m]=string[m+save-j+1];
count=count-(save-j+1);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -