C#查找素?cái)?shù)實(shí)現(xiàn)方法
本文所述為C#查找素?cái)?shù)的程序代碼,包括了可視化窗體的代碼,找素?cái)?shù)的方法可以借鑒。雖然實(shí)現(xiàn)的功能簡(jiǎn)單,不過(guò)為了演示一些C#技巧,本文實(shí)例中還用到了線程技術(shù)、ListBox列表框的使用、設(shè)置程序掛起等操作,其中備有詳盡的注釋,幫助大家更好的理解。
具體實(shí)現(xiàn)代碼如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace SuspendAndResume
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
//公共委托,用于輸出素?cái)?shù)
public delegate void UD(string returnVal);
//聲明私有線程
private Thread pNT;
//用于標(biāo)識(shí)是否掛起線程
bool suspend = false;
//用于標(biāo)識(shí)線程時(shí)候開(kāi)始運(yùn)行
bool pNTstart = false;
public Form1()
{
InitializeComponent();
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
// label1
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "已找到的素?cái)?shù):";
// listBox1
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(8, 32);
this.listBox1.MultiColumn = true;
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(272, 136);
this.listBox1.TabIndex = 1;
// button1
this.button1.Location = new System.Drawing.Point(19, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 23);
this.button1.TabIndex = 2;
this.button1.Text = "創(chuàng)建";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(88, 184);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(48, 23);
this.button2.TabIndex = 3;
this.button2.Text = "掛起";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(157, 184);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(48, 23);
this.button3.TabIndex = 4;
this.button3.Text = "恢復(fù)";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(226, 184);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(48, 23);
this.button4.TabIndex = 5;
this.button4.Text = "銷(xiāo)毀";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 224);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(200, 23);
this.label2.TabIndex = 6;
this.label2.Text = "線程未啟動(dòng)";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.label2);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "素?cái)?shù)";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
//創(chuàng)建線程實(shí)例,設(shè)置屬性
pNT = new Thread(new ThreadStart(GPN));
pNT.Name = "Prime Numbers Exaple";
pNT.Priority = ThreadPriority.BelowNormal;
//設(shè)置按鍵,停用開(kāi)始按鍵,啟用掛起按鍵和銷(xiāo)毀按鍵
button1.Enabled = false;
button2.Enabled = true;
button4.Enabled = true;
//線程開(kāi)始,并設(shè)置標(biāo)識(shí)
pNT.Start();
pNTstart = true;
}
private void button2_Click(object sender, System.EventArgs e)
{
//設(shè)置掛起bool變量為真
suspend = true;
//設(shè)置按鍵,停用掛起按鍵, 啟用恢復(fù)按鍵
button2.Enabled = false;
button3.Enabled = true;
}
private void button3_Click(object sender, System.EventArgs e)
{
//設(shè)置掛起bool變量為假
suspend = false;
//當(dāng)線程當(dāng)前狀態(tài)為掛起時(shí),恢復(fù)該線程
if(pNT.ThreadState == System.Threading.ThreadState.Suspended
|| pNT.ThreadState == System.Threading.ThreadState.SuspendRequested)
{
try
{
//恢復(fù)線程
pNT.Resume();
//設(shè)置按鍵,停用恢復(fù)按鍵,啟用掛起按鍵
button3.Enabled = false;
button2.Enabled = true;
}
catch(ThreadStateException Ex)
{
MessageBox.Show(Ex.ToString(), "提示");
}
}
}
private void button4_Click(object sender, System.EventArgs e)
{
//設(shè)置按鍵,啟用開(kāi)始按鍵,停用其他按鍵
button1.Enabled = true;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
//銷(xiāo)毀線程
pNT.Abort();
}
//GPN為GetPrimeNumber的縮寫(xiě),用于查找并顯示素?cái)?shù)
public void GPN()
{
//聲明變量
long Counter; //素?cái)?shù)個(gè)數(shù)
long NumberNow; //當(dāng)前數(shù)
long SqrtOfNow; //輔助數(shù),做數(shù)組下標(biāo)
bool IsPrime = false; //標(biāo)識(shí)是否為素?cái)?shù)
//數(shù)組,用于存儲(chǔ)已找到素?cái)?shù)
long[] PrimeArray = new long[256];
//委托,用于顯示素?cái)?shù),即將其添加到ListBox中
string[] args = new string[] {"2"};
UD UIDel = new UD(UpdateUI);
//初始化,從3開(kāi)始計(jì)算,從第2個(gè)素?cái)?shù)開(kāi)始計(jì)算
NumberNow = 3;
Counter = 2;
//添加2為素?cái)?shù),放入素?cái)?shù)數(shù)組并將其顯示
PrimeArray[1] = 2;
this.Invoke(UIDel, args);
//循環(huán),用于找到并輸出256個(gè)素?cái)?shù)
while(Counter <= 255)
{
IsPrime = true;
//從1到當(dāng)前數(shù)的平方根,窮盡計(jì)算是否為素?cái)?shù)
for(SqrtOfNow = 1; (PrimeArray[SqrtOfNow]
* PrimeArray[SqrtOfNow] <= NumberNow);
SqrtOfNow++)
{
//若能被已找到的素?cái)?shù)整除,則不是素?cái)?shù)
if(NumberNow % PrimeArray[SqrtOfNow] == 0)
{
//若不是素?cái)?shù),跳出for循環(huán)
IsPrime = false;
break;
}
}
//若為素?cái)?shù),將其添加到ListBox以顯示
if(IsPrime)
{
//將素?cái)?shù)存入數(shù)組中儲(chǔ)存
PrimeArray[Counter] = NumberNow;
Counter++;
//將素?cái)?shù)顯示到ListBox中
args[0] = NumberNow.ToString();
this.Invoke(UIDel,args);
//利用bool變量,控制是否掛起線程
if( suspend == true)
{
//調(diào)用Suspend方法,并捕捉異常
try
{
pNT.Suspend();
}
catch(ThreadStateException Ex)
{
MessageBox.Show(Ex.ToString(), "提示");
}
}
//使線程睡眠,使過(guò)程清楚顯示,且有時(shí)間掛起線程
Thread.Sleep(500);
}
//除2外,素?cái)?shù)必為奇數(shù),故跳過(guò)偶數(shù)的檢查,優(yōu)化算法
NumberNow += 2;
}
}
//更新ListBox的方法
void UpdateUI(string result)
{
listBox1.Items.Add(result);
}
//利用Timer控件顯示線程當(dāng)前狀態(tài)
private void timer1_Tick(object sender, System.EventArgs e)
{
//在線程開(kāi)時(shí)候再獲取狀態(tài)并顯示
if( pNTstart )
{
label2.Text = "線程當(dāng)前狀態(tài)是:" + pNT.ThreadState.ToString();
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
//設(shè)置按鍵,啟用開(kāi)始按鍵,停用其他按鍵
button1.Enabled = true;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
}
}
}
感興趣的讀者可以動(dòng)手調(diào)試一下該程序代碼,相信會(huì)有一定的啟發(fā)與借鑒價(jià)值。
相關(guān)文章
C#使用正則表達(dá)式實(shí)現(xiàn)常見(jiàn)的格式驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了C#如何使用正則表達(dá)式實(shí)現(xiàn)常見(jiàn)的格式驗(yàn)證,例如:電話號(hào)碼、密碼、郵編等,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
Unity3D開(kāi)發(fā)教程:憤怒的小鳥(niǎo)
這篇文章詳細(xì)的講解了如何從0開(kāi)發(fā)出一個(gè)Unity3D的小游戲憤怒的小鳥(niǎo),本文包含大量的圖片與文字描述,也含有大量的源代碼,可以讓你快速入手,希望本篇文章對(duì)你有所幫助2021-06-06
c# 動(dòng)態(tài)構(gòu)建LINQ查詢表達(dá)式
這篇文章主要介紹了c# 如何動(dòng)態(tài)構(gòu)建LINQ查詢表達(dá)式,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2020-11-11

