static void Main(string[] args) { int r = 10; int n = 1; int[] result = new int[2]; Console.WriteLine("请输入生成题目的数量"); string nn = Console.ReadLine(); while (int.TryParse(nn, out n) == false) { Console.WriteLine("输入有误,请重新输入数字"); nn = Console.ReadLine(); } Console.WriteLine("请输入题目中数值的范围"); nn = Console.ReadLine(); while (int.TryParse(nn, out r) == false) { Console.WriteLine("输入有误,请重新输入数字"); nn = Console.ReadLine(); } calculation c = new calculation(); int[][] ic = new int[n][];//n个计算式中计算数的和数 for (int i = 0; i < n; i++) { string a = c.GetProblem(r, ref result, ref ic[i]); for (int j = 0; j < i; j++) { if (ic[j] == ic[i]) { a = "重新生成"; } } while (a == "重新生成") { a = c.GetProblem(r, ref result, ref ic[i]); for (int j = 0; j < i; j++) { if (ic[j] == ic[i]) { a = "重新生成"; } } } //将题目存入Exercises.txt中,可追加 using (StreamWriter sw = new StreamWriter(@"D:\CACULATION\Exercises.txt", true)) { sw.WriteLine(i + ".四则运算题目 " + a); } //将答案存入Answers.txt using (StreamWriter sw = new StreamWriter(@"D:\CACULATION\Answer.txt", true)) { if (result[0] == 0 || result[1] == 1) { sw.WriteLine(i + ".答案 " + result[0].ToString()); } else { sw.WriteLine(i + ".答案 " + result[0].ToString() + "/" + result[1].ToString()); } } } Console.ReadKey(); }
public int[] Arithmetic(int[] b1, int[] b2, char s, ref string problem) { int[] result = new int[2]; switch (s) { case '+': s1 = b1[0] * b2[1] + b2[0] * b1[1]; s2 = b1[1] * b2[1]; calculation c1 = new calculation(); s3 = c1.GetCommon(s1, s2); result[0] = s1 / s3; result[1] = s2 / s3; break; case '-': s1 = b1[0] * b2[1] - b2[0] * b1[1]; s2 = b1[1] * b2[1]; calculation c2 = new calculation(); s3 = c2.GetCommon(s1, s2); result[0] = s1 / s3; result[1] = s2 / s3; //运算过程中出现负数,则主函数进行判断,舍去后重新生成计算式 if (s1 < 0) { problem = "重新生成"; } break; case '*': s1 = b1[0] * b2[0]; s2 = b1[1] * b2[1]; calculation c3 = new calculation(); s3 = c3.GetCommon(s1, s2); result[0] = s1 / s3; result[1] = s2 / s3; break; case '/': s1 = b1[0] * b2[1]; s2 = b1[1] * b2[0]; calculation c4 = new calculation(); s3 = c4.GetCommon(s1, s2); result[0] = s1 / s3; result[1] = s2 / s3; break; } return(result); }