zoukankan      html  css  js  c++  java
  • javaweb-四则运算

    这次作业,我们选择的是网页开发,后来我们小组才知道自己这方面的知识还是太匮乏了。

    主要代码:

    public class calcu extends HttpServlet{
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		String smax="";
    		String smin="";
    		String snum="";
    		int[] control=new int[2];
    		smax=request.getParameter("Maxnum");
    		smin=request.getParameter("Minnum");
    		snum=request.getParameter("Num"); 
    		String a=request.getParameter("1");
    		String b=request.getParameter("2");
    		control[1]=Integer.parseInt(a);
    		control[0]=Integer.parseInt(b);
    		int max=Integer.parseInt(smax);
    		int min=Integer.parseInt(smin);
    		int num=Integer.parseInt(snum);
    		caculate(max,min,num,control);
    	}
    	char ops[] = { '+', '-', '*', '/', '(', ')', '=' };
    	char cmp[][] = { { '>', '>', '<', '<', '<', '>', '>' },
    						{ '>', '>', '<', '<', '<', '>', '>' },
    						{ '>', '>', '>', '>', '<', '>', '>' },
    						{ '>', '>', '>', '>', '<', '>', '>' },
    						{ '<', '<', '<', '<', '<', '=', ' ' },
    						{ '>', '>', '>', '>', ' ', '>', '>' },
    						{ '<', '<', '<', '<', '<', ' ', '=' } };
    
    	boolean IsOperator(char ch)
    	{
    		for (int i = 0; i < 7; i++)
    			if (ch == ops[i])
    				return true;
    		return false;
    	}
    	char Compare(char ch1, char ch2)
    	{
    		int i, m = 0, n = 0;
    		char priority;
    		for (i = 0; i < 7; i++) { //找到相比较的两个运算符在比较矩阵里的相对位置
    			if (ch1 == ops[i])
    				m = i;
    			if (ch2 == ops[i])
    				n = i;
    		}
    		priority = cmp[m][n];
    		return priority;
    	}
    
    	Boolean Compute(double x, char op, double y, double z)
    	{
    		switch (op) {
    		case '+':	z = x + y;
    			break;
    		case '-':	z = x - y;
    			break;
    		case '*':	z = x * y;
    			break;
    		case '/':	if (y==0) {
    			z = x / y;
    			break;
    		}
    			return false;
    		}
    		return true;
    	}
    	int ExpEvaluation(char str[], int result)
    	{
    		double a, b, v = 0;
    		char ch, op;
    		int temp, i = 0;
    		Stack<Character> optr = new Stack<Character>();
    		Stack<Integer> opnd = new Stack<Integer>();
    		optr.push('=');
    		ch = str[i++];
    		while (ch != '=' || optr.pop() != '=') {
    			while (ch == ' ')   //跳过空格
    				ch = str[i++];
    			if (IsOperator(ch)) {  //是7种运算符之一
    				switch (Compare(optr.pop(), ch)) {
    				case '<':         //栈顶运算符优先级低
    					optr.push(ch);
    					ch = str[i++];
    					break;
    				case '=':         //脱括号并接收下一字符
    					optr.pop();
    					ch = str[i++];
    					break;
    				case '>':         //栈顶运算符优先级高,退栈并将运算结果入栈
    					op = optr.pop();
    					optr.pop();
    					b = opnd.pop();
    					opnd.pop();
    					a = opnd.pop();
    					opnd.pop();
    					if (Compute(a, op, b, v)) { //计算v = a <op> b
    						opnd.push((int) v);
    						break;
    					}
    					else {
    						result = 0;
    						return result;
    					}
    				}
    			}
    			else {    //是数字
    				temp = ch - '0';    //将字符转换为十进制数
    				ch = str[i++];
    				while (!IsOperator(ch) && ch != ' ') {
    					temp = temp * 10 + ch - '0'; //将逐个读入运算数的各位转化为十进制数
    					ch = str[i++];
    				}
    				opnd.push(temp);    //数值入栈
    			}
    		}
    		result = opnd.pop();
    		return result;
    	}
    
    	public void caculate(int num_max,int num_min,int itemnum,int control[]) {
    	int bracket_num;
    	int num_num;
    	String express = "";
    	char symbol[]=new char[]{'+','-','*','/'};
    	char[] c=new char[20]; 
    	String item[]=new String[itemnum];
    	int[] result = new int[itemnum];
    	for(int count=0; count<itemnum; count++)
    	{
    		num_num =(int) (Math.random() % 10);
    		String str[] = new String[num_num];
    		if (num_num == 0 || num_num == 1)
    		{
    			count--;
    			continue;
    		}
    		int num[] = new int[num_num];
    		int symnum[] = new int[num_num - 1];
    		char sym[] = new char[num_num - 1];
    
    		int bracket_leftposition, bracket_rightposition;
    		int bracket_left_time[] = new int[num_num];           //定义左、右括号生成次数数组,下标为数字位置
    		int bracket_right_time[] = new int[num_num];
    		String bracket_left[] = new String[num_num];        //定义左右括号字符串型数组
    		String bracket_right[] =new String[num_num];
    		for (int rcount = 0; rcount<num_num; rcount++)          //左、右括号生成次数初始化
    		{
    			bracket_left_time[rcount] = 0;
    			bracket_right_time[rcount] = 0;
    		}
    
    		//给参与计算的数赋值(指定数值范围)
    		for (int cnum = 0; cnum<num_num; cnum++)
    		{
    			num[cnum] =(int) (Math.random() % (num_max - num_min + 1) + num_min);
    		}
    
    		//随机生成式子的各个位置的符号
    		for (int snum = 0; snum<num_num - 1; snum++)
    		{
    			symnum[snum] =(int) (Math.random() % control[0]);
    			sym[snum] = symbol[symnum[snum]];
    		}
    
    
    		if (control[1] == 0)
    		{
    			bracket_num = (int) (Math.random() % 3 + 1);
    			//生成括号次数
    			for (int bcount = 0; bcount<bracket_num; bcount++)
    			{
    				bracket_leftposition = (int) (Math.random() % num_num);                //随机生成左右括号的位置
    				bracket_rightposition = (int) (Math.random() % num_num);
    				if ((bracket_leftposition >= bracket_rightposition)||((bracket_leftposition==0)&&(bracket_rightposition==num_num-1)))     //先剔除部分一次性在一个数左右同时生成左右括号和在整个式子前后生成式子的情况
    				{
    					continue;
    				}
    				bracket_left_time[bracket_leftposition]++;            //该位置数左括号生成次数+1
    				bracket_right_time[bracket_rightposition]++;
    			}
    		}
    		for (int stnum = 0; stnum < num_num-2; stnum++)
    		{
    			if ((symbol[symnum[stnum]] == '/') && (symbol[symnum[stnum + 1]] == '/'))
    			{
    				bracket_left_time[stnum]++;
    				bracket_right_time[stnum + 1]++;
    			}
    		}
    		for (int snum = 0; snum < num_num ; snum++)
    		{
    			
    			if (bracket_left_time[snum] == 1)
    			{
    				bracket_left[snum] = "(";
    			}
    			if (bracket_left_time[snum] == 2)
    			{
    				bracket_left[snum] = "((";
    			}
    			if (bracket_left_time[snum] == 3)
    			{
    				bracket_left[snum] = "(((";
    			}
    			if (bracket_right_time[snum] == 1)
    			{
    				bracket_right[snum] = ")";
    			}
    			if (bracket_right_time[snum] == 2)
    			{
    				bracket_right[snum] = "))";
    			}
    			if (bracket_right_time[snum] == 3)
    			{
    				bracket_right[snum] = ")))";
    			}
    			for (int bpcount = 0; bpcount<num_num; bpcount++)            //再次扫描数字左右括号生成次数相等的情况并排除
    			{
    				if (bracket_left_time[bpcount] == bracket_right_time[bpcount])
    				{
    					bracket_right[bpcount] = "";
    					bracket_left[bpcount] = "";
    				}
    			}
    		}
    
    		
    		for (int i = 0; i < num_num; i++)
    		{
    			int temp = num[i];
    			str[i] = temp+"";
    		}
    		for (int ph = 0; ph<num_num - 1; ph++)
    		{
    			express =express+bracket_left[ph]+str[ph]+bracket_right[ph]+sym[ph];
    		}
    		express = express + bracket_left[num_num - 1] +str[num_num - 1]+ bracket_right[num_num - 1];
    		item[count] = express+"=";
    
    		int ans=ExpEvaluation(c, result[count]); //求表达式的值
    		String anstr=ans+"";
    		JavaBean jBean=new JavaBean();
    		DBBean sBean=new DBBean();
    		jBean.setStr(item[count]);
    		jBean.setAnswer1(anstr);
    		String sql="insert into record(str,ans1) values(?,?)";
    	    int numresult=sBean.getInsert(sql,jBean);
    		express="";
    	}
    	
    }
    
    }
    

      

  • 相关阅读:
    每日一篇文献:Robotic pick-and-place of novel objects in clutter with multi-affordance grasping and cross-domain image matching
    每日一篇文献:Intuitive Bare-Hand Teleoperation of a Robotic Manipulator Using Virtual Reality and Leap Motion
    每日一篇文献:Virtual Kinesthetic Teaching for Bimanual Telemanipulation
    HEBI Robotic Arm VR Teleoperation
    「iQuotient Case」AR device teleoperated robotic arm
    VR and Digital Twin Based Teleoperation of Robotic Arm
    HEBI Robotic Arm VR Teleoperation
    Human Robot Interaction
    Immersive Teleoperation Project
    机器人演示学习
  • 原文地址:https://www.cnblogs.com/zzcs/p/5364869.html
Copyright © 2011-2022 走看看