The second work of --AchaoCalculator

AchaoCalculator (A super-calculator)

GIT address My GitHub
GIT Username Pastrain
Student ID after five 62213
blog address My blog address
Job Link contents of homework

Part.1 configuration in VS C # environment:

Because the summer before freshman year of training, the use of language is C #, so my computer is already configured in the VS.

As shown, it has been possible to create a project:

Project created as shown:
在这里插入图片描述
Because it is already configured before, so I do not have any extra questions on the process of configuring the environment.

Part.2 cloning project:

He said the course is to use Git to clone projects locally. But I choose to use directly on GitHub "Open in Desktop" mode, as shown:

在这里插入图片描述
Then clone can also be found down the project locally:
在这里插入图片描述
By this time, cloning environment configuration and GitHub project would have been completed. The next step is the most important part of writing code.

Part.3 AchaoCalculator coding:

Problem background:

On A super family of children in first grade, and this summer the teachers to the parents arranged a job: Parents give their children every day some reasonable, but the four operations subject some difficulty, and parents to the child's job scoring record .

A super programmer as I thought, since the topic is required every day, why not do a primary school can automatically generate the title four operations to solve the problem with the command line "software" it. He then translate what the teacher, on the formation of the demand of this software:

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

13+17-1=29
11*15-5=160
3+10+4-16=1
15÷5+3-2=4

我的解决方案:

使用了Random的随机数功能和DataTable的字符串计算功能。

Calculator为主要的生成题目函数,MainCal为核心计算结果的函数。

我的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;


namespace AchaoCalculator
{
    public class Program
    {
        private static string[] s = { "+", "-", "*", "/" };
        static void Main(string[] args)
        {
            Console.WriteLine("输入题目数量:");
            int n = int.Parse(Console.ReadLine());

            Calculator(n);
            
            Console.ReadLine();
                
        }
        public static void Calculator(int n)
        {
            Random r = new Random();
            for (int j = 0; j < n; j++)
            {
                int c = r.Next(2, 4);
                int[] num = new int[10];

                for (int i = 0; i < c + 1; i++)
                {
                    num[i] = r.Next(1, 101);
                }
                string[] x = new string[c * 2 + 1];
                for (int k = 0; k < c * 2 + 1; k++)
                {
                    if (k % 2 == 0)
                    {
                        x[k] = num[k / 2].ToString();
                    }
                    else
                    {
                        x[k] = s[r.Next(0, 4)];
                    }
                }
                string finals = x[0];
                for (int z = 0; z < c * 2 + 1; z++)
                {
                    if (z != 0)
                    {
                        finals += x[z];
                    }
                }
                if (MainCal(finals) < 0 || MainCal(finals) % 1 != 0)
                {
                    j--;
                    continue;
                }
                finals += "=" + MainCal(finals).ToString();
                Console.WriteLine(finals);
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"test.txt", true))
                {
                    file.WriteLine(finals);

                }    
            }
        }
        public static double MainCal(string finals)
        {
            DataTable d = new DataTable();
            
            return double.Parse(d.Compute(finals, null).ToString());
        }
    }
}

运行效果图如下:
在这里插入图片描述
在文件中查看:
在这里插入图片描述

Part.4 项目的单元测试,回归测试:

首先建立项目的单元测试
在这里插入图片描述

测试代码为:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using AchaoCalculator;


namespace AchaoCalculator测试单元
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            string s = "2*3+5-10";
            Assert.AreEqual(Program.MainCal(s),1);

        }
    }
}

测试结果:
在这里插入图片描述

在做单元测试的时候我遇到了这么一个问题,就是刚开始写上面的计算器代码的时候,class是默认属性,应该是private,那么在单元测试这边的项目里写代码的时候,无法使用class。就算是写了using 命名空间,依然无法使用。就要把class的属性改为public,才能在单元测试中调用。

接下来是回归测试

工具在这里:
在这里插入图片描述

性能分析报告如下:
在这里插入图片描述

在这里插入图片描述

Part.5 项目代码提交到GitHub:

我还是选择使用GitHub Desktop的方式来提交。

首先,在文件夹中找到我们克隆下来的仓库的位置,然后将我们的项目文件夹直接复制粘贴到这里:
在这里插入图片描述
然后在GitHub Desktop中会出现我们要提交的代码:
在这里插入图片描述
然后在左下角填一下基本的信息:
在这里插入图片描述
点击“Commit to master”。然后点击push:
在这里插入图片描述
等待一会儿便提交成功。
然后我们到GitHub网站,我们的仓库中去查看:
在这里插入图片描述
证明提交成功。

Part.6 心得:

怎么说呢,这次的作业任务是说要掌握工具的使用,但是对于这个问题的代码的设计其实花费了更多的时间。我的代码其实就还是有很大的问题的...自己看来还是有点不舒服。
之前就有接触过GitHub,但没有具体的使用过。这一次算是真正的学习了这个工具的具体使用方法,挺不错的。
再就是写代码用的是C#,很久不用了,用起来不是很舒服。不过感觉C#也挺骚的,封装了一堆工具,只要能想到用什么,就算不知道,去网上一搜就会有一大堆用法的介绍,很快就上手使用。傻子都会用。

Guess you like

Origin www.cnblogs.com/pastrain/p/11541263.html