欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#查找素數實現方法

 更新時間:2014年08月12日 11:37:28   投稿:shichen2014  
這篇文章主要介紹了C#查找素數實現方法,程序中有很多使用的功能模塊,非常適合C#初學者學習借鑒,需要的朋友可以參考下

本文所述為C#查找素數的程序代碼,包括了可視化窗體的代碼,找素數的方法可以借鑒。雖然實現的功能簡單,不過為了演示一些C#技巧,本文實例中還用到了線程技術、ListBox列表框的使用、設置程序掛起等操作,其中備有詳盡的注釋,幫助大家更好的理解。

具體實現代碼如下:

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;
 //公共委托,用于輸出素數
 public delegate void UD(string returnVal);
 //聲明私有線程
 private Thread pNT;
 //用于標識是否掛起線程
 bool suspend = false;
 //用于標識線程時候開始運行
 bool pNTstart = false;
 public Form1()
 {
  InitializeComponent();
  // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
 }
 protected override void Dispose( bool disposing )
 {
  if( disposing )
  {
  if (components != null)
  {
   components.Dispose();
  }
  }
  base.Dispose( disposing );
 }
 #region Windows 窗體設計器生成的代碼
 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 = "已找到的素數:";
  // 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 = "恢復";
  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 = "銷毀";
  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 = "線程未啟動";
  //
  // 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 = "素數";
  this.Load += new System.EventHandler(this.Form1_Load);
  this.ResumeLayout(false);
 }
 #endregion
 /// <summary>
 /// 應用程序的主入口點。
 /// </summary>
 [STAThread]
 static void Main()
 {
  Application.Run(new Form1());
 }
 private void button1_Click(object sender, System.EventArgs e)
 {
  //創(chuàng)建線程實例,設置屬性
  pNT = new Thread(new ThreadStart(GPN));
  pNT.Name = "Prime Numbers Exaple";
  pNT.Priority = ThreadPriority.BelowNormal;
  //設置按鍵,停用開始按鍵,啟用掛起按鍵和銷毀按鍵
  button1.Enabled = false;
  button2.Enabled = true;
  button4.Enabled = true;
  //線程開始,并設置標識
  pNT.Start();
  pNTstart = true;
 }
 private void button2_Click(object sender, System.EventArgs e)
 {
  //設置掛起bool變量為真
  suspend = true;
  //設置按鍵,停用掛起按鍵, 啟用恢復按鍵
  button2.Enabled = false;
  button3.Enabled = true;
 }
 private void button3_Click(object sender, System.EventArgs e)
 {
  //設置掛起bool變量為假
  suspend = false;
  //當線程當前狀態(tài)為掛起時,恢復該線程
  if(pNT.ThreadState == System.Threading.ThreadState.Suspended
  || pNT.ThreadState == System.Threading.ThreadState.SuspendRequested)
  {
  try
  {
   //恢復線程
   pNT.Resume();
   //設置按鍵,停用恢復按鍵,啟用掛起按鍵
   button3.Enabled = false;
   button2.Enabled = true;
  }
  catch(ThreadStateException Ex)
  {
   MessageBox.Show(Ex.ToString(), "提示");
  }
  }
 }
 private void button4_Click(object sender, System.EventArgs e)
 {
  //設置按鍵,啟用開始按鍵,停用其他按鍵
  button1.Enabled = true;
  button2.Enabled = false;
  button3.Enabled = false;
  button4.Enabled = false;
  //銷毀線程
  pNT.Abort();
 }
 //GPN為GetPrimeNumber的縮寫,用于查找并顯示素數
 public void GPN()
 {
  //聲明變量
  long Counter;  //素數個數
  long NumberNow;  //當前數
  long SqrtOfNow;  //輔助數,做數組下標
  bool IsPrime = false; //標識是否為素數
  //數組,用于存儲已找到素數
  long[] PrimeArray = new long[256];
  //委托,用于顯示素數,即將其添加到ListBox中
  string[] args = new string[] {"2"};
  UD UIDel = new UD(UpdateUI);
  //初始化,從3開始計算,從第2個素數開始計算
  NumberNow = 3;
  Counter = 2;
  //添加2為素數,放入素數數組并將其顯示
  PrimeArray[1] = 2;
  this.Invoke(UIDel, args);
  //循環(huán),用于找到并輸出256個素數
  while(Counter <= 255)
  {
  IsPrime = true;
  //從1到當前數的平方根,窮盡計算是否為素數
  for(SqrtOfNow = 1; (PrimeArray[SqrtOfNow]
   * PrimeArray[SqrtOfNow] <= NumberNow);
   SqrtOfNow++)
  {
   //若能被已找到的素數整除,則不是素數
   if(NumberNow % PrimeArray[SqrtOfNow] == 0)
   {
   //若不是素數,跳出for循環(huán)
   IsPrime = false;
   break;
   }
  }
  //若為素數,將其添加到ListBox以顯示
  if(IsPrime)
  {
   //將素數存入數組中儲存
   PrimeArray[Counter] = NumberNow;
   Counter++;
   //將素數顯示到ListBox中
   args[0] = NumberNow.ToString();
   this.Invoke(UIDel,args);
   //利用bool變量,控制是否掛起線程
   if( suspend == true)
   {
   //調用Suspend方法,并捕捉異常
   try
   {
    pNT.Suspend();
   }
   catch(ThreadStateException Ex)
   {
    MessageBox.Show(Ex.ToString(), "提示");
   }
   }
   //使線程睡眠,使過程清楚顯示,且有時間掛起線程
   Thread.Sleep(500);
  }
  //除2外,素數必為奇數,故跳過偶數的檢查,優(yōu)化算法
  NumberNow += 2;
  }
 }
 //更新ListBox的方法
 void UpdateUI(string result)
 {
  listBox1.Items.Add(result);
 }
 //利用Timer控件顯示線程當前狀態(tài)
 private void timer1_Tick(object sender, System.EventArgs e)
 {
  //在線程開時候再獲取狀態(tài)并顯示
  if( pNTstart )
  {
  label2.Text = "線程當前狀態(tài)是:" + pNT.ThreadState.ToString();
  }
 }
 private void Form1_Load(object sender, System.EventArgs e)
 {
  //設置按鍵,啟用開始按鍵,停用其他按鍵
  button1.Enabled = true;
  button2.Enabled = false;
  button3.Enabled = false;
  button4.Enabled = false;
 }
 }
}

感興趣的讀者可以動手調試一下該程序代碼,相信會有一定的啟發(fā)與借鑒價值。

相關文章

  • 使用xmltextreader對象讀取xml文檔示例

    使用xmltextreader對象讀取xml文檔示例

    這篇文章主要介紹了使用xmltextreader對象讀取xml文檔的示例,需要的朋友可以參考下
    2014-02-02
  • C# 利用StringBuilder提升字符串拼接性能的小例子

    C# 利用StringBuilder提升字符串拼接性能的小例子

    一個項目中有數據圖表呈現,數據量稍大時顯得很慢,在使用了StringBuilder后效果提升很明顯,下面有例子
    2013-07-07
  • Unity平臺模擬自動擋駕駛汽車

    Unity平臺模擬自動擋駕駛汽車

    這篇文章主要為大家詳細介紹了Unity平臺模擬自動擋駕駛汽車,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • C# 禁用鼠標中間鍵的方法

    C# 禁用鼠標中間鍵的方法

    關于?。? System.Windows.Forms.NumericUpDown 控件,如何禁用鼠標中間鍵?
    2013-03-03
  • C#讀寫共享文件夾的方法

    C#讀寫共享文件夾的方法

    這篇文章主要為大家詳細介紹了C#讀寫共享文件夾的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • C#識別出圖片里的數字和字母

    C#識別出圖片里的數字和字母

    本文給大家分享的是C#識別出圖片里的數字和字母的代碼,主要是識別以前公司的軟件注冊碼截圖里的數字和字母,功能很簡單,也存在很大的局限性,這里僅僅是分享,小伙伴們參考下。
    2015-03-03
  • C#中使用ArrayPool和MemoryPool實例

    C#中使用ArrayPool和MemoryPool實例

    對資源的可復用是提升應用程序性能的一個非常重要的手段,比如本篇要分享的 ArrayPool 和 MemoryPool,它們就有效的減少了內存使用和對GC的壓力,從而提升應用程序性能。感興趣的可以了解一下
    2021-05-05
  • C#實現獲取程序路徑方法小結

    C#實現獲取程序路徑方法小結

    這篇文章主要介紹了C#實現獲取程序路徑方法,實例分析了C#獲取文件路徑的各種常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • C#調用7z實現文件的壓縮與解壓

    C#調用7z實現文件的壓縮與解壓

    這篇文章主要介紹了C#調用7z實現文件的壓縮與解壓,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-12-12
  • C#中使用DevExpress中的ChartControl實現極坐標圖的案例詳解

    C#中使用DevExpress中的ChartControl實現極坐標圖的案例詳解

    這篇文章主要介紹了在C#中使用DevExpress中的ChartControl實現極坐標圖,本案例是使用的是DevExpress 18.1.3版本,之前在14版本上也試過,但是有一個弊端就是實現極坐標圖的時候,第一個點和最后一個點總是自動多一條閉合線,會形成一個閉合的多邊形,因此升級了一下版
    2022-02-02

最新評論