C#查找素数实现方法

 更新时间:2020年6月25日 11:35  点击:1749

本文所述为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 = "创建";
  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)
 {
  //创建线程实例,设置属性
  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;
  //当线程当前状态为挂起时,恢复该线程
  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);
  //循环,用于找到并输出256个素数
  while(Counter <= 255)
  {
  IsPrime = true;
  //从1到当前数的平方根,穷尽计算是否为素数
  for(SqrtOfNow = 1; (PrimeArray[SqrtOfNow]
   * PrimeArray[SqrtOfNow] <= NumberNow);
   SqrtOfNow++)
  {
   //若能被已找到的素数整除,则不是素数
   if(NumberNow % PrimeArray[SqrtOfNow] == 0)
   {
   //若不是素数,跳出for循环
   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外,素数必为奇数,故跳过偶数的检查,优化算法
  NumberNow += 2;
  }
 }
 //更新ListBox的方法
 void UpdateUI(string result)
 {
  listBox1.Items.Add(result);
 }
 //利用Timer控件显示线程当前状态
 private void timer1_Tick(object sender, System.EventArgs e)
 {
  //在线程开时候再获取状态并显示
  if( pNTstart )
  {
  label2.Text = "线程当前状态是:" + pNT.ThreadState.ToString();
  }
 }
 private void Form1_Load(object sender, System.EventArgs e)
 {
  //设置按键,启用开始按键,停用其他按键
  button1.Enabled = true;
  button2.Enabled = false;
  button3.Enabled = false;
  button4.Enabled = false;
 }
 }
}

感兴趣的读者可以动手调试一下该程序代码,相信会有一定的启发与借鉴价值。

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 经典实例讲解C#递归算法

    这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#中list用法实例

    这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25