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

    package task;
    import java.util.Random;
    import java.util.Scanner;
    import java.util.Stack;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class FourOperations{
    	static int[] s2=new int[13];
    	static String[] q1=new String[10000];
    	static String[] q2=new String[10000];
    	static int sfg=0;
    	public static void main(String args[]) throws Exception//主函数 
    	{
    		zeyrm();		
    	}
    	public static void zeyrm() throws Exception//主页面
    	{
    		int a,b,c,e;
    		long d = 1;
    		Scanner m=new Scanner(System.in);
    		System.out.println("1.设置参数;");
    		System.out.println("2.小学二年级口算题;");
    		System.out.println("3.小学三年级口算题;");
    		System.out.println("4.小学四年级口算题;");
    		System.out.println("5.错题集;");
    		int wer=m.nextInt();
    		switch(wer)
    		{
    		case 1:
    			System.out.println("出题个数:");
    			a=m.nextInt();
    			System.out.println("操作数个数:");
    			b=m.nextInt();
    			System.out.println("确定操作数的取值范围:");
    			System.out.println("下限:");
    			c=m.nextInt();
    			System.out.println("上限:");
    			e=m.nextInt();
    			e-=c;
    			if(e<=0) 
    			{
    				System.out.println("取值范围有误;");
    			}else
    			{
    				for(int u=0;u<=b;u++)
    				{
    					d*=e;
    				}
    				d*=4;
    				if(d>=a|| d<-1000000000)
    				{
    					Randomnum(a,b,e,c);
    					Output(a);
    				}else {
    					System.out.println("在该条件下,无法完成无重复出题。");
    				}	
    			}	
    			break;
    		case 2:
    			a=30;
    			b=2;
    			c=0;
    			e=100;
    			e-=c;
    			Randomnum(a,b,e,c);
    			Output(a);	
    			break;
    		case 3:
    			a=30;
    			b=4;
    			c=0;
    			e=1000;
    			e-=c;
    			Randomnum(a,b,e,c);
    			Output(a);	
    			break;
    		case 4:
    			a=30;
    			b=5;
    			c=0;
    			e=1000;
    			e-=c;
    			Randomnum(a,b,e,c);
    			Output(a);	
    			break;
    		case 5:	
    			Output(sfg);
    			break;			
    		}
    	}
    	public static void Randomnum(int a,int b,int e,int c)//出题
    	{
    		for(int y=0;y<a;y++)
    		{
    			int hj=0,gh=1;
    			Random t0 = new Random();
    			s2[hj]=t0.nextInt(e)+c;
    			hj++;
    			int kj=0;
    			for(int g=0;g<b-1;g++)
    			{
    				Random t2 = new Random();
    				s2[hj]=t2.nextInt(4);
    				hj++;
    				if(s2[hj-1]==3)
    				{
    					if(kj!=0)
    					{
    						s2[hj-1]=1;
    						Random t1 = new Random();
    						s2[hj]=t1.nextInt(e)+c;
    						hj++;
    					}else
    					{
    						Random t1 = new Random();
    						s2[hj]=t1.nextInt(e/4)+c;
    						if(s2[hj]==0) s2[hj]=1;
    						Random t3 = new Random();
    						s2[hj-2]=(t3.nextInt(4)+1)*s2[hj];
    						kj++;
    						hj++;
    					}					
    				}else
    				{
    					Random t1 = new Random();
    					s2[hj]=t1.nextInt(e)+c;
    					hj++;	
    				}				
    			}
    			if(y!=0) gh=Duplicatecheck(y,b);
    			if(gh==1)
    			{
    				StringBuilder conversion = new StringBuilder();
    				for(int i=0;i<(b*2-1);i++)
    				{
    					if(i%2==1)
    					{
    						switch(s2[i])
    						{
    						case 0:
    							conversion.append('+');
    							break;
    						case 1:
    							conversion.append('-');
    							break;
    						case 2:
    							conversion.append('*');
    							break;
    						case 3:
    							conversion.append('/');
    							break;
    						}
    					}else
    					{
    						conversion.append(s2[i]);
    					}					
    				}
    				q1[y]=conversion.toString();			
    			}else
    			{
    				y--;
    			}
    		}		
    	}
    	public static int Duplicatecheck(int y,int b)//查重
    	{
    		String fd=null;
    		StringBuilder conversion = new StringBuilder();
    		for(int i=0;i<(b*2-1);i++)
    		{
    			if(i%2==1)
    			{
    				switch(s2[i])
    				{
    				case 0:
    					conversion.append('+');
    					break;
    				case 1:
    					conversion.append('-');
    					break;
    				case 2:
    					conversion.append('*');
    					break;
    				case 3:
    					conversion.append('/');
    					break;
    				}
    			}else
    			{
    				conversion.append(s2[i]);
    			}
    		}
    		fd=conversion.toString();
    		for(int kl=0;kl<y;kl++)
    		{
    			if(fd.equals(q1[kl]))
    			{
    				return 0;
    			}			
    		}
    		return 1;
    	}
    	public static void Output(int a) throws Exception//打印,判断,收集错题
    	{
    		int v=0;
    		Scanner m=new Scanner(System.in);
    		int ok=0;
    		double tyu,s=0.00;
    		String lk;
    		for(int u=0;u<a;u++)
    		{
    			System.out.print(u+1+" ");
    			System.out.print(q1[u]);
    			System.out.print("=");
    			ok=m.nextInt();
    			lk=getResult(q1[u]);
    			double nm=Double.valueOf(lk).intValue();
    			int ks=(int)nm;
    			if(ok==ks)
    			{
    				
    				System.out.println("回答正确;");
    				s++;
    			}else
    			{
    				System.out.println("回答错误;");
    				q2[v]=q1[u];
    				v++;
    			}
    		}
    		sfg=v;
    		for(int fgv=0;fgv<v;fgv++)
    		{
    			q1[fgv]=q2[fgv];
    		}
    		System.out.print("正确率:");
    		double vb=a;
    		tyu=s/vb;
    		System.out.println((tyu*100)+"%");
    		zeyrm();
    	}
    	private static double doubleCal(double a1, double a2, char operator) throws Exception //符号运算
    	{
            switch (operator) {
            case '+':
                return a1 + a2;
            case '-':
                return a1 - a2;
            case '*':
                return a1 * a2;
            case '/':
                return a1 / a2;
            default:
                break;
            }
            throw new Exception("illegal operator!");
        }
    	private static int getPriority(String s) throws Exception //符号优先级
    	{
            if(s==null) return 0;
            switch(s) {
            case "(":return 1;
            case "+":;
            case "-":return 2;
            case "*":;
            case "/":return 3;
            default:break;
            }
            throw new Exception("illegal operator!");
        }
    	private static String toSufExpr(String expr) throws Exception //四则运算
    	{
            StringBuffer sufExpr = new StringBuffer();
            Stack<String> operator = new Stack<String>();
            operator.push(null);
            Pattern p = Pattern.compile("(?<!\d)-?\d+(\.\d+)?|[+\-*/()]");
            Matcher m = p.matcher(expr);
            while (m.find()) {
                String temp = m.group();
                if (temp.matches("[+\-*/()]")) {
                    if (temp.equals("(")) {
                        operator.push(temp);
                    } else if (temp.equals(")")) {
                        String topItem = null;
                        while (!(topItem = operator.pop()).equals("(")) {
                            sufExpr.append(topItem+" ");
                        }
                    } else {
                        while(getPriority(temp) <= getPriority(operator.peek())) {
                            sufExpr.append(operator.pop()+" ");
                        }
                        operator.push(temp);
                    }
                }else {
                    sufExpr.append(temp+" ");
                }
            }
            String topItem = null;
            while(null != (topItem = operator.pop())) {
                sufExpr.append(topItem+" ");
            }
            return sufExpr.toString();
        }
    	public static String getResult(String expr) throws Exception//运算返回
    	{
            String sufExpr = toSufExpr(expr);
            Stack<Double> number = new Stack<Double>();
            Pattern p = Pattern.compile("-?\d+(\.\d+)?|[+\-*/]");
            Matcher m = p.matcher(sufExpr);
            while (m.find()) {
                String temp = m.group();
                if (temp.matches("[+\-*/]")) {
                    double a1 = number.pop();
                    double a2 = number.pop();
                    double res = doubleCal(a2, a1, temp.charAt(0));
                    number.push(res);
                } else {
                    number.push(Double.valueOf(temp));
                }
            }
            return number.pop() + "";
        }
    }
    

      

  • 相关阅读:
    python Unittest中setUp与setUpClass的区别
    Python的range和xrange的区别
    Python列表中的列表元素(嵌套列表)访问
    python字符串转为列表
    python正则匹配
    Python自动化测试用例设计--自动化测试用例与手工测试用例区别与联系
    Python+Selenium学习--自动化测试模型
    Python+Selenium学习--cookie处理
    Python+Selenium学习--控制浏览器控制条
    Python+Selenium学习--下载文件
  • 原文地址:https://www.cnblogs.com/da48/p/14094994.html
Copyright © 2011-2022 走看看