1.在源代码的基础上,增加了选项,其执行为随机若干个数,与随机数代表的字符相连,成为一个字符串。将其转换为后则表达式,并放入栈中运算。

2.代码

package 课堂;

import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.text.NumberFormat;

public class TheAirthmeticOperation_Fuction
{
    private final static char ADD = '+';
    private final static char SUBTRACT = '-';
    private final static char MULTIPLY = '*';
    private final static char DIVIDE = '/';
    
    private Stack <Character> stack;//构造函数初始化栈
    public TheAirthmeticOperation_Fuction()
    {
        stack = new Stack<Character>();
    }
     
    Scanner sc = new Scanner(System.in);
    
    public int Randomnumber(int n)//重写的随机函数,避免随机出零。
    {
        int number = 0;
        while(number == 0)
        {
            number = new Random().nextInt(n);
        }
        return number;
    }
    
    public int Setnumber()//设置数组,及习题数量。
    {
        int n = 30;
        System.out.println("输入数组数量");
        n = sc.nextInt();
        return n;
    }
    
    public int Setrange()//设置随机数范围。
    {
        int n = 100;
        System.out.println("输入数值范围");
        n = sc.nextInt();
        return n;
    }
    
    public int Set_mul_and_div()//设置习题中是否有乘除法。
    {
        int n = 0;int m = 2;
        System.out.println("是否有乘除法:(0:无  1:有)");
        n = sc.nextInt();
        if(n == 0)
        {
            m = 2;
        }
        else
            m = 4;
        return m;
    }
    
    public int Printmethod()//设置输出习题方式。
    {
        int n = 0;
        System.out.println("打印方式:(0:直接输出  1:打印到txt)");
        n = sc.nextInt();
        return n;
    }
    
    public int Is_customization()//是否定制多数运算。
    {
        int n = 0;
        System.out.println("是否定制多数运算:(0:否  1:是)");
        n = sc.nextInt();
        return n;
    }
    
    public int customization_number()//设置多数运算数量。
    {
        int n = 0;
        System.out.println("输入多数运算数量");
        n = sc.nextInt();
        return n;
    }
    
    public int customization_brackets()//设置多数运算数量。
    {
        int n = 0;
        System.out.println("十位数运算是否有括号:(0:否  1:是)");
        n = sc.nextInt();
        return n;
    }
    
    public String customization(int n)//定制多数习题。
    {
        int []Array = new int[10];//定义一个数组。
        int []a = new int[10];//控制数与数之间符号。
        int []b = new int[20];//控制在某位值是否出现括号。
        String []brackets = new String[20];//用于存放括号。
        String str = "";
        String str1 = "";
        for(int i = 0;i < 10;i++)//随机数。
        {
            a[i] = new Random().nextInt(4);
            Array[i] = Randomnumber(20);
        }
        for(int i = 0;i < 20;i++)//初始化括号数组。
        {
            brackets[i] = "";
        }
        
        if(n == 1)//用户选择定制。
        {
            for(int i = 0;i < 20;i += 2)
            {
                b[i] = new Random().nextInt(4);/*为b[i生成随机数,当b[i]为零时,
                brackets[i]添加左括号,brackets[i + 3]添加右括号,括号套括号未考虑。*/
                if(i >= 0 && i < 17)//i > 16 时,没有位置添加右括号,遂向下执行。
                {
                    if(b[i] == 0)
                    {
                        brackets[i] += "(";
                        brackets[i + 3] += ")";
                        i += 2;
                    }
                }
            }
        }
                
        for(int i = 0;i < 9;i++)//将数,括号,符号相连。
        {
            str += brackets[2 * i] + Array[i] + brackets[2 * i + 1];
            str = Symbol(str,a[i]);
        }
        str += Array[9] + brackets[19];
        str1 = analysisString(str);
        System.out.println(str1);
        int result = evaluate(str1);//计算
        System.out.println("\n这个表达式的值是:" + result);
        str += " =";
        return str;
    }
    
    private String analysisString(String str)
    {
        String str1 = "";
        for (int i = 0; i < str.length(); i++)
        {
            char c = str.charAt(i);
            if (c == '+' || c == '-')
            {
                if (stack.isEmpty() || stack.peek() == '(')
                {
                    stack.push(c);
                }
                else 
                {
                    while (!stack.isEmpty()&& (stack.peek() == '*' || stack.peek() == '/'|| stack.peek() == '+' || stack.peek() == '-'))
                    {
                        /*System.out.print(stack.pop());*/
                        str1 += stack.pop() + " ";
                    }
                    stack.push(c);
                }
            }
            else if (c == '*' || c == '/')
            {
                if (stack.isEmpty() || stack.peek() == '+'|| stack.peek() == '-' || stack.peek() == '(')
                {
                    stack.push(c);
                }
                else
                {
                    while (!stack.isEmpty()
        && (stack.peek() == '/' || stack.peek() == '*'))
                    {
                        /*System.out.print(stack.pop());*/
                        str1 += stack.pop() + " ";
                    }
                    stack.push(c);
                }
            }
            else if (c == '(')
            {
                stack.push(c);
            }
            else if (c == ')')
            {
                char temp = ' ';
                while ((temp = stack.pop()) != '(')
                {
                    /*System.out.print(temp);*/
                    str1 += temp + " ";
                }
            }
            else
            {
                /*System.out.print(c);*/
                str1 += c + " ";
            }
        }
        if (!stack.isEmpty())
        {
            while (!stack.isEmpty())
            {
                System.out.print(stack.pop());
            }
        }
        return str1;
    }
    
    public int evaluate(String expresion)
    {
        double op1,op2;//临时变量,保存操作符和中间结果
        Character result = 0;
        String token;//字符
        @SuppressWarnings("resource")
        Scanner parser = new Scanner(expresion);//循环获取每一个字符
        while (parser.hasNext())
        {
            token = parser.next();
            //如果是表达式,就弹出
            if(isOperator(token))
            {
                op2 = (stack.pop()).charValue();
                op1 = (stack.pop()).charValue();//计算中间结果
                result = evaluateSingleOperator(token.charAt(0),op1,op2);//将中间结果压入栈
                stack.push(result);
            }
            else//不是操作符就直接压入栈
            {
                stack.push((char) Integer.parseInt(token));
            }
        }
        return result;
    }
    private Character evaluateSingleOperator(char operator,double op1,double op2)
    {
        Character result = 0;//分情况计算
        switch(operator)
        {
            case ADD:
                result = (char) (op1 + op2);
                break;
             case SUBTRACT:
                 result = (char) (op1 - op2);
                 break;
             case MULTIPLY:
                 result = (char) (op1 * op2);
                 break;
             case DIVIDE:
                 result = (char) (op1 / op2);
                 break;
        }
        return result;
    }
    private boolean isOperator(String token)
    {
        return(token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/"));
    }
    
    public void Setremainder(int []Array1,int []Array2,int num,int n2)
    {//若设置除法无余数,另Array1[i] < Array2[i]。
        if(n2 == 2)
        {
            return;
        }
        int n = 0;
        System.out.println("除法是否有余数:(0:无  1:有)");
        n = sc.nextInt();
        if(n == 0)
        {
            for(int i = 0;i < num;i++)
            {
                Absolutevaluecomparison(Array1,Array2,i);
            }
        }
        else
            return;
    }
    
    public void Setnegativenumber(int []Array1,int []Array2,int num)
    {//若设置有负数,随机为Array[i]添加符号,i为随机
        int n = 0;int stochastic1 = 0;int stochastic2 = 0;
        System.out.println("运算有无负数:(0:无  1:有)");
        n = sc.nextInt();
        if(n == 0)
        {
            return;
        }
        else
        {
            while(true)
            {
                stochastic1 = Randomnumber(num);
                stochastic2 = Randomnumber(num);
                Array1[stochastic1] = -Array1[stochastic1];
                Array2[stochastic2] = -Array2[stochastic2];
                if(stochastic1 == 1)//当stochastic1 = 1 时,跳出循环。
                {
                    break;
                }
            }
        }
    }

    public void Absolutevaluecomparison(int []Array1,int []Array2,int i)
    {//使两个数最小的数放在Array[i]中,保证无余数或分数为真分数。
        if(Math.abs(Array1[i]) > Math.abs(Array2[i]))
        {
            int t = Array1[i];
            Array1[i] = Array2[i];
            Array2[i] = t;
        }
    }
    
    public int[] Isrepetition(int[] Array,int num,int n1)
    {//删除Array数组中重复元素,缺省数重新随机(此处未考虑随机后还有相等情况,因重复几率较低)。
        int length = 1;
        boolean flag = false;
        for(int i = 1;i < Array.length;i++)
        {
            for(int j = 0;j < length;j++)
            {
                if(Array[i] == Array[j])
                {  
                    flag = true;
                }
            }
            if(!flag)
            {
                Array[length] = Array[i];  
                length++;  
            }
            flag = false;  
        }
        int[] newArray = new int[num];
        System.arraycopy(Array,0,newArray,0,length);
        for(int i = length;i < num;i++)
        {
            newArray[i] = Randomnumber(n1);
        }
        return newArray;
    }
    
    public String Symbol(String str,int value)
    {//根据值为str添加符号。
        if(value == 0)
        {
            str += " + ";
        }
        if(value == 1)
        {
            str += " - ";
        }
        if(value == 2)
        {
            str += " * ";
        }
        if(value == 3)
        {
            str += " / ";
        }
        return str;
    }
    public void integerOperationAnswer(int number1,int number2,int value)
    {  
        NumberFormat DF = NumberFormat.getNumberInstance();
        System.out.println("请输入结果(给出两位小数)");
        double answer = 0;
        double result = 0;
        String s1 = "1",s2 = "0";
        if(value != 3)
        {
            answer = sc.nextInt();
        }
        else
        {
            answer = sc.nextFloat();
            DF.setMaximumFractionDigits(2); 
            s1 = DF.format(answer); 
        }
        if(value == 0)
        {
            result = number1 + number2;
        }
        if(value == 1)
        {
            result = number1 - number2;
        }
        if(value == 2)
        {
            result = number1 * number2;
        }
        if(value == 3)
        {
            double number3 = number1;
            double number4 = number2;
            result = (double)(number3 / number4);
            s2= DF.format(result);
        }
        if(result == answer && value != 3)
        {
            System.out.println("结果正确");
        }
        else if(s1.equals(s2) && value == 3)
        {
            System.out.println("结果正确");
        }
        else
            System.out.println("结果错误");
    }
    public void fractionOperationAnswer(int number1,int number2,int number3,int number4,int value)
    {
        int numberator = 0;
        int denominator = 0;
        String result = "1";
        String answer;
        System.out.println("请输入结果(给出最简分数)");
        answer = sc.next();
        if((number1 < 0 && number2 < 0) || (number1 > 0 && number2 < 0))
        {
            number1 = -number1;
            number2 = -number2;
        }
        if((number3 < 0 && number4 < 0) || (number3 > 0 && number4 < 0))
        {
            number3 = -number3;
            number4 = -number4;
        }
        if(value == 0)
        {
            number1 *= number4;
            number3 *= number2;
            numberator = number1 + number3;
            denominator = number2 * number4;
        }
        if(value == 1)
        {

            number1 *= number4;
            number3 *= number2;
            numberator = number1 - number3;
            denominator = number2 * number4;
        }
        if(value == 2)
        {
            numberator = number1 * number3;
            denominator = number2 * number4;
        }
        if(value == 3)
        {
            int n = number3;
            number3 = number4;
            number4 = n;
            numberator = number1 * number3;
            denominator = number2 * number4;
        }
        result = String.valueOf(numberator/getGreatestCommonDivisor(numberator,denominator))
                + "/" + String.valueOf(denominator/getGreatestCommonDivisor(numberator,denominator));
        if(answer.equals(result))
        {
            System.out.println("结果正确");
        }
        else
            System.out.println("结果错误,正确答案为:" + result);    
    }
    public int getGreatestCommonDivisor(int number1,int number2)
    {
        number1 = Math.abs(number1);
        int n;
        while(number2 > 0)
        {
            n = number1 % number2;
            number1 = number2;
            number2 = n;
        }
        return number1;
    }
}

3.图片

4.分析

  两位整数的运算除法没有用分子表示,因为就两个数再不出结果就是约分了,没意义。两位分数运算是根据通分运算化简而得。

  难点在多位数运算,这里本来想用小数参与运算的。不过在网上看了看例子,觉得用栈还可以,但是当初学栈的时候是用的c,所以不是很熟练。姑且没报错误吧,改了很久,结果也没对上,等再改吧。

5.

  听课 编程 读书 总计
周一 2 1.5 0 3.5
周二 0 1.5 0 1.5
周三 0 1 0 1
周四 0 1 0 1
周五 0 0.5 0 0.5
周六 0 1 0 1
周日 0 0 1.5 1.5

6.时间记录日志

日期 开始时间 结束时间 中断时间(min) 净时间(min) 活动 备注
3.14 8:00 9:50 10 100 上课 课间休息十分钟
15:35 15:51 0 16 编程  
3.15 16:19 17:28 0 71 编程  
20:53 21:37 8 46 编程  
3.16 6:50 7:23 0 33 编程  
15:10 16:25 10 65 上网 学习栈和后缀表达式 
20:08 20:50 0 42 编程    调试bug

3.18
12:35 13:10 0 35 编程
16:10 17:38 6 82 编程
20:08 21:10 0 62 编程   逻辑上无错误,结果错误。
3.19 17:20 19:15 0 115 编程
总计 / / 34 667 /  

7.合作照片

posted on 2016-03-21 15:41  消失。  阅读(139)  评论(0编辑  收藏  举报