码迷,mamicode.com
首页 > 其他好文 > 详细

软件工程概论个人作业03

时间:2017-03-17 20:50:16      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:yun   while   index   com   tostring   valueof   value   ase   bst   

  一个小软件从第一代的几行小程序,成长到现在的充满了困难的大程序。

  程序设计思想:先对随机生成的随机数进行排查,筛选出符合条件的数字参与运算,然后在随机生成符合条件的算式。并且使用这些算式和数字组成新的算式,并对新生成的算式进行条件筛选,经符合全部条件的算式存储到数据库中。

  对生成算式的每一个子算式进行条件查询,如果是减法和除法则对俩个数字进行运算,如果不符合条件,就进行调整(如对减数和被减数的位置对调,或重新生成新的算式)。

  对生出的字符串进行对比如果出现重复的就将第二次的字符串重新生成即可。

  程序源代码:

  yuansuan.java

  1 package 四则运算;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Scanner;
  5 
  6 public class yunsuan {
  7 
  8     
  9     public static void main(String[] args) {
 10         // TODO Auto-generated method stub
 11         String array1[]=new String[1000];
 12         input(array1);
 13 
 14     }
 15     public static void input(String array1[]){
 16         int a,b,c,d,e,f;
 17         Scanner scanner=new Scanner(System.in);
 18         System.out.println("请输入生成题目的数量(少于1000道):");
 19         a=scanner.nextInt();
 20         System.out.println("请输入数字的范围(1-?):");
 21         b=scanner.nextInt();
 22         System.out.println("是否有乘除法(包含乘除法请输入1,不包含请输入0)");
 23         c=scanner.nextInt();
 24         System.out.println("是否有括号(最多可以支持十个数参与计算,包含括号请输入1,不包含请输入0)");
 25         d=scanner.nextInt();
 26         System.out.println("加减有无负数(有负数请输入1,没有负数请输入0)");
 27         e=scanner.nextInt();
 28         System.out.println("除法有无余数(有余数请输入1,没有余数请输入0)");
 29         f=scanner.nextInt();
 30         int m;
 31         boolean n,o,p;
 32         if(c==1)
 33             m=4;
 34         else m=2;
 35         if(d==1)
 36             n=true;
 37         else n=false;
 38         if(e==1)
 39             o=true;
 40         else o=false;
 41         if(f==1)
 42             p=true;
 43         else p=false;
 44         for(int i=0;i<a;i++){
 45             array1[i]=birth(b,m,n,o,p);
 46         }
 47         output(a,array1);
 48     }
 49 
 50     public static void output(int a,String []array1 ){
 51         Calculator calculate = new Calculator();
 52         int j;
 53         for(int i=0;i<a;i++){
 54             
 55             System.out.print(array1[i]);
 56             String s = array1[i];
 57             ArrayList result = calculate.getStringList(s);  //String转换为List
 58             result = calculate.getPostOrder(result);   //中缀变后缀
 59              j = calculate.calculate(result);   //计算
 60             System.out.println("="+j);
 61         }
 62     }
 63     public static String birth(int fanwei,int fuhao,boolean bra,boolean o,boolean p){
 64         int a=((int)(Math.random()*9))+2;
 65         int b,c;
 66         String str ="";
 67         for(int i=0;i<a;i++){
 68             b=(int)(Math.random()*fanwei)+1;
 69             c=(int)(Math.random()*fuhao);
 70             if(i==a-1)
 71                  str+=b+"";
 72             else {
 73                 switch(c){
 74                 case 0: str+=b+"+";break;
 75                 case 1: str+=b+"-";break;
 76                 case 2: str+=b+"*";break;
 77                 case 3:    str+=b+"/";break;
 78                 }
 79                 
 80             }
 81             
 82         }
 83         if(bra&&((int)( Math.random()*100))>50){
 84             //在这里为express中插入一对括号
 85             
 86             int first = (int)(Math.random()*(a-1))+1;//括起来的第一个数
 87             int next = (int)(Math.random()*(a-1))+1;//括起来的最后一个数
 88             if(first > next){//如果第一个数比第二个数大,则交换两个数的值
 89                 first = first^next;
 90                 next = first^next;
 91                 first=first^next;
 92             }
 93             if(next-first >= 2){//如果next和first的差值小于2,则不执行以下步骤,因为括号中至少有两个数
 94                 //length记下表达式字符串的长度,j记下遍历字符串中数字的个数
 95                 int length = str.length(),j=0;
 96                 //用字符串建立一个动态字符串。
 97                 StringBuffer temp = new StringBuffer(str);
 98                 //开始循环遍历字符串
 99                 for(int i=0; i < length; i++){
100                     char cc = temp.charAt(i);//取出下标为i的字符
101                     if(first==1&&i==0)        //如果括号括起来的第一个数字为表达式中第一个操作数,在表达式最前面插入(
102                         temp.insert(0, ‘(‘);
103                     if(cc > ‘9‘ || cc < ‘0‘){//如果当前字符是运算符字符
104                         j++;                //表示刚刚遍历一个数字,所以j加一
105                         if(j==first-1){        //如果j到达first的前一个位置,在当前字符的后面插入“(”
106                             temp.insert(i+1, ‘(‘);i++;
107                         }
108                         if(j==next)            //如果j到达next的位置,在当前字符前面插入一个“)”
109                             temp.insert(i, ‘)‘);
110                     }
111                 }
112                 str = temp.toString();//将动态字符串转换为普通字符串
113             }
114         }
115         //加减有无负数
116         /*
117         if(!o){
118 
119             int length2 = str.length(),j=0,i;
120             int m=str.indexOf(‘-‘);
121             StringBuffer temp = new StringBuffer(str);
122             if(m>0){
123                 for( i=m-1; i>0; i--){
124                     char cc = temp.charAt(i);
125                     if(cc > ‘9‘ || cc < ‘0‘)
126                         break;
127                 }
128                 
129                 String hh=temp.substring(i+1, m-1);
130                 int x=Integer.valueOf(hh);
131                 
132                 for( i=m; i<length2; i++){
133                     char cc = temp.charAt(i);
134                     if(cc > ‘9‘ || cc < ‘0‘)
135                         break;
136                 }
137                 hh=temp.substring(m+1, i-1);
138                 int y=Integer.valueOf(hh);
139                 if(x-y<0){
140                     int v=x;
141                     x=y;
142                     y=v;
143                     }
144                     
145             }
146         
147         }
148         //除法有无余数
149         if(!p){
150             int length2 = str.length(),j=0,i;
151             int m=str.indexOf(‘/‘);
152             StringBuffer temp = new StringBuffer(str);
153             if(m>0){
154                 for( i=m-1; i>0; i--){
155                     char cc = temp.charAt(i);
156                     if(cc > ‘9‘ || cc < ‘0‘)
157                         break;
158                 }
159                 String hh=temp.substring(i+1, m-1);
160                 int x=Integer.valueOf(hh);
161                 for( i=m; i<length2; i++){
162                     char cc = temp.charAt(i);
163                     if(cc > ‘9‘ || cc < ‘0‘)
164                         break;
165                 }
166                 hh=temp.substring(m+1, i-1);
167                 int y=Integer.valueOf(hh);
168                 if(x%y!=0)
169                     birth( fanwei, fuhao, bra,o, p);
170                     
171             }
172         }
173         */
174         //余数的判断由于存在括号的原因无法使用。
175         
176         return str;
177     }
178 }

   Calculator.java

  1 package 四则运算;
  2 import java.util.ArrayList;
  3 import java.util.Stack;
  4 
  5 /**
  6  * 
  7  * @author yhh
  8  *
  9  */
 10 public class Calculator {
 11     
 12     /**
 13      * 将字符串转化成List
 14      * @param str
 15      * @return
 16      */
 17     public ArrayList<String> getStringList(String str){
 18         ArrayList<String> result = new ArrayList<String>();
 19         String num = "";
 20         for (int i = 0; i < str.length(); i++) {
 21             if(Character.isDigit(str.charAt(i))){
 22                 num = num + str.charAt(i);
 23             }else{
 24                 if(num != ""){
 25                     result.add(num);
 26                 }
 27                 result.add(str.charAt(i) + "");
 28                 num = "";
 29             }
 30         }
 31         if(num != ""){
 32             result.add(num);
 33         }
 34         return result;
 35     }
 36     
 37     /**
 38      * 将中缀表达式转化为后缀表达式
 39      * @param inOrderList
 40      * @return
 41      */
 42     public ArrayList<String> getPostOrder(ArrayList<String> inOrderList){
 43         
 44         ArrayList<String> result = new ArrayList<String>();
 45         Stack<String> stack = new Stack<String>();
 46         for (int i = 0; i < inOrderList.size(); i++) {
 47             if(Character.isDigit(inOrderList.get(i).charAt(0))){
 48                 result.add(inOrderList.get(i));
 49             }else{
 50                 switch (inOrderList.get(i).charAt(0)) {
 51                 case ‘(‘:
 52                     stack.push(inOrderList.get(i));
 53                     break;
 54                 case ‘)‘:
 55                     while (!stack.peek().equals("(")) {
 56                         result.add(stack.pop());
 57                     }
 58                     stack.pop();
 59                     break;
 60                 default:
 61                     while (!stack.isEmpty() && compare(stack.peek(), inOrderList.get(i))){
 62                         result.add(stack.pop());
 63                     }
 64                     stack.push(inOrderList.get(i));
 65                     break;
 66                 }
 67             }
 68         }
 69         while(!stack.isEmpty()){
 70             result.add(stack.pop());
 71         }
 72         return result;
 73     }
 74     
 75     /**
 76      * 计算后缀表达式
 77      * @param postOrder
 78      * @return
 79      */
 80     public Integer calculate(ArrayList<String> postOrder){
 81         Stack stack = new Stack();
 82         for (int i = 0; i < postOrder.size(); i++) {
 83             if(Character.isDigit(postOrder.get(i).charAt(0))){
 84                 stack.push(Integer.parseInt(postOrder.get(i)));
 85             }else{
 86                 Integer back = (Integer)stack.pop();
 87                 Integer front = (Integer)stack.pop();
 88                 Integer res = 0;
 89                 switch (postOrder.get(i).charAt(0)) {
 90                 case ‘+‘:
 91                     res = front + back;
 92                     break;
 93                 case ‘-‘:
 94                     res = front - back;
 95                     break;
 96                 case ‘*‘:
 97                     res = front * back;
 98                     break;
 99                 case ‘/‘:
100                     res = front / back;
101                     break;
102                 }
103                 stack.push(res);
104             }
105         }
106         return (Integer)stack.pop();
107     }
108     
109     /**
110      * 比较运算符等级
111      * @param peek
112      * @param cur
113      * @return
114      */
115     public static boolean compare(String peek, String cur){
116         if("*".equals(peek) && ("/".equals(cur) || "*".equals(cur) ||"+".equals(cur) ||"-".equals(cur))){
117             return true;
118         }else if("/".equals(peek) && ("/".equals(cur) || "*".equals(cur) ||"+".equals(cur) ||"-".equals(cur))){
119             return true;
120         }else if("+".equals(peek) && ("+".equals(cur) || "-".equals(cur))){
121             return true;
122         }else if("-".equals(peek) && ("+".equals(cur) || "-".equals(cur))){
123             return true;
124         }
125         return false;
126     }
127     
128     public static void main(String[] args) {
129         Calculator calculate = new Calculator();
130         String s = "12+(23*3-56+7)*(2+90)/2";
131         ArrayList result = calculate.getStringList(s);  //String转换为List
132         result = calculate.getPostOrder(result);   //中缀变后缀
133         int i = calculate.calculate(result);   //计算
134         System.out.println(i);
135 
136     }
137     
138     
139 }

  运行结果截图

技术分享

  编程总结分析:这次的实验和以前的实验完全不同,他离开了我们通常的认识,站在使用者的方面想我们提出新的程序设计的要求。

  PSP0

技术分享技术分享技术分享

 

软件工程概论个人作业03

标签:yun   while   index   com   tostring   valueof   value   ase   bst   

原文地址:http://www.cnblogs.com/wangfengbin/p/6568047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!