• 二柱子——四则运算——王建民


    源码: 

    //信1905-1 20194191 薛宪光
    import java.lang.Math;
    import java.util.*;
    public class SIZE {
    public static void main(String[]args) {

    //

    //int ques;
    //int uk;
    //int shangjie,xiajie;
    int right =0,wrong=0;
    String []array = new String[1000];
    //String []cuo_ti_ben = new String[1000];
    List<String> list=new ArrayList<String>();
    Arrays.fill(array, "");
    List<Integer> can_shu=new ArrayList<Integer>();

    can_shu.add(0,30);
    can_shu.add(1,2);
    can_shu.add(2,10);
    can_shu.add(3,1);
    can_shu.add(4,2);
    /*for (int i = 0; i < can_shu.size(); i++) {
    int alEach = (int) can_shu.get(i);

    System.out.println(alEach);
    }*/
    mainmenu(can_shu,list);
    // Arrays.fill(cuo_ti_ben, "");
    /* ques = scanner.nextInt();
    System.out.println("please enter the number of unknows:");
    uk = scanner.nextInt();
    System.out.println("please enter the unknows's shangjie ");
    shangjie = scanner.nextInt();
    System.out.println("please enter the unknows's xiajie ");
    xiajie = scanner.nextInt();*/
    zuoti(can_shu,array,list);
    }
    public static void zuoti(List<Integer>can_shu,String[]array,List<String>list) {
    String timu;
    int right = 0,wrong=0;
    Scanner scanner = new Scanner(System.in);
    long starTime=System.currentTimeMillis();
    for(int j =1;j<100;j++)
    {
    System.out.println("下面开始进行第"+j+"套题的训练");
    for(int i=1;i<=(int)can_shu.get(0);i++)
    {

    timu=Random(can_shu,can_shu.get(4));
    System.out.print("第"+i+"个题:");

    while(panchong(array,timu)==0)//1Bu重,0重
    {
    timu=Random(can_shu,can_shu.get(4));
    }
    array[i] = timu;
    System.out.print(array[i]);
    System.out.println("=");
    String linshi = array[i];

    //System.out.println(b);

    double result1 = jisuan.compute(linshi);
    System.out.println("请输入你的答案");
    int daan = scanner.nextInt();
    if(daan==(int)result1)
    {
    right++;
    System.out.println("恭喜你,答对了!");
    }
    else {
    System.out.println("很遗憾,答错了,这题将加入错题本!");
    wrong++;
    list.add(linshi);
    }

    }
    System.out.println("总共"+(right+wrong)+"道题目,其中有"+right+"道题目正确,正确率为:"+((double)right/(double)(right+wrong))*100+"%");
    System.out.println("是否要进行下一套题的训练,1.是.2.否");
    int choice2 = scanner.nextInt();
    if(choice2==1)
    continue;
    else
    break;
    }
    long endTime=System.currentTimeMillis();
    long Time=endTime-starTime;
    System.out.println("本次答题用时"+Time/1000+"秒");
    /* System.out.println("是否要进行错题练习,1.是,2.否。");
    int choice=scanner.nextInt();
    array = null;
    if(choice ==2)
    {System.out.println("本次答题结束");
    }
    else {
    choice = 0;
    System.out.println("是否要继续进行错题本练习,1.是,2.否。");
    while(choice==1) {
    CuoTiBen(list);
    }
    }
    System.out.println("谢谢使用");*/

    //界面
    public static void mainmenu(List<Integer> can_shu,List<String> list) {
    System.out.println("****************欢迎使用四则运算程序****************"
    +' '+"1.参数设置。"+' '+"2.小学二年级口算题。"
    +' '+"3.小学三年级口算题。"+' '+"4.小学四年级口算题。"
    +' '+"5.错题集"+' '+"****************************************************");
    Scanner sca = new Scanner(System.in);
    int op = sca.nextInt();

    switch(op) {
    case 1:
    can_shu.add(4,0);
    canshu(can_shu,list);
    break;
    case 2:
    can_shu.add(4,2);
    Random(can_shu,(int)can_shu.get(4));
    break;
    case 3:
    can_shu.add(4,3);
    Random(can_shu,(int)can_shu.get(4));
    break;
    case 4:
    can_shu.add(4,4);
    Random(can_shu,(int)can_shu.get(4));
    break;
    case 5:
    CuoTiBen(list);
    break;
    default:
    System.exit(0);
    break;
    }
    }
    //参数设置
    public static void canshu(List<Integer> can_shu,List<String> list) {
    Scanner scb = new Scanner(System.in);
    System.out.println("请输入出题的数目:");
    int a1 = scb.nextInt();
    can_shu.add(0,a1);
    System.out.println("请输入未知数个数:");
    int b1 = scb.nextInt(); 
    can_shu.add(1,b1);
    System.out.println("请输入参数上界");
    int c1 = scb.nextInt();
    can_shu.add(2,c1);
    System.out.println("请输入参数下界");
    int d1 = scb.nextInt();
    can_shu.add(3,d1);
    mainmenu(can_shu,list);
    }
    //题目生成
    public static String Random(List<Integer>can_shu,int z)

    int flag = 0;
    String s ;//题目形式
    s = "";
    int b = (int)can_shu.get(1);//未知数个数
    int c = (int)can_shu.get(2);//上界
    int d = (int)can_shu.get(3);//下界
    if(z>=2)
    {
    b=2;
    c = 100;
    d = 1;
    if(z>=3)
    {
    b=4;
    c = 1000;
    if(z==4)
    {
    b=5;
    flag=1;
    }
    }
    }
    int f = (int)(Math.random()*(b-1)+1);
    for(int j = 1;j<=b;j++)

    int random = (int)(Math.random()*(c-d)+d);//生成操作数
    s =s+String.valueOf(random);
    //System.out.print(random);
    if(j==b)
    {
    System.out.print("");
    if(flag==1)
    // System.out.print(")");
    s= s+")";
    // s =s+"=";
    //System.out.println("=");
    }
    else{
    int e = (int)(Math.random()*4);
    switch(e) {
    case 0:
    s=s+"+";
    // System.out.print("+");
    break;
    case 1:
    s= s+"-";
    // System.out.print("-");
    break;
    case 2:
    s= s+"*";
    // System.out.print("*");
    break;
    case 3:
    s= s+"/";
    // System.out.print("/");
    break;

    }
    if(flag==1)
    {
    if(f==j)
    //System.out.print("(");
    s= s+"(";


    }
    }

    }

    return s;



    //判断重复
    public static int panchong(String []a,String b) {
    int flag = 1;
    for(int i = 0;i<a.length;i++)
    {

    if(a[i].equals(b))
    {
    flag = 0;//chong
    break;
    }
    else
    continue;



    }
    return flag;

    }
    //错题本
    public static void CuoTiBen(List<String>list) {
    System.out.println("欢迎进入错题本练习");
    for(int i = 0;i<list.size();i++) 
    {
    Scanner sc = new Scanner(System.in);
    String cuotiben = (String)list.get(i);
    System.out.print(cuotiben); 
    System.out.println("=");
    double result2=jisuan.compute(cuotiben);
    System.out.println("请输入你的答案");
    int cuotidaan = sc.nextInt();
    if(cuotidaan==(int)result2)
    {

    System.out.println("恭喜你,答对了!");
    list.remove(i);
    }
    else 
    {
    System.out.println("很遗憾,这题又错了!");
    }
    }
    if(list==null||list.size()==0)
    System.out.println("错题本练习结束");


    }




    //calculate class
    class jisuan {

    /* public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String str = scanner.nextLine();
    //double result1 = compute("-3*-3");
    double result1 = compute(str);
    System.out.println((int) result1);
    }*/

    public static int priority(char s) {
    switch (s) {
    case '(':
    case ')':
    return 0;
    case '-':
    case '+':
    return 1;
    case '*':
    case '%':
    case '/':
    return 2;
    default:
    return -1;

    }
    }

    public static double compute(double num1, double num2, char s) {
    switch (s) {
    case '(':
    case ')':
    return 0;
    case '-':
    return num1 - num2;
    case '+':
    return num1 + num2;
    case '%':
    return num1 % num2;
    case '*':
    return num1 * num2;
    case '/':
    return num1 / num2;
    default:
    return 0;

    }
    }

    public static double compute(String str) {
    double num[] = new double[20];
    int flag = 0, begin = 0, end = 0, now;
    now = -1;
    Stack<Character> st = new Stack<Character>();
    for (int i = 0; i < str.length(); i++) {
    char s = str.charAt(i);
    if (s == ' ') {

    } else if (s == '+' || s == '-' || s == '*' || s == '/' || s == '(' || s == ')' || s == '%') {
    if (flag == 1) {
    now += 1;
    if (end < begin) {
    num[now] = Integer.valueOf(str.substring(begin, begin + 1));
    } else {
    num[now] = Integer.valueOf(str.substring(begin, end + 1));
    }
    // System.out.println(num[now]);
    flag = 0;
    }
    if (s == '-') {
    if (i == 0) {
    flag = 1;
    begin = 0;
    } else if (str.charAt(i - 1) == '(' || str.charAt(i - 1) == '*'
    || str.charAt(i - 1) == '/') {
    flag = 1;
    begin = i;
    }
    else {
    if (st.empty()) {
    st.push(s);
    } else if (s == ')') {
    num[now - 1] = compute(num[now - 1], num[now], st.pop());
    now -= 1;
    st.pop();
    } else if (s == '(') {
    st.push(s);
    } else if (priority(s) <= priority(st.peek())) {
    num[now - 1] = compute(num[now - 1], num[now], st.pop());
    now -= 1;
    st.push(s);
    } else {
    st.push(s);
    }
    }
    } else if (st.empty()) {
    st.push(s);
    } else if (s == ')') {
    num[now - 1] = compute(num[now - 1], num[now], st.pop());
    now -= 1;
    st.pop();
    } else if (s == '(') {
    st.push(s);
    } else if (priority(s) <= priority(st.peek())) {
    num[now - 1] = compute(num[now - 1], num[now], st.pop());
    now -= 1;
    st.push(s);
    } else {
    st.push(s);
    }

    } else if (flag == 0) {
    flag = 1;
    begin = i;
    } else {
    end = i;
    }

    }
    if (flag == 1) {
    now += 1;
    if (end < begin) {
    num[now] = Integer.valueOf(str.substring(begin, begin + 1));
    } else {
    num[now] = Integer.valueOf(str.substring(begin, end + 1));
    }
    // System.out.println(num[now]);
    }
    while (now > 0) {
    num[now - 1] = compute(num[now - 1], num[now], st.pop());
    now -= 1;
    }
    return num[0];
    }


    }

  • 相关阅读:
    jquery正则表达式验证:正整数(限制长度)
    H5页面快速搭建之高级字体应用实践
    如何用Python写一个贪吃蛇AI
    HashMap多线程并发问题分析
    为RecyclerView打造通用Adapter 让RecyclerView更加好用
    学好Mac常用命令,助力iOS开发
    使用 Realm 和 Swift 创建 ToDo 应用
    看Facebook是如何优化React Native性能
    利用github搭建个人maven仓库
    Objective-C Runtime之着魔的UIAlertView
  • 原文地址:https://www.cnblogs.com/sakura-xxg/p/13870959.html
Copyright © 2020-2023  润新知