第二次作业~
作业前的要求:
GIT地址 | https://github.com/SJMrJoker/Calculator |
GIT用户名 | SJMrJoker |
学号后五位 | 62531 |
博客地址 | https://www.cnblogs.com/sjmrjoker/ |
作业链接 | https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2/homework/2795 |
作业正文:
Part 1. 配置环境
VS是去年就装好了的,官网安装,也没有出现什么问题。
Part 2. 克隆项目
在安装GIT的时候,我是直接下载的GitHubDesktop,步骤有点不一样。。。不过结果是一样的:
四则运算代码部分:
代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace AchaoCalculator 8 { 9 class Program 10 { 11 string path = "E:/My files/SJMrJoker/Calculator/SJMrJoker/subject.txt"; 12 List<string> formulas = new List<string>(); 13 Random random = new Random(); 14 Stack<string> stack_operator = new Stack<string>(); 15 Stack<string> stack_number = new Stack<string>(); 16 string str = ""; 17 string substr = ""; 18 bool flag = true; 19 20 static void Main(string[] args) 21 { 22 Program program = new Program(); 23 int n = 0; 24 n = Convert.ToInt32(Console.ReadLine()); 25 program.FormulaGeneration(n); 26 foreach (var str in program.formulas) 27 { 28 Console.WriteLine(str); 29 } 30 System.IO.File.WriteAllLines(program.path, program.formulas); 31 Console.ReadKey(); 32 } 33 34 #region 35 private bool judgenumber(string text) 36 { 37 try 38 { 39 int var1 = Convert.ToInt32(text); 40 return true; 41 } 42 catch 43 { 44 return false; 45 } 46 } 47 private bool judgeoperator(string text) 48 { 49 if (text == "(" || text == ")" || text == "+" || text == "-" || text == "*" || text == "/") 50 { 51 return true; 52 } 53 else 54 return false; 55 } 56 public object addition(object a, object b) 57 { 58 Decimal d1 = Decimal.Parse(a.ToString()); 59 Decimal d2 = Decimal.Parse(b.ToString()); 60 return d2 + d1; 61 } 62 public object subduction(object a, object b) 63 { 64 Decimal d1 = Decimal.Parse(a.ToString()); 65 Decimal d2 = Decimal.Parse(b.ToString()); 66 return d2 - d1; 67 } 68 public object multiplication(object a, object b) 69 { 70 Decimal d1 = Decimal.Parse(a.ToString()); 71 Decimal d2 = Decimal.Parse(b.ToString()); 72 return d2* d1; 73 } 74 public object division(object a, object b) 75 { 76 Decimal d1 = Decimal.Parse(a.ToString()); 77 Decimal d2 = Decimal.Parse(b.ToString()); 78 if(d2%d1!=0||d2<d1) 79 { 80 flag = false; 81 return -10000; 82 } 83 else 84 { 85 flag = true; 86 return d2 / d1; 87 } 88 89 } 90 int judgelevel(string text) 91 { 92 if (text.Equals("(")) 93 { 94 return 1; 95 } 96 else if (text.Equals(")") || text.Equals("(") || text.Equals(")")) 97 { 98 return 1; 99 } 100 else if (text.Equals("+") || text.Equals("-")) 101 { 102 return 2; 103 } 104 else if (text.Equals("*") || text.Equals("/")) 105 { 106 return 3; 107 } 108 else 109 return 10; 110 111 112 } 113 int operator_dected(string types, string a, string b) 114 { 115 if (types == "+") 116 { 117 return Convert.ToInt32(addition(a, b)); 118 } 119 else if (types == "-") 120 { 121 return Convert.ToInt32(subduction(a, b)); 122 } 123 else if (types == "*") 124 { 125 return Convert.ToInt32(multiplication(a, b)); 126 } 127 else if (types == "/") 128 { 129 return Convert.ToInt32(division(a, b)); 130 } 131 else 132 return 999; 133 } 134 double operate(string Str) 135 { 136 stack_number.Clear(); 137 stack_operator.Clear(); 138 str = Str + "!"; 139 int temp_count = 0; 140 try 141 { 142 for (int i = 0; i < str.Length; i++) 143 { 144 substr = str.Substring(i, 1); 145 if (judgenumber(substr)) 146 { 147 if (temp_count == 0) 148 { 149 stack_number.Push(substr); 150 } 151 else 152 temp_count--; 153 if (judgenumber(str.Substring(i + 1, 1))) 154 { 155 string link1 = stack_number.Pop(); 156 link1 += str.Substring(i + 1, 1); 157 stack_number.Push(link1); 158 temp_count++; 159 } 160 } 161 else if (judgeoperator(substr)) 162 { 163 if (stack_operator.Count >= 1) 164 { 165 int new1 = judgelevel(substr); 166 int old1 = judgelevel(stack_operator.Peek()); 167 if (old1 < new1 || substr == "(") 168 { 169 stack_operator.Push(substr); 170 } 171 else 172 { 173 if (substr == ")") 174 { 175 for (; stack_operator.Count > 0; stack_operator.Pop()) 176 { 177 if (stack_operator.Contains("(") && stack_operator.Peek() == "(") 178 { 179 stack_operator.Pop(); 180 break; 181 } 182 else 183 { 184 int temp1 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); 185 int temp2 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); 186 stack_number.Push(operator_dected(stack_operator.Peek(), temp1.ToString(), temp2.ToString()).ToString()); 187 } 188 } 189 } 190 else 191 { 192 for (; stack_operator.Count > 0 && stack_number.Count >= 2 && stack_operator.Peek() != "("; stack_operator.Pop()) 193 { 194 string temp_a = substr; 195 int new2 = judgelevel(temp_a); 196 int old2 = judgelevel(stack_operator.Peek()); 197 if (old2 < new2 || substr == "(") 198 { 199 break; 200 } 201 else 202 { 203 int temp3 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); 204 int temp4 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); 205 stack_number.Push(operator_dected(stack_operator.Peek(), temp3.ToString(), temp4.ToString()).ToString()); 206 } 207 } 208 stack_operator.Push(substr); 209 } 210 } 211 } 212 else 213 { 214 stack_operator.Push(substr); 215 216 217 } 218 } 219 else if (substr == "!") 220 { 221 for (; stack_operator.Count > 0 && stack_number.Count >= 2 && stack_operator.Peek() != "("; stack_operator.Pop()) 222 { 223 int temp3 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); 224 int temp4 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop(); 225 stack_number.Push(operator_dected(stack_operator.Peek(), temp3.ToString(), temp4.ToString()).ToString()); 226 } 227 return Convert.ToDouble(stack_number.Peek()); 228 } 229 else 230 { 231 232 break; 233 } 234 } 235 } 236 catch 237 { 238 239 240 } 241 return 0; 242 } 243 #endregion 244 245 void FormulaGeneration(int num) 246 { 247 string formula = ""; 248 int n = 0; 249 string str = ""; 250 double ans = 0; 251 for (int i = 0; i < num; i++) 252 { 253 n = random.Next(2, 4); 254 255 switch (n) 256 { 257 case 2: 258 do 259 { 260 str = FormulaGeneration_01(); 261 formula = str + "!"; 262 ans = operate(formula); 263 if (ans % 1 == 0 && ans > 0 && flag == true) 264 { 265 break; 266 } 267 } while (true); 268 formula = str + "=" + operate(formula); ; 269 formulas.Add(formula); 270 break; 271 case 3: 272 273 do 274 { 275 str = FormulaGeneration_03(); 276 formula = str + "!"; 277 ans = operate(formula); 278 if (ans % 1 == 0 && ans > 0 && flag == true) 279 { 280 break; 281 } 282 } while (true); 283 formula = str + "=" + operate(formula); ; 284 formulas.Add(formula); 285 break; 286 } 287 } 288 } 289 290 string FormulaGeneration_01() 291 { 292 string formula = ""; 293 int a = 0; 294 int b = 0; 295 int c = 0; 296 int x = 0; 297 int y = 0; 298 a = random.Next(0, 101); 299 b = random.Next(0, 101); 300 c = random.Next(0, 101); 301 x = random.Next(0, 4); 302 y = random.Next(0, 4); 303 switch (x) 304 { 305 case 0: 306 formula = a + "+" + b; 307 formula = FormulaGeneration_0101(formula, y, c); 308 break; 309 case 1: 310 formula = a + "-" + b; 311 formula = FormulaGeneration_0101(formula, y, c); 312 break; 313 case 2: 314 formula = a + "*" + b; 315 formula = FormulaGeneration_0101(formula, y, c); 316 break; 317 case 3: 318 formula = a + "/" + b; 319 formula = FormulaGeneration_0101(formula, y, c); 320 break; 321 } 322 return formula; 323 } 324 325 326 string FormulaGeneration_0101(string form, int y, int c) 327 { 328 switch (y) 329 { 330 case 0: 331 form = form + "+" + c; 332 break; 333 case 1: 334 form = form + "-" + c; 335 break; 336 case 2: 337 form = form + "*" + c; 338 break; 339 case 3: 340 form = form + "/" + c; 341 break; 342 } 343 return form; 344 } 345 346 string FormulaGeneration_03() 347 { 348 string formula = ""; 349 int a = 0; 350 int x = 0; 351 a = random.Next(0, 101); 352 x = random.Next(0, 4); 353 switch (x) 354 { 355 case 0: 356 formula = a + "+" + FormulaGeneration_01(); 357 break; 358 case 1: 359 formula = a + "-" + FormulaGeneration_01(); 360 break; 361 case 2: 362 formula = a + "*" + FormulaGeneration_01(); 363 break; 364 case 3: 365 formula = a + "/" + FormulaGeneration_01(); 366 break; 367 } 368 return formula; 369 } 370 } 371 }
运行截图:
单元测试:(单元测试有些没有弄懂,可能做出来有些不对)
效能测试:
用Githubdesktop提交代码:
向阿超的仓库请求:
本次作业感受:
1、学会了VS新的功能单元测试、效能测试,及其作用。
2、学习了一些GitHub的一些基本功能,会简单的运用提交。(还不是很熟练,需要多加练习)
3、在进行单元测试的时候出现了一些问题,因为要求给出的是用C#,而示例用的是C++,后来在CSDN及同学的帮助下,成功进行了测试。
4、四则运算代码存在可优化的空间。。