最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#查找素數(shù)實現(xiàn)方法

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

本文所述為C#查找素數(shù)的程序代碼,包括了可視化窗體的代碼,找素數(shù)的方法可以借鑒。雖然實現(xiàn)的功能簡單,不過為了演示一些C#技巧,本文實例中還用到了線程技術(shù)、ListBox列表框的使用、設(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;
 //公共委托,用于輸出素數(shù)
 public delegate void UD(string returnVal);
 //聲明私有線程
 private Thread pNT;
 //用于標(biāo)識是否掛起線程
 bool suspend = false;
 //用于標(biāo)識線程時候開始運行
 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è)計器生成的代碼
 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 = "已找到的素數(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 = "銷毀";
  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 = "素數(shù)";
  this.Load += new System.EventHandler(this.Form1_Load);
  this.ResumeLayout(false);
 }
 #endregion
 /// <summary>
 /// 應(yīng)用程序的主入口點。
 /// </summary>
 [STAThread]
 static void Main()
 {
  Application.Run(new Form1());
 }
 private void button1_Click(object sender, System.EventArgs e)
 {
  //創(chuàng)建線程實例,設(shè)置屬性
  pNT = new Thread(new ThreadStart(GPN));
  pNT.Name = "Prime Numbers Exaple";
  pNT.Priority = ThreadPriority.BelowNormal;
  //設(shè)置按鍵,停用開始按鍵,啟用掛起按鍵和銷毀按鍵
  button1.Enabled = false;
  button2.Enabled = true;
  button4.Enabled = true;
  //線程開始,并設(shè)置標(biāo)識
  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)為掛起時,恢復(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è)置按鍵,啟用開始按鍵,停用其他按鍵
  button1.Enabled = true;
  button2.Enabled = false;
  button3.Enabled = false;
  button4.Enabled = false;
  //銷毀線程
  pNT.Abort();
 }
 //GPN為GetPrimeNumber的縮寫,用于查找并顯示素數(shù)
 public void GPN()
 {
  //聲明變量
  long Counter;  //素數(shù)個數(shù)
  long NumberNow;  //當(dāng)前數(shù)
  long SqrtOfNow;  //輔助數(shù),做數(shù)組下標(biāo)
  bool IsPrime = false; //標(biāo)識是否為素數(shù)
  //數(shù)組,用于存儲已找到素數(shù)
  long[] PrimeArray = new long[256];
  //委托,用于顯示素數(shù),即將其添加到ListBox中
  string[] args = new string[] {"2"};
  UD UIDel = new UD(UpdateUI);
  //初始化,從3開始計算,從第2個素數(shù)開始計算
  NumberNow = 3;
  Counter = 2;
  //添加2為素數(shù),放入素數(shù)數(shù)組并將其顯示
  PrimeArray[1] = 2;
  this.Invoke(UIDel, args);
  //循環(huán),用于找到并輸出256個素數(shù)
  while(Counter <= 255)
  {
  IsPrime = true;
  //從1到當(dāng)前數(shù)的平方根,窮盡計算是否為素數(shù)
  for(SqrtOfNow = 1; (PrimeArray[SqrtOfNow]
   * PrimeArray[SqrtOfNow] <= NumberNow);
   SqrtOfNow++)
  {
   //若能被已找到的素數(shù)整除,則不是素數(shù)
   if(NumberNow % PrimeArray[SqrtOfNow] == 0)
   {
   //若不是素數(shù),跳出for循環(huán)
   IsPrime = false;
   break;
   }
  }
  //若為素數(shù),將其添加到ListBox以顯示
  if(IsPrime)
  {
   //將素數(shù)存入數(shù)組中儲存
   PrimeArray[Counter] = NumberNow;
   Counter++;
   //將素數(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(), "提示");
   }
   }
   //使線程睡眠,使過程清楚顯示,且有時間掛起線程
   Thread.Sleep(500);
  }
  //除2外,素數(shù)必為奇數(shù),故跳過偶數(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)
 {
  //在線程開時候再獲取狀態(tài)并顯示
  if( pNTstart )
  {
  label2.Text = "線程當(dāng)前狀態(tài)是:" + pNT.ThreadState.ToString();
  }
 }
 private void Form1_Load(object sender, System.EventArgs e)
 {
  //設(shè)置按鍵,啟用開始按鍵,停用其他按鍵
  button1.Enabled = true;
  button2.Enabled = false;
  button3.Enabled = false;
  button4.Enabled = false;
 }
 }
}

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

相關(guān)文章

  • C#實現(xiàn)線性搜索算法

    C#實現(xiàn)線性搜索算法

    線性搜索算法是一種基本的搜索算法,通過逐個比較元素來查找目標(biāo)元素,學(xué)習(xí)線性搜索算法有助于培養(yǎng)算法思維和編程能力,對于初學(xué)者來說是一種重要的算法訓(xùn)練,感興趣的可以了解一下
    2024-10-10
  • C#實現(xiàn)加密bat文件的示例詳解

    C#實現(xiàn)加密bat文件的示例詳解

    這篇文章主要為大家詳細(xì)介紹了C#如何實現(xiàn)加密bat文件的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-01-01
  • 關(guān)于C#委托三種調(diào)用的分享使用

    關(guān)于C#委托三種調(diào)用的分享使用

    這篇文章主要介紹了關(guān)于C#委托三種調(diào)用的分享使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • C#使用正則表達(dá)式實現(xiàn)常見的格式驗證

    C#使用正則表達(dá)式實現(xiàn)常見的格式驗證

    這篇文章主要為大家詳細(xì)介紹了C#如何使用正則表達(dá)式實現(xiàn)常見的格式驗證,例如:電話號碼、密碼、郵編等,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • Unity3D開發(fā)教程:憤怒的小鳥

    Unity3D開發(fā)教程:憤怒的小鳥

    這篇文章詳細(xì)的講解了如何從0開發(fā)出一個Unity3D的小游戲憤怒的小鳥,本文包含大量的圖片與文字描述,也含有大量的源代碼,可以讓你快速入手,希望本篇文章對你有所幫助
    2021-06-06
  • C#中IntPtr類型的具體使用

    C#中IntPtr類型的具體使用

    本文主要介紹了C#中IntPtr類型的具體使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 使用C#高效解析HTML的實戰(zhàn)指南

    使用C#高效解析HTML的實戰(zhàn)指南

    在網(wǎng)頁開發(fā)和數(shù)據(jù)處理的場景中,經(jīng)常需要從 HTML 文檔里提取有用的信息,C# 作為一門強(qiáng)大的編程語言,提供了豐富的工具和庫來實現(xiàn) HTML 的解析,這篇博客就帶你深入了解如何使用 C# 高效地解析 HTML
    2025-01-01
  • C#數(shù)組的常用操作方法小結(jié)

    C#數(shù)組的常用操作方法小結(jié)

    Array數(shù)組在C#中同樣是最基本的數(shù)據(jù)結(jié)構(gòu),下面為大家C#數(shù)組的常用操作方法小結(jié),皆為細(xì)小的代碼段,歡迎收看收藏
    2016-05-05
  • C# 6.0 新特性匯總

    C# 6.0 新特性匯總

    這篇文章主要介紹了C# 6.0 新特性匯總的相關(guān)資料,本文給大家?guī)砹?1種新特征,非常不錯,感興趣的朋友一起看看吧
    2016-09-09
  • c# 動態(tài)構(gòu)建LINQ查詢表達(dá)式

    c# 動態(tài)構(gòu)建LINQ查詢表達(dá)式

    這篇文章主要介紹了c# 如何動態(tài)構(gòu)建LINQ查詢表達(dá)式,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-11-11

最新評論

永年县| 高邮市| 抚州市| 富顺县| 克东县| 城口县| 沙湾县| 沁水县| 荣成市| 宣武区| 封丘县| 永川市| 永平县| 哈密市| 江津市| 汉川市| 恩平市| 吴川市| 德安县| 余江县| 齐齐哈尔市| 长武县| 綦江县| 普兰店市| 棋牌| 凤城市| 成安县| 宝丰县| 临颍县| 吉林市| 雅安市| 长阳| 华池县| 临邑县| 上犹县| 东山县| 库尔勒市| 全南县| 郸城县| 天峨县| 平山县|