结队成员:陈欣容、孙奇文
1.要求
1.四则运算3:将表达式和结果存入数据库;
2.破坏性测试,存入10000条数据;
2.思路
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条表达式记录存入数据库中。
3.代码
package 四则运算3;
import java.util.Random;
import java.util.Scanner;
import java.util.Vector;
import java.sql.*;
public class MathHomeWork3 {
public static int choose,from,to,number,ifChengChu; public static int randomArr[]=new int[4];
public static Random ran=new Random();
public static char randomCh[]={'+','-','*','÷'}; 13 public static Expression ex[];
@SuppressWarnings({ "resource" })
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("题目个数:");
number=in.nextInt();
System.out.println("数字范围(从小到大输入两个正整数):");
from=in.nextInt();
to=in.nextInt();
System.out.println("是否有乘除法(1有2无):");
ifChengChu=in.nextInt();
DBBean db=new DBBean();
ex=new Expression[number];
for(int i=0;i<number;i++){
ex[i]=new Expression();
choose=ran.nextInt(2)+1;
ex[i]=creaExpression(choose,ifChengChu); if(ex[i]==null)
i--;
}
showExpression(ex);
for(int i=0;i<number;i++){
db.insertEx(ex[i]);
}
System.out.println("是否清空数据表(1是,其他数字否)?");
int ifClear=in.nextInt();
if(ifClear==1){
db.clear();
}
else{
return ;
}
db.clear();
}
//生成表达式
public static Expression creaExpression(int choose,int ifChengChu){
char operator;
if(ifChengChu==1){
int oper=ran.nextInt(4);
operator=randomCh[oper];
}
else{
int oper=ran.nextInt(2);
operator=randomCh[oper];
}
if(choose==1){
Expression ex=new Expression(true);
for(int i=0;i<2;i++){
randomArr[i]=randomNum(from,to);
}
if(operator=='-'||operator=='÷'){
int ra=0,rb=0;
while(true){
ra=randomNum(from,to);
rb=randomNum(from,to);
if(ra>=rb&&ra%rb==0)
break;
}
randomArr[0]=ra;
randomArr[1]=rb;
}
ex.push(""+operator);
ex.push(""+randomArr[1]);
ex.push("=");
int result=Function.calcuInt(ex.get(0),ex.get(1),ex.get(2));
String s=""+result;
ex.setValue(s);
return ex;
}
else if(choose==2){
Expression ex=new Expression(false);
if(operator=='-'){
int newindex=ran.nextInt(2)+2;
operator=randomCh[newindex];
}
FenShu fs[]=new FenShu[2];
String fenshu[]=new String[2];
for(int i=0;i<2;i++){
int a=0,b=0;
while(a>=b){
a=randomNum(from,to);
b=randomNum(from,to);
}
int kk=Function.fun(a,b);
a/=kk;
b/=kk;
fs[i]=new FenShu(a,b);
fenshu[i]=fs[i].getFenshu();
}
ex.push(""+fenshu[0]);
ex.push(""+operator);
ex.push(""+fenshu[1]);
ex.push("=");
FenShu result=Function.calcuFenshu(fs[0],""+operator,fs[1]);
ex.setValue(result.getFenshu());
return ex;
}
else
return null;
}
//打印表达式
public static void showExpression(Expression f[]){
int len=f.length;
for(int i=0;i<len;i++){
System.out.print("No."+(i+1)+": ");
f[i].show();
System.out.print(" ");
}
}
//范围生成随机数
public static int randomNum(int fromA,int toB){
int x=ran.nextInt(toB-fromA)+fromA+1;
return x;
}
}
class Function{
//求分子分母最大公约数
public static int fun(int x,int y){
int min,i;
if(x>y) min=y;
else
min=x;
for(i=min;i>=1;i--){
if(x%i==0&&y%i==0){
break;
}
}
return i;
}
//计算整数表达式
public static int calcuInt(String numleft,String oper,String numright){
int a=Integer.parseInt(numleft);
int b=Integer.parseInt(numright);
if(oper.equals("+"))
return a+b;
else if(oper.equals("-"))
return a-b;
else if(oper.equals("*"))
return a*b;
else if(oper.equals("÷"))
return a/b;
else
return -1;
}
//计算分数表达式
public static FenShu calcuFenshu(FenShu x,String oper,FenShu y){
FenShu result=new FenShu();
if(oper.equals("+")){
result=addFenshu(x,y);
}
else if(oper.equals("÷")){
result=divideFenshu(x,y);
}
else if(oper.equals("*")){
result=multiFenshu(x,y);
}
else result=new FenShu(-1,-1);
return result;
}
//分数加
public static FenShu addFenshu(FenShu x,FenShu y){
int zi,mu,yue;
mu=x.getFenmu()*y.getFenmu();
zi=x.getFenzi()*y.getFenmu()+y.getFenzi()*x.getFenmu();
yue=Function.fun(zi, mu);
zi/=yue;
mu/=yue;
FenShu fenshu=new FenShu(zi,mu);
return fenshu;
}
//分数除
public static FenShu divideFenshu(FenShu x,FenShu y){
int zi,mu,yue;
mu=x.getFenmu()*y.getFenzi();
zi=x.getFenzi()*y.getFenmu();
yue=Function.fun(zi, mu);
zi/=yue;
mu/=yue;
FenShu fenshu=new FenShu(zi,mu);
return fenshu;
}
//分数除
public static FenShu multiFenshu(FenShu x,FenShu y){
int zi,mu,yue;
mu=x.getFenmu()*y.getFenmu();
zi=x.getFenzi()*y.getFenzi();
yue=Function.fun(zi, mu);
zi/=yue;
mu/=yue;
FenShu fenshu=new FenShu(zi,mu);
return fenshu;
}
}
class Expression{
private Vector<String> expression;
private boolean ifInt;
private String value="";
public Expression(boolean ifint){
expression=new Vector<String>();
ifInt=ifint;
}
88public Expression() {
expression=new Vector<String>();
}
public void push(String x){
expression.addElement(x);
}
public String get(int i){
if(expression!=null&&i>=0&&i<expression.size()){
return expression.get(i);
}
else return null;
}
public void set(int i,String x){
if(expression!=null&&i>=0&&i<expression.size()){
expression.set(i,x);
}
else return;
}
public void insert(int i,String x){
if(expression!=null&&i>=0&&i<expression.size()){
expression.insertElementAt(x,i);
}
else return;
}
public void add(int i,String x){
if(expression!=null&&i>=0&&i<expression.size()){
expression.add(i,x);
}
else return ;
}
public void delete(int i){
if(expression!=null&&i>=0&&i<expression.size()){
expression.removeElementAt(i);
}
else return ;
}
public void show(){
for(int i=0;i<expression.size();i++){
System.out.print(expression.get(i));
}
System.out.print(value);
}
public int size(){
if(expression!=null)
return expression.size();
else return -1;
}
public void setValue(String v){
value=v;
}
public String getValue(){
return value;
}
public boolean getIfint(){
return ifInt;
}
}
class FenShu{
private int fenzi;
private int fenmu;
public FenShu(){
fenzi=1; fenmu=1;
}
public FenShu(int x,int y){
fenzi=x; fenmu=y;
}
public int getFenzi(){
return fenzi;
}
public int getFenmu(){
return fenmu;
}
public String getFenshu(){
String s="["+fenzi+"/"+fenmu+"]";
return s;
}
}
//数据库操作
class DBBean {
private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=yfyf";
private String dbusername = "sa";
private String dbpassword = "123456";
private Connection conn = null;
private PreparedStatement pstmt = null;
public DBBean()
{
try
{
Class.forName(driverStr);
conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
}
catch (Exception ex) {
System.out.println("数据连接失败!");
}
}
public void insertEx(Expression e){
if(e.size()!=4 && (!e.getValue().equals(""))){
System.out.println("插入表达式失败!");
return ;
}else{
try{
String sql = "insert into expression values('" +e.get(0)+ "','" +e.get(1)+ "','" +e.get(2)+ "','" +e.getValue()+ "')";
pstmt=conn.prepareStatement(sql);
int rst=pstmt.executeUpdate();
if(rst!=0){
System.out.print("成功插入表达式:");
e.show();
System.out.print(" ");
}
else
System.out.println("插入表达式失败!");
}
catch(Exception ex){
System.out.println("插入表达式失败!"); }
}
}
//清空数据表
public void clear(){
try{
String sql="delete expression";
pstmt=conn.prepareStatement(sql);
int rst=pstmt.executeUpdate();
if(rst!=0){
System.out.println("成功清空数据表");
}
}
catch(Exception e){
System.out.println("delete语句执行失败!");365 }
}
}