MVC实现复数运算

M:计算模型--javabean

V:视图--jsp

C:控制器--servlet

先写在前面吧,困了我好久的一个错误,竟然是:String 类型的比较需要用 “equals”  而不是“==”  我在上面栽了好几个小时,这回一定记住了。emmm

 equals  与  ==  区别:https://www.cnblogs.com/dongguol/p/5845076.html

下面是原文。

效果图:

MVC实现复数运算_第1张图片

输入数据--jsp

 





复数运算


第一个数实部:
第一个数虚部:
运算:
第二个数实部:
第二个数虚部:
  

 计算Js(js)--sevlet

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		double x1=Double.parseDouble(request.getParameter("j1"));
		double y1=Double.parseDouble(request.getParameter("i1"));
		double x2=Double.parseDouble(request.getParameter("j2"));
		double y2=Double.parseDouble(request.getParameter("i2"));
		
		Fs c1 = new Fs(x1,y1);
		Fs c2 = new Fs(x2,y2);
		
		request.setAttribute("js1", c1);
		request.setAttribute("js2", c2);
		
//		Fs c3 = new Fs();
		
		String op=request.getParameter("op");
		
/*	第一版本,与javabean里的第一版本对应,但是错误,不知道原因
     	if(op=="+") {c3=c1.add(c2); }
		if(op=="-") {c3=c1.subtract(c2);}
		if(op=="*") {c3=c1.mutiply(c2);}
		if(op=="/") {c3=c1.divide(c2);}
		request.setAttribute("mm", c3);
*/	
	    Fs c3 = c1.opp(c2, op);
	    request.setAttribute("mm", c3);
	    
		RequestDispatcher rd=request.getRequestDispatcher("show.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

 模型Fs--javabean

package javabean;

public class Fs {
	private double x;
	private double y;
	
	public double getX() {
		return x;
	}
	public void setX(double x) {
		this.x = x;
	}
	public double getY() {
		return y;
	}
	public void setY(double y) {
		this.y = y;
	}
	public Fs(double x, double y) {
		this.x = x;
		this.y = y;
	}
	public Fs() {}

	public Fs opp(Fs c,String op) {
		double j = 0;
		double i = 0;
		
		switch(op) {
		case "+" :
		    j=this.x+c.x;
		    i=this.y+c.y;
		    break;
		case "-" :
			j=this.x-c.x;
			i=this.y-c.y;
			break;
		
		case "*" :
			j=(this.x*c.x)-(this.y*c.y);
			i=(this.y*c.x)+(this.x*c.y);
			break;
		case "/" :
			j=(this.x*c.x+this.y*c.y)/((c.x*c.x)+(c.y*c.y));
			i=((this.y*c.x)-(this.x*c.y))/((c.x*c.x)+(c.y*c.y));
			break;
		}	
		return new Fs(j,i); 
	 }
	public String pp() {
		String r=null;
		if(y!=0) {
		    if(y>0)
			    r=""+x+"+"+y+"i";
		    else
			    r=""+x+y+"i";
		}
		else
			r=""+x;
		return r;
	}
//以下是计算函数的修改过程,错误原因应该都是c3没有正确的调用函数
//在if分支语句好像并不起作用,换成switch-case则正确,原因?
//原因:String 类型的比较需要用 “equals”  而不是“==”
/*	第一版本
		public Fs add(Fs c) {
			double j=this.x+c.x;
			double i=this.y+c.y;
			Fs m = new Fs(j,i);
			return m; 
		}
		
		public Fs subtract(Fs c) {
			double j=this.x-c.x;
			double i=this.y-c.y; 
			Fs m = new Fs(j,i);
			return m; 
		}
		public Fs mutiply(Fs c) {
			//(ac-bd)+(bc+ad)i 
			double j=(this.x*c.x)-(this.y*c.y);
			double i=(this.y*c.x)+(this.x*c.y);
			Fs m = new Fs(j,i);
			return m; 
		}
		
		public Fs divide(Fs c) {
			//(a+bi)/(c+di)=(ac+bd)/(c2+d2) +((bc-ad)/(c2+d2))i
			double j=(this.x*c.x+this.y*c.y)/((c.x*c.x)+(c.y*c.y));
			double i=((this.y*c.x)-(this.x*c.y))/((c.x*c.x)+(c.y*c.y));
			Fs m = new Fs(j,i);
			return m; 
		}
*/	
		
/* 第三版本
     public Fs opp(Fs c,String op) {
 		if(op=="+") {
			
			return new Fs(this.x+c.x,this.y+c.y);
		}
		if(op=="-") {
			return new Fs(this.x-c.x,this.y-c.y);
		}
	}
*/	
		
/*	第二版本
      public Fs opp(Fs c,String op) {
    	if(op=="+") {
			j=this.x+c.x;
			i=this.y+c.y;
			return new Fs(this.x+c.x,this.y+c.y);
		}
		if(op=="-") {
			j=this.x-c.x;
			i=this.y-c.y;
		}
		if(op=="*") {
			j=(this.x*c.x)-(this.y*c.y);
			i=(this.y*c.x)+(this.x*c.y);
		}
		if(op=="/") {
			j=(this.x*c.x+this.y*c.y)/((c.x*c.x)+(c.y*c.y));
			i=((this.y*c.x)-(this.x*c.y))/((c.x*c.x)+(c.y*c.y));
		}
		Fs m = new Fs(j,i);
		return m;
	}
*/		

}

显示结果show--jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="javabean.Fs"%>




Insert title here


<%
   Fs x=(Fs)request.getAttribute("js1");
   Fs y=(Fs)request.getAttribute("js2");
   Fs z=(Fs)request.getAttribute("mm");
   String op=request.getParameter("op");
%>
(<%=x.pp()%>)<%=op%>(<%=y.pp() %>)=(<%= z.pp() %>)

 

你可能感兴趣的:(JavaWeb)