zoukankan      html  css  js  c++  java
  • 作业5.2 5.3

    结对伙伴博客地址:http://www.cnblogs.com/fitfit/

    这次的作业中添加设置运算符个数来进行加减乘除计算的功能和对程序中的一些异常进行了处理

    1.代码:

    修改过后的计算功能的类:

    package fiv;
    public class calc {
        int na[]=new int[5];
        String nc[]=new String[5];
        int ncn=0,nm;
        int re=0;
        calc(){
            
        }
        calc(int[][] a,String[][] c,int m,int cn){
            for(int i=0;i<cn+1;i++)
                na[i]=a[m][i];
            for(int i=0;i<cn;i++)
                nc[i]=c[m][i];
            ncn=cn;
            nm=m;
        }
    void setting(){
            int rn=0,r=0;
            int[] b=new int[5];
            String[] cc=new String[5];
            re=0;
            for(int i=0;i<ncn;i++){
                if(rn==0){
                    if(nc[i]=="*"||nc[i]=="/"){
                        if(nc[i]=="*"){
                            b[r]=na[i]*na[i+1];
                            rn=1;
                        }
                        if(nc[i]=="/"){
                            b[r]=na[i]/na[i+1];
                            rn=1;
                        }
                    }
                    else{
                        if(nc[i]=="+"){
                            b[r]=na[i];
                            cc[r]="+";
                            if(ncn==i+1)
                                b[r+1]=na[i+1];
                            r++;
                            }
                        if(nc[i]=="-"){
                            b[r]=na[i];
                            cc[r]="-";
                            if(ncn==i+1)
                                b[r+1]=na[i+1];
                            r++;
                        }
                    }
                }
                else{
                    if(nc[i]=="*"||nc[i]=="/"){
                        if(nc[i]=="*"){
                            b[r]=b[r]*na[i+1];
                        }
                        if(nc[i]=="/"){
                            b[r]=b[r]/na[i+1];
                        }
                    }
                    else{
                        if(nc[i]=="+"){
                            cc[r]="+";
                            r++;
                            b[r]=na[i+1];
                            rn=0;
                            }
                        if(nc[i]=="-"){
                            cc[r]="-";
                            r++;
                            b[r]=na[i+1];
                            rn=0;
                        }
                    }
                    }
            }
            if(cc[0]=="+"){
                re=b[0]+b[1];
            }
            if(cc[0]=="-"){
                re=b[0]-b[1];
            }
                    for(int i=1;i<r;i++){
                if(cc[i]=="+"){
                    re=re+b[i+1];
                                }
                if(cc[i]=="-"){
                    re=re-b[i+1];
                                }
            }
         }
    }

     主代码:

    package fiv;
    import java.awt.*; 
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.io.IOException;
    
    import javax.swing.*; 
    public  class rrr extends JFrame implements ActionListener{
        JLabel L1=new JLabel("题目:");
        JLabel L2=new JLabel("输入题目个数:"); 
        JLabel L3=new JLabel("输入答案");
        public JLabel L4=new JLabel("        "); 
        JLabel L5=new JLabel("        ");
        JLabel L6=new JLabel("运算符个数:");
        JTextField T1=new JTextField(6);
        JTextField T2=new JTextField("1");
        JTextField T3=new JTextField("1");
        JButton B1=new JButton("开始");
        JButton B2=new JButton("重新开始");
        JButton B3=new JButton("下一题");
        JButton B4=new JButton("提交");
        JPanel p1=new JPanel();
        int n=0,ri,m=0,p,cn=1;
        int a[][]=new int[100][5];
        int b[]=new int[100];
        int d[]=new int[100];
        int e[]=new int[100];
        int f[]=new int[100];
        String s;
        String c[][]=new String[100][4];
        String ch[]={"+","-","*","/"};
        public static void main(String[] args) {
            new rrr();
            
            // TODO Auto-generated method stub
        }
        rrr()
        {
            
                this.setTitle("四则运算");
                this.setSize(400,400);
                this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                this.add(p1); 
                p1.setLayout(null);
                L2.setBounds(50, 20, 90, 20);
                p1.add(L2);
                T2.setBounds(150, 20, 90, 30);
                p1.add(T2);
               L1.setBounds(50, 70, 90, 30);
                p1.add(L1);
                L4.setBounds(150, 70, 170, 30);
                p1.add(L4);
                L3.setBounds(50,120, 90, 30);
                p1.add(L3);
                T1.setBounds(150, 120, 90, 30);
                p1.add(T1);
                L6.setBounds(50,170, 90, 30);
                p1.add(L6);
                T3.setBounds(150, 170, 90, 30);
                p1.add(T3);
                B1.setBounds(50, 220, 90, 30);
                p1.add(B1);
                B1.addActionListener(this);
                B2.setBounds(150, 220, 90, 30);
                p1.add(B2);
                B2.setEnabled(false);
                B2.addActionListener(this);
                B3.setBounds(50,270,90,30);
                p1.add(B3);
                B3.setEnabled(false);
                B3.addActionListener(this);
                B4.setBounds(150, 270,90,30);
                p1.add(B4);
                B4.setEnabled(false);
                B4.addActionListener(this);
                L5.setBounds(50, 320, 220, 30);
                p1.add(L5);
                this.setVisible(true);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭窗体     
                B1.addActionListener(this);
        }
        public void actionPerformed(ActionEvent ee){
            if(ee.getSource()==B1){
                String s1=new String("输入的个数不能大于5或者小于1");
                String s2=new String("运算符数不能大于4或者小于1");
                m=0;
                ri=0;
                try{n=(int) Double.parseDouble(T2.getText());
                cn=(int) Double.parseDouble(T3.getText());
                }
                catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(null,"输入错误","注意",JOptionPane.INFORMATION_MESSAGE);
               }
                if(cn>4||cn<1||n>5||n<1){
               if((n>5&&cn>4)||(n>5&&cn<1)||(n<1&&cn<1)||((n<1&&cn>4))){
                        JOptionPane.showMessageDialog(null,s1+"且"+s2,"注意",JOptionPane.INFORMATION_MESSAGE);
                    }
                else if(cn>4||cn<1)
                    {
                        JOptionPane.showMessageDialog(null,s2,"注意",JOptionPane.INFORMATION_MESSAGE);
                    }
                else JOptionPane.showMessageDialog(null,s1,"注意",JOptionPane.INFORMATION_MESSAGE);
                }
                else {begin();
                B1.setEnabled(false);
                B2.setEnabled(true);
                B3.setEnabled(true);
                B4.setEnabled(true);
                T2.setEditable(false);;
                T3.setEditable(false);
                L5.setText(" ");
               }    
            }
            if(ee.getSource()==B2){
                B1.setEnabled(true);
                B2.setEnabled(false);
                B3.setEnabled(false);
                B4.setEnabled(false);
                T2.setEditable(true);
                T3.setEditable(true);
                T1.setText("");
                T2.setText("1");
                T3.setText("1");
            }
            if(ee.getSource()==B3){
                    begin();
                    T1.setText("");
                    B4.setEnabled(true);
                    L5.setText(" ");
            }
            if(ee.getSource()==B4){
               answer();
                B4.setEnabled(false);
                if(m==n){
                    B2.setEnabled(true);
                    B3.setEnabled(false);
                    B4.setEnabled(false);
                    JOptionPane.showMessageDialog(null,"答对了"+ri+"题,答错了"+(n-ri)+"题","成绩",JOptionPane.INFORMATION_MESSAGE);
                }
            }
      }
        void begin(){
            for(int i=0;i<cn+1;i++)
                a[m][i]=(int)(Math.random()*200-100);
            for(int i=0;i<cn;i++)
                c[m][i]=ch[(int)(Math.random()*3)];
            for(int i=1;i<cn;i++){
            while(a[m][i]==0&&c[m][i-1]==ch[3]){
                a[m][i]=(int)(Math.random()*200-100);
            }
           }
            s=Double.toString(a[m][0]);
            for(int j=1;j<cn+1;j++)
            {
            if(a[m][j]<0){
                s=s+c[m][j-1]+"("+Double.toString(a[m][j])+")";
                }
            else {
                s=s+c[m][j-1]+Double.toString(a[m][j]);
            }
            }
            s=s+"=";
            L4.setText(s);
        }
        void answer(){
            calc re=new calc(a,c,m,cn);
            double an = 0;
           try{  an=Double.parseDouble(T1.getText());
           }
           catch(NumberFormatException e){
               JOptionPane.showMessageDialog(null,"输入错误","注意",JOptionPane.INFORMATION_MESSAGE);
           }
           re.setting();
           if((re.re)==an){
                L5.setText("答案正确!");
                ri++;
            }
            else{
                L5.setText("答案错误!正确答案是"+re.re);
            }
            m++;
        }
    }

    2.测试结果:

    对计算功能进行测试:

    package fiv;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    public class calcTest {
        public int[][] test={{7,40,30,20},{5,8,4},{50,75,35,46,53},{80,60,25,35,45},{25,50,5,3},{40,8,5,6,2}};
        public String[][] tc={{"+","-","*"},{"*","+"},{"*","-","+","*"},{"+","*","*","-"},{"*","/","+"},{"/","+","*","/"}};
        calc te=new calc(test,tc,0,3);
        @Test
        public void testSetting1() {
            calc te=new calc(test,tc,0,3);
            te.setting();
            assertEquals(-553,te.re);
        }
        @Test
        public void testSetting2() {
            calc te=new calc(test,tc,1,2);
            te.setting();
            assertEquals(44,te.re);
        }
        @Test
        public void testSetting3() {
            calc te=new calc(test,tc,2,4);
            te.setting();
            assertEquals(6153,te.re);
        }
        @Test
        public void testSetting4() {
            calc te=new calc(test,tc,3,4);
            te.setting();
            assertEquals(52535,te.re);
        }
        @Test
        public void testSetting5() {
            calc te=new calc(test,tc,4,3);
            te.setting();
            assertEquals(253,te.re);
        }
        @Test
        public void testSetting6() {
            calc te=new calc(test,tc,5,4);
            te.setting();
            assertEquals(20,te.re);
        }
    }
    View Code

    1、7+40-30*20
    2、5*8+4

    3、50*75-35+46*53

    4、80+60*25*35-45

    5、25*50/5+3

    6、40/8+5*/2

    异常处理:

    对题目个数和运算符个数的异常处理:

    try{n=(int) Double.parseDouble(T2.getText());
                cn=(int) Double.parseDouble(T3.getText());
                }
                catch(NumberFormatException e){
                    JOptionPane.showMessageDialog(null,"输入错误","注意",JOptionPane.INFORMATION_MESSAGE);
               }

    对输入答案框的异常处理:

    try{  an=Double.parseDouble(T1.getText());
           }
           catch(NumberFormatException e){
               JOptionPane.showMessageDialog(null,"输入错误","注意",JOptionPane.INFORMATION_MESSAGE);
           }

    输入框不为数字:

    输入答案不为数字:

    3.问题

    因为单元测试时assertEquals()只能用Int型,所以变量只用了int型,除法有余数时不能准确计算显示结果 

    4.心得感想:

    在本次编程中添加了修改运算符个数的功能,因为之前都是个数固定的运算的代码,所以多个运算符的运算需要重新编写过。一开始我和小伙伴都没有思路怎么,在经过和小伙伴几个的讨论和思考,尝试多种方法后,终于把修改运算符个数来进行加减乘除的代码改好,也了解到结对编程的有用之处。

  • 相关阅读:
    设计一个栈,除了pop与push方法,还支持Min方法,可返回栈元素中的最小值,push、pop、min三个方法的时间复杂度必须是O(1)
    删除单向链表中间的某个节点,假定你只能访问该节点
    找出单向链表中倒数第K个节点
    关于ssh纠错笔记
    3维图像分割显示 标签: 图像分割level set3dmatlab 2015-04-16 17:59 228人阅读 评论(0)
    EM 算法 标签: clusteringalgorithm图像分割EM算法 2015-03-24 21:26 426人阅读 评论(0)
    kmeans算法分割图像 标签: kmeans图像分割算法 2015-03-23 20:02 509人阅读 评论(0)
    随机数
    IDL 日期时间函数整理
    显示程序运行时间_matlab
  • 原文地址:https://www.cnblogs.com/poipoi/p/4487523.html
Copyright © 2011-2022 走看看