软件工程概论-个人作业04

一、要求:

  1.四则运算3:将表达式和结果存入数据库;

  2.破坏性测试,存入10000条数据;

二、思路

  1.重点为数据库操作以及表达式结果的计算(本次实验表达式只有两个操作数)其他大致与四则运算2相仿;

  2.整数运算:结果没有负数,出现除号一定能整除这两个条件在生成表达式时用一个while语句控制。由于表达式中的操作数都是字符串形式的,所以要先将字符串转化为整数,再用if else判断操作符计算结果。

  3.分数运算:建立一个分数类,有分子(fenzi)分母(fenmu)两个私有int变量,加减乘除调用外界函数;

  4.数据库操作:建立一个数据库连接的类(class DBBean),构造函数连接数据库,公共方法public void insertEx(Expression e)执行sql-insert语句向数据表中插入一条表达式记录,方法public void clear()执行sql-delete语句清空数据表。

  5.主函数中由用户输入10000,生成一个表达书类的对象数组,长度为10000,再实例化类DBBean,利用for循环和insertEx(Expression e)方法将10000条表达式记录存入数据库中。

三、实现代码

  1 package 四则运算3;
  2 
  3 import java.util.Random;
  4 import java.util.Scanner;
  5 import java.util.Vector;
  6 import java.sql.*;
  7 
  8 public class MathHomeWork3 {
  9     public static int choose,from,to,number,ifChengChu;
 10     public static int randomArr[]=new int[4];
 11     public static Random ran=new Random();
 12     public static char randomCh[]={'+','-','*','÷'};
 13     public static Expression ex[];
 14     @SuppressWarnings({  "resource" })
 15     public static void main(String[] args){
 16         Scanner in=new Scanner(System.in);
 17         System.out.println("题目个数:");
 18         number=in.nextInt();
 19         System.out.println("数字范围(从小到大输入两个正整数):");
 20         from=in.nextInt();
 21         to=in.nextInt();
 22         System.out.println("是否有乘除法(1有2无):");
 23         ifChengChu=in.nextInt();
 24         DBBean db=new DBBean();
 25         
 26         ex=new Expression[number];
 27         for(int i=0;i<number;i++){
 28             ex[i]=new Expression();
 29             choose=ran.nextInt(2)+1;
 30             ex[i]=creaExpression(choose,ifChengChu);
 31             if(ex[i]==null)
 32                 i--;
 33             
 34         }
 35         showExpression(ex);
 36         for(int i=0;i<number;i++){
 37             db.insertEx(ex[i]);
 38         }
 39         System.out.println("是否清空数据表(1是,其他数字否)?");
 40         int ifClear=in.nextInt();
 41         if(ifClear==1){
 42             db.clear();
 43         }
 44         else{
 45             return ;
 46         }
 47         db.clear();
 48     }
 49 
 50     //生成表达式
 51     public static Expression creaExpression(int choose,int ifChengChu){
 52         char operator;
 53         if(ifChengChu==1){
 54             int oper=ran.nextInt(4);
 55             operator=randomCh[oper];
 56         }
 57         else{
 58             int oper=ran.nextInt(2);
 59             operator=randomCh[oper];
 60         }
 61 
 62         if(choose==1){
 63             Expression ex=new Expression(true);
 64             for(int i=0;i<2;i++){
 65                 randomArr[i]=randomNum(from,to);
 66             }
 67             if(operator=='-'||operator=='÷'){
 68                 int ra=0,rb=0;
 69                 while(true){
 70                     ra=randomNum(from,to);
 71                     rb=randomNum(from,to);
 72                     if(ra>=rb&&ra%rb==0)
 73                         break;
 74                 }
 75                 randomArr[0]=ra;
 76                 randomArr[1]=rb;
 77             }
 78 
 79             ex.push(""+randomArr[0]);
 80             ex.push(""+operator);
 81             ex.push(""+randomArr[1]);
 82             ex.push("=");
 83             int result=Function.calcuInt(ex.get(0),ex.get(1),ex.get(2));
 84             String s=""+result;
 85             ex.setValue(s);
 86             return ex;
 87 
 88         }
 89         else if(choose==2){
 90             Expression ex=new Expression(false);
 91             if(operator=='-'){
 92                 int newindex=ran.nextInt(2)+2;
 93                 operator=randomCh[newindex];
 94             }
 95             FenShu fs[]=new FenShu[2];
 96             String fenshu[]=new String[2];
 97             for(int i=0;i<2;i++){
 98                 int a=0,b=0;
 99                 while(a>=b){
100                     a=randomNum(from,to);
101                     b=randomNum(from,to);
102                 }
103                 int kk=Function.fun(a,b);
104                 a/=kk;
105                 b/=kk;
106                 fs[i]=new FenShu(a,b);
107                 fenshu[i]=fs[i].getFenshu();
108             }
109 
110             ex.push(""+fenshu[0]);
111             ex.push(""+operator);
112             ex.push(""+fenshu[1]);
113             ex.push("=");
114             FenShu result=Function.calcuFenshu(fs[0],""+operator,fs[1]);
115             ex.setValue(result.getFenshu());
116             return ex;
117         }
118         else
119             return null;
120         
121 
122     }
123     //打印表达式
124     public static void showExpression(Expression f[]){
125         int len=f.length;
126         for(int i=0;i<len;i++){
127             System.out.print("No."+(i+1)+": ");
128             f[i].show();
129             System.out.print("\n");
130         }
131     }
132 
133 
134 
135     //范围生成随机数
136     public static int randomNum(int fromA,int toB){
137         int x=ran.nextInt(toB-fromA)+fromA+1;
138         return x;
139     }
140 }
141 class Function{
142     //求分子分母最大公约数
143     public static int fun(int x,int y){
144         int min,i;
145         if(x>y) min=y;
146         else
147             min=x;
148         for(i=min;i>=1;i--){
149             if(x%i==0&&y%i==0){
150                 break;
151             }
152         }
153         return i;
154     }
155     //计算整数表达式
156     public static int calcuInt(String numleft,String oper,String numright){
157         int a=Integer.parseInt(numleft);
158         int b=Integer.parseInt(numright);
159         if(oper.equals("+"))
160             return a+b;
161         else if(oper.equals("-"))
162             return a-b;
163         else if(oper.equals("*"))
164             return a*b;
165         else if(oper.equals("÷"))
166             return a/b;
167         else 
168             return -1;
169     }
170     //计算分数表达式
171     public static FenShu calcuFenshu(FenShu x,String oper,FenShu y){
172         FenShu result=new FenShu();
173         if(oper.equals("+")){
174             result=addFenshu(x,y);
175         }
176         else if(oper.equals("÷")){
177             result=divideFenshu(x,y);
178         }
179         else if(oper.equals("*")){
180             result=multiFenshu(x,y);
181         }
182         else result=new FenShu(-1,-1);
183         return result;
184         
185     }
186     //分数加
187     public static FenShu addFenshu(FenShu x,FenShu y){
188         int zi,mu,yue;
189         mu=x.getFenmu()*y.getFenmu();
190         zi=x.getFenzi()*y.getFenmu()+y.getFenzi()*x.getFenmu();
191         yue=Function.fun(zi, mu);
192         zi/=yue;
193         mu/=yue;
194         FenShu fenshu=new FenShu(zi,mu);
195         return fenshu;
196     }
197     //分数除
198     public static FenShu divideFenshu(FenShu x,FenShu y){
199         int zi,mu,yue;
200         mu=x.getFenmu()*y.getFenzi();
201         zi=x.getFenzi()*y.getFenmu();
202         yue=Function.fun(zi, mu);
203         zi/=yue;
204         mu/=yue;
205         FenShu fenshu=new FenShu(zi,mu);
206         return fenshu;
207     }
208     //分数除
209     public static FenShu multiFenshu(FenShu x,FenShu y){
210         int zi,mu,yue;
211         mu=x.getFenmu()*y.getFenmu();
212         zi=x.getFenzi()*y.getFenzi();
213         yue=Function.fun(zi, mu);
214         zi/=yue;
215         mu/=yue;
216         FenShu fenshu=new FenShu(zi,mu);
217         return fenshu;
218     }
219 }
220 class Expression{
221     private Vector<String> expression;
222     private boolean ifInt;
223     private String value="";
224     public Expression(boolean ifint){
225         expression=new Vector<String>();
226         ifInt=ifint;
227     }
228     public Expression() {
229         expression=new Vector<String>();
230     }
231     public void push(String x){
232         expression.addElement(x);
233     }
234     public String get(int i){
235         if(expression!=null&&i>=0&&i<expression.size()){
236             return expression.get(i);
237         }
238         else return null;
239     }
240     public void set(int i,String x){
241         if(expression!=null&&i>=0&&i<expression.size()){
242             expression.set(i,x);
243         }
244         else return;
245     }
246     public void insert(int i,String x){
247         if(expression!=null&&i>=0&&i<expression.size()){
248             expression.insertElementAt(x,i);
249         }
250         else return;
251     }
252     public void add(int i,String x){
253         if(expression!=null&&i>=0&&i<expression.size()){
254             expression.add(i,x);
255         }
256         else return ;
257     }
258     public void delete(int i){
259         if(expression!=null&&i>=0&&i<expression.size()){
260             expression.removeElementAt(i);
261         }
262         else return ;
263     }
264     public void show(){
265         for(int i=0;i<expression.size();i++){
266             System.out.print(expression.get(i));
267         }
268         System.out.print(value);
269     }
270     public int size(){
271         if(expression!=null)
272             return expression.size();
273         else return -1;
274     }
275     public void setValue(String v){
276         value=v;
277     }
278     public String getValue(){
279         return value;
280     }
281     public boolean getIfint(){
282         return ifInt;
283     }
284 }
285 
286 class FenShu{
287     private int fenzi;
288     private int fenmu;
289     public FenShu(){
290         fenzi=1; fenmu=1;
291     }
292     public FenShu(int x,int y){
293         fenzi=x; fenmu=y;
294     }
295     public int getFenzi(){
296         return fenzi;
297     }
298     public int getFenmu(){
299         return fenmu;
300     }
301     public String getFenshu(){
302         String s="["+fenzi+"/"+fenmu+"]";
303         return s;
304     }
305     
306 }
307 
308 //数据库操作
309 class DBBean {
310     private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
311     private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=yfyf";
312     private String dbusername = "sa";
313     private String dbpassword = "123456";
314     private Connection conn = null;
315     private PreparedStatement pstmt = null;
316 
317     public DBBean()
318     {
319         try
320         {
321             Class.forName(driverStr);
322             conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
323         } 
324         catch (Exception ex) {
325             System.out.println("数据连接失败!");
326         } 
327         
328     }
329     public void insertEx(Expression e){
330         if(e.size()!=4 && (!e.getValue().equals(""))){
331             System.out.println("插入表达式失败!");
332             return ;
333         }
334         else{
335             try{
336                 String sql = "insert into expression values('" +e.get(0)+ "','" +e.get(1)+ "','" +e.get(2)+ "','" +e.getValue()+ "')";
337                 pstmt=conn.prepareStatement(sql);
338                 int rst=pstmt.executeUpdate();
339                 if(rst!=0){
340                     System.out.print("成功插入表达式:");
341                     e.show();
342                     System.out.print("\n");
343                 }
344                 else
345                     System.out.println("插入表达式失败!");
346             }
347             catch(Exception ex){
348                 System.out.println("插入表达式失败!");
349             }
350 
351         }
352     }
353     //清空数据表
354     public void clear(){
355         try{
356             String sql="delete expression";
357             pstmt=conn.prepareStatement(sql);
358             int rst=pstmt.executeUpdate();
359             if(rst!=0){
360                 System.out.println("成功清空数据表");
361             }            
362         }
363         catch(Exception e){
364             System.out.println("delete语句执行失败!");
365         }
366     }
367 }
四则运算3

四、结果

  1.数据表

  

  2.初始数据表

  

 

  3.生成10000条表达式并存入数据库

  

 

  

  4.运行程序之后的数据库

  

 

转载于:https://www.cnblogs.com/yifengyifeng/p/6622181.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值