datastringdeal.java

来自「做了一个还算复杂的计算器,还是有一定的 参考价值的(比较适合中初级者)」· Java 代码 · 共 1,041 行 · 第 1/3 页

JAVA
1,041
字号
            				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 + =
减小字号Ctrl + -
显示快捷键?