软件工程第二次作业

github

 https://github.com/lcxyj1231

博客地址https://www.cnblogs.com/lcxyjst/
学号后五位61227
作业地址https://www.cnblogs.com/lcxyjst/p/11565202.html

这次的作业让我很伤脑筋。先前以为很简单,拖到最后一天再做,最后发现我高估自己了:

写代码花了不到一个小时,弄GitHub却搞了4个多小时。。。

 

 下面是我的一些问题:

1.我下载好了Git后发现我的Git bash here 用不了,就是要报错

《fatal: destination path '.' already exists and is not an empty directory. 》

最后喊了大三学帮忙还是弄不起,再加上我不会c#,瞬间崩溃。

 

解决方法:最后我放弃了vs,使用idea去管理Git,java我也比较熟练。以下是我的connection截的图:

 

 

 

 

 

 

**** 用idea去管理Git真的很方便


2.我从idea直接将Git上的项目弄过来没法用:

报错信息:

《IDEA 'Error:java: 无效的源发行版: 12' 解决方案》

解决方法:最后百度知道,原来从Git上弄过来的项目还没有配置默认的jdk和out路径,另外最重要的是Mark directory as选项不正确,所以不能按正常平时新建的项目一样运行

截图:

 

 

 ***我也通过这次机会更加熟悉了idea的强大配置

 3.代码的问题,我的代码中间出了一些小差错,因为我使用的是边写边改的模型。。。

每次运行结果都会报异常。(代码参考了一些博客,但是最后还是自己完成的)

解决:最后我判断了一下0才解决,为了让除法运算能够整除,我用Hashset储存了除号上一个数的因子,除号下一个数将从因子集合中挑选:

public static int getset(int last){
        Set<Integer> set=new HashSet<>();
        for(int i=1;i<=last;i++){
            if(last%i==0)
                set.add(i);
        }
        int m= (int) (Math.random() * set.size())+1;
        for(int q:set){
           m--;
           if(m==0)return q;
        }
        return 1;
    }

4.最艰难的时刻到了,我不会上传,看了几个教程还是不会,于是我请教了助教(侯肖*)学长,终于我发现了自己的错误。

错误:我的代码上传到了另外新建的仓库,后来才知道原来代码必须上传到自己克隆过来的那个repositories,不然无法pull  request。。。

自己被自己蠢哭。。。

解决方法:没啥办法,直接返工。

上传截图

 

 

总结:总的来说,这次作业我完成的有点糟糕,被GitHub玩弄于鼓掌,还好我是计科院团队的人(学长资源比较丰富),很多学长给我了建议。其实几个小时的忙活,我也有很多收获,我会使用一些GitHub的基本功能了,以前就是咋学也看不懂(可能是因为英文) 我也学会了用idea去操作Git和GitHub。不得不说idea真的很强大。。但是我还是用不来(吐血)

 

以下是我的全部代码:

 

 

package src.mysido;

import com.sun.javafx.css.CalculatedValue;
import sun.applet.Main;

import java.io.*;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class fourprogram {
    private static String[] op = { "+", "-", "*", "/" };
    public static void main(String[] args) {
        int n;
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();

        BufferedWriter bw=null;
        File f=new File("E:\\result.txt");
        if(!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            bw = new BufferedWriter(new FileWriter(f));
        }  catch (IOException e) {
            e.printStackTrace();
        }
        for(int i=0;i<n;i++){
            String myneed=MakeFormula();
            int p= solve.Calcula(myneed);
            myneed+=p;
            System.out.println(myneed);
            try {
                bw.write(myneed);
                bw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static int getset(int last){
        Set<Integer> set=new HashSet<>();
        for(int i=1;i<=last;i++){
            if(last%i==0)
                set.add(i);
        }
        int m= (int) (Math.random() * set.size())+1;
        for(int q:set){
           m--;
           if(m==0)return q;
        }
        return 1;
    }
    public static String MakeFormula(){

        StringBuilder build = new StringBuilder();

        int count = (int) (Math.random() * 2) + 1;// generate random count

        int start = 0;

        int number1 = (int) (Math.random() * 99) + 1;

        build.append(number1);
        int last=number1;
        while (start <= count){

            int operation = (int) (Math.random() * 4); // generate operator
            int number2 = (int) (Math.random() * 99) + 1;
            if(operation==3){
                if (last%number2!=0){
                    number2 = getset(last);
                }
            }
            last=number2;
            build.append(op[operation]).append(number2);
            start ++;
        }
        build.append('=');
        return build.toString();
    }
}


package src.mysido;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class solve {
    static Map<String, Integer> OPT_PRIORITY_MAP = new HashMap<String, Integer>() {
        private static final long serialVersionUID = 6968472606692771458L;
        {
            put("+", 2);
            put("-", 2);
            put("*", 3);
            put("/", 3);
            put("=", -1);
        }
    };
    public static int getPriority(String opt1, String opt2) {
        int priority = OPT_PRIORITY_MAP.get(opt2) - OPT_PRIORITY_MAP.get(opt1);
        return priority;
    }
    public static void compareAndCalc(Stack<String> optStack, Stack<Integer> numStack,
                                      String curOpt) {
        // 比较当前运算符和栈顶运算符的优先级
        String peekOpt = optStack.peek();
        int priority = getPriority(peekOpt, curOpt);
        if (priority == -1 || priority == 0) {
        // 栈顶运算符优先级大或同级,触发一次二元运算
            String opt = optStack.pop(); // 当前参与计算运算符
            int num2 = numStack.pop(); // 当前参与计算数值2
            int num1 = numStack.pop(); // 当前参与计算数值1
            int res = frealCalc(opt, num1, num2);
            numStack.push(res);
            // 运算完栈顶还有运算符,则还需要再次触发一次比较判断是否需要再次二元计算
            if (optStack.empty()) {
                optStack.push(curOpt);
            } else {
                compareAndCalc(optStack, numStack, curOpt);
            }
        } else {
            optStack.push(curOpt);
        }
    }


    static int Calcula(String exp) {

        StringBuilder curNumBuilder = new StringBuilder(16);
        Stack<String> optStack = new Stack<>(); // 运算符栈
        Stack<Integer> numStack = new Stack<>();
        for (int i = 0; i < exp.length(); i++) {
            char c = exp.charAt(i);
            if (c >= '0' && c <= '9') curNumBuilder.append(c);
            else {
                if (curNumBuilder.length() > 0) {// 如果追加器有值,说明之前读取的字符是数值,而且此时已经完整读取完一个数值
                    numStack.push(Integer.valueOf(curNumBuilder.toString()));
                    curNumBuilder.delete(0, curNumBuilder.length());
                }
                String curOpt = String.valueOf(c);
                if (optStack.empty()) {// 运算符栈栈顶为空则直接入栈
                    optStack.push(curOpt);
                } else {
                    if (curOpt.equals("=")) {
                        if(numStack.size()==1)return numStack.pop();
                        while (!optStack.empty()){
                            int s1=numStack.pop();
                            int s2=numStack.pop();
                            String p=optStack.pop();
                            int res=frealCalc(p,s2,s1);
                            numStack.push(res);
                        }
                        return numStack.pop();

                    } else {
                        compareAndCalc(optStack,numStack,curOpt);
                    }

                }


            }
        }
        return 0;
    }
    private static int frealCalc(String curOpt, int a, int b) {
        if(curOpt.equals("+"))return a+b;
        if(curOpt.equals("-"))return a-b;
        if(curOpt.equals("*"))return a*b;
        if(curOpt.equals("/"))return a/b;
        return 0;
    }
}

 

运行截图:

 

 

 

 

 

终于---心累的软工第二次作业完成了

 

转载于:https://www.cnblogs.com/lcxyjst/p/11565202.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值