验证哥德巴赫猜想。

我已经记不清作业要求了:

1. 随机两个数,验证是否为质数并且和为用户输入;

2. 能够暂停,继续;

3. 线程;

延迟了一天才交作业,原因是我忘了= =C#快被我打入冷宫了~

这次作业理论上很简单,但是我貌似没有按照要求来做(要用到某知识点,但是我忘了)。

上图:

主要代码:

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.Threading;

namespace GoldbachGuess
{
    public partial class Form1 : Form
    {
        Thread iThread;
        int a, b, c, count;
        delegate void iInvoke();
        delegate void jInvoke(object sender, EventArgs e);
        bool isStart, isFlag;
        Random iRandom;
        object iObject = new object();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            iRandom = new Random((int)DateTime.Today.ToBinary());
            isStart = isFlag = false;
            button1.Enabled = true;
            button2.Enabled = false;
            count = 0;
        }

        void SearchNumber()
        {
            bool isRight = false;
            while (!isRight)
            {
                a = iRandom.Next() % c;
                b = iRandom.Next() % c;
                ++count;
                this.Invoke(new iInvoke(Display));
                if (a + b == c && isPrime(a) && isPrime(b))
                {
                    isRight = true;
                }
                Thread.Sleep(1);
            }
            this.Invoke(new iInvoke(Found));
        }

        bool isPrime(int n)
        {
            if (n <= 1) return false;
            for (int i = 2; i * i <= n; ++i)
            {
                if (n % i == 0) return false;
            }
            return true;
        }

        void Found()
        {
            iThread.Abort();
            isStart = isFlag = false;
            button1.Text = "开   始";
            button2.Enabled = false;
            int tcount = count;
            count = 0;
            label1.Text = "已经随机:0 次";
            textBox1.Text = "";
            textBox1.Enabled = true;
            MessageBox.Show(a.ToString() + " + " + b.ToString() + " = " + c.ToString() + "\n共随机:" + tcount.ToString() + " 次", "找到答案", MessageBoxButtons.OK);
            textBox1.Focus();
        }

        void Display()
        {
            label1.Text = "已经随机:" + count.ToString() + " 次";
            return;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Enabled = false;
            if (!isStart)
            {
                string stringc = textBox1.Text.ToString();
                c = 0;
                for (int i = 0; i < stringc.Length; ++i)
                    if (stringc[i] >= '0' && stringc[i] <= '9')
                    {
                        c *= 10;
                        c += stringc[i] - '0';
                    }
                if (c <= 2 || c % 2 == 1)
                {
                    MessageBox.Show("请输入一个大于等于2的偶数", "错误", MessageBoxButtons.OK);
                    textBox1.Enabled = true;
                    textBox1.Focus();
                    return;
                }
                button1.Text = "暂   停";
                isStart = true;
                button2.Enabled = true;
                iThread = new Thread(SearchNumber);
                iThread.IsBackground = true;
                iThread.Start();
            }
            else
            {
                if (!isFlag)
                {
                    isFlag = true;
                    button1.Text = "继   续";
                    iThread.Abort();
                }
                else
                {
                    isFlag = false;
                    button1.Text = "暂   停";
                    isStart = false;
                    button1_Click(sender, e);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            iThread.Abort();
            isStart = isFlag = false;
            button1.Text = "开   始";
            button2.Enabled = false;
            count = 0;
            label1.Text = "已经随机:0 次";
            textBox1.Text = "";
            textBox1.Enabled = true;
            textBox1.Focus();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isStart) iThread.Abort();
        }
    }
}

转载保留版权:http://haipz.com/blog/i/2990 - 海胖博客