四则运算-进度二

 

 实验目的:这次是对上一次代码的进一步实现,增加新的功能,使功能更加完善,主要对功能进行细分,包括只做加减法加减法中是否做负数得,成除法中是否有余数,可以自己设置做题范围大小,可以设置打印的个数,还可以不做两个数的运算,


    进行多个数的混合运算,还可以进行有括号的运算,这个难度比上一次会大一些,其实只稍微复杂了一  些,  还需要用到数据结构的栈的知识,其他的难度不大。


    实验思路:首先要对进行的功能的细分,让用户选择做那种类型的题目,我选择的是


      1.是做两个数的运算,还是做十个数以内的混合运算,是否有括号。如果做两个数的运算,进行下面 2到5项的选择,


      2.只做加减法,在其中选择是否有负数。


      3.做加减乘除法,选择是做否真分数的题


      4.做真分数的题。


      5.不做真分数的题,选择除法是否有余数。


      6进行多个数的混合运算,利用栈存储产生的随机数,分别用来存储 数,符号和括号。


      7.含括号的混合运算.


        补充(关于含括号的运算)


          1.栈的使用 例如数据结构中的pop(),push()等可以调用Java中的类,不用自己定义。


            Stack <String>num=new Stack <String>();
            Stack <String>fuhao=new Stack<String>();


            2.步骤:(1)产生括号。


              (2)产生完整表达式


              (3)定义两个运算符的优先级。


              (4)进行两个数之间运算。


              (5)利用栈的思想,得出结果(中缀表达式转后缀表达式)。

//混合运算比较难的产生括号的部分


//思路:(1)每个数左右都可能加括号,所以为了方便,产生一个和表达式等长的数组,让每个数都对应一个运算数,设表达式长度为m,创建数组a[m],规定当为a[]=+n,说明在这个数左边加上n个括号,a[]=-n说明在这个数右边产生n个括号,a[]=0说明这个数左右都没有括号,

(2)开始第一个for循环,对创建的数组初始化,然后再写一个循环,括号长度从2,逐步扩展到m,这个是所有可能产生括号的地方,也就是两个数之间加括号,三个数之间加括号,一直到m个数之间加括号。

(3)在里面在for循环,产生随机数,如果随机数产生说需要加括号,这个需要对括号进行匹配看看是否有和他匹配的,比如说,左括号的位置显示要加括号,那就看他后面的括号长度后有没有位置,如果是右括号显示要加括号,那就看他前面的括号长度处有没有一个左括号,

(4)另外,还需要注意整个数组的的和为0,才能保证括号的匹配。

 1 private static int [] chansheng(int num)//随机产生括号
 2     {
 3         int []b=new int[num];
 4         for(int i=0;i<b.length;i++)
 5         {
 6             b[i]=0;
 7         }
 8         Random rd=new Random();
 9         for(int i=2;i<num;i++)
10         {
11             for(int j=0;j<num-i+1;j++)
12             {
13                 int t=rd.nextInt(2);
14                 if(t==1)
15                 {
16                     if(b[j]>=0&&b[j+i-1]<=0)
17                     {
18                         int c=0;
19                         for(int k=j;k<j+i;k++)
20                         {
21                             c+=b[k];
22                         }
23                         if(c==0)
24                         {
25                             b[j]++;
26                             b[j+i-1]--;
27                         }
28                     }
29                     
30                 }
31             }
32         }
33         return b;
34     }
View Code
//这是其中的关于栈的思想,以及将表达式运算的代码.

思路:利用中缀表达式转后缀表达式,然后根据数据结构书上的方法,即可作出.


  这里介绍一下这个方法,首先传入的是一个String类型的表达式,表达式的每个数字的前后都加上空格,创建两个栈,一个用来存取数字,另一个栈用来存去运算符,在表达式后面加上“#”,然后往运算符栈中压入一个“#”,接下来开始循环,如果
遇到的是空字符说明是即将遇到数字,用String类型变量接受这个数字,如果遇到的不是空格,说明遇到的是运算符,判断两个运算符的优先级,如果小于就压栈,如果等于说明是右括号,直接下一个运算,如果是大于,就弹出两个值和一个运算符,
最后得出结果
 1 private static String jisuanbh(int n,String a)//表达式的运算
 2     {
 3         Stack <String>num=new Stack <String>();
 4         Stack <String>fuhao=new Stack<String>();
 5         a+="#";
 6         fuhao.push("#");
 7         char ch;
 8         int i=0;
 9         int s=0;
10         int y=0;
11         ch=a.charAt(i);
12         while(!(ch+"").equals("#") || !fuhao.peek().equals("#"))
13         {
14             if(ch==' ')//如果遇到字符为空,说明遇到数字
15             {
16                 String rn="";//用来记录数据
17                 while(true)
18                 {
19                     ch=a.charAt(++i);
20                     if(ch==' ')
21                     {
22                         break;
23                     }
24                     rn+=ch;
25                 }
26                 if((i+1)<a.length()){
27                     ch=a.charAt(++i);
28                 }
29                 num.push(rn);
30             }
31             else//遇到的是字符
32             {
33                 char comp=youxian(fuhao.peek(),ch+"");//比较两个字符的优先级
34                 if(comp=='='){//说明遇到右括号
35                     fuhao.pop();
36                     if((i+1)<a.length()){
37                         ch=a.charAt(++i);
38                     }
39                 }
40                 else if(comp=='>')//优先级高,弹出两个数和一个运算符,进行运算
41                 {
42                     String st1=num.pop();
43                     String st2=num.pop();
44                     int ai1 = Integer.parseInt(st1);
45                     int ai2 = Integer.parseInt(st2);
46                     String fuh1=fuhao.pop();
47                     char fuh2=fuh1.charAt(0);//将String类型转为char类型
48                     int []sz=new int[2];
49                     sz=tys(ai2,ai1,fuh2);
50                     if(sz[1]==1)//如果运算中有问题,就结束运算
51                     {
52                         return "error";
53                     }
54                     else
55                     {
56                         num.push(sz[0]+"");//将两数结果压入栈中
57                     }
58                 }
59                 else//优先级比较低,把运算符压入栈中
60                 {
61                     fuhao.push(ch+"");
62                     if((i+1)<a.length())
63                     {
64                         ch=a.charAt(++i);
65                     }
66                 }
67             }
68         }
69         return num.pop();
70     }
View Code

下面是全部的代码:

  1 //实现了真分数,和简单的两数相加运算
  2 //2017.3.6 王志伟
  3 //准备实现:
  4 //是否有乘除法;
  5 //是否有括号(最多可以支持十个数参与计算);
  6 //数值范围;
  7 //加减有无负数;
  8 //除法有无余数!
  9 package Demo;
 10 import java.util.Random;
 11 import java.util.Scanner;
 12 import java.util.Stack;
 13 
 14 public class size2 {
 15     public static void main(String[]args)
 16     {
 17         menu();
 18     }
 19     static void menu()
 20     {
 21 
 22         String ch="y";//是否退出循环
 23         int choose=0;//判断是否添加真分数
 24         int []cc1=new int [2];//记录正确和错误的题数
 25         int []cc2=new int [2];//用来接收正确和错误的题数
 26         Scanner scan=new Scanner(System.in);
 27         System.out.println("请选择所做题目的类型:");
 28         System.out.println("1.两个数的加减乘除,除法可以选择有无余数");
 29         System.out.println("2.两个数的加减乘除,包含真分数");
 30         System.out.println("3.两个数的加减法,可以选择有无负数");
 31         System.out.println("4.多个数的加减乘除,含括号");
 32         choose=scan.nextInt();
 33         while(ch=="y")
 34         {
 35                 if(choose==1)
 36                 {
 37                     System.out.println("请选择有无余数:1.无余数    2.有余数");
 38                     int xz=scan.nextInt();
 39                     if(xz==1)
 40                     {
 41                         System.out.println("你想做几个题呢");
 42                         int num=scan.nextInt();
 43                         cc2=simpleys(num, 1);
 44                         cc1[0]+=cc2[0];
 45                         cc1[1]+=cc2[1];
 46                         System.out.println("好棒,是继续努力还是先休息一会,请选择:1.继续。2休息一会");
 47                         {
 48                             int rl=scan.nextInt();
 49                             if(rl==2)
 50                             {
 51                                 ch="z";
 52                                 System.out.println("你一共作答"+(cc1[0]+cc1[1])+"道题,"+"其中共答对了"+cc1[0]+"道题,"+"答错了"+cc1[1]+"道题.");
 53                             }
 54                         }
 55                     }
 56                     else
 57                     {
 58 
 59                         System.out.println("你想做几个题呢");
 60                         int num=scan.nextInt();
 61                         cc2=simpleys(num, 2);
 62                         cc1[0]+=cc2[0];
 63                         cc1[1]+=cc2[1];
 64                         System.out.println("好棒,是继续努力还是先休息一会,请选择:1.继续。2休息一会");
 65                         {
 66                             int rl=scan.nextInt();
 67                             if(rl==2)
 68                             {
 69                                 ch="z";
 70                                 System.out.println("你一共作答"+(cc1[0]+cc1[1])+"道题,"+"其中共答对了"+cc1[0]+"道题,"+"答错了"+cc1[1]+"道题.");
 71                             }
 72                         }
 73                     }
 74                 
 75                 }
 76                 else if(choose==2)
 77                 {
 78                     
 79                     System.out.println("你想做几个题呢");
 80                     int num=scan.nextInt();
 81                     cc2=fuza(num);
 82                     cc1[0]+=cc2[0];
 83                     cc1[1]+=cc2[1];
 84                     System.out.println("好棒,是继续努力还是先休息一会,请选择:1.继续。2休息一会");
 85                     {
 86                         int rl=scan.nextInt();
 87                         if(rl==2)
 88                         {
 89                             ch="z";
 90                             System.out.println("你一共作答"+(cc1[0]+cc1[1])+"道题,"+"其中共答对了"+cc1[0]+"道题,"+"答错了"+cc1[1]+"道题.");
 91                         }
 92                     }
 93                 }
 94                 else if(choose==3)
 95                 {
 96                     System.out.println("请选择是否含有负数:1.无负数    2.有负数");
 97                     int xz=scan.nextInt();
 98                     if(xz==1)
 99                     {
100                         System.out.println("你想做几个题呢");
101                         int num=scan.nextInt();
102                         
103                         cc2=wucc(num,2);
104                         cc1[0]+=cc2[0];
105                         cc1[1]+=cc2[1];
106                         System.out.println("好棒,是继续努力还是先休息一会,请选择:1.继续。2休息一会");
107                         {
108                             int rl=scan.nextInt();
109                             if(rl==2)
110                             {
111                                 ch="z";
112                                 System.out.println("你一共作答"+(cc1[0]+cc1[1])+"道题,"+"其中共答对了"+cc1[0]+"道题,"+"答错了"+cc1[1]+"道题.");
113                             }
114                         }
115                     }
116                     else
117                     {
118 
119                         System.out.println("你想做几个题呢");
120                         int num=scan.nextInt();
121                         
122                         cc2=wucc(num,1);
123                         cc1[0]+=cc2[0];
124                         cc1[1]+=cc2[1];
125                         System.out.println("好棒,是继续努力还是先休息一会,请选择:1.继续。2休息一会");
126                         {
127                             int rl=scan.nextInt();
128                             if(rl==2)
129                             {
130                                 ch="z";
131                                 System.out.println("你一共作答"+(cc1[0]+cc1[1])+"道题,"+"其中共答对了"+cc1[0]+"道题,"+"答错了"+cc1[1]+"道题.");
132                             }
133                         }
134                     }
135                 }
136                 else if(choose==4)
137                 {
138                     System.out.println("你想做几个题呢");
139                     int num=scan.nextInt();
140                     cc2=yunsh(num);
141                     cc1[0]+=cc2[0];
142                     cc1[1]+=cc2[1];
143                     System.out.println("好棒,是继续努力还是先休息一会,请选择:1.继续。2休息一会");
144                     {
145                         int rl=scan.nextInt();
146                         if(rl==2)
147                         {
148                             ch="z";
149                             System.out.println("你一共作答"+(cc1[0]+cc1[1])+"道题,"+"其中共答对了"+cc1[0]+"道题,"+"答错了"+cc1[1]+"道题.");
150                         }
151                     }
152                 }    
153         }
154     
155     }
156     static int[] yunsuan2(int a,int b,int c,int d,int e)//用来实现真分数的四则运算
157     {
158         int []cc1=new int[3];
159         cc1[2]=0;
160         int f=0,g=0,h=0,t=0;
161         if(e==0)
162         {
163             if(b==d)
164             {
165                 f=a+c;
166                 g=b;
167                 if(f<g)
168                 {
169                     h=maxyue(g,f);
170                     f=f/h;
171                     g=g/h;
172                 }
173                 else
174                 {
175                     h=maxyue(f,g);
176                     f=f/h;
177                     g=g/h;
178                 }
179             }
180             else
181             {
182                 g=b*d/maxyue(b,d);
183                 a=g/b*a;
184                 c=g/d*c;
185                 f=a+c;
186                 if(f<g)
187                 {
188                     h=maxyue(g,f);
189                     f=f/h;
190                     g=g/h;
191                 }
192                 else{
193                     h=maxyue(f,g);
194                     f=f/h;
195                     g=g/h;
196                 }
197             }
198         }
199         else if(e==1)
200         {
201             if(b==d)
202             {
203                 f=a-c;
204                 if(f<=0)
205                 {
206                     cc1[2]=1;
207                 }
208                 g=b;
209                 if(f<g)
210                 {
211                     h=maxyue(g,f);
212                     f=f/h;
213                     g=g/h;
214                 }
215                 else{
216                     h=maxyue(f,g);
217                     f=f/h;
218                     g=g/h;
219                 }
220             }
221             else
222             {
223                 g=b*d/maxyue(b,d);
224                 a=g/b*a;
225                 c=g/d*c;
226                 f=a-c;
227                 if(f<0)
228                 {
229                     cc1[2]=1;
230                 }
231                 if(f<g)
232                 {
233                     h=maxyue(g,f);
234                     f=f/h;
235                     g=g/h;
236                 }
237                 else{
238                     h=maxyue(f,g);
239                     f=f/h;
240                     g=g/h;
241                 }
242             }
243         }
244         else if(e==2)
245         {
246             f=a*c;
247             g=b*d;
248             if(f<g)
249             {
250                 h=maxyue(f,g);
251             }
252             else
253             {
254                 h=maxyue(g,f);
255             }
256             f=f/h;
257             g=g/h;
258         }
259         else
260         {
261             f=a*d;
262             g=b*c;
263             if(f>g)
264             {
265                 h=maxyue(f,g);
266             }
267             else
268             {
269                 h=maxyue(g,f);
270             }
271             f=f/h;
272             g=g/h;
273         }
274         cc1[0]=f;
275         cc1[1]=g;
276         return cc1;
277     }
278     static int[] fuza(int num) {//含有真分数,控制随机数产生,并调用yunsuan2将结果保存并打印出来
279         String []a1=new String[num+1];
280         String []a2=new String[num+1];
281         int []cc1=new int [2];
282         int []cc2=new int[2];
283         int zj=0,cj=0;//分别记录正确的题数和错误的题数
284         int i=0;
285         int m=0;//判断是否打印出来
286         String l="";
287         String rs="";
288         Scanner sc=new Scanner(System.in);
289         String rs2;//用户的回答
290         int r1=0;
291         while(i<num)
292         {
293             m=0;
294             l="";
295             rs="";
296             int a=(int)(Math.random()*10);
297             int b=(int)(Math.random()*10);
298             int c=(int)(Math.random()*10);
299             int d=(int)(Math.random()*10);
300             int e=(int)(Math.random()*4);
301             
302             String f="";
303             String g="";
304             if(a>=b||c>=d||c==0||d==0||b==0||a==0)
305             {
306                 m=1;
307                 
308             }
309             else
310             {
311                 cc1=yunsuan2(a,b,c,d,e);
312                 if(cc1[2]==1)
313                 {
314                     m=1;
315                 }
316                 else
317                 {
318                     int h=maxyue(a,b);
319                     int k=maxyue(c,d);
320                     if(h==1)
321                     {
322                         f="("+a+"/"+b+")";
323                     }
324                     else 
325                     {
326                         a=a/h;
327                         b=b/h;
328                         f="("+a+"/"+b+")";
329                     }
330                     
331                      if(k==1)
332                     {
333                         g="("+c+"/"+d+")";
334                         
335                     }
336                     else
337                     {
338                         c=c/k;
339                         d=d/k;
340                         g="("+c+"/"+d+")";
341                     }
342                     a1[i]=f;
343                     a2[i]=g;
344                     for(int j=0;j<i;j++)
345                     {
346                         if(a1[j].equals(f)&&a2[j].equals(g))
347                         {
348                             m=1;
349                             a1[i]="";
350                             a2[i]="";
351                             break;
352                         }
353                     }
354                     if(m==0)
355                     {
356                         if(e==0)
357                         {
358                             l="+";
359                         }
360                         else if(e==1)
361                         {
362                             l="-";
363                         }
364                         else if(e==2)
365                         {
366                             l="*";
367                         }
368                         else 
369                         {
370                             l="/";
371                         }
372                         
373                         if(cc1[0]>=cc1[1]&&cc1[0]%cc1[1]==0)
374                         {
375                             r1=cc1[0]/cc1[1];
376                             rs+=""+r1;
377                         }
378                         else
379                         {
380                             rs+="("+cc1[0]+"/"+cc1[1]+")";
381                         }
382                         System.out.println("请作答第"+(i+1)+"题"+":"+f+l+g+"=");
383                         System.out.print("请输入答案:");
384                         rs2=sc.next();
385                         if(rs2.length()>1)
386                         {
387                             rs2="("+rs2+")";
388                         }
389                         if(rs2.equals(rs))
390                         {
391                             System.out.println("恭喜你,回答正确!");
392                             zj++;
393                         }
394                         else
395                         {
396                             cj++;
397                             System.out.println("哦哦,回答错误!正确答案是:"+rs);
398                         }
399                         i++;
400                         
401                     }
402                     
403                 }
404             }
405         }
406         System.out.println("你刚刚做了"+(i)+"个题,"+"其中共做对了"+zj+"道题,"+"做错了"+cj+"个题");
407         cc2[0]=zj;
408         cc2[1]=cj;
409         return cc2;
410     }
411     static int[] simpleys(int num,int n)//控制随机数产生,调用yunsuan1,在运算后将结果打印出来。
412     {
413         Scanner sc=new Scanner(System.in);
414         int []cc1=new int [num+1];
415         int []cc2=new int[num+1];
416         int []cc3=new int[2];
417         int i=0;//控制打印个数
418         int m=0;//判断是否打印出来
419         int rs;//输入结果
420         int rs1;
421         int t1=0;//记录正确的题数
422         int f1=0;//记录错误的题数
423         while(i<num)
424         {
425             int a=(int)(Math.random()*100);
426             int b=(int)(Math.random()*100);
427             int c=(int)(Math.random()*4);
428             String d="";
429             int t=0;
430             if(a<b)
431             {
432                 t=a;
433                 a=b;
434                 b=t;
435             }
436                 cc1[i]=a;
437                 cc2[i]=b;
438                 for(int j=0;j<i;j++)
439                 {
440                     if(cc1[j]==a&&cc2[j]==b)
441                     {
442                         m=1;
443                         break;
444                     }
445                 }
446                 if(c==0)
447                 {
448                     d="+";
449                 }
450                 else if(c==1)
451                 {
452                     d="-";
453                 }
454                 else if(c==2)
455                 {
456                     d="*";
457                 }
458                 else 
459                 {
460                     m=0;
461                     if(n==1)
462                     {
463                         if(b==0||a % b!=0)
464                         {
465                             m=1;
466                         }
467                         d="/";
468                     }
469                     else
470                     {
471                         if(b==0)
472                         {
473                             m=1;
474                         }
475                         d="/";
476                     }
477                 }
478             if(m==0)
479             {
480                 if(n==1||c!=3)
481                 {
482                     int n1=yunsuan1(c,a,b);
483                     System.out.println("请作答第"+(i+1)+"个题"+":"+a+d+b+"=");
484                     i++;
485                     System.out.println("请输入你的答案:");
486                     rs=sc.nextInt();
487                     if(rs==n1)
488                     {
489                         System.out.println("回答正确!");
490                         t1++;
491                     }
492                     else
493                     {
494                         f1++;
495                         System.out.println("回答错误!正确答案是:"+n1);
496                     }
497                 }
498                 else
499                 {
500                     int n1=a/b;
501                     int n2=a%b;
502                     System.out.println("请作答第"+(i+1)+"个题"+":"+a+d+b+"=");
503                     i++;
504                     System.out.println("请输入你的答案:");
505                     System.out.println("商(记得输入完一个值后,按enter键):");
506                     rs=sc.nextInt();
507                     System.out.println("余数:");
508                     rs1=sc.nextInt();
509                     if(rs==n1&&rs1==n2)
510                     {
511                         System.out.println("回答正确!");
512                         t1++;
513                     }
514                     else
515                     {
516                         f1++;
517                         System.out.println("回答错误!正确答案是:");
518                         System.out.println("商:"+n1);
519                         System.out.println("余数:"+n2);
520                     }
521                 }
522             }
523         }
524         System.out.println("你刚刚做了"+(i)+"个题,"+"其中共做对了"+t1+"道题,"+"做错了"+f1+"个题");
525         cc3[0]=t1;
526         cc3[1]=f1;
527         return cc3;
528     }
529     
530     //实现两个数运算,
531     
532     
533     static int yunsuan1(int c,int a,int b)//用来实现最简单的两个数之间的四则运算
534     {
535         int x;
536         if(c==0)
537         {
538             x=a+b;
539         }
540         else if(c==1)
541         {
542             x=a-b;
543         }
544         else if(c==2)
545         {
546             x=a*b;
547         }
548         else
549         {
550             x=a/b;
551         }
552         return x;
553     }
554     static int wuccys(int c,int a,int b)//实现无乘除运算的两个数之间的运算。
555     {
556         int a1=0;
557         if(c==0)
558         {
559             a1=a+b;
560         }
561         else if(c==1)
562         {
563             a1=a-b;
564         }
565         return a1;
566     }
567     static int[] wucc(int num,int n)//无乘除运算,可选择有无负数,控制随机数产生,并调用wuccys将结果保存并打印出来
568     {
569         int a2=0;//用来接收运算结果
570         int i=0;
571         String[]a3={"+","-"};
572         int []a4=new int[2];
573         a4[0]=0;
574         a4[1]=0;
575         Scanner sc=new Scanner(System.in);
576         int a5[]=new int[num];
577         int a6[]=new int[num];
578         int a7[]=new int[num];
579         a5[0]=0;
580         a6[0]=0;
581         a7[0]=0;
582         while(i<num)
583         {
584             int a=(int)(Math.random()*100);
585             int b=(int)(Math.random()*100);
586             int c=(int)(Math.random()*2);
587             if(a5[i]==a&&a6[i]==b&&a7[i]==c)
588             {
589                 continue;
590             }
591             a5[i]=a;
592             a6[i]=b;
593             a7[i]=c;
594             
595             a2=wuccys(c, a, b);
596             if(n==2)//结果不能含有负数
597             {
598                 if(a2<0)
599                 {
600                     continue;
601                 }
602                 else{
603                     System.out.println("请作答第"+(i+1)+"道题:"+a+a3[c]+b+"=");
604                     i++;
605                     System.out.print("请输入你的答案:");
606                     int rs=sc.nextInt();
607                     if(rs==a2)
608                     {
609                         System.out.println("回答正确!");
610                         a4[0]++;
611                     }
612                     else
613                     {
614                         a4[1]++;
615                         System.out.println("回答错误!正确答案是:"+a2);
616                     }
617                 }
618             }
619             else
620             {
621                 System.out.println("请作答第"+(i+1)+"道题:"+a+a3[c]+b+"=");
622                 i++;
623                 System.out.print("请输入你的答案:");
624                 int rs=sc.nextInt();
625                 if(rs==a2)
626                 {
627                     System.out.println("回答正确!");
628                     a4[0]++;
629                 }
630                 else
631                 {
632                     a4[1]++;
633                     System.out.println("回答错误!正确答案是:"+a2);
634                 }
635             }
636         }
637         System.out.println("刚刚你一共做了"+i+"道题,其中作对的题数是:"+a4[0]+",做错的题数是:"+a4[1]);
638         return a4;
639     }
640     static int maxyue(int y,int x)//最大公约数
641     {
642         int r=y;
643         while(r!=0)
644         {
645             r=x%y;
646             x=y;
647             y=r;
648         }
649         return x;
650     }
651     
652     //多个运算数进行混合运算,并且含有括号。
653     
654     
655     private static char youxian(String f,String s)//计算两个符号的优先级
656     {
657         char a1[][]={
658                 {'>','>','<','<','<','>','>'},
659                 {'>','>','<','<','<','>','>'},
660                 {'>','>','>','>','<','>','>'},
661                 {'>','>','>','>','<','>','>'},
662                 {'<','<','<','<','<','=',' '},
663                 {'>','>','>','>',' ','>','>'},
664                 {'<','<','<','<','<',' ','='}
665         };
666         String a="+-*/()#";
667         int a11=a.indexOf(f);
668         int a12=a.indexOf(s);
669         return a1[a11][a12];
670     }
671     private static int [] chansheng(int num)//随机产生括号
672     {
673         int []b=new int[num];
674         for(int i=0;i<b.length;i++)
675         {
676             b[i]=0;
677         }
678         Random rd=new Random();
679         for(int i=2;i<num;i++)
680         {
681             for(int j=0;j<num-i+1;j++)
682             {
683                 int t=rd.nextInt(2);
684                 if(t==1)
685                 {
686                     if(b[j]>=0&&b[j+i-1]<=0)
687                     {
688                         int c=0;
689                         for(int k=j;k<j+i;k++)
690                         {
691                             c+=b[k];
692                         }
693                         if(c==0)
694                         {
695                             b[j]++;
696                             b[j+i-1]--;
697                         }
698                     }
699                     
700                 }
701             }
702         }
703         return b;
704     }
705     private static String chanshengbiaodashi(int num)//产生带括号的表达式
706     {
707         int a1[]=new int[num];
708         int a2[]=new int[num-1];
709         int a3[]=new int[num];
710         String[]a5=new String[num];
711         String[] a4={"+","-","*","/"};
712         for(int i=0;i<num;i++)
713         {
714             a1[i]=(int) (Math.random()*100+1);
715         }
716         for(int i=0;i<num-1;i++)
717         {
718             a2[i]=(int) (Math.random()*4);
719         }
720         a3=chansheng(num);
721         for(int i=0;i<num;i++)
722         {
723             a5[i]="";
724             if(a3[i]<0)
725             {
726                 int c=0-a3[i];
727                 for(int j=0;j<c;j++)
728                 {
729                     a5[i]+=")";
730                 }
731             }
732             else
733             {
734                 for(int j=0;j<a3[i];j++)
735                 {
736                     a5[i]+="(";
737                 }
738             }
739         }
740         String t="";
741         for(int i=0;i<num-1;i++)
742         {
743             if(a3[i]>0)
744             {
745                 t+=a5[i]+" "+a1[i]+" "+a4[a2[i]];
746             }
747             else
748             {
749                 t+=" "+a1[i]+" "+a5[i]+a4[a2[i]];
750             }
751         }
752         if(a3[num-1]>0)
753         {
754             t+=a5[num-1]+" "+a1[num-1]+" ";
755         }
756         else
757         {
758             t+=" "+a1[num-1]+" "+a5[num-1];
759         }
760         return t;
761     }
762     private static int[] tys(int a,int b,char c)//两个数的运算
763     {
764         int []a1=new int [2];//a1[0]用来记录两数运算结果,a1[1]用来记录两数能否继续算下去
765         a1[0]=a1[1]=0;
766         int d=0;
767         if(c=='+')
768         {
769             d=a+b;
770         }
771         else if(c=='-')
772         {
773             if(a<b)
774             {
775                 a1[1]=1;
776             }
777             else
778             {
779                 d=a-b;
780             }
781         }
782         else if(c=='*')
783         {
784             d=a*b;
785         }
786         else
787         {
788             if(a%b!=0||a<b||b==0)
789             {
790                 a1[1]=1;
791             }
792             else
793             d=a/b;
794         }
795         a1[0] = d;
796         return a1;
797     }
798     private static String jisuanbh(int n,String a)//表达式的运算
799     {
800         Stack <String>num=new Stack <String>();
801         Stack <String>fuhao=new Stack<String>();
802         a+="#";
803         fuhao.push("#");
804         char ch;
805         int i=0;
806         int s=0;
807         int y=0;
808         ch=a.charAt(i);
809         while(!(ch+"").equals("#") || !fuhao.peek().equals("#"))
810         {
811             if(ch==' ')//如果遇到字符为空,说明遇到数字
812             {
813                 String rn="";//用来记录数据
814                 while(true)
815                 {
816                     ch=a.charAt(++i);
817                     if(ch==' ')
818                     {
819                         break;
820                     }
821                     rn+=ch;
822                 }
823                 if((i+1)<a.length()){
824                     ch=a.charAt(++i);
825                 }
826                 num.push(rn);
827             }
828             else//遇到的是字符
829             {
830                 char comp=youxian(fuhao.peek(),ch+"");//比较两个字符的优先级
831                 if(comp=='='){//说明遇到右括号
832                     fuhao.pop();
833                     if((i+1)<a.length()){
834                         ch=a.charAt(++i);
835                     }
836                 }
837                 else if(comp=='>')//优先级高,弹出两个数和一个运算符,进行运算
838                 {
839                     String st1=num.pop();
840                     String st2=num.pop();
841                     int ai1 = Integer.parseInt(st1);
842                     int ai2 = Integer.parseInt(st2);
843                     String fuh1=fuhao.pop();
844                     char fuh2=fuh1.charAt(0);//将String类型转为char类型
845                     int []sz=new int[2];
846                     sz=tys(ai2,ai1,fuh2);
847                     if(sz[1]==1)//如果运算中有问题,就结束运算
848                     {
849                         return "error";
850                     }
851                     else
852                     {
853                         num.push(sz[0]+"");//将两数结果压入栈中
854                     }
855                 }
856                 else//优先级比较低,把运算符压入栈中
857                 {
858                     fuhao.push(ch+"");
859                     if((i+1)<a.length())
860                     {
861                         ch=a.charAt(++i);
862                     }
863                 }
864             }
865         }
866         return num.pop();
867     }
868     private static int[] yunsh(int n)//混合运算的操作
869     {
870         System.out.println("请选择运算的个数");
871         Scanner sc=new Scanner(System.in);
872         int a[]=new int[2];//用来存取正确和错误的题数
873         a[0]=0;
874         a[1]=0;
875         int num=sc.nextInt();
876         int i=0;
877         while(i<n)
878         {
879             String rs=chanshengbiaodashi(num);
880             String judgers=jisuanbh(num,rs);
881             if(judgers.equals("error"))
882             {
883                 System.out.print("");
884             }
885             else
886             {
887                 System.out.println("请作答第"+(i+1)+"道题:"+rs+"=");
888                 System.out.print("你的答案是:");
889                 int rs1=sc.nextInt();
890                 if(rs1==Integer.parseInt(judgers))
891                 {
892                     System.out.println("恭喜你,回答正确!");
893                     a[0]+=1;
894                 }
895                 else
896                 {
897                     System.out.println("回答错误!正确答案是:"+judgers);
898                     a[1]+=1;
899                 }
900                 i++;
901             }
902         }
903         System.out.println("你一共作答"+n+"道题,其中回答正确的题数是:"+a[0]+",回答错误的题数是:"+a[1]);
904         return a;
905     }
906 }
View Code

 代码总结:通过这个的编写,里面最难得是多个数的混合运算,从括号的产生到数值的存储,到计算,这些都不是很容易实现的,需要事先计划好,并通过查询一些资料,才能得出结果,例如,括号的产生,这需要我们去想括号产生的位置,还有判断,

计算时优先级的判断,计算的过程会用到栈,这些会用到数据结构的内容,还有如果上网查询后,会发现栈的使用不用自己定义,会节省一些时间,通过这个项目的训练,我终于意识到上学期的数据结构还是很有用的。

 

 

PSP个人流程

项目计划总结:

时间记录日志

 

缺陷记录日志

 

 

posted @ 2017-03-10 19:54  倾恬xt  阅读(221)  评论(0编辑  收藏  举报