WPF计算

时间:2022-06-02 00:37:38

设计思路:

用WPF窗体设计,在第一个数和第二个数的文本框中输入数值,单击录题按钮,数值保存在n1,n2文档中,把要做的题都保存完后,单击开始按钮,开始做题,每做完一道题,按Enter键,进入下一题,同时提示回答是否正确。如果在时间内做完题就单击结束按钮,弹出对话框“答题结束” 总计,正确的个数及正确率显示出来。

MainWindow.xaml设计为:

WPF计算

代码为:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO; namespace xiaoshitou
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static int Count = ;//总计的个数
public static int right = ;//正确的个数
int n = ;
private void button1_Click(object sender, RoutedEventArgs e)
{
label4.Content = "+"; } private void button2_Click(object sender, RoutedEventArgs e)
{
label4.Content = "-"; } private void button3_Click(object sender, RoutedEventArgs e)
{
label4.Content = "-";
;
} private void button4_Click(object sender, RoutedEventArgs e)
{
label4.Content = "-"; } private void button5_Click(object sender, RoutedEventArgs e)
{ StreamWriter n1 = File.AppendText("n1.txt");//第一个数存入第一文档
n1.WriteLine(textBox1.Text);
n1.Close();
StreamWriter n2 = File.AppendText("n2.txt");//第二个数存入第二个文档
n2.WriteLine(label4.Content);
n2.Close();
StreamWriter n3 = File.AppendText("n3.txt");//结果存入第三个文档
n3.WriteLine(textBox2.Text);
n3.Close();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
MessageBox.Show("录题成功");
} private void button6_Click(object sender, RoutedEventArgs e)
{ string[] n1 = new string[];
n1 = File.ReadAllLines("n1.txt");//数值一的文档
textBox1.Text = n1[n];
string[] n2 = new string[];
n2 = File.ReadAllLines("n2.txt");
label4.Content = n2[n];
string[] n3 = new string[];
n3 = File.ReadAllLines("n3.txt");
textBox2.Text = n3[n];
n++;
} private void button7_Click(object sender, RoutedEventArgs e)
{ textBox3.IsEnabled = false;
MessageBox.Show("运算结束!");
textBox4.Text = Count.ToString();//题目总数
textBox5.Text =right.ToString();//正确的个数
textBox6.Text = ((right / (double)(Count)) * ).ToString() + "%";//正确率
} private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
Char c = Convert.ToChar(label4.Content);
Class1 con = new Class1();
con.js(a, b, c);
if (e.Key == Key.Enter)
{
if (con.y1 == int.Parse(textBox3.Text))
{
MessageBox.Show("回答正确!下一题请按开始按钮!");
right++;
Count++;
} else
{ MessageBox.Show("回答错误!下一题请按开始按钮!");
Count++; }
//清空
textBox3.Clear();
textBox1.Clear();
textBox2.Clear(); }
}
}
}

Class.cs代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace xiaoshitou
{
class Class1
{
public int result;
public int y1
{
get
{
return result;
}
} public int js(int n1, int n2, char mark)
{
if (mark == '+')
{
return result = n1 + n2;
}
else if (mark == '-')
{
return result = n1 - n2;
}
else if (mark == '*')
{
return result = n1 * n2;
}
else if (mark == '/')
{
return result = n1 / n2;
}
return result;
}
}
}

运行过程的各部分显示为以下图片:

WPF计算

WPF计算

WPF计算

WPF计算

WPF计算