⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydivide.java

📁 数据结构java--简单功能的计算器
💻 JAVA
字号:
package myPackage;
public class myDivide{
    Linlist mylist;
    
    public String Dividision(String str_1){
    	mylist=new Linlist();
    	String str_2=str_1.concat("#");	
    	String str;
    	int flag=0;
    	int flag_1=0;
    	int flag_2=0;
    	int flag_3=0;
    	int flag_4=0;
    	int flag_5=0;
    	int flag_6=0;
    	int flag_7=0;
    	int i=0;
    	int j=0;
    	int b=0;
    	int myJudgment=0;
    	float num=0;
    	int point=0;
    	char ch,ch_1='x';
    	while(str_2.charAt(i)!='#'){
    	
    		 ch=str_2.charAt(i);
    		 	if(ch=='('){	
    		 	   if(i>=1&&str_2.charAt(i-1)=='-'){
    		 			flag=1;    //表示括号前是"-"
    		 		}
    		 		i++;
    		 	}
    		 	if(ch==')'){
    		 		       i++;
    		 		     flag=0;//表示括号前是“+”
    		 		    }
    		 	if(ch=='+'){
    		 		if(str_2.charAt(i-1)=='('){
                          i++;
    		 		}else if(flag==0){
    		 			flag_1=0;  //表示做"+"
    		 			i++;
    		 		}else if(flag==1){
    		 			flag_1=1;   //做"-"运算
    		 			i++;
    		 		}
    		 		
    		 	}
    		 	if(ch=='-'){
    		 		if(flag_2==0){
    		 			if(str_2.charAt(i-1)=='('&&flag==1){
    		 			flag_1=0;//括号后面紧跟着"-"
    		 			i++;
    		 		}
    		 		else if(str_2.charAt(i-1)=='('&&flag==0){
    		 			flag_1=1; //括号内"-"变成"+"
    		 			i++;
    		 		}else if(flag==1){
    		 			flag_1=0; //括号内"-"变成"+"
    		 			i++;
    		 		}
    		 		else  {
    		 			flag_1=1;//括号内的数默认做"-"运算
    		 			i++;
    		 		}
    		 		}
    		 		else if(flag_2==1)	{
    		 			flag_6=1;
    		 			i++;
    		 		}
    		 	}
    		 	if(ch=='x'){
    		 		ch_1='x';
    		 		flag_2=1;//字符在读读指数
    		 		if(i==0||str_2.charAt(i-1)<'0'||str_2.charAt(i-1)>'9'){
    		 			num=1;
    		 		}
    		 		i++;
    		 	}
    		 	if(ch=='.'){
    		 		flag_7=1; //表示有小数点
    		 		b=i;
    		 		i++;
    		 	}
    		    if(ch>='0'&&ch<='9'){
    		    	
    		    	if(flag_2==1){
    		    		if(flag_6==1)
    		    		flag_5=1;       //表示指数为负数
    		    		else
    		    		flag_5=0;       //表示指数为正数
    		    	}
    		    	while((ch=str_2.charAt(i))>='0'&&(ch=str_2.charAt(i))<='9'){
    		    		if(flag_2==0){
    		    			num=num*10+calculator(ch);
    		    		    i++;
    		    		    
    		    		}
    		    		else if(flag_2==1){
    		    			point=point*10+calculator_1(ch);
    		    			i++;
    		    			flag_3=1;
    		    		}
    		    	}
    		    	if(flag_7==1){
    		    		int sqr=1;
    		    		for(int h=0;h<i-1-b;h++){
    		    			sqr=sqr*10;
    		    		}
    		    		num=num/sqr;
    		    		flag_7=0;
    		    	}
    		        if(flag_5==1){
    		        	point=0-point;
    		        }
    		    	if(flag_3==1){
    		    		int k=mylist.Size();
    		    		if(flag_1==0){
    		    			myJudgment=0;//判断系数正负0表示正1表示负
    		    		}
    		    		else{
    		    			myJudgment=1;//判断系数正负0表示正1表示负
    		    		}
    		    		if(k==0){
    		    			try{
    		    				mylist.insert(j,num,point,ch_1,myJudgment);//创建链表第一个节点
    		    			}
    		    			catch(Exception e){
    		    				System.out.println(e.getMessage());
    		    			}
    		    			num=0;
    		    			point=0;	
    		    		}
    		    		else{ 
    		    			   mylist.current=mylist.head;
    		    			while(mylist.current.next!=null&&mylist.current.next.getX_Point()<point){//比较大小进行插入
    		    				mylist.current=mylist.current.next;
    		    				j++; 
    		    			}
    		    			if(mylist.current.next==null) { //插在链表结尾
    		    				
    		    				try{
    		    				        mylist.insert(j,num,point,ch_1,myJudgment);
    		    				}
    		    			    catch(Exception e){
    		    			    	System.out.println(e.getMessage());
    		    			    }
    		    			    num=0;
    		    			    point=0;
    		    			    j=0;
    		    			}
    		    			else if(mylist.current.next.getX_Point()==point){  //指数相等时系数相加减
    		    					if(mylist.current.next.Judgment==myJudgment){
    		    						mylist.current.next.setX_NUM((mylist.current.next.getX_NUM()+num));
    		    					}
    		    					else{
    		    						if(mylist.current.next.getX_NUM()-num<0){ 
    		    							if(mylist.current.next.getJudgment()==1){
    		    							    mylist.current.next.setJudgment(0);
    		    							    mylist.current.next.setX_NUM((num-mylist.current.next.getX_NUM()));
    		    							}
    		    							else{
    		    								mylist.current.next.setJudgment(1);
    		    								mylist.current.next.setX_NUM((num-mylist.current.next.getX_NUM()));
    		    							}	
    		    						}
    		    						else{
    		    							if(mylist.current.next.getJudgment()==1){
    		    							    mylist.current.next.setJudgment(1);
    		    							    mylist.current.next.setX_NUM((mylist.current.next.getX_NUM()-num));
    		    							}
    		    							else{
    		    								mylist.current.next.setJudgment(0);
    		    								mylist.current.next.setX_NUM((mylist.current.next.getX_NUM()-num));
    		    							}	
    		    						}
    		    					}
    		    				num=0;
    		    				point=0;
    		    				j=0;
    		    			}
    		    			else if(mylist.current.next.getX_Point()>point){//指数不等时
    		    				try{
    		    					mylist.insert(j,num,point,ch_1,myJudgment);
    		    				}
    		    				catch(Exception e){
    		    					System.out.println(e.getMessage());
    		    				}
    		    				num=0;
    		    				point=0;
    		    				j=0;
    		    			}
    		    		}	
    		    	}
    		    	flag_6=0;
    		    	flag_2=0;//表示读系数
    		    	flag_3=0;//表示还没读完不能创建链表
    		    }			  
    	}
    	str=Head_Change(mylist); //将链表中的数据转换为字符串
    	return str;
    }
    
    public float  calculator(char ch){ //字符转换为浮点型
    	int temp;
    	temp=ch%'0';
    	float a=new Integer(temp).floatValue();
    	return a;
    }
    
    public int  calculator_1(char ch){  //字符转换为整型  
    	int temp_1;
    	temp_1=ch%'0';
    	int a=new Integer(temp_1);
    	return a;
    }
    
    public String Head_Change(Linlist list){  //链表转换为字符串
    	String str_3="(";
    	int k=0;
    	list.current=list.head.next;
    	while(list.current!=null){
    		String 	s1=Float.toString(list.current.getX_NUM());
    		String  s3=Integer.toString(list.current.getX_Point());
    		if(k==0){
    			if(list.current.getJudgment()==1)
    			str_3=str_3.concat("-");
    		    if(list.current.getX_NUM()==Float.valueOf(0)){
    		    	str_3=str_3.concat("");   
    			}
    			else{
    				if(list.current.getX_NUM()!=1)
    				str_3=str_3.concat(s1);
    				if(list.current.getX_Point()!=0){
    					str_3=str_3.concat(""+(list.current.getX_CHAR()));   
    				}
    				if(list.current.getX_Point()!=1&&list.current.getX_Point()!=0)
    				 str_3=str_3.concat(s3);
    			}
    			k++;
    			list.current=list.current.next;	
    		}
    		else if(k!=0){
    			if(list.current.getJudgment()==0)
    			str_3=str_3.concat("+");
    			else if(list.current.getJudgment()==1)
    			str_3=str_3.concat("-");
    			if(list.current.getX_NUM()==Float.valueOf(0)){
    		    	str_3=str_3.concat("");   
    			}
    			else{
    				if(list.current.getX_NUM()!=1)
    				str_3=str_3.concat(s1);
    				if(list.current.getX_Point()!=0){
    					str_3=str_3.concat(""+(list.current.getX_CHAR()));  
    				}
    				if(list.current.getX_Point()!=1&&list.current.getX_Point()!=0)
    				str_3=str_3.concat(s3);
    			}
    			k++;
    			list.current=list.current.next;	
    		}
    		
    	}
    	str_3=str_3.concat(")");
    	return str_3;
    }
    
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -