zoukankan      html  css  js  c++  java
  • java web 开发四则运算 添加部分功能

    此次的四则运算中除了自动出题还添加了题目的计算结果,增加了参数的取值范围,在里边引用了java bean的类文件,主要实现生成的计算式在字符串的形式下完成计算,部分功能仍无法实现:1、求出余数2、出带括号的题目3、完成打一道题目判断对错以下是此次程序的代码

    A.java

    package tianaoweb.com;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Stack;
    public class A {
        public static Map pro=new HashMap();
        public static void init()
        {
        pro.put('+', 1);
        pro.put('-', 1);
        pro.put('*', 2);
        pro.put('/', 2);
        }
        public static int getIndex(String str)
        {
        int index1=(str.indexOf('+')==-1?str.length():str.indexOf('+'));
           int index2=(str.indexOf('-')==-1?str.length():str.indexOf('-'));
           int index3=(str.indexOf('*')==-1?str.length():str.indexOf('*'));
           int index4=(str.indexOf('/')==-1?str.length():str.indexOf('/'));
           int index=index1<index2?index1:index2;
           index=index<index3?index:index3;
           index=index<index4?index:index4;
           return index;
        }
        public static double cal(char op,double num1,double num2)
        {
        switch(op)
        {
          case '+':
          return num1+num2;
          case '-':
          return num1-num2;
          case '*':
          return num1*num2;
          default:
              return num1/num2;
        }
        }
        public static double fun1(String str)
        {
        init();
        Stack st1=new Stack();
        Stack st2=new Stack();
        int fop=0;
        while(str.length()>0)
        {
        int index=getIndex(str);
        st1.push(Double.parseDouble(str.substring(0,index)));
        if(index!=str.length())
        {
        char op=str.charAt(index);
        str=str.substring(index+1);
        while(true)
        {
        if((int)pro.get(op)>fop)
        {
        st2.push(op);
        fop=(int)pro.get(op);
        break;
        }
        else
        {
        double num2= (double) st1.pop();
        double num1=(double) st1.pop();
        double result=cal((char)st2.pop(),num1,num2);
        st1.push(result);
        if(st2.size()==0)
        {
        st2.push(op);
        fop=(int)pro.get(op);
        break;
        }
        char cop=(char) st2.pop();
        fop=(int)pro.get(cop);
        st2.push(cop);
        }
        }
           }
        else
        {
        break;
        }
        }
        while(st2.size()!=0)
        {
        double num2=(double) st1.pop();
        double num1=(double) st1.pop();
        char op=(char) st2.pop();
        st1.push(cal(op,num1,num2));
        }
        double result=(double) st1.pop();
        return result;
        } 
        public static double fun2(String str)
        {
        while(str.indexOf('(')!=-1)
        {
        int left=0;
        int right=str.length();
        char op;
        for(int i=0;i<str.length();i++)
        {
        if(str.charAt(i)=='(')
        {
        left=i;
        }
        if(str.charAt(i)==')')
        {
        right=i;
        break;
        }
        }
        str=str.substring(0,left)+fun1(str.substring(left+1,right))+str.substring(right+1);
        }
    
        return fun1(str);
    
    }
        }

    该类计算的主要思路是通过index of来实现String类型数据中的加减乘除号的获取,在根据优先级进行计算,用到栈的思想实现优先级判断及符号两边的数据计算;

    test.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
     <%@ page import ="tianaoweb.com.*" %>   
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    <%
    JspWriter mout=pageContext.getOut();
    String cho = request.getParameter("choose");//运算方式
    String mm = request.getParameter("numm");//参与运算的个数
    String require=request.getParameter("cho");//是否具有乘除
    String numm=request.getParameter("num");//出题目的个数
    String fw=request.getParameter("fanwei");
    int choose1=Integer.parseInt(cho);//运算方式
    int req=Integer.parseInt(require);//是否乘除
    int m=Integer.parseInt(mm);//参与运算个数
    int num1=Integer.parseInt(numm);//出题个数
    int fw1=Integer.parseInt(fw);
    String pp[]=new String[num1+1];
    int i=0;
    if(choose1==1)
    {
        for(i=0;i<num1;i++)
        {
            pp[i]=zhengshu(m,i,pp[i],req,fw1);
            A jisuan=new A();
            A.fun1(pp[i]);
            mout.println(pp[i]+" "+"=?"+A.fun1(pp[i]));
            mout.print("<br>");    
    
        }
    }
    if(choose1==2)
    {
        for(i=0;i<num1;i++)
        {
            pp[i]=fenshu(m,i,pp[i],req,fw1);
            A jisuan=new A();
            A.fun1(pp[i]);
            mout.println(pp[i]+" "+"=?"+A.fun1(pp[i]));
            mout.print("<br>");
        }
    }
    
    %>
    <%!
    public String zhengshu(int numm,int i,String ti,int p,int fw)
    {
           ti="";
            for(i=0;i<numm;i++)
            {
                int a=(int)(Math.random()*fw+1);
                int c=(int)(Math.random()*4+1);
                int d=(int)(Math.random()*2+1);
                ti=ti+a;
                if(p==1)
                {
                    if(i<numm-1)
                    {
                    if(c==1)
                    {ti=ti+'+';}
                    if(c==2)
                    {ti=ti+'-';}
                    if(c==3)
                    {ti=ti+'*';}
                    if(c==4)
                    {ti=ti+'/';}
                    }    
                    if(i>numm-1)
                    {}
                }
                if(p==2)
                {
                    
                    if(i<numm-1)
                    {
                        if(d==1)
                        {ti=ti+'+';}
                        if(d==2)
                        {ti=ti+'-';}
                    }    
                    if(i>numm-1)
                    {}
                }
            }    
            return ti;
    }    
    %>
    <%!
    public String  fenshu(int m,int i,String ti,int w,int fw)
    {
        ti="";
        String gg="";
        for(i=0;i<m;i++)
        {
            int p=(int)(Math.random()*fw+1);
            int q=(int)(Math.random()*fw+1);
            int x=(int)(Math.random()*4+1);
            int o=(int)(Math.random()*2+1);
            while(p==q)
            { 
            p=(int)(Math.random()*fw+1);
            q=(int)(Math.random()*fw+1);
            }
            if(p>q)
            {
                gg=p+"/"+q;
            }
            if(q>p)
            {
                gg=q+"/"+p;
            }
            ti=ti+gg;
            if(w==1)
            {
                if(i<m-1)
                {
                if(x==1)
                {
                    ti=ti+"+";
                }
                if(x==2)
                {
                    ti=ti+"-";
                }
                if(x==3)
                {
                    ti=ti+"*";
                }
                if(x==4)
                {            
                    ti=ti+"/";
                }
                else{}            
                }
            }
            if(w==2)
            {
                if(i<m-1)
                {
                    if(o==1)
                    {
                    ti=ti+"+";
                    }
                    if(o==2)
                    {
                    ti=ti+"-";
                    }
                }
                else
                {}
            }
            }
        return ti;
    }
    %>
    
    </body>
    </html>

    与上篇博客中四则运算的主要区别在于,将方法通过<%! %>进行声明,在用<%%>进行定义

    主方法实现方法调用,其余思想与上篇类似。

    random.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form action=test.jsp method=post>
    <center>
    <h1>
    出题系统 
    </h1>
    出题形式:整数运算<input type="radio" name=choose value=1>分数运算<input type="radio" name=choose value=2 ><br>
                          具有乘除运算<input type="radio" name=cho value=1>不具有乘除运算<input type="radio" name=cho value=2><br>
                          参与运算个数<input type="text" name=numm><br>
                          数据范围:<input type ="text" name=fanwei><br>
                          出题数量:<input type="text" name=num ><br>
    <input type="submit" value="提交">
    <center>
    </body>
    </form>
    </html>

    新加入了radio的形式用按钮来实现操作的选择!

  • 相关阅读:
    h5基本单词
    啦啦
    新内容
    Apache.PHP和MySQL的配置
    static关键字
    访问控制
    PHP Session
    PHP cookie
    全局空间
    使用use操作符导入/使用别名
  • 原文地址:https://www.cnblogs.com/yuezhihao/p/6531299.html
Copyright © 2011-2022 走看看