码迷,mamicode.com
首页 > 其他好文 > 详细

计算器

时间:2015-12-03 15:34:42      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

设计思路:

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

Form1的设计为:

技术分享

Form1.cs的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace szys.c
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int Count = 0;//总计的个数
        private int t = 60;//限制的时间为60s
        public static int right = 0;//正确的个数
        int n = 0;
//四个button的单击事件,分别赋值lable1为+,-,*,/ private void button1_Click_1(object sender, EventArgs e) { label1.Text = "+"; label1.Visible = true; } private void button2_Click_1(object sender, EventArgs e) { label1.Text = "-"; label1.Visible = true; } private void button3_Click(object sender, EventArgs e) { label1.Text = "*"; label1.Visible = true; } private void button4_Click(object sender, EventArgs e) { label1.Text = "/"; label1.Visible = true; } private void button5_Click_1(object sender, EventArgs e) { label8.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); string[] n1 = new string[100]; n1 = File.ReadAllLines("n1.txt"); textBox1.Text = n1[n]; string[] n2 = new string[100]; n2 = File.ReadAllLines("n2.txt"); label1.Text = n2[n]; string[] n3 = new string[100]; n3 = File.ReadAllLines("n3.txt"); textBox2.Text = n3[n]; n++; } //开始的单击事件,运用算法 private void button6_Click_1(object sender, EventArgs e) { textBox3.Enabled = false; MessageBox.Show("运算结束!"); textBox4.Text = Form1.Count.ToString();//题目总数 textBox5.Text = Form1.right.ToString();//正确的个数 textBox6.Text = ((Form1.right / (double)(Form1.Count)) * 100).ToString() + "%";//正确率 } //结束的单击事件 private void timer1_Tick(object sender, EventArgs e) { if (t <= 0) { timer1.Enabled = false; textBox3.Enabled = false; MessageBox.Show("时间到了!"); } t = t - 1; label8.Text = t.ToString(); } private void button7_Click(object sender, EventArgs e)//录题存放文件夹 { StreamWriter n1 = File.AppendText("n1.txt");//第一个数存入第一文档 n1.WriteLine(textBox1.Text); n1.Close(); StreamWriter n2 = File.AppendText("n2.txt");//第二个数存入第二个文档 n2.WriteLine(label1.Text); n2.Close(); StreamWriter n3 = File.AppendText("n3.txt");//结果存入第三个文档 n3.WriteLine(textBox2.Text); n3.Close(); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); MessageBox.Show("录题成功"); } private void textBox3_KeyDown(object sender, KeyEventArgs e)//对类的调用 { int a = int.Parse(textBox1.Text); int b = int.Parse(textBox2.Text); Char c = Convert.ToChar(label1.Text); Class1 con = new Class1(); con.js(a, b, c); if (e.KeyCode == Keys.Enter)//做完每一题按enter键进入下一题 { if (con.y1 == int.Parse(textBox3.Text)) { MessageBox.Show("回答正确!下一题请按开始按钮!"); right++; Count++; } else { MessageBox.Show("回答错误!下一题请按开始按钮!"); Count++; } //清空 textBox3.Clear(); textBox1.Clear(); textBox2.Clear(); timer1.Enabled = false; } } } }

Class.cs的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace szys.c
{
    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;

        }
    }
}

测试的过程图:

录题开始:

技术分享

 

 

答完题按Enter键后:

技术分享

 

单击结束按钮弹出对话框并且总计,正确的个数以及正确率显示出来:

 

 

 

 

 

 

 

 

 

技术分享

 

 

 

 

 

技术分享

 

 

时间到的提示:

 

 

技术分享

对知识的掌握不够好,做题速度太慢了点,不过会努力的!

 

计算器

标签:

原文地址:http://www.cnblogs.com/xueyanan/p/5016366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!