zoukankan      html  css  js  c++  java
  • 第二次作业

    Git 地址https://github.com/Eleanoren

    Git 用户名:Eleanoren

    学号后五位:62403

    博客地址:https://www.cnblogs.com/Sabrian-Liu/

    作业链接:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2/homework/2795

    Part 0. 环境配置

    这里我们用 C# 完成本次作业,编辑器使用vs2017。关于vs 2017怎么配置C#,相信大家都懂。在这里我就不说太多了,不懂的可以参考:https://blog.csdn.net/Camel_Wang/article/details/78696606

    废话不多说,Let's do it!

     

    如果安装vs2017时没有安装相应的模块,可以看这里:https://www.cnblogs.com/love007/p/7879534.html

    Part 1. fork 相关项目

    为本课程新创建一个 GitHub 账号,专门用作学校课堂学习

    将其 git 到本地

    Part 2. 项目设计

    基本思路就是使用随机数。一开始在对字符串长出了点小问题,想疯狂使用 if else 来处理字符串。后来发现C#有一种类似于 python 中 eval() 的操作,省了不少力气。简单来说,就是使用 datatable 直接计算字符串表达式的值。(关于 datatable 的用法,可以参考这里:https://www.cnblogs.com/qianqian528/p/8456351.html

    我们知道,要实现随机数,可以

    Random ran = new Random();
    int m = ran.Next(0, 100);//随机数的范围在0-100之间

    还有就是对+ - * /进行随机的问题,后来发现可以这么做,这样就实现了字符串的随机。

    public string GetRandom(string[] arr)
    {
        Random ran = new Random();
        int n = ran.Next(arr.Length - 1);
        return arr[n];
    }
    string[] arr = { "+", "-", "*", "/", };
    GetRandom(arr);

    接下来就很简单了。我定义了一个类,专门用作随机产生字符串

    public class Number
    {
          public Number() { }
          public string number()
          {
               string[] arr = { "+", "-", "*", "/", };
               //string num ;
               Random ran = new Random();
               int m = ran.Next(2, 3);
               int i = 0;
               string num;
               while(i<m)
               {
                    int n = ran.Next(1, 100);
                    num = n.ToString() + GetRandom(arr);
                }
                num = num + ran.Next(1, 100);
                return num;
    
            }
            public string GetRandom(string[] arr)
            {
                 Random ran = new Random();
                  int n = ran.Next(arr.Length - 1);
                 return arr[n];
            }
    }

    下面是源代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    
    namespace ConsoleApp2
    {
        class Program
        {
            public class Number
            {
                public Number() { }
                public string number()
                {
                    string[] arr = { "+", "-", "*", "/", };
                    //string num ;
                    Random ran = new Random();
                    int m = ran.Next(2, 3);
                    int i = 0;
                    string num;
                    while(i<m)
                    {
                        int n = ran.Next(1, 100);
                        num = n.ToString() + GetRandom(arr);
                    }
                    num = num + ran.Next(1, 100);
                    return num;
    
                }
                public string GetRandom(string[] arr)
                {
                    Random ran = new Random();
                    int n = ran.Next(arr.Length - 1);
                    return arr[n];
                }
            }
            public static string Compute(string expression)
            {
                DataTable dt = new DataTable();
                string result = dt.Compute(expression, null).ToString();
                return result;
            }
            static void Main(string[] args)
            {
                Console.WriteLine("Please input the number of arithmetic formulas:");
                int n = int.Parse(Console.ReadLine());
                for (int i=0;i<n;i++)
                {
                    Number num = new Number();
                    string Num = num.number();
                    string result = Compute(Num);
                    Console.WriteLine(Num + "=" + result);
                    Console.WriteLine(Num);
    }
    Console.ReadKey(); }    } }

    效果如图

    Part 3. 单元测试

    对写好的项目单元测试:

    新建好单元测试项目,就可以开始测试啦

    我参考的是这篇博文:https://www.cnblogs.com/dreamq/p/5299080.html

    测试通过:

    断点测试

    调试时,下面会出现一些变量的情况

    Part 3. 将代码 push 到 GitHub 上

    可以参考:https://www.jianshu.com/p/c70ca3a02087

     

    Part 4. 总结

    对 C# 掌握不牢,导致反反复复查看 C# 语法,浪费了很多时间。代码还有很多地方可以优化,希望下次可以做的更好。用 Python 我应该可以写的更好(大雾)

  • 相关阅读:
    Add Two Numbers
    Reverse Linked List II
    Reverse Linked List
    Remove Duplicates from Sorted List
    Remove Duplicates from Sorted List II
    Partition List
    Intersection of Two Linked Lists
    4Sum
    3Sum
    2Sum
  • 原文地址:https://www.cnblogs.com/Sabrian-Liu/p/10621831.html
Copyright © 2011-2022 走看看