datastringdeal.java
来自「做了一个还算复杂的计算器,还是有一定的 参考价值的(比较适合中初级者)」· Java 代码 · 共 1,041 行 · 第 1/3 页
JAVA
1,041 行
/**
* @(#)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 + =
减小字号Ctrl + -
显示快捷键?