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

四则运算封装

时间:2015-11-28 23:16:34      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:

form1

技术分享

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 四则运算
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int right = 0;
        private int t = 60;
        public static int Count = 0;
        string path = "d:\text.txt";
    
        private void button4_Click(object sender, EventArgs e)
        {
            label2.Text = t.ToString();
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
        }
       

        private void timer1_Tick_1(object sender, EventArgs e)
        {
             if (t <= 0)
            {
                timer1.Enabled = false;
                //textBox7.Enabled = false;
                MessageBox.Show("答题时间到!");
                textBox4.Enabled = false;
            }
            t = t- 1;
            label2.Text = t.ToString();

        }

        

        private void button7_Click(object sender, EventArgs e)
        {
            textBox2.Text = "*";
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox2.Text = "+";
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox2.Text = "-";
           
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox2.Text = "/";
           
        }

       


        private void button2_Click(object sender, EventArgs e)
        {
            textBox4.Enabled = false;
            Form2 frm = new Form2();
            frm.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog TxtSaveDialog = new SaveFileDialog();
            TxtSaveDialog.Filter = "文本文档(*.txt)|*.txt";
            if (File.Exists(path))
            {

                this.richTextBox1.LoadFile(path, RichTextBoxStreamType.PlainText);
                MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.richTextBox1.Clear();

            }
            else
            {
                if (TxtSaveDialog.ShowDialog() == DialogResult.OK)
                {

                    this.richTextBox1.SaveFile(TxtSaveDialog.FileName, RichTextBoxStreamType.PlainText);
                    MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.richTextBox1.Clear();


                }

            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            StreamWriter bc1 = File.AppendText("bc1.txt");
            bc1.WriteLine(textBox1.Text);
            bc1.Close();
            StreamWriter bc2 = File.AppendText("bc2 .txt");
            bc2.WriteLine(textBox2.Text);
            bc2.Close();
            StreamWriter bc3 = File.AppendText("bc3.txt");
            bc3.WriteLine(textBox3.Text);
            bc3.Close();
            
            richTextBox1.Text += textBox1.Text + textBox2.Text + textBox3.Text + label3.Text + textBox4.Text + "\n";
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            int n1 = int.Parse(textBox1.Text.Trim());
            int n2 = int.Parse(textBox3.Text.Trim());
            Char y = Convert.ToChar(textBox2.Text.Trim());
            fengzhaung con = new  fengzhaung();
            con.cl(n1, n2, y);

            if (con.result == int.Parse(textBox4.Text.Trim()))
            {
                MessageBox.Show("回答正确!");
                right++;
                Count++;
            }

            else
            {

                MessageBox.Show("回答错误");
                Count++;

            }
               
    
        }

    }
}

  form2

技术分享

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;

namespace 四则运算
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = Form1.Count.ToString();
            textBox2.Text = Form1.right.ToString();
            textBox3.Text = ((Form1.right /(double)(Form1.Count)) * 100).ToString() + "%";
       
            
        }
    }
}

封装类代码:

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

namespace 四则运算
{
    class fengzhaung
    {
       
       public char ysf;

       public int result = 0;
       public void cl(int number1, int number2, char ysf)
        {          
            if(ysf==‘+‘)
            { 
                 result=number1+number2;
                 
                
            }
            if (ysf == ‘-‘)
            {
               result=number1-number2;
              
              
              
            }
            if (ysf== ‘*‘)
            {
              result=number1*number2;
             
                }
           if (ysf == ‘/‘)
            {
              result=number1/number2;
               

               }
         }
 }
    }

测试:

 

技术分享技术分享

技术分享

 保存:

技术分享

技术分享技术分享

  

 

四则运算封装

标签:

原文地址:http://www.cnblogs.com/64gong/p/5003617.html

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