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è)置程序掛起等操作,其中備有詳盡的注釋?zhuān)瑤椭蠹腋玫睦斫狻?/p>
具體實(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)文章
使用xmltextreader對(duì)象讀取xml文檔示例
這篇文章主要介紹了使用xmltextreader對(duì)象讀取xml文檔的示例,需要的朋友可以參考下2014-02-02C# 利用StringBuilder提升字符串拼接性能的小例子
一個(gè)項(xiàng)目中有數(shù)據(jù)圖表呈現(xiàn),數(shù)據(jù)量稍大時(shí)顯得很慢,在使用了StringBuilder后效果提升很明顯,下面有例子2013-07-07Unity平臺(tái)模擬自動(dòng)擋駕駛汽車(chē)
這篇文章主要為大家詳細(xì)介紹了Unity平臺(tái)模擬自動(dòng)擋駕駛汽車(chē),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07C#中使用ArrayPool和MemoryPool實(shí)例
對(duì)資源的可復(fù)用是提升應(yīng)用程序性能的一個(gè)非常重要的手段,比如本篇要分享的 ArrayPool 和 MemoryPool,它們就有效的減少了內(nèi)存使用和對(duì)GC的壓力,從而提升應(yīng)用程序性能。感興趣的可以了解一下2021-05-05C#實(shí)現(xiàn)獲取程序路徑方法小結(jié)
這篇文章主要介紹了C#實(shí)現(xiàn)獲取程序路徑方法,實(shí)例分析了C#獲取文件路徑的各種常用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#調(diào)用7z實(shí)現(xiàn)文件的壓縮與解壓
這篇文章主要介紹了C#調(diào)用7z實(shí)現(xiàn)文件的壓縮與解壓,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-12-12C#中使用DevExpress中的ChartControl實(shí)現(xiàn)極坐標(biāo)圖的案例詳解
這篇文章主要介紹了在C#中使用DevExpress中的ChartControl實(shí)現(xiàn)極坐標(biāo)圖,本案例是使用的是DevExpress 18.1.3版本,之前在14版本上也試過(guò),但是有一個(gè)弊端就是實(shí)現(xiàn)極坐標(biāo)圖的時(shí)候,第一個(gè)點(diǎn)和最后一個(gè)點(diǎn)總是自動(dòng)多一條閉合線,會(huì)形成一個(gè)閉合的多邊形,因此升級(jí)了一下版2022-02-02