• 练习5.1更新——四则运算 测试与封装


      1 package ppackage;
      2 import java.util.Random;
      3 import java.util.Scanner;
      4 import java.util.regex.Pattern;
      5 
      6 
      7 public class Colc {
      8     public int sighfh,fuhaosl;
      9     public float sighsz1,sighsz2,answer2,sum2=0,sum3=0;
     10     public float []sighsz=new float[30];
     11     public String []fuhaoq={"+","-","*","/"};
     12     public String []fuhao=new String[30];
     13     public String fuhao2,answer1,s;
     14     public String []zhengshi=new String[200];
     15     public String []first=new String[100];
     16     public String []second=new String[100];
     17     public String []three=new String[100];
     18     public String temp1="null";
     19     public int i=0,jia=0,jian=0,q=0,x=0,chen=0,chu=0,temp2,j=0,c,op=0;
     20     public static float sum=0;
     21     
     22      String Colc(String x)
     23      {
     24         String[]xx=new String[200];
     25         for(c=0;c<10;c++)
     26             xx[c]="";
     27         c=0;
     28         for(int x1=0;x1<Integer.parseInt(x);x1++)
     29         {
     30         Random z=new Random();
     31         sighsz1=z.nextFloat()*10;
     32 
     33         int k=0;
     34         sighsz1=(float)(Math.round(sighsz1*100))/100;
     35 
     36         zhengshi[k]=String.valueOf(sighsz1);
     37 
     38         fuhaosl=z.nextInt(3)+1;
     39     
     40 
     41         for(i=1;i<=fuhaosl;i++)
     42         {                                                                    
     43             sighfh=z.nextInt(4);
     44 
     45             switch(sighfh)
     46             {
     47             case 0:zhengshi[k]+="	+	";sighsz[k]=z.nextFloat()*100;sighsz[k]=(float)(Math.round(sighsz[k]*100))/100;
     48             zhengshi[k]+=String.valueOf(sighsz[k]);
     49 
     50             jia++;
     51 
     52             break;
     53             case 1:zhengshi[k]+="	-	";
     54             sighsz[k]=z.nextFloat()*100;sighsz[k]=(float)(Math.round(sighsz[k]*100))/100;
     55             zhengshi[k]+=String.valueOf(sighsz[k]);
     56 
     57             jian++;
     58             break;
     59             case 2:zhengshi[k]+="	*	";
     60             sighsz[k]=z.nextFloat()*100;sighsz[k]=(float)(Math.round(sighsz[k]*100))/100;
     61             zhengshi[k]+=String.valueOf(sighsz[k]);
     62 
     63             chen++;
     64             break;
     65             case 3:zhengshi[k]+="	/	";
     66             sighsz[k]=z.nextFloat()*100;sighsz[k]=(float)(Math.round(sighsz[k]*100))/100;
     67             zhengshi[k]+=String.valueOf(sighsz[k]);
     68 
     69             chu++;
     70             break;
     71 
     72             }
     73 
     74         }
     75         zhengshi[k]+="	=	";
     76         
     77         String temp="";
     78     
     79 
     80         System.out.printf("%s",zhengshi[k]);
     81         String []first=new String[200];
     82         String []second=new String[200];
     83 
     84         String[] zhengshi1=new String[200];
     85         int q=0,i1=0,i2=0,j=0;
     86         i=0;
     87 
     88         temp = zhengshi[k];
     89     
     90         zhengshi1= temp.trim().split("	");
     91         while(op<=fuhaosl*2+1)
     92         {
     93             xx[c]+=zhengshi1[op];
     94     
     95             op++;
     96         }
     97         
     98         int y=0;
     99         while(zhengshi1[y]==null)
    100         {
    101             if(zhengshi1[y].equals("/"))
    102             {
    103                 while(Float.parseFloat(zhengshi1[y+1])/1==0)
    104                 {
    105                     zhengshi1[y+1]=String.valueOf((float)(Math.round(sighsz1*100))/100);
    106                 }
    107             }
    108             y++;
    109         }
    110         
    111         i=0;j=0;k=0;
    112         while(zhengshi1[i].equals("=")==false )
    113         {
    114             
    115             
    116             try
    117             {
    118                 if(Float.parseFloat(zhengshi1[i])>=0 && Float.parseFloat(zhengshi1[i])<=100)
    119                 {
    120                     first[j]=zhengshi1[i];
    121                     j++;i++;
    122                 }
    123             }
    124             catch(NumberFormatException E)
    125             {
    126                 if(second[0]==null)
    127                 {
    128                     second[k]=zhengshi1[i];
    129         
    130                     k++;i++;
    131                 }
    132                 else
    133                 {
    134                 
    135                     if(compare(second[k-1],zhengshi1[i],first))
    136                     {
    137         
    138                             if(first[k].equals(null))
    139                             {
    140                                 second[k]=zhengshi1[i];
    141                                 k++;i++;
    142                             }
    143                             else
    144                             {
    145                                 if(second[k-1].equals("+"))
    146                                 {
    147                                     sum=Float.parseFloat(first[k-1])+Float.parseFloat(first[k]);
    148                         
    149                                     first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
    150             
    151                                     j--;i++;
    152                                 }
    153                                 else if(second[k-1].equals("-"))
    154                                 {
    155                                     sum=Float.parseFloat(first[k-1])-Float.parseFloat(first[k]);
    156     
    157                                     first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
    158 
    159                                     j--;i++;
    160                                 }
    161                                 else if(second[k-1].equals("*"))
    162                                 {
    163                                     sum=Float.parseFloat(first[k-1])*Float.parseFloat(first[k]);
    164 
    165                                     first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
    166 
    167                                     j--;i++;
    168                                 }
    169                                 else if(second[k-1].equals("/"))
    170                                 {
    171                                     sum=Float.parseFloat(first[k-1])/Float.parseFloat(first[k]);
    172         
    173                                     first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
    174 
    175                                     j--;i++;
    176                                 }
    177                                     
    178                             }
    179                         }
    180                         
    181                     
    182                     else
    183                     {
    184                         
    185                         second[k]=zhengshi1[i];
    186 
    187                         
    188                         i++;k++;
    189                         
    190                     }
    191                     
    192                 }
    193             }
    194     
    195             
    196 
    197 
    198         }
    199         
    200     
    201             
    202             while(second[0]!=null)
    203             {
    204 
    205                 if(second[k-1].equals("+"))
    206                 {
    207                     sum=Float.parseFloat(first[k-1])+Float.parseFloat(first[k]);
    208     
    209                     first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    210                     j--;k=k-1;i++;
    211         
    212                 }
    213                 else if(second[k-1].equals("-"))
    214                 {
    215                     sum=Float.parseFloat(first[k-1])-Float.parseFloat(first[k]);
    216 
    217                     first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    218                     j--;k=k-1;i++;
    219                     
    220         
    221                 }
    222                 else if(second[k-1].equals("*"))
    223                 {
    224                     sum=Float.parseFloat(first[k-1])*Float.parseFloat(first[k]);
    225         
    226                     first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    227                     j--;k=k-1;i++;
    228                     
    229 
    230                 }
    231                 else if(second[k-1].equals("/"))
    232                 {
    233                     sum=Float.parseFloat(first[k-1])/Float.parseFloat(first[k]);
    234 
    235                     first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    236                     j--;k=k-1;i++;
    237 
    238                 }
    239                 
    240             }
    241             sum=(float)(Math.round(sum*100))/100;
    242             System.out.print("
    答案为:"+sum+"	
    ");
    243             c++;
    244             xx[c]=String.valueOf(sum);c++;op=0;
    245         }
    246         int qw=0;
    247         return String.valueOf(sum);
    248 
    249         
    250         
    251     }
    252     boolean compare(String x1,String x2,String x3[]) //true 出栈,flase 进栈
    253     {
    254         int i,j,k;
    255         if(x2.equals("+") || x2.equals("-"))
    256             return true;
    257         else
    258         {
    259             if(x1.equals("*") || x1.equals("/"))
    260             {
    261                 return true;
    262             }
    263             else
    264             {
    265                 return false;
    266             }
    267         }
    268     }
    269     static String certain(String ts)
    270     {
    271         Pattern pattern = Pattern.compile("[0-9]*");
    272         String ts1=ts;
    273         if(pattern.matcher(ts1).matches())
    274         {
    275             return "true";
    276         }
    277         else
    278         {
    279             System.out.print("输入的数值有误
    请重新输入:");
    280             return "false";
    281         }
    282         
    283     }
    284     public static void main(String[]args)
    285     {
    286         Scanner in =new Scanner(System.in);
    287         String panding;
    288         String numm;
    289         System.out.print("请输入题目数量:");
    290         do
    291         {
    292             numm=in.next();
    293             panding=certain(numm);
    294         }while(panding.equals("false"));
    295         for(int oo=1;oo<=Integer.parseInt(numm);oo++)
    296         {
    297             Colc aa=new Colc();
    298             String sum2=aa.Colc("1");
    299             System.out.print("输入答案:");
    300             String an=in.next();
    301             if(an.equals(sum2))
    302             {
    303                 System.out.print("√
    ");
    304             }
    305             else
    306             {
    307                 System.out.print("×
    ");
    308             }
    309         }
    310         
    311     }
    312 }
    Colc
      1 package ppackage;
      2 
      3 public class Calcute {
      4     public int sighfh,fuhaosl;
      5     public float sighsz1,sighsz2,answer2,sum2=0,sum3=0;
      6     public float []sighsz=new float[30];
      7     public String []fuhaoq={"+","-","*","/"};
      8     public String []fuhao=new String[30];
      9     public String fuhao2,answer1,s;
     10     public String []zhengshi=new String[200];
     11     public String []first=new String[100];
     12     public String []second=new String[100];
     13     public String []three=new String[100];
     14     public String temp1="null";
     15     public int i=0,jia=0,jian=0,q=0,x=0,chen=0,chu=0,temp2,j=0,c,op=0,k=0;
     16     public float sum=0;
     17     float Calcute(char oop,String []data)
     18     {
     19         String[]xx=new String[200];
     20         for(c=0;c<10;c++)
     21             xx[c]="";
     22         if(data[1].equals("0")) throw new ArithmeticException();
     23         zhengshi[k]=data[0]+"	"+oop+"	"+data[1];
     24         zhengshi[k]+="	=	";
     25         String temp="";
     26         System.out.printf("%s",zhengshi[k]);
     27         String []first=new String[200];
     28         String []second=new String[200];
     29 
     30         String[] zhengshi1=new String[200];
     31         int q=0,i1=0,i2=0,j=0;
     32         i=0;
     33 
     34         temp = zhengshi[k];
     35         zhengshi1= temp.trim().split("	");
     36         while(op<=1+1)
     37         {
     38             xx[c]+=zhengshi1[op];
     39             op++;
     40         }
     41 
     42         int y=0;
     43         while(zhengshi1[y]==null)
     44         {
     45             if(zhengshi1[y].equals("/"))
     46             {
     47                 while(Float.parseFloat(zhengshi1[y+1])/1==0)
     48                 {
     49                     zhengshi1[y+1]=String.valueOf((float)(Math.round(sighsz1*100))/100);
     50                 }
     51             }
     52             y++;
     53         }
     54 
     55         i=0;j=0;k=0;
     56         while(zhengshi1[i].equals("=")==false )
     57         {
     58             try
     59             {
     60                 if(Float.parseFloat(zhengshi1[i])>=0 && Float.parseFloat(zhengshi1[i])<=100)
     61                 {
     62                     first[j]=zhengshi1[i];
     63                     j++;i++;
     64                 }
     65             }
     66             catch(NumberFormatException E)
     67             {
     68                 if(second[0]==null)
     69                 {
     70                     second[k]=zhengshi1[i];
     71                     k++;i++;
     72                 }
     73                 else
     74                 {
     75 
     76                     if(compare(second[k-1],zhengshi1[i],first))
     77                     {
     78 
     79                         if(first[k].equals(null))
     80                         {
     81                             second[k]=zhengshi1[i];
     82                             k++;i++;
     83                         }
     84                         else
     85                         {
     86                             if(second[k-1].equals("+"))
     87                             {
     88                                 sum=Float.parseFloat(first[k-1])+Float.parseFloat(first[k]);
     89                                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
     90                                 j--;i++;
     91                             }
     92                             else if(second[k-1].equals("-"))
     93                             {
     94                                 sum=Float.parseFloat(first[k-1])-Float.parseFloat(first[k]);
     95                                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
     96                                 //System.out.print("
    答案暂时是:"+sum+"
    ");
     97                                 j--;i++;
     98                             }
     99                             else if(second[k-1].equals("*"))
    100                             {
    101                                 sum=Float.parseFloat(first[k-1])*Float.parseFloat(first[k]);
    102                                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
    103                                 //System.out.print("
    答案暂时是:"+sum+"
    ");
    104                                 j--;i++;
    105                             }
    106                             else if(second[k-1].equals("/"))
    107                             {
    108                                 sum=Float.parseFloat(first[k-1])/Float.parseFloat(first[k]);
    109                                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
    110                                 j--;i++;
    111                             }
    112 
    113                         }
    114                     }
    115 
    116 
    117                     else
    118                     {
    119 
    120                         second[k]=zhengshi1[i];
    121 
    122                         i++;k++;
    123 
    124                     }
    125 
    126                 }
    127             }
    128 
    129 
    130 
    131 
    132         }
    133 
    134         while(second[0]!=null)
    135         {
    136 
    137             if(second[k-1].equals("+"))
    138             {
    139                 sum=Float.parseFloat(first[k-1])+Float.parseFloat(first[k]);
    140 
    141                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    142                 j--;k=k-1;i++;
    143             }
    144             else if(second[k-1].equals("-"))
    145             {
    146                 sum=Float.parseFloat(first[k-1])-Float.parseFloat(first[k]);
    147 
    148                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    149                 j--;k=k-1;i++;
    150 
    151 
    152             }
    153             else if(second[k-1].equals("*"))
    154             {
    155                 sum=Float.parseFloat(first[k-1])*Float.parseFloat(first[k]);
    156 
    157                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    158                 j--;k=k-1;i++;
    159 
    160 
    161             }
    162             else if(second[k-1].equals("/"))
    163             {
    164                 sum=Float.parseFloat(first[k-1])/Float.parseFloat(first[k]);
    165 
    166                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    167                 j--;k=k-1;i++;
    168 
    169             }
    170 
    171         }
    172         sum=(float)(Math.round(sum*100))/100;
    173         System.out.print("
    答案为:"+sum+"	
    ");
    174         c++;
    175         xx[c]=String.valueOf(sum);c++;op=0;
    176         return sum;
    177     }
    178     int qw=0;
    179 
    180 
    181 
    182     boolean compare(String x1,String x2,String x3[]) //true 出栈,flase 进栈
    183     {
    184         int i,j,k;
    185         if(x2.equals("+") || x2.equals("-"))
    186             return true;
    187         else
    188         {
    189             if(x1.equals("*") || x1.equals("/"))
    190             {
    191                 return true;
    192             }
    193             else
    194             {
    195                 return false;
    196             }
    197         }
    198     }
    199 
    200     public static void main(String[] args) {
    201 
    202         char oop='/';
    203         String[]data={"8.2","2"};
    204         Calcute aa=new Calcute();
    205         aa.Calcute(oop, data);
    206     }
    207 
    208 }
    Calcute
     1 package ppackage;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import org.junit.Test;
     6 
     7 public class CalcuteTest {
     8 
     9     @Test
    10     public void testJia() {
    11         String[]data={"10.0","5.5"};
    12         char oop='+';
    13         Calcute c=new Calcute();
    14         float answer=c.Calcute(oop, data);
    15         System.out.println(answer);
    16         assertEquals(15.5,answer,100);
    17     }
    18     @Test
    19     public void testJian() {
    20         String[]data={"10.0","5.0"};
    21         char oop='-';
    22         Calcute c=new Calcute();
    23         //c.Calcute(oop, data);
    24         float answer=c.Calcute(oop, data);
    25         System.out.println(answer);
    26         assertEquals(5,answer,100);
    27     }
    28     @Test
    29     public void testChen() {
    30         String[]data={"10.0","5.0"};
    31         char oop='*';
    32         Calcute c=new Calcute();
    33         float answer=c.Calcute(oop, data);
    34         System.out.println(answer);
    35         assertEquals(50.00,answer,100);
    36     }
    37     @Test
    38     public void testChu() {
    39         String[]data={"10.0","5.0"};
    40         char oop='/';
    41         Calcute c=new Calcute();
    42         float answer=c.Calcute(oop, data);
    43         System.out.println(answer);
    44         assertEquals(2,answer,100);
    45     }
    46     @Test(expected = ArithmeticException.class)
    47     public void testException() {
    48         String[]data={"10.0","0"};
    49         char oop='/';
    50         Calcute c=new Calcute();
    51         float answer=c.Calcute(oop, data);
    52     }
    53 
    54 }
    CalcuteTest
      1 package Exception1;
      2 
      3 
      4 public class Calcute {
      5     public int sighfh,fuhaosl;
      6     public float sighsz1,sighsz2,answer2,sum2=0,sum3=0;
      7     public float []sighsz=new float[30];
      8     public String []fuhaoq={"+","-","*","/"};
      9     public String []fuhao=new String[30];
     10     public String fuhao2,answer1,s;
     11     public String []zhengshi=new String[200];
     12     public String []first=new String[100];
     13     public String []second=new String[100];
     14     public String []three=new String[100];
     15     public String temp1="null";
     16     public int i=0,jia=0,jian=0,q=0,x=0,chen=0,chu=0,temp2,j=0,c,op=0,k=0;
     17     public float sum=0;
     18     float Calcute(char oop,String []data) throws clException,fsException 
     19     {
     20         char opo=oop;
     21         if(Float.parseFloat(data[0])<0)  
     22         {  
     23             throw new fsException("您输入的是"+data[0]+",规定除数不能为负数!");//抛出异常  
     24         }  
     25         else if(Float.parseFloat(data[1])<0)  
     26         {  
     27             throw new fsException("您输入的是"+data[1]+",规定除数不能为负数!");//抛出异常  
     28         }  
     29         if(opo=='/')
     30         {
     31         if(Float.parseFloat(data[1])==0 )  
     32         {  
     33             throw new clException("您输入的是"+data[1]+",除数不能为0!");  
     34         }  
     35         }
     36         if(opo!='+' && opo!='-' && opo!='*' && opo!='/' )
     37         {
     38             throw new clException("您输入的是"+opo+",符号不能为非法字符!");  
     39         }
     40         String[]xx=new String[200];
     41         for(c=0;c<10;c++)
     42             xx[c]="";
     43         if(data[1].equals("0")) throw new ArithmeticException();
     44         zhengshi[k]=data[0]+"	"+oop+"	"+data[1];
     45         zhengshi[k]+="	=	";
     46         String temp="";
     47         System.out.printf("%s",zhengshi[k]);
     48         String []first=new String[200];
     49         String []second=new String[200];
     50 
     51         String[] zhengshi1=new String[200];
     52         int q=0,i1=0,i2=0,j=0;
     53         i=0;
     54 
     55         temp = zhengshi[k];
     56         zhengshi1= temp.trim().split("	");
     57         while(op<=1+1)
     58         {
     59             xx[c]+=zhengshi1[op];
     60             op++;
     61         }
     62 
     63         int y=0;
     64         while(zhengshi1[y]==null)
     65         {
     66             if(zhengshi1[y].equals("/"))
     67             {
     68                 while(Float.parseFloat(zhengshi1[y+1])/1==0)
     69                 {
     70                     zhengshi1[y+1]=String.valueOf((float)(Math.round(sighsz1*100))/100);
     71                 }
     72             }
     73             y++;
     74         }
     75 
     76         i=0;j=0;k=0;
     77         while(zhengshi1[i].equals("=")==false )
     78         {
     79             try
     80             {
     81                 if(Float.parseFloat(zhengshi1[i])>=0 && Float.parseFloat(zhengshi1[i])<=100)
     82                 {
     83                     first[j]=zhengshi1[i];
     84                     j++;i++;
     85                 }
     86             }
     87             catch(NumberFormatException E)
     88             {
     89                 if(second[0]==null)
     90                 {
     91                     second[k]=zhengshi1[i];
     92                     k++;i++;
     93                 }
     94                 else
     95                 {
     96 
     97                     if(compare(second[k-1],zhengshi1[i],first))
     98                     {
     99 
    100                         if(first[k].equals(null))
    101                         {
    102                             second[k]=zhengshi1[i];
    103                             k++;i++;
    104                         }
    105                         else
    106                         {
    107                             if(second[k-1].equals("+"))
    108                             {
    109                                 sum=Float.parseFloat(first[k-1])+Float.parseFloat(first[k]);
    110                                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
    111                                 j--;i++;
    112                             }
    113                             else if(second[k-1].equals("-"))
    114                             {
    115                                 sum=Float.parseFloat(first[k-1])-Float.parseFloat(first[k]);
    116                                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
    117                                 //System.out.print("
    答案暂时是:"+sum+"
    ");
    118                                 j--;i++;
    119                             }
    120                             else if(second[k-1].equals("*"))
    121                             {
    122                                 sum=Float.parseFloat(first[k-1])*Float.parseFloat(first[k]);
    123                                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
    124                                 //System.out.print("
    答案暂时是:"+sum+"
    ");
    125                                 j--;i++;
    126                             }
    127                             else if(second[k-1].equals("/"))
    128                             {
    129                                 sum=Float.parseFloat(first[k-1])/Float.parseFloat(first[k]);
    130                                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=zhengshi1[i];
    131                                 j--;i++;
    132                             }
    133 
    134                         }
    135                     }
    136 
    137 
    138                     else
    139                     {
    140 
    141                         second[k]=zhengshi1[i];
    142 
    143                         i++;k++;
    144 
    145                     }
    146 
    147                 }
    148             }
    149 
    150 
    151 
    152 
    153         }
    154 
    155         while(second[0]!=null)
    156         {
    157 
    158             if(second[k-1].equals("+"))
    159             {
    160                 sum=Float.parseFloat(first[k-1])+Float.parseFloat(first[k]);
    161 
    162                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    163                 j--;k=k-1;i++;
    164             }
    165             else if(second[k-1].equals("-"))
    166             {
    167                 sum=Float.parseFloat(first[k-1])-Float.parseFloat(first[k]);
    168 
    169                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    170                 j--;k=k-1;i++;
    171 
    172 
    173             }
    174             else if(second[k-1].equals("*"))
    175             {
    176                 sum=Float.parseFloat(first[k-1])*Float.parseFloat(first[k]);
    177 
    178                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    179                 j--;k=k-1;i++;
    180 
    181 
    182             }
    183             else if(second[k-1].equals("/"))
    184             {
    185                 sum=Float.parseFloat(first[k-1])/Float.parseFloat(first[k]);
    186 
    187                 first[k-1]=String.valueOf(sum);first[k]=null;second[k-1]=null;
    188                 j--;k=k-1;i++;
    189 
    190             }
    191 
    192         }
    193         sum=(float)(Math.round(sum*100))/100;
    194         System.out.print("
    答案为:"+sum+"	
    ");
    195         c++;
    196         xx[c]=String.valueOf(sum);c++;op=0;
    197         return sum;
    198     }
    199     int qw=0;
    200 
    201 
    202 
    203     boolean compare(String x1,String x2,String x3[]) //true 出栈,flase 进栈
    204     {
    205         int i,j,k;
    206         if(x2.equals("+") || x2.equals("-"))
    207             return true;
    208         else
    209         {
    210             if(x1.equals("*") || x1.equals("/"))
    211             {
    212                 return true;
    213             }
    214             else
    215             {
    216                 return false;
    217             }
    218         }
    219     }
    220 
    221     public static void main(String[] args) {
    222 
    223         char oop='/';
    224         String[]data={"8.2","2"};
    225         Calcute aa=new Calcute();
    226         try{
    227         aa.Calcute(oop, data);
    228         }
    229         catch(clException e)
    230         {
    231             System.out.println(e.getMessage());  
    232         }
    233         catch(fsException e)
    234         {
    235             System.out.println(e.getMessage());  
    236         }
    237         catch(NumberFormatException e)
    238         {
    239             System.out.println("您输入的是"+data[0]+','+data[1]+",规定除数不能为非法字符!");  
    240         }
    241     }
    242 
    243 }
    Calcute
    1 package Exception1;
    2 
    3 public class clException extends Exception{
    4     public clException(String mes)
    5     {
    6         super(mes);
    7     }
    8 }
    clException
    1 package Exception1;
    2 
    3 public class fsException extends Exception{
    4     public fsException(String mes)
    5     {
    6         super(mes);
    7     }
    8 }
    fsException
    1 package Exception1;
    2 
    3 public class zfException extends Exception{
    4         public zfException(String mes)
    5         {
    6             super(mes);
    7         }
    8 }
    zfException

    以上3条程序代码分别是:

    1、主程序;

    2、计算功能模块程序;

    3、单元测试程序;

    -(1)黑盒子测试.这个测试主要就是以用户角度测试代码的功能与用途:

    测试用例

    输入条件

    有效等价类

    无效等价类

    代码途径

    题目有效性判断

    只允许数字

    10

    A

    Line269-283

    输入字符的有效性判断

    (10,2,A)

    10,2

    A

    题目数量

    小于等于100

    1-100

    >100

    参数类型

    String[],char

    {“100”,”200”},+,/

    ‘100’,’100’

    答案类型

    匹配

    正确√

    错误×

    测试对象

    测试说明

    测试数据

    测试结果

    题目数量

    输入非法数据

    Abc

    输入不正确,请从新输入:

    题目数量

    输入数据过大

    101

    抛出异常,提示错误

    加减乘除计算

    输入符号有误

    10++20

    提示输入有误

    加减乘除计算

    输入数值有误

    10a+20bd

    提示输入有误

    计算范围

    精确度

    12.0和12和12.00

    正确

    (2)白盒子测试。对软件的过程性细节做细致的检查。

    测试用例

    用例说明

    通过途径

    实际结果

    加法处理

    10.0+5.5

    Line9-17

    5.5

    减法处理

    10.0-5.5

    Line18-27

    4.5

    乘法处理

    10.0*5.0

    Line28-37

    50.0

    除法处理

    10.0/5.0

    Line37-45

    2.0

    除零处理

    10.0/0

    Line46-52

    抛出异常

    代码覆盖率

    100%

    100%

    100%

     

    (3)异常处理测试(自定义异常,非用户自定义异常。

    异常类型 用例说明 异常反馈  
    clException(除零异常) String[]data={"8.2","0"};||String[]data={"0","2"}; 您输入的是0,规定除数不能为0!  
    fsException(负数异常) String[]data={"8.2","-1"};||String[]data={"-1","2"}; 您输入的是-1,规定除数不能为负数!  
    zfException(非法字符异常) char oop='a'; 您输入的是a,符号不能为非法字符!  
           
    NumberFormatException(除数非法字符异常) String[]data={"a","4"};||String[]data={"4","a"}; 您输入的是a,4,规定除数不能为非法字符!  

     代码主要来源于测试类程序:由于截屏的屏幕的原因,无法全部截图下来,请参考上面的折叠代码

    主要测试结果,及其分析截图:

    我们团队的工作照片:

    最后总结:

    本次实验总体而言,对于我本人来说,难度着实很大,因为是第一次接触这一类测试(单元测试,黑白盒测试),一开始我们并不明白这一些测试到底什么情况,做起来也非常棘手,一度让我和我的小伙伴在进度上停止下来了,总体上我个人感觉对本次实验,不是很满意,自己还有需要需要改进的地方,还需要我和我的小伙伴多下点功夫才行。同时需要说明一点,由于本次实验,老师说需要交换队友,我们团队一开始是尝试过交换队友的,但是在前期出现了许多的沟通障碍,因为彼此间对程序的理解程度存在障碍,所以后面我们决定本次实验,我和我的小伙伴一直觉得,让我和我上一位队友一起带着本次实验新的小伙伴一起工作,初步先让新的成员先熟悉代码。然后再逐步进行下面的工作。不管怎样,经过本次实验我们都发现了许多问题,还存在许多地方需要改进。所以我们团队会继续努力,力争在接下来的实验能完成的更好。

    本次实验,与我并肩作战的小伙伴是:铭琪姐姐:http://www.cnblogs.com/mikky/

  • 相关阅读:
    搭建zabbix监控
    liunx 下ctrl+D问题解决方案
    linux配置双线策略
    Discuz! X2.5读写分离
    慢谈MYSQL常用SQL语句
    CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境
    自动抓包shell脚本
    zabbix实施部署原理架构
    ftp搭建教程
    DNS搭建教程
  • 原文地址:https://www.cnblogs.com/kevinkeke/p/4469349.html
Copyright © 2020-2023  润新知