标签:
private void Form1_Load(object sender, EventArgs e)
{
ReadXml();
CreateExpressionAndResult();
}
protected override void OnClosing(CancelEventArgs e)
{
SaveXml();
base.OnClosing(e);
}
在程序启动的时候从sml文件中读取数据,设置一下难度,用户名等信息。
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();
}
}
通过变量的赋值来更新界面上所显示的内容。
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, ")");
}
难度是困难时,插入了括号,枚举括号的位置。
结对伙伴:李俞寰
标签:
原文地址:http://www.cnblogs.com/duq11/p/5954789.html