第二次作业 熟悉使用工具

GIT地址

 https://github.com/iron-man45

GIT用户名

 iron-man45

学号后五位

62513

博客地址

 https://www.cnblogs.com/iron-man6/

作业链接

 

 https://www.cnblogs.com/harry240/p/11515697.html

 

 

一、项目背景

阿超家里的孩子上小学一年级了,这个暑假老师给家长们布置了一个作业:家长每天要给孩子出一些合理的,但要有些难度的四则运算题目,并且家长要对孩子的作业打分记录。

作为程序员的阿超心想,既然每天都需要出题,那何不做一个可以自动生成小学四则运算题目与解决题目的命令行 “软件”呢。他把老师的话翻译一下,就形成了这个软件的需求:

  • 程序接收一个命令行参数 n,然后随机产生 n 道加减乘除(分别使用符号+-*/来表示)练习题,每个数字在 0 和 100 之间,运算符在 2 个 到 3 个之间。
  • 由于阿超的孩子才上一年级,并不知道分数。所以软件所出的练习题在运算过程中不得出现非整数,比如不能出现 3÷5+2=2.6 这样的算式。
  • 练习题生成好后,将生成的 n 道练习题及其对应的正确答案输出到一个文件 subject.txt 中。

二、配置环境

1、vs2017安装

 

                                            

 

 

 

 

 

 

 

 2、注册下载github

注册账号截图

 

 

 

安装github截图

 

 github安装成功

 

 

三、具体代码

 method

using System;
using System.Collections.Generic;
using System.Text;

namespace Colotu
{
    class method
    {
        public method()
        {

        }
        public int Resr(int x1)
        {
            Random c = new Random();

            int x2 = c.Next(1, 101);
            while ((x1 / x2) * x2 != x1)
                x2 = c.Next(1, 101);
            return x2;
        }
        public int Result(int[] x, int[] y, int d)
        {
            List<int> list = new List<int>();
            list.Add(x[0]);
            for (int i = 0; i < d; i++)
            {
                list.Add(y[i]);
                list.Add(x[i + 1]);

            }
            for (int i = 1; i < list.Count; i += 2)
            {
                //遍历时不需要类型转换
                if (list[i] == 3)
                {
                    list[i - 1] = list[i - 1] / list[i + 1];
                    list.RemoveRange(i, 2);
                    i = -1;
                }
                else if (list[i] == 2)
                {
                    list[i - 1] = list[i - 1] * list[i + 1];
                    list.RemoveRange(i, 2);
                    i = -1;
                }


            }
            for (int i = 1; i < list.Count; i += 2)
            {
                //遍历时不需要类型转换
                if (list[i] == 1)
                {
                    list[i - 1] = list[i - 1] - list[i + 1];
                    list.RemoveRange(i, 2);
                    i = -1;
                }
                else if (list[i] == 0)
                {
                    list[i - 1] = list[i - 1] + list[i + 1];
                    list.RemoveRange(i, 2);
                    i = -1;
                }

            }
            return list[0];
        }

    }
}

 

 

main

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace Colotu
{
    class Program
    {
        
        static void Main(string[] args)
        {
            method md = new method();
            char[] e = new char[4] { '+', '-', '*', '/' };

            Console.WriteLine("输入一个数目按回车结束");

            int a = Convert.ToInt32(Console.ReadLine());
            
            for(int b=0;b<a;b++)
            {
                Random c = new Random();
                int d = c.Next(2, 4);
                int[] f = new int[4];
                int[] g = new int[3];

                f[0] = c.Next(0, 101);
                int parm = f[0];
                //d是运算符个数

                int tru = 1;
                for (int h=0;h<d;h++)
                {
                    
                    g[h] = c.Next(0, 4);
                    
                    
                    switch(g[h])
                    {
                        case 0: f[h+1] = c.Next(0, 101); if ((md.Result(f, g, h) < 0)&&(h!=0)) tru = 0; parm = f[h + 1]; break;
                        case 1: f[h+1] = c.Next(0, 101); if ((md.Result(f, g, h) < 0)&&(h!=0 ))tru = 0; parm = f[h + 1];  break;
                            //上面要检验正负
                        case 2: f[h+1] = c.Next(0, 101); parm *= f[h + 1]; break;
                        case 3: f[h+1] = md.Resr(parm); parm /= f[h + 1]; break;
                    }
                    if (tru == 0)
                        break;
                }
                int sum;
                sum = md.Result(f, g, d);
                if ((tru == 1)&&(sum>=0))
                {
                    



                    System.Text.StringBuilder sb = new System.Text.StringBuilder();

                    sb.Append(f[0]);

                    sb.Append(e[g[0]]);

                    sb.Append(f[1]);
                    sb.Append(e[g[1]]);
                    sb.Append(f[2]);

                    if (d == 3)
                    {

                        sb.Append(e[g[2]]);
                        sb.Append(f[3]);
                    }
                    sb.Append('=');
                    sb.Append(sum);
                    string str = sb.ToString();
                    Console.WriteLine(str);
                    //输入到文件中
                    string s = "G:/08test/subject.txt";
                    StreamWriter sw = new StreamWriter(@s, true);//true表示追加

                    sw.WriteLine(str);
                    sw.Flush();
                    sw.Close();
                }
                else
                    b--;
            }
            
        }
    }
}

 

 

 

 

四、单元测试

 

添加项目,新建单元测试类,重命名为UnitTestproject2,在TestMethod中写要测试的方法,在上面工具栏点击测试  

 

 

 

 

 四、调试

设断点

 

 

调试

 

 

五、回归测试:更改之前的代码测试和改之后的代码做测试,对比就是回归测试。

六、效能分析

点击性能探查器

 

 输入100,生成100道题目

 

 

结果如下

 

 

七、提交到gitHub

输入:git add .

git commit -m'提交'

git push 

 

 

 然后创建请求

 

 

创建成功就可以了

 

 八总结:1、编写c#代码不够熟练,提交github也遇到了问题,克隆的时候出现了问题而且之前把分支切换为c++,导致输入git add .命令一直出错,后来没有解决这个问题,只能重新克隆了一遍。

 

posted @ 2019-09-19 23:37  im-iron_man  阅读(159)  评论(1编辑  收藏  举报