首页 > 代码库 > 20160929-四则运算
20160929-四则运算
结对伙伴:杜桥
结对项目:四则运算
版本控制:
HTTPS:https://git.coding.net/li_yuhuan/CalculateTest.git
SSH:git@git.coding.net:li_yuhuan/CalculateTest.git
GIT:git://git.coding.net/li_yuhuan/CalculateTest.git
---------------------------------------------------------------------
在原solution中新建了一个winForm的project,add reference添加之前的控制台版本project,就可以调用写好的方法.
winform界面设计为:
---------------------------------------------------------------------
private void Form1_Load(object sender, EventArgs e) { ReadXml(); CreateExpressionAndResult(); } protected override void OnClosing(CancelEventArgs e) { SaveXml(); base.OnClosing(e); }
启动程序时从xml文件中读取数据,设置用户名,难度,题数等显示;
关闭程序时将数据保存到xml文件.
---------------------------------------------------------------------
private void SaveXml() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "Setting.xml"); xmlDoc.RemoveAll(); XmlElement xe = xmlDoc.CreateElement("saveData"); xe.SetAttribute("userName", m_userName); xe.SetAttribute("difficulty", m_difficulty); xe.SetAttribute("totalCount", m_totalCount.ToString()); xe.SetAttribute("correctCount", m_correctCount.ToString()); xmlDoc.AppendChild(xe); xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "Setting.xml"); } private void ReadXml() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "Setting.xml"); XmlElement xe = (XmlElement)xmlDoc.SelectSingleNode("saveData"); m_userName = xe.GetAttribute("userName"); m_difficulty = xe.GetAttribute("difficulty"); m_totalCount = int.Parse(xe.GetAttribute("totalCount")); m_correctCount = int.Parse(xe.GetAttribute("correctCount")); m_wrongCount = m_totalCount - m_correctCount; }
读取和写入xml文件的方法
---------------------------------------------------------------------
private void CreateExpressionAndResult() { m_expression = CalculateTest.Program.CreateExpression(m_difficulty); label1.Text = m_expression + " = "; m_result = CalculateTest.Program.Calculate(m_expression); }
用旧项目中的方法生成表达式并计算出结果;
---------------------------------------------------------------------
private void button1_Click(object sender, EventArgs e) { string messageText; if (resultInput.Text == m_result.ToString()) { messageText = "回答正确!"; m_correctCount++; } else { messageText = "回答错误!正确答案是 " + m_result.ToString() ; m_wrongCount++; } m_totalCount++; DialogResult dr = MessageBox.Show(messageText, "提示:"); if (dr == DialogResult.OK) { CreateExpressionAndResult(); resultInput.Text = ""; } }
弹出提示显示回答是否正确,并更新总答题数,正确题数,错误题数,清空input并出下一题.
---------------------------------------------------------------------
string m_userName { get { return userNameLabel.Text; } set { userNameLabel.Text = value; } } string m_difficulty { get { return difficultyLabel.Text; } set { difficultyLabel.Text = value; } } int m_totalCount { get { return int.Parse(totalCountLabel.Text); } set { totalCountLabel.Text = value.ToString(); } } int m_correctCount { get { return int.Parse(correctCountLabel.Text); } set { correctCountLabel.Text = value.ToString(); } } int m_wrongCount { get { return int.Parse(wrongCountLabel.Text); } set { wrongCountLabel.Text = value.ToString(); } }
通过对变量赋值来更新界面上显示的内容;
---------------------------------------------------------------------
题目难度设计分为Easy,Normal,Difficult三个等级,均为四个数字进行运算;
Easy:只有加减法且无括号;
Normal:包含加减乘除,无括号;
Difficult:包含加减乘除,有括号;
static public string CreateExpression(string difficulty)
原方法新增参数string difficulty根据难度生成表达式;
if (difficulty == "Easy") { oplist = new List<char>() { ‘+‘, ‘-‘, ‘+‘, ‘-‘ }; } else { oplist = new List<char>() { ‘+‘, ‘-‘, ‘*‘, ‘/‘ }; }
如果是简单,则符号列表里只有加减号,其余则为加减乘除四种;
if (difficulty == "Difficult") { 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, ")"); }
只有在难度为difficult的时候选择位置插入括号
20160929-四则运算