第二次作业
GIT地址:https://github.com/
GIT用户名:littlecat
学号后五位:62530
博客地址:https://www.cnblogs.com/hy666666/
作业链接:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2/homework/2795
PART2 克隆
克隆项目到本地,新建一个文件夹并以自己GitHub用户名命名
GIT安装,下载的github for windows,
新建项目文件夹
在vs2017上创建控制台应用程序,添加项目目录,打开VS2017并新建项目,将位置改为上一步创建好的文件夹所在地址
用的github for windows,所以就没用git命令行那里的步骤。
PART3 单元测试
源代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp5 { class Program { Random random = new Random(); Stack<string> stack_number = new Stack<string>(); Stack<string> stack_operator = new Stack<string>(); List<string> formulas = new List<string>(); string str = ""; string substr = ""; string nstr = ""; int m = 0; static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); Program program = new Program(); program.FormulaGeneration(n); foreach(var str in program.formulas) { Console.WriteLine(str); } } private bool judgenumber(string text)//判断是否为数字 { try { int var1 = Convert.ToInt32(text); return true; } catch { return false; } } private bool judgeoperator(string text)//判断是否为运算符 { if (text == "(" || text == ")" || text == "+" || text == "-" || text == "*" || text == "/") { return true; } else return false; } public object addition(object a, object b)//加法 { Decimal d1 = Decimal.Parse(a.ToString()); Decimal d2 = Decimal.Parse(b.ToString()); return d2 + d1; } public object subduction(object a, object b)//减法 { Decimal d1 = Decimal.Parse(a.ToString()); Decimal d2 = Decimal.Parse(b.ToString()); return d2 - d1; } public object multiplication(object a, object b)//乘法 { Decimal d1 = Decimal.Parse(a.ToString()); Decimal d2 = Decimal.Parse(b.ToString()); return d2 * d1; } public object division(object a, object b)//除法 { Decimal d1 = Decimal.Parse(a.ToString()); Decimal d2 = Decimal.Parse(b.ToString()); if (d2 % d1 != 0 || d2 < d1) { m++; return -10000; } else { return d2 / d1; } } int judgelevel(string text)//判断优先级 { if (text.Equals("(")) { return 1; } else if (text.Equals(")") || text.Equals("(") || text.Equals(")")) { return 1; } else if (text.Equals("+") || text.Equals("-")) { return 2; } else if (text.Equals("*") || text.Equals("/")) { return 3; } else return 10; } int operator_dected(string types, string a, string b)//根据运算符的类型返回对应的值 { if (types == "+") { return Convert.ToInt32(addition(a, b)); } else if (types == "-") { return Convert.ToInt32(subduction(a, b)); } else if (types == "*") { return Convert.ToInt32(multiplication(a, b)); } else if (types == "/") { return Convert.ToInt32(division(a, b)); } else return 999; } double operate(string Str) { stack_number.Clear();//清空栈 stack_operator.Clear(); str = Str + "!";//!为结束运算符 int temp_count = 0; try { for (int i = 0; i < str.Length; i++) { substr = str.Substring(i, 1); if (judgenumber(substr))//如果是数字 { if (temp_count == 0) { stack_number.Push(substr); } else temp_count--; if (judgenumber(str.Substring(i + 1, 1))) { string link1 = stack_number.Pop(); link1 += str.Substring(i + 1, 1); stack_number.Push(link1); temp_count++; } } else if (judgeoperator(substr)) { if (stack_operator.Count >= 1) { int new1 = judgelevel(substr); int old1 = judgelevel(stack_operator.Peek()); if (old1 < new1 || substr == "(")//判断优先级 { stack_operator.Push(substr); //将运算符插入栈中 } else { if (substr == ")") { for (; stack_operator.Count > 0; stack_operator.Pop()) { if (stack_operator.Contains("(") && stack_operator.Peek() == "(") { stack_operator.Pop(); break; } else { int temp1 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); int temp2 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); stack_number.Push(operator_dected(stack_operator.Peek(), temp1.ToString(), temp2.ToString()).ToString()); } } } else { for (; stack_operator.Count > 0 && stack_number.Count >= 2 && stack_operator.Peek() != "("; stack_operator.Pop()) { string temp_a = substr; int new2 = judgelevel(temp_a); int old2 = judgelevel(stack_operator.Peek()); if (old2 < new2 || substr == "(")//判断优先级 { break; } else { int temp3 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); int temp4 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); stack_number.Push(operator_dected(stack_operator.Peek(), temp3.ToString(), temp4.ToString()).ToString()); } } stack_operator.Push(substr); } } } else { stack_operator.Push(substr); } } else if (substr == "!") { for (; stack_operator.Count > 0 && stack_number.Count >= 2 && stack_operator.Peek() != "("; stack_operator.Pop()) { int temp3 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); int temp4 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); stack_number.Push(operator_dected(stack_operator.Peek(), temp3.ToString(), temp4.ToString()).ToString()); } return Convert.ToDouble(stack_number.Peek()); } else { break; } } } catch { } return 0; } void FormulaGeneration(int n) { int i = 0; int num = 0; int a, b, c, d; int x, y, z; string formula = ""; for (; i < n; i++) { do { m = 0; a = random.Next(0, 101); b = random.Next(0, 101); c = random.Next(0, 101); x = random.Next(0, 4); y = random.Next(0, 4); num = random.Next(2, 4); if (num == 2) { nstr = a + "" + Operator(x) + "" + b + "" + Operator(y) + "" + c + "!"; formula = a + "" + Operator(x) + "" + b + "" + Operator(y) + "" + c + "=" + operate(nstr); } else if (num == 3) { d = random.Next(0, 101); z = random.Next(0, 4); nstr = a + "" + Operator(x) + "" + b + "" + Operator(y) + "" + c + "" + Operator(z) + "" + d + "!"; formula = a + "" + Operator(x) + "" + b + "" + Operator(y) + "" + c + "" + Operator(z) + "" + d + "=" + operate(nstr); } else { Console.WriteLine("运算符数量错误!"); } } while (!(operate(nstr) % 1 == 0 && operate(nstr) > 0 && m == 0)); formulas.Add(formula); } } string Operator(int n) { switch (n) { case 0: return "+"; case 1: return "-"; case 2: return "*"; case 3: return "/"; } return ""; } double op(int num1, int num2) { if (num1 < num2 || num1 % num2 != 0) { m++; return 0; } else { return num1 / num2; } } } }
设置断点
运行效果
提交代码:将源代码提交到github上。
感想:
过程很艰难,C#学了有很久了,有一些知识掌握得不是很牢固,然后重新看了一下书,补了一下知识。第一次接触github,对它的使用很生疏,但总算开了头。
程序写得有些乱,虽然编译和测试都通过了。嗯...应该还是存在很多问题。
这是第一次对代码进行单元测试,是根据步骤一步一步来的,虽然通过了,但是感觉...没有很大的收获。
完成这次作业有很多的艰难险阻...算是勉强完成吧...