zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#查找素数实现方法

c#方法 实现 查找 素数
2023-06-13 09:15:42 时间

本文所述为C#查找素数的程序代码,包括了可视化窗体的代码,找素数的方法可以借鉴。虽然实现的功能简单,不过为了演示一些C#技巧,本文实例中还用到了线程技术、ListBox列表框的使用、设置程序挂起等操作,其中备有详尽的注释,帮助大家更好的理解。

具体实现代码如下:

usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.Threading;
namespaceSuspendAndResume
{
publicclassForm1:System.Windows.Forms.Form
{
privateSystem.Windows.Forms.Labellabel1;
privateSystem.Windows.Forms.ListBoxlistBox1;
privateSystem.Windows.Forms.Buttonbutton1;
privateSystem.Windows.Forms.Buttonbutton2;
privateSystem.Windows.Forms.Buttonbutton3;
privateSystem.Windows.Forms.Buttonbutton4;
privateSystem.Windows.Forms.Labellabel2;
privateSystem.Windows.Forms.Timertimer1;
privateSystem.ComponentModel.IContainercomponents;
//公共委托,用于输出素数
publicdelegatevoidUD(stringreturnVal);
//声明私有线程
privateThreadpNT;
//用于标识是否挂起线程
boolsuspend=false;
//用于标识线程时候开始运行
boolpNTstart=false;
publicForm1()
{
InitializeComponent();
//TODO:在InitializeComponent调用后添加任何构造函数代码
}
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#regionWindows窗体设计器生成的代码
privatevoidInitializeComponent()
{
this.components=newSystem.ComponentModel.Container();
this.label1=newSystem.Windows.Forms.Label();
this.listBox1=newSystem.Windows.Forms.ListBox();
this.button1=newSystem.Windows.Forms.Button();
this.button2=newSystem.Windows.Forms.Button();
this.button3=newSystem.Windows.Forms.Button();
this.button4=newSystem.Windows.Forms.Button();
this.label2=newSystem.Windows.Forms.Label();
this.timer1=newSystem.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//label1
this.label1.Location=newSystem.Drawing.Point(8,8);
this.label1.Name="label1";
this.label1.TabIndex=0;
this.label1.Text="已找到的素数:";
//listBox1
this.listBox1.ItemHeight=12;
this.listBox1.Location=newSystem.Drawing.Point(8,32);
this.listBox1.MultiColumn=true;
this.listBox1.Name="listBox1";
this.listBox1.Size=newSystem.Drawing.Size(272,136);
this.listBox1.TabIndex=1;
//button1
this.button1.Location=newSystem.Drawing.Point(19,184);
this.button1.Name="button1";
this.button1.Size=newSystem.Drawing.Size(48,23);
this.button1.TabIndex=2;
this.button1.Text="创建";
this.button1.Click+=newSystem.EventHandler(this.button1_Click);
//
//button2
//
this.button2.Location=newSystem.Drawing.Point(88,184);
this.button2.Name="button2";
this.button2.Size=newSystem.Drawing.Size(48,23);
this.button2.TabIndex=3;
this.button2.Text="挂起";
this.button2.Click+=newSystem.EventHandler(this.button2_Click);
//
//button3
//
this.button3.Location=newSystem.Drawing.Point(157,184);
this.button3.Name="button3";
this.button3.Size=newSystem.Drawing.Size(48,23);
this.button3.TabIndex=4;
this.button3.Text="恢复";
this.button3.Click+=newSystem.EventHandler(this.button3_Click);
//
//button4
//
this.button4.Location=newSystem.Drawing.Point(226,184);
this.button4.Name="button4";
this.button4.Size=newSystem.Drawing.Size(48,23);
this.button4.TabIndex=5;
this.button4.Text="销毁";
this.button4.Click+=newSystem.EventHandler(this.button4_Click);
//
//label2
//
this.label2.Location=newSystem.Drawing.Point(24,224);
this.label2.Name="label2";
this.label2.Size=newSystem.Drawing.Size(200,23);
this.label2.TabIndex=6;
this.label2.Text="线程未启动";
//
//timer1
//
this.timer1.Enabled=true;
this.timer1.Tick+=newSystem.EventHandler(this.timer1_Tick);
//
//Form1
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.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+=newSystem.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()
{
Application.Run(newForm1());
}
privatevoidbutton1_Click(objectsender,System.EventArgse)
{
//创建线程实例,设置属性
pNT=newThread(newThreadStart(GPN));
pNT.Name="PrimeNumbersExaple";
pNT.Priority=ThreadPriority.BelowNormal;
//设置按键,停用开始按键,启用挂起按键和销毁按键
button1.Enabled=false;
button2.Enabled=true;
button4.Enabled=true;
//线程开始,并设置标识
pNT.Start();
pNTstart=true;
}
privatevoidbutton2_Click(objectsender,System.EventArgse)
{
//设置挂起bool变量为真
suspend=true;
//设置按键,停用挂起按键,启用恢复按键
button2.Enabled=false;
button3.Enabled=true;
}
privatevoidbutton3_Click(objectsender,System.EventArgse)
{
//设置挂起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(ThreadStateExceptionEx)
{
MessageBox.Show(Ex.ToString(),"提示");
}
}
}
privatevoidbutton4_Click(objectsender,System.EventArgse)
{
//设置按键,启用开始按键,停用其他按键
button1.Enabled=true;
button2.Enabled=false;
button3.Enabled=false;
button4.Enabled=false;
//销毁线程
pNT.Abort();
}
//GPN为GetPrimeNumber的缩写,用于查找并显示素数
publicvoidGPN()
{
//声明变量
longCounter;//素数个数
longNumberNow;//当前数
longSqrtOfNow;//辅助数,做数组下标
boolIsPrime=false;//标识是否为素数
//数组,用于存储已找到素数
long[]PrimeArray=newlong[256];
//委托,用于显示素数,即将其添加到ListBox中
string[]args=newstring[]{"2"};
UDUIDel=newUD(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(ThreadStateExceptionEx)
{
MessageBox.Show(Ex.ToString(),"提示");
}
}
//使线程睡眠,使过程清楚显示,且有时间挂起线程
Thread.Sleep(500);
}
//除2外,素数必为奇数,故跳过偶数的检查,优化算法
NumberNow+=2;
}
}
//更新ListBox的方法
voidUpdateUI(stringresult)
{
listBox1.Items.Add(result);
}
//利用Timer控件显示线程当前状态
privatevoidtimer1_Tick(objectsender,System.EventArgse)
{
//在线程开时候再获取状态并显示
if(pNTstart)
{
label2.Text="线程当前状态是:"+pNT.ThreadState.ToString();
}
}
privatevoidForm1_Load(objectsender,System.EventArgse)
{
//设置按键,启用开始按键,停用其他按键
button1.Enabled=true;
button2.Enabled=false;
button3.Enabled=false;
button4.Enabled=false;
}
}
}

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