zoukankan      html  css  js  c++  java
  • 四则运算2作业实验报告




    //
    2017.3.10 //20153278解昊兴 //信1505-2班 package jisuan; import java.util.Random; import java.text.DecimalFormat; import java.util.Scanner; import java.util.*; public class arithmetic { public static void main(String args[]) { int ch;//是否继续 int option,range,negative_OK,fraction,decimal,num; Scanner cin=new Scanner(System.in); do{ menu0(); option=cin.nextInt(); switch(option) { case 1:System.out.println("您选择了的是:“加减法简单运算”!"); System.out.print("输入操作数最大值:"); range=cin.nextInt(); //System.out.println(); System.out.print("输入题目的个数:"); num=cin.nextInt(); String list[]; list=new String[num]; list[0]=jiaFa(range,0,0,0); for(int i=1;i<num;i++) { int flag; //避免题目重复 do{ flag=0; if((int)(Math.random()*2)==1) { list[i]=jiaFa(range,0,0,0); } else { list[i]=jianFa(range,0,0,0); } for(int t=0;t<i;t++) { if(list[i].equals(list[t])) flag=1; } }while(flag==1); } System.out.print("请输入每行的题目数:"); int n=cin.nextInt(); for(int i=0;i<num;i++) { System.out.print(list[i]+"="+" "); if((i+1)%n==0) System.out.println(); } break; case 2: System.out.println("您选择了的是:“含乘、除法的四则运算”!"); System.out.print("输入操作数最大值:"); range=cin.nextInt(); System.out.print("输入题目的个数:"); num=cin.nextInt(); System.out.print("输入每行题目的个数:"); n=cin.nextInt(); for(int i=0;i<num;i++) { int flag=(int)(Math.random()*4); if(flag==0) System.out.print(jiaFa(range,0,0,0)+"="+" "); else if(flag==1) System.out.print(jianFa(range,0,0,0)+"="+" "); else if(flag==2) System.out.print(chuFa(range,0)+"="+" "); else if(flag==3) System.out.print(chengFa(range,0)+"="+" "); if((i+1)%n==0) System.out.println(); } break; case 3: System.out.println("您选择了的是:“包括真分数的四则运算”!"); System.out.print("输入操作数最大值:"); range=cin.nextInt(); System.out.print("输入题目的个数:"); num=cin.nextInt(); System.out.print("输入每行题目的个数:"); n=cin.nextInt(); for(int i=0;i<num;i++) { String str=""; int flag=(int)(Math.random()*4); if(flag==0) { str=jiaFa(range,0,1,0)+"="; System.out.print(str); } else if(flag==1) { str=jianFa(range,0,1,0)+"="; System.out.print(str); } else if(flag==2) { str=chengFa(range,1)+"="; System.out.print(str); } else if(flag==3) { str=chuFa(range,1)+"="; System.out.print(str); } for(int k=0;k<20-str.length();k++) { System.out.print(" "); } if((i+1)%n==0) System.out.println(); } break; case 4: System.out.println("您选择了的是:“包括假分数的四则运算”!"); System.out.print("输入操作数最大值:"); range=cin.nextInt(); //System.out.println(); System.out.print("输入题目的个数:"); num=cin.nextInt(); System.out.print("输入每行题目的个数:"); n=cin.nextInt(); for(int i=0;i<num;i++) { String str=""; int flag=(int)(Math.random()*4); if(flag==0) { str=jiaFa(range,0,2,0)+"="; System.out.print(str); } else if(flag==1) { str=jianFa(range,0,2,0)+"="; System.out.print(str); } else if(flag==2) { str=chengFa(range,2)+"="; System.out.print(str); } else if(flag==3) { str=chuFa(range,2)+"="; System.out.print(str); } for(int k=0;k<20-str.length();k++) { System.out.print(" "); } if((i+1)%n==0) System.out.println(); } break; case 5: System.out.println("您选择了的是:“包括小数的运算”!"); System.out.print("输入所要保留小数点的位数:"); int r=cin.nextInt(); // System.out.println(); System.out.print("输入操作数最大值:"); range=cin.nextInt(); System.out.println(); System.out.print("输入题目的个数:"); num=cin.nextInt(); System.out.print("输入每行题目的个数:"); n=cin.nextInt(); for(int i=0;i<num;i++) { String str=""; int flag=(int)(Math.random()*4); if(flag==0) { str=jiaFa(range,0,0,r)+"="; System.out.print(str); } else if(flag==1) { str=jianFa(range,0,0,r)+"="; System.out.print(str); } else if(flag==2) { str=chengFa(range,0)+"="; System.out.print(str); } else if(flag==3) { str=chuFa(range,0)+"="; System.out.print(str); } for(int k=0;k<20-str.length();k++) { System.out.print(" "); } if((i+1)%n==0) System.out.println(); } break; case 6: System.out.println("您选择了的是:“持带括号的运算”!"); System.out.print("输入题目的个数:"); num=cin.nextInt(); System.out.print("输入每行题目的个数:"); n=cin.nextInt(); for(int i=0;i<num;i++) { String str=""; str=three_Operands()+"="; System.out.print(str); for(int k=0;k<15-str.length();k++) { System.out.print(" "); } if((i+1)%n==0) System.out.println(); } break; } System.out.print("是否继续(1:继续,0:退出):"); ch=cin.nextInt(); }while(ch==1); } public static void menu0()//主菜单函数 { System.out.println(" 欢迎使用出题器!"); System.out.println(); System.out.println("|| 1、加、减法简单运算 ||"); System.out.println("|| 2、含乘、除法的四则运算 ||"); System.out.println("|| 3、包括真分数的四则运算 ||"); System.out.println("|| 4、包括假分数的四则运算 ||"); System.out.println("|| 5、包括小数的运算 ||"); System.out.println("|| 6、支持带括号的运算 ||"); System.out.println(); System.out.print(" 请选择您需要的功能: "); } public static String jiaFa(int range/*数值范围*/,int negative_OK/*0表示无负数,1表示可有负数*/, int fraction/*0表示无分数,1表示真分数,2表示支持假分数*/,int decimal/*支持小数位数*/)//加法运算 { String str1="",str2=""; int rdm1,rdm2,rdm,rdm0; if(fraction==0)//不支持分数 { rdm1=((int)(Math.random()*range)); rdm2=((int)(Math.random()*range)); if(negative_OK==1) { if((int)(Math.random()*2)==1) { rdm1=-rdm1; } if((int)(Math.random()*2)==1) { rdm2=-rdm2; } } if(((int)(Math.random()*2)==1)&&(decimal>0)) { double rdm00; switch(decimal) { case 1:DecimalFormat df1 = new DecimalFormat( "0.0"); rdm00=Math.random(); str1=df1.format(rdm00+rdm1); break; case 2:DecimalFormat df2 = new DecimalFormat( "0.00"); rdm00=Math.random(); str1=df2.format(rdm00+rdm1); break; case 3:DecimalFormat df3 = new DecimalFormat( "0.000"); rdm00=(Math.random()); str1=df3.format(rdm00+rdm1); break; case 4:DecimalFormat df4 = new DecimalFormat( "0.0000"); rdm00=(Math.random()); str1=df4.format(rdm00+rdm1); break; case 5:DecimalFormat df5 = new DecimalFormat( "0.00000"); rdm00=(Math.random()); str1=df5.format(rdm00+rdm1); break; } } else { str1=""+rdm1; } if(((int)(Math.random()*2)==1)&&(decimal>0)) { double rdm00; switch(decimal) { case 0:DecimalFormat df0 = new DecimalFormat( "0."); rdm00=Math.random(); str2=df0.format(rdm00+rdm2); break; case 1:DecimalFormat df1 = new DecimalFormat( "0.0"); rdm00=Math.random(); str2=df1.format(rdm00+rdm2); break; case 2:DecimalFormat df2 = new DecimalFormat( "0.00"); rdm00=Math.random(); str2=df2.format(rdm00+rdm2); break; case 3:DecimalFormat df3 = new DecimalFormat( "0.000"); rdm00=(Math.random()); str2=df3.format(rdm00+rdm2); break; case 4:DecimalFormat df4 = new DecimalFormat( "0.0000"); rdm00=(Math.random()); str2=df4.format(rdm00+rdm2); break; case 5:DecimalFormat df5 = new DecimalFormat( "0.00000"); rdm00=(Math.random()); str2=df5.format(rdm00+rdm2); break; } } else { str2=""+rdm2; } if(rdm1<0) { str1="("+str1+")"; } if(rdm2<0) { str2="("+str2+")"; } } else if(fraction==1)//支持真分数 { if((int)(Math.random()*2)==0)//rdm1为整数 { rdm1=((int)(Math.random()*range)); if(negative_OK==1) { if((int)(Math.random()*2)==1) { rdm1=-rdm1; } } str1=""+rdm1; if(rdm1<0) { str1="("+rdm1+")"; } } else { rdm1=((int)(Math.random()*range)); rdm=((int)(Math.random()*range)); str1="("+(rdm1>rdm?(""+rdm+"/"+rdm1):(""+rdm1+"/"+rdm))+")"; } if((int)(Math.random()*2)==0)//rdm2为整数 { rdm2=((int)(Math.random()*range)); if(negative_OK==1) { if((int)(Math.random()*2)==1) { rdm2=-rdm2; } } str2=""+rdm2; if(rdm2<0) { str2="("+rdm2+")"; } } else { rdm2=((int)(Math.random()*range)); rdm=((int)(Math.random()*range)); str2="("+(rdm2>rdm?(""+rdm+"/"+rdm2):(""+rdm2+"/"+rdm))+")"; } } else if(fraction==2)//支持假分数 { if((int)(Math.random()*2)==0)//rdm1为整数 { rdm1=((int)(Math.random()*range)); if(negative_OK==1) { if((int)(Math.random()*2)==1) { rdm1=-rdm1; } } str1=""+rdm1; if(rdm1<0) { str1="("+rdm1+")"; } } else { rdm1=((int)(Math.random()*range)); rdm=((int)(Math.random()*range)); str1="("+rdm+"/"+rdm1+")"; } if((int)(Math.random()*2)==0)//rdm2为整数 { rdm2=((int)(Math.random()*range)); if(negative_OK==1) { if((int)(Math.random()*2)==1) { rdm2=-rdm2; } } str2=""+rdm2; if(rdm2<0) { str2="("+rdm2+")"; } } else { rdm2=((int)(Math.random()*range)); rdm=((int)(Math.random()*range)); str2="("+rdm2+"/"+rdm+")"; } } return(str1+"+"+str2); } public static String jianFa(int range/*数值范围*/,int negative_OK/*0表示无负数,1表示可有负数*/, int fraction/*0表示无分数,1表示真分数,2表示支持假分数*/,int decimal/*支持小数位数*/)//加法运算 { String str1="",str2=""; int rdm1,rdm2,rdm,rdm0; if(fraction==0)//不支持分数 { rdm1=((int)(Math.random()*range)); rdm2=((int)(Math.random()*range)); if(negative_OK==1) { if((int)(Math.random()*2)==1) { rdm1=-rdm1; } if((int)(Math.random()*2)==1) { rdm2=-rdm2; } } if(((int)(Math.random()*2)==1)&&(decimal>0)) { double rdm00; switch(decimal) { case 1:DecimalFormat df1 = new DecimalFormat( "0.0"); rdm00=Math.random(); str1=df1.format(rdm00+rdm1); break; case 2:DecimalFormat df2 = new DecimalFormat( "0.00"); rdm00=Math.random(); str1=df2.format(rdm00+rdm1); break; case 3:DecimalFormat df3 = new DecimalFormat( "0.000"); rdm00=(Math.random()); str1=df3.format(rdm00+rdm1); break; case 4:DecimalFormat df4 = new DecimalFormat( "0.0000"); rdm00=(Math.random()); str1=df4.format(rdm00+rdm1); break; case 5:DecimalFormat df5 = new DecimalFormat( "0.00000"); rdm00=(Math.random()); str1=df5.format(rdm00+rdm1); break; } } else { str1=""+rdm1; } if(((int)(Math.random()*2)==1)&&(decimal>0)) { double rdm00; switch(decimal) { case 0:DecimalFormat df0 = new DecimalFormat( "0."); rdm00=Math.random(); str2=df0.format(rdm00+rdm2); break; case 1:DecimalFormat df1 = new DecimalFormat( "0.0"); rdm00=Math.random(); str2=df1.format(rdm00+rdm2); break; case 2:DecimalFormat df2 = new DecimalFormat( "0.00"); rdm00=Math.random(); str2=df2.format(rdm00+rdm2); break; case 3:DecimalFormat df3 = new DecimalFormat( "0.000"); rdm00=(Math.random()); str2=df3.format(rdm00+rdm2); break; case 4:DecimalFormat df4 = new DecimalFormat( "0.0000"); rdm00=(Math.random()); str2=df4.format(rdm00+rdm2); break; case 5:DecimalFormat df5 = new DecimalFormat( "0.00000"); rdm00=(Math.random()); str2=df5.format(rdm00+rdm2); break; } } else { str2=""+rdm2; } if(rdm1<0) { str1="("+str1+")"; } if(rdm2<0) { str2="("+str2+")"; } } else if(fraction==1)//支持真分数 { if((int)(Math.random()*2)==0)//rdm1为整数 { rdm1=((int)(Math.random()*range)); if(negative_OK==1) { if((int)(Math.random()*2)==1) { rdm1=-rdm1; } } str1=""+rdm1; if(rdm1<0) { str1="("+rdm1+")"; } } else { rdm1=((int)(Math.random()*range)); rdm=((int)(Math.random()*range)); str1="("+(rdm1>rdm?(""+rdm+"/"+rdm1):(""+rdm1+"/"+rdm))+")"; } if((int)(Math.random()*2)==0)//rdm2为整数 { rdm2=((int)(Math.random()*range)); if(negative_OK==1) { if((int)(Math.random()*2)==1) { rdm2=-rdm2; } } str2=""+rdm2; if(rdm2<0) { str2="("+rdm2+")"; } } else { rdm2=((int)(Math.random()*range)); rdm=((int)(Math.random()*range)); str2="("+(rdm2>rdm?(""+rdm+"/"+rdm2):(""+rdm2+"/"+rdm))+")"; } } else if(fraction==2)//支持假分数 { if((int)(Math.random()*2)==0)//rdm1为整数 { rdm1=((int)(Math.random()*range)); if(negative_OK==1) { if((int)(Math.random()*2)==1) { rdm1=-rdm1; } } str1=""+rdm1; if(rdm1<0) { str1="("+rdm1+")"; } } else { rdm1=((int)(Math.random()*range)); rdm=((int)(Math.random()*range)); str1="("+rdm+"/"+rdm1+")"; } if((int)(Math.random()*2)==0)//rdm2为整数 { rdm2=((int)(Math.random()*range)); if(negative_OK==1) { if((int)(Math.random()*2)==1) { rdm2=-rdm2; } } str2=""+rdm2; if(rdm2<0) { str2="("+rdm2+")"; } } else { rdm2=((int)(Math.random()*range)); rdm=((int)(Math.random()*range)); str2="("+rdm2+"/"+rdm+")"; } } return(str1+"-"+str2); } public static String chengFa(int range,int fraction)//乘法运算 { String str1="",str2=""; int rdm0,rdm1,rdm2,temp; if(fraction==0) { rdm1=((int)(Math.random()*range)); rdm2=((int)(Math.random()*range)); str1=""+rdm1; str2=""+rdm2; } else if(fraction==1) { rdm1=((int)(Math.random()*range)); rdm2=((int)(Math.random()*range)); str1=""+rdm1; str2=""+rdm2; if((int)(Math.random()*2)==0) { rdm0=(int)(Math.random()*range); if(rdm0>rdm1) { temp=rdm1; rdm1=rdm0; rdm0=temp; } str1="("+rdm0+"/"+rdm1+")"; } if((int)(Math.random()*2)==0) { rdm0=(int)(Math.random()*range); if(rdm0>rdm2) { temp=rdm2; rdm2=rdm0; rdm0=temp; } str1="("+rdm0+"/"+rdm2+")"; } } else if(fraction==2) { rdm1=((int)(Math.random()*range)); rdm2=((int)(Math.random()*range)); str1=""+rdm1; str2=""+rdm2; if((int)(Math.random()*2)==0) { rdm0=(int)(Math.random()*range); if(rdm0<rdm1) { temp=rdm1; rdm1=rdm0; rdm0=temp; } str1="("+rdm0+"/"+rdm1+")"; } if((int)(Math.random()*2)==0) { rdm0=(int)(Math.random()*range); if(rdm0<rdm2) { temp=rdm2; rdm2=rdm0; rdm0=temp; } str1="("+rdm0+"/"+rdm2+")"; } } return (str1+"*"+str2); } public static String chuFa(int range,int fraction)//除法运算 { String str1="",str2=""; int rdm0,rdm1,rdm2,temp; if(fraction==0) { rdm1=((int)(Math.random()*range)); rdm2=((int)(Math.random()*range)); str1=""+(rdm1+1); str2=""+(rdm2+1); } else if(fraction==1) { rdm1=((int)(Math.random()*range))+1; rdm2=((int)(Math.random()*range))+1; str1=""+rdm1; str2=""+rdm2; if((int)(Math.random()*2)==0) { rdm0=(int)(Math.random()*range); if(rdm0>rdm1) { temp=rdm1; rdm1=rdm0; rdm0=temp; } str1="("+rdm0+"/"+rdm1+")"; } if((int)(Math.random()*2)==0) { rdm0=(int)(Math.random()*range); if(rdm0>rdm2) { temp=rdm2; rdm2=rdm0; rdm0=temp; } str1="("+rdm0+"/"+rdm2+")"; } } else if(fraction==2) { rdm1=((int)(Math.random()*range))+1; rdm2=((int)(Math.random()*range))+1; str1=""+rdm1; str2=""+rdm2; if((int)(Math.random()*2)==0) { rdm0=(int)(Math.random()*range); if(rdm0<rdm1) { temp=rdm1; rdm1=rdm0; rdm0=temp; } str1="("+rdm0+"/"+rdm1+")"; } if((int)(Math.random()*2)==0) { rdm0=(int)(Math.random()*range); if(rdm0<rdm2) { temp=rdm2; rdm2=rdm0; rdm0=temp; } str1="("+rdm0+"/"+rdm2+")"; } } return (str1+"/"+str2); } public static String three_Operands()//有括号 { int rdm=(int)(Math.random()*4); int n=(int)(Math.random()*10)+1; switch(rdm) { case 0:return("("+jiaFa(100,0,0,0)+")*"+n); case 1:return("("+jiaFa(100,0,0,0)+")/"+n); case 2:return("("+jianFa(100,0,0,0)+")*"+n); case 3:return("("+jianFa(100,0,0,0)+")/"+n); } return ""; } }

    运行结果截图

     

    程序设计思路:

    three_Operands()涉及3个操作数,由这个控制是否有括号

    产生的运算题目设置为字符串类型并且放到一个数组中利用String类中的equals方法进行比较新生成的题和已经存储的题

    利用Math.random()函数生成随机数产生计算题

    利用%判断是否换行,由用户自己输入控制行数和每行的题数

                                                                               一周之内的学习编程统计                                                                       时间:2017.3.9

    听课

    编程

    阅读

    备考

    当日总计

    星期一

    100min

    35min

     

     

    135min

    星期二

    40min

    25min

     

    65min

    星期三

     

     

          

     

     

    星期四

     

    120min

     

     

    120min

    星期五

     

    30min

     

     

    30min

                                                                                     时间记录日志                                                                               时间:2017.3.9

    开始时间

    结束时间

    休息时间

    总计

    学习内容

    3.6

    14:00

    15:50

    10

     

    听课、编程练习

    3.7

    19:30

    20:15

     

    1

    四则运算二

    3.8

    20:30

    21:00

          

     

    四则运算二

    3.9

    15:00

    17:00

    30

     

    四则运算二

    3.10

     

     

     

     

     

    缺陷记录日志

    时间:2017.3.9

    姓名:解昊兴

    指导教师:王建民

    程序:四则运算

    3.6

    上课练习中忘记 很多常用函数  就连Random()都是现百度的

    所以下课查了 一些相关知识

    3.7

    程序一直不能运行 后来发现{ }没用好 把一些重要函数都给括没了

    3.8

    3.9

    忽略了被除数中除数不能是0的问题

    防止重复方法进行百度查到的--利用String类中的equals方法进行比较新生成的题和已经存储的题

    实验体会:这次程序书写的过程不是很顺利,一些问题都是进行百度和问同学的,总算是写出来了。当然,这次收获 最大的就是上课老师的讲解给带来的启发,就像老师说的以后工作了,用半天时间书写代码,而用一天半来写实验报告,实验报告才是重中之重,是顾客老板经理参看的标准。老师这是在毕业前就培养我们认真书写实验报告的习惯。以后要提高对书写实验报告的重视,认真书写。

  • 相关阅读:
    leetcode211
    leetcode209
    leetcode201
    leetcode1396
    leetcode1395
    leetcode1394
    leetcode1386
    leetcode1387
    leetcode1382
    leetcode1376
  • 原文地址:https://www.cnblogs.com/xhxdeblog/p/6530264.html
Copyright © 2011-2022 走看看