首页 > 代码库 > 第二周-四则运算更新
第二周-四则运算更新
四则运算功能更新:
HTTPS:https://git.coding.net/li_yuhuan/CalculateTest.git
SSH:git@git.coding.net:li_yuhuan/CalculateTest.git
代码:
static void Main(string[] args) { int length = args.Length; if (0 == length) { for (int i = 0; i < 20; i++) { ShowTest(false); } Console.WriteLine("你一共答对" + m_correctNum + "道题,共20道题"); } else if (2 == length) { if ("-c" == args[0]) { string str = args[1]; if (IsPositiveInt(str)) { int count = int.Parse(str); for (int i = 0; i < count; i++) { ShowTest(true); } } else { Console.WriteLine("题目数量必须是 正整数!"); } } }
判断通过控制台传入主函数的参数个数分情况处理;
1)当参数个数为0时,循环出20道题并判断输入的答案是否正确,最后给出正确的个数;
2)参数为2个,格式为-c + 正整数时,则打印与正整数相等个数的题目(带答案);当-c后的输入不是正整数,则显示提示;
----------------------------------------------------------------------------------------------------------------------
static private void ShowTest(bool showResult) { string expression = CreateExpression(); //生成试题 Queue<string> qexp = SplitExpress(expression); //把表达式存入队列 List<string> exp = InorderToPostorder(qexp); //表达式由中缀转为后缀 double result = 0; IsResult(exp, out result);//根据后缀表达式计算结果 if (showResult) {//打印试题及结果 Console.WriteLine(expression + "= " + result); } else {//出题,并读入用户输入的值,判断是否正确 Console.WriteLine(expression); Console.Write("?"); string input = Console.ReadLine(); if (IsNumeric(input)) { double num = double.Parse(input); if (num == result) { Console.WriteLine("答对啦,你真是个天才!"); m_correctNum++; } else { Console.WriteLine("再想想吧,答案似乎是" + result + "喔!"); } } } }
根据传入的bool参数,进行出题或打印试题及答案;
-----------------------------------------------------------------------------------------------------------------
static private string CreateExpression() { string expression = string.Empty; int num; int index; char op; List<int> position = new List<int>() {0, 2, 4 }; List<char> oplist = new List<char>() { ‘+‘,‘-‘, ‘*‘, ‘/‘}; List<string> divlist = new List<string>() { "1","2","4","5","8"}; List<string> expList = new List<string>(); num = ran.Next(10); expList.Add(num.ToString()); for (int i = 0; i < 3; i++) { index = ran.Next(oplist.Count()); op = oplist[index]; if (‘/‘ == op) { oplist.RemoveAt(3); index = ran.Next(divlist.Count()); num = int.Parse(divlist[index]); } else { num = ran.Next(10); } expList.Add(op.ToString()); expList.Add(num.ToString()); } int pos; index = expList.IndexOf("/"); if (-1 == index) { pos = position[ran.Next(3)]; } else { if (1 == index) { position.RemoveAt(1); } else if (3 == index) { position.RemoveAt(2); } pos = position[ran.Next(position.Count())]; } expList.Insert(pos, "("); expList.Insert(pos + 4, ")"); foreach (string str in expList) { expression += str; } return expression; }
生成算式:
生成四个随机数,和三个随机符号,如果符号是除号,则下一个随机数从(1,2,4,5,8)中随机,因为我怕会出现无限循环的小数,并且限制算式中最多只有一个除号.生成算式成功后,根据除号的位置来设定括号的位置.(除号后如果有括号且是加减法,可能会除不尽).最后返回算式;
这段算法有待改进,规避了很多情况;具体的完整代码上传到了coding.net
-----------------------------------------------------------------------------------------------------------------
运行样例:
功能1&2:
---------------------------------------------------------------------------------------------------------------------
功能3:
第二周-四则运算更新