?? datastringdeal.java
字號:
i=i-(save-j+1);
}
if (string[j]=="sqrt"){
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!";
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.sqrt(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]=="ln"){
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);
}
if (data<=0)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
data = Math.log(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);
}
}
//先計算乘除
for (int j=list[num]+1;j<i;j++){
if (string[j]=="*"){
double data = Double.parseDouble(string[j-1])*Double.parseDouble(string[j+1]);
String dataString = String.valueOf(data);
string[j-1]=dataString;
for (int p=j;p<=count-2;p++)
string[p]=string[p+2];
i=i-2;
count=count-2;
}
if (string[j]=="/"){
if (Double.parseDouble(string[j+1])==0)
return "The Expression is not valid \nor may be misconceived!\nYou must enter the start button to restart!";
double data = Double.parseDouble(string[j-1])/Double.parseDouble(string[j+1]);
String dataString = String.valueOf(data);
string[j-1]=dataString;
for (int p=j;p<=count-2;p++)
string[p]=string[p+2];
i=i-2;
count=count-2;
}
}
//再計算加減
if (string[list[num]+1]=="-"||string[list[num]+1]=="+"){
if (string[list[num]+1]=="-"){
double data =-Double.parseDouble(string[list[num]+2]);
String dataString =String.valueOf(data);
string[list[num]+1]=dataString;
for (int p=list[num]+2;p<count;p++)
string[p]=string[p+1];
count--;
i--;
}
if (string[list[num]+1]=="+"){
double data =Double.parseDouble(string[list[num]+2]);
String dataString =String.valueOf(data);
string[list[num]+1]=dataString;
for (int p=list[num]+2;p<count;p++)
string[p]=string[p+1];
count--;
i--;
}
}
for (int j=list[num]+1;j<i;j++){
if (string[j]=="+"){
double data = Double.parseDouble(string[j-1])+Double.parseDouble(string[j+1]);
String dataString = String.valueOf(data);
string[j-1]=dataString;
for (int p=j;p<=count-2;p++)
string[p]=string[p+2];
j--;
i=i-2;
count=count-2;
}
if (string[j]=="-"){
double data = Double.parseDouble(string[j-1])-Double.parseDouble(string[j+1]);
String dataString = String.valueOf(data);
string[j-1]=dataString;
for (int p=j;p<=count-2;p++)
string[p]=string[p+2];
j--;
i=i-2;
count=count-2;
}
}
string[list[num]]=string[list[num]+1];
for (int p=list[num]+1;p<count-1;p++)
string[p]=string[p+2];
count =count-2;
i=i-2;
}
}
}
//處理沒有括號的表達式
for (int i=0;i<=count;i++){
if (string[i]=="^"){
int p=i+1;
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>i;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);
}
String dataString=String.valueOf(Math.pow(Double.parseDouble(string[i-1]),data));
string[i-1]=dataString;
for (int j=i;j<=count-(2+save-i);j++)
string[j]=string[j+2+save-i];
count=count-2-save+i;
i=i-1-save+i;
}
if (string[i]=="log"){
int p=i+1;
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[i-1])<=0||Double.parseDouble(string[i-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>i;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);
}
String dataString=String.valueOf(Math.log10(Double.parseDouble(string[i-1]))/Math.log10(data));
string[i-1]=dataString;
for (int j=i;j<=count-(2+save-i);j++)
string[j]=string[j+2+save-i];
count=count-2-save+i;
i=i-1-save+i;
}
if (string[i]=="sin"){
int p=i+1;
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>i;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);
}
String dataString = String.valueOf(Math.sin(data));
string[i]=dataString;
for (int j=i+1;j<=count-1-save+i;j++)
string[j]=string[j+1+save-i];
count=count-1-save+i;
i=i-save+i;
}
if (string[i]=="cos"){
int p=i+1;
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>i;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);
}
String dataString = String.valueOf(Math.cos(data));
string[i]=dataString;
for (int j=i+1;j<=count-1-save+i;j++)
string[j]=string[j+1+save-i];
count=count-1-save+i;
i=i-save+i;
}
if (string[i]=="ln"){
int p=i+1;
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>i;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);
}
String dataString = String.valueOf(Math.log(data));
string[i]=dataString;
for (int j=i+1;j<=count-1-save+i;j++)
string[j]=string[j+1+save-i];
count=count-1-save+i;
i=i-save+i;
}
if (string[i]=="sqr"){
int p=i+1;
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>i;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);
}
String dataString = String.valueOf(Math.pow(data,2));
string[i]=dataString;
for (int j=i+1;j<=count-1-save+i;j++)
string[j]=string[j+1+save-i];
count=count-1-save+i;
i=i-save+i;
}
if (string[i]=="sqrt"){
int p=i+1;
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!";
p--;
int save=p;
for (;p>i;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);
}
String dataString = String.valueOf(Math.sqrt(data));
string[i]=dataString;
for (int j=i+1;j<=count-1-save+i;j++)
string[j]=string[j+1+save-i];
count=count-1-save+i;
i=i-save+i;
}
if (string[i]=="n!"){
double data =Double.parseDouble(string[i-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 j=data-1;j>0;j--)
data=data*j;
string[i-1]=String.valueOf(data);
for (int j=i;j<=count-1;j++)
string[j]=string[j+1];
count--;
}
}
//開始處理只有加減乘除的表達式
//首先是乘除的處理
for (int i=0;i<=count;i++){
if (string[i]=="*"){
string[i-1]=String.valueOf(Double.parseDouble(string[i-1])*Double.parseDouble(string[i+1]));
for (int j=i;j<=count-2;j++)
string[j]=string[j+2];
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -