第二次作业

git地址https://github.com/robert-qin
git用户名robert-qin
学号后5位62432
博客地址https://home.cnblogs.com/u/robertqin/
作业链接

1.配置环境

vs是之前就配置好了
用git的时候有时候clone之后文件依然为空,就不得不下载压缩包

2.编写代码

因为很久没有写C#了,所以比较生疏,然后差了很多资料,才知道怎么写C#。
···
while (result.ToString().Contains('.') || result.ToString().Contains('-'))
{
forml = Formula();
result = dt.Compute(forml, "");
}
···
写这段代码的时候,一直报错,后来才发现原来是没有重新构造公式,导致无法生成新的结果。

using System;
using System.Data;
using System.IO;
using System.Text;


namespace Calculator
{
    class Program
    {
        public string Formula()
        {
            Random ran = new Random();
            char[] operation = { '+', '-', '*', '/' };
            string formula = Convert.ToString(ran.Next(0, 100));
            int count = ran.Next(2, 4);
            for (int i = 0; i < count; i++)
            {
                int symbol = ran.Next(0, 4);
                int number = ran.Next(1, 100);
                formula += operation[symbol] + Convert.ToString(number);
            }
            return formula;
        }
        public string Solve(string forml)
        {
            object result = null;
            DataTable dt = new DataTable();
            result = dt.Compute(forml, "");
            while (result.ToString().Contains('.') || result.ToString().Contains('-'))
            {
                forml = Formula();
                result = dt.Compute(forml, "");
            }
            return forml + "=" + result.ToString();
            
        }
        public static void Write(string res)
        {
            StreamWriter writer = new StreamWriter("D:\\program\final\ti.txt", true, System.Text.Encoding.Default);
            writer.Write(res);
            writer.Close();
        }
        public static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine("how many questions you want:");
            int n = int.Parse(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                string forml = p.Formula();
                string res = p.Solve(forml);
                Write(res);
                Console.WriteLine(res);
                
            }

        }
    }
}

运行结果如下

效能分析

单元测试

git

clone过程

posted on 2019-09-20 19:18  青易q  阅读(164)  评论(1编辑  收藏  举报

导航