四则运算001

package 第三周;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;

public class Start extends JFrame{
    int rightSum=0;
    CreateArithmetic createArithmeticnew=new CreateArithmetic();
    private JPanel panel1=new JPanel();
    private JLabel label=new JLabel();
    private JLabel label2=new JLabel();
    JTextField text=new JTextField(10);
    ArrayList<String> list=new ArrayList<String>();
    ArrayList<String> list2=new ArrayList<String>();
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame= new  Start();
        frame.setTitle("小学四则运算训练小程序");
        frame.setSize(250,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    
    public void Start1(){
    	
    }
    
    public Start(){
        setLayout(new GridLayout(3, 2,5,5));
        addAnswer();
        JPanel panel2=new JPanel();
        //panel2.setSize(10,100);
        panel2.add(new JLabel("请输入答案:"));
        panel2.add(text);
        this.add(panel2);
        JButton buttonSubmit=new JButton("提交答案");
        buttonSubmit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                int size=list.size();
                int errorSum=size-rightSum;
                JFrame resultFrame=new JFrame();
                resultFrame.setTitle("运算结果");
                resultFrame.setSize(500,500);
                resultFrame.setLocationRelativeTo(null);
                resultFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                resultFrame.setVisible(true);
                JPanel panel=new JPanel();
                Font font = new Font(Font.DIALOG, Font.PLAIN, 20);
                panel.add(new JLabel("做题总数: "+size+" "+"正确数:"+rightSum));
                setLayout(new GridLayout(100, 1,5,5));
                for(int i=0;i<size;i++){
                	JLabel temp = new JLabel(list.get(i));
                	if (list2.get(i).compareTo("正确") == 0){
                		temp.setForeground(Color.blue);
                	}else{
                		temp.setForeground(Color.red);
                	}
                    panel.add(temp);
                }
                resultFrame.add(panel);
            }});
        JButton buttonNext=new JButton("下一题");
        buttonNext.addActionListener(new ActionListener() {//定义时间监听器
            public void actionPerformed(ActionEvent e) {
                String question=label.getText();
                double result=Double.parseDouble(text.getText());
                String ifRight="";
                if(Math.abs(createArithmeticnew.getRes() - result) < 0.01){
                    ifRight="正确";
                    rightSum++;
                }else{
                    ifRight="错误";
                }
                String message=question+", "+"你的答案:"+result+", "+"正确答案: "+String.format("%.2f", createArithmeticnew.getRes())+", 状态: "+ifRight;
                list.add(message);
                list2.add(ifRight);
                text.setText("");
                String answer=createArithmeticnew.creat();
                label.setText(answer);
            }
        });
        JPanel panel3=new JPanel();
        //button.setSize(10,10);
        panel3.add(buttonNext);
        panel3.add(buttonSubmit);
        add(panel3);
    }
    public void addAnswer(){
        //System.out.println("hah");
        //panel1.setSize(0,50);
        Font font = new Font(Font.DIALOG, Font.PLAIN, 20);
        label.setFont(font);
        label2.setFont(font);
        String answer=createArithmeticnew.creat();
        label.setText(answer);
        label2.setText("(小数保留后两位)");
        panel1.add(label);
        panel1.add(label2);

        add(panel1);
    }
}
package 第三周;
import java.util.HashMap;
import java.util.Random;

public class CreateArithmetic {
	private static int NUM_MAX = 3;
	private static int OP_MAX = NUM_MAX - 1;
	private static char op[] = {'+', '-', '*', '/', '(', ')'};
	private HashMap<Character, Integer> map = new HashMap<>();
	private Random r = new Random();
    private int nums[] = new int[NUM_MAX];
    private char chars[] = new char[OP_MAX];
    private double result;
    private String str = "";
    private CreateArithmetic a = null;
    public double getRes(){
    	return a.result;
    }
    @Override
    public String toString() {
    	String str = "";
    	for (int i = 0, j = 0; i < nums.length; ++i){
    		while (j < chars.length && chars[j] == '(') str += chars[j++];
    		str += String.format("%d", nums[i]);
    		while (j < chars.length && chars[j] == ')') str += chars[j++];
    		if (j < chars.length) str += chars[j++];
    	}
//    	System.out.println(result);
//    	str += "=" + String.format("%.2f", result);
        return str;
    }
    public CreateArithmetic(){};
    public CreateArithmetic(int num){
    	NUM_MAX = num;
    	OP_MAX = NUM_MAX - 1;
    	nums = new int[NUM_MAX];
    	chars = new char[OP_MAX];
    };
    public CreateArithmetic(int nums[], char chars[], double ans){
        for (int i = 0; i < NUM_MAX; ++i) this.nums[i] = nums[i];
        for (int i = 0; i < OP_MAX; ++i) this.chars[i] = chars[i];
        this.result = ans;
    }
    public CreateArithmetic generate_exercise(){
        double ans = 101;
        double res = 0;
	      while ((int)ans <= 0 || ans > 100){
	      double stack_double[] = new double[NUM_MAX + 1];
	      char stack_char[] = new char[OP_MAX * 2 + 1];
	      int top1 = 0, top2 = 0;
	      int indexn = 0, indexc = 0;
	      for (int i = 0; i < NUM_MAX; ++i) nums[i] = r.nextInt(100) + 1;
	      int cnt1 = 0, cnt2 = 0;
	      for (int i = 0; i < OP_MAX; ++i){
	    	  chars[i] = generate_op();
	    	  if (chars[i] == '(') cnt1++;
	    	  else if (chars[i] == ')') cnt1--;
	    	  else cnt2++;
	      }
	      if (cnt1 != 0 || cnt2 != NUM_MAX - 1) continue;
	      // 生成运算符的中缀表达式
	      String str1 = generate_str(nums, chars);
	      // 判断中缀表达式是否合法
	      if (str1 == "") continue;
	      else str = str1;
	      stack_double[top1++] = (double)nums[indexn++];
	      while (top1 > 0 && indexn < NUM_MAX){
	        if (top2 >= 1 && compareToch(stack_char[top2 - 1], chars[indexc])){
	        	if (chars[indexc] == ')'){
	        		while (stack_char[top2 - 1] != '('){
			            res = handle(stack_double[top1 - 2], stack_double[top1 - 1], stack_char[top2 - 1]);
			            top1 -= 2;
			            stack_double[top1++] = res;
			            top2 -= 1;
	        		}
	        		top2 -= 1;
	        	}else{
		            res = handle(stack_double[top1 - 2], stack_double[top1 - 1], stack_char[top2 - 1]);
		            top1 -= 2;
		            stack_double[top1++] = res;
		            top2 -= 1;
		            stack_char[top2++] = chars[indexc++];
	        	}
	        }else {
	            stack_char[top2++] = chars[indexc++];
	        }
	        stack_double[top1++] = (double)nums[indexn++];
	      }
	      while (top1 >= 2){
	        res = handle(stack_double[top1 - 2], stack_double[top1 - 1], stack_char[top2 - 1]);
	        top1 -= 2;
	        stack_double[top1++] = res;
	        top2 -= 1;
	      }
	      ans = res;
//	      System.out.println(ans);
	   }
        return new CreateArithmetic(nums, chars, ans);
    }
    private char generate_op(){
        map.put('+', 1);
        map.put('-', 1);
        map.put('*', 2);
        map.put('/', 2);
        map.put('(', 3);
        map.put(')', 4);
        return op[r.nextInt(4)];
    }
    private boolean compareToch(char ch1, char ch2){
    	if (map.get(ch1) >= map.get(ch2)) return true;
    	return false;
    }
    public double handle(double num1, double num2, char op){
    	double res = 0;
        if (op == '+') res = num1 + num2;
        else if (op == '-') res = num1 - num2;
        else if (op == '*') res = num1 * num2;
        else if (op == '/') res = num1 / num2;
        return res;
    }
    
    
    private String generate_str(int nums[], char ch[]){
    	String str = "";
    	int cnt = 0;
    	for (int i = 0, j = 0; i < nums.length; ++i){
    		while (j < ch.length && ch[j] == '('){
    			str += ch[j++];
    			cnt++;
    		}
    		str += String.format("%d", nums[i]);
    		while (j < ch.length && ch[j] == ')'){
    			str += ch[j++];
    			cnt--;
    			if (cnt < 0){
    				return "";
    			}
    		}
    		if (j < ch.length) str += ch[j++];
    	}
    	return str;
    }
    public String creat(){
    	a = new CreateArithmetic();
    	a = a.generate_exercise();
    	return "题目:" + a.toString() + "=?";
    }
}

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值