你是说质数?那上面的你肯定错了啦~我做过一个质合计算器,方法很蠢,但还可以用...
有兴趣的话你可以读一下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 质合计算器
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.NumericUpDown Numbox;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox text;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.Panel panel1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TOD Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.label1 = new System.Windows.Forms.Label();
this.Numbox = new System.Windows.Forms.NumericUpDown();
this.text = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.panel1 = new System.Windows.Forms.Panel();
((System.ComponentModel.ISupportInitialize)(this.Numbox)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 16);
this.label1.TabIndex = 0;
this.label1.Text = "要进行判断的数字:";
this.toolTip1.SetToolTip(this.label1, "注意:计算器的最大值为2147483647");
//
// Numbox
//
this.Numbox.ContextMenu = this.contextMenu1;
this.Numbox.Font = new System.Drawing.Font("SimSun", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.Numbox.ForeColor = System.Drawing.Color.Orange;
this.Numbox.Location = new System.Drawing.Point(128, 16);
this.Numbox.Maximum = new System.Decimal(new int[] {
2147483647,
0,
0,
0});
this.Numbox.Name = "Numbox";
this.Numbox.Size = new System.Drawing.Size(160, 23);
this.Numbox.TabIndex = 1;
this.Numbox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.toolTip1.SetToolTip(this.Numbox, "注意:计算器的最大值为2147483647");
this.Numbox.Value = new System.Decimal(new int[] {
2,
0,
0,
0});
//
// text
//
this.text.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.text.ContextMenu = this.contextMenu1;
this.text.ForeColor = System.Drawing.Color.MediumSpringGreen;
this.text.Location = new System.Drawing.Point(64, 48);
this.text.Name = "text";
this.text.ReadOnly = true;
this.text.Size = new System.Drawing.Size(224, 21);
this.text.TabIndex = 2;
this.text.Text = "";
this.toolTip1.SetToolTip(this.text, "注意:计算器的最大值为2147483647");
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(40, 24);
this.label2.TabIndex = 3;
this.label2.Text = "结果:";
this.toolTip1.SetToolTip(this.label2, "注意:计算器的最大值为2147483647");
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 24);
this.button1.TabIndex = 4;
this.button1.Text = "判断";
this.toolTip1.SetToolTip(this.button1, "注意:计算器的最大值为2147483647");
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "关于";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// panel1
//
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.text);
this.panel1.Controls.Add(this.Numbox);
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(304, 136);
this.panel1.TabIndex = 5;
this.toolTip1.SetToolTip(this.panel1, "注意:计算器的最大值为2147483647");
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(306, 112);
this.ContextMenu = this.contextMenu1;
this.Controls.Add(this.panel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(312, 144);
this.MinimumSize = new System.Drawing.Size(312, 144);
this.Name = "Form1";
this.Text = "质合计算器";
((System.ComponentModel.ISupportInitialize)(this.Numbox)).EndInit();
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
int num = (int)Numbox.Value;
int yuannum = num;
if(num<=1)
{
MessageBox.Show("小于或者等于一的整数都不是质数或者合数","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
if(num==2)
{
text.Text="2是一个质数";
return;
}
//对特殊例子的处理完成!
if(num>999999999)
{
DialogResult ok =MessageBox.Show("如果"+num.ToString()+"是一个质数,计算器可能会要花相当长的时间进行计算,确认要继续吗?","是否继续",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if(ok!=DialogResult.OK)
return;
}
//接下来进行真正的算法!
ArrayList numlst = new ArrayList();
bool doesit = false;
for(int x=2;x<num/2+1;x++)
{
if(num==x)
break;
if(num%x==0)
{
numlst.Add(x);
num=num/x;
doesit = true;
if(x==4)
{
MessageBox.Show("num:"+num.ToString(),"4");
}
x=1;
}
}
if(doesit==false)
{
text.Text=yuannum.ToString()+"是一个质数";
}
else
{
string log = null;
for(int x=0;x<numlst.Count;x++)
{
log+=((int)numlst[x]).ToString()+"*";
}
text.Text=yuannum.ToString()+"是一个合数,"+yuannum.ToString()+"="+log+num.ToString();
}
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(" 作者:Geoge小猴儿 \n QQ:362938909 \n E-mail:houyunqing@yeah.net \n 闲着没事写了这个东东,它有时候还真有点用哦!! \n 注意:计算器可以输入的最大值为"+int.MaxValue.ToString(),"关于");
}
}
}