超级无限位乘法计算系统

来源:互联网 发布:js 鼠标离开时隐藏 编辑:程序博客网 时间:2024/05/11 09:24
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace SuperCalculate
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.TextBox textBox1;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.TextBox textBox2;
        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.GroupBox groupBox1;
        
private System.Windows.Forms.TextBox textBox3;
        
private System.Windows.Forms.Button button2;
        
private System.Windows.Forms.Button button3;
        
private System.Windows.Forms.Label label4;
        
private System.Windows.Forms.Label label3;
        
private System.Windows.Forms.ImageList imageList1;
        
private System.Windows.Forms.ProgressBar progressBar1;
        
private System.Windows.Forms.ProgressBar progressBar2;
        
private System.ComponentModel.IContainer components;
        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();
            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            thread.Abort();
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }

        Thread thread;
        
private void button1_Click(object sender, System.EventArgs e)
        
{
            thread.Start();
            button1.Enabled
=false;
            button2.Enabled
=true;
            button3.Enabled
=true;
        }

        
private void calc()//计算过程
        {
            Console.WriteLine(
" 超大型乘法计算系统-蓝天^_^软件工作室");
            
if(textBox1.Text=="" || textBox2.Text=="")
            
{
                textBox3.Text
="请先输入要计算的数字!";
                button1.Enabled
=true;
                button2.Enabled
=false;
                button3.Enabled
=false;
                thread
=new Thread(new ThreadStart(calc));
                
return;
            }

            textBox3.Text
=null;
            
string n1=textBox1.Text;//被乘数
            string n2=textBox2.Text;//乘数
            string mulJieguo=null;//乘法结果
            int[,] addNum=new int[n2.Length,n1.Length+1];//各位加数
            int jinwei=0;//进位
            
//每一层的值相乘
            for(int i=n2.Length-1;i>=0;i--)
            
{
                progressBar1.Value
=(int)((n2.Length-double.Parse(i.ToString()))/(n2.Length)*100);
                label4.Text
="正在装入数据...总进度已完成:"+(int)((n2.Length-double.Parse(i.ToString()))/(n2.Length)*100)+"%";
                
for(int j=n1.Length;j>0;j--)
                
{
                     progressBar2.Value
=(int)((n1.Length-double.Parse(j.ToString()))/(n1.Length)*100);
                    label3.Text
="正在装入数据...子进度已完成:"+(int)((n1.Length-double.Parse(j.ToString()))/(n1.Length)*100)+"%";
                    
try
                    
{
                        addNum[i,j]
=int.Parse(n2[i].ToString())*int.Parse(n1[j-1].ToString());
                    }

                    
catch
                    
{
                        textBox3.Text
="您输入的不是有效的数字!";
                        button1.Enabled
=true;
                        button2.Enabled
=false;
                        button3.Enabled
=false;
                        thread
=new Thread(new ThreadStart(calc));
                        
return;
                    }

                    
if(addNum[i,j].ToString().Length==1)
                    
{
                        addNum[i,j]
=addNum[i,j]+jinwei;
                        jinwei
=0;
                    }

                    
else
                    
{
                        addNum[i,j]
=addNum[i,j]+jinwei;
                        jinwei
=int.Parse(addNum[i,j].ToString().Substring(0,1));//获取进位值;
                        addNum[i,j]=int.Parse(addNum[i,j].ToString().Substring(1));//获取个位值
                    }

                }

                addNum[i,
0]=int.Parse(jinwei.ToString());
                jinwei
=0;//进位清空
            }

            Console.WriteLine (
" ***************");
            
int[][] mulNum=new int[n2.Length][];
            
for(int i=0;i<mulNum.GetLength(0);i++)
                mulNum[mulNum.GetLength(
0)-i-1]=new int[n1.Length+i+1];
            
for(int i=0;i<mulNum.GetLength(0);i++)
                
for(int j=0;j<mulNum[i].Length;j++)
                    
if(j>=addNum.GetLength(1))
                        mulNum[i][j]
=0;
                    
else
                        mulNum[i][j]
=addNum[i,j];
            
            
for(int i=0;i<mulNum.Length;i++)
            
{
                
for(int j=0;j<mulNum[i].Length;j++)
                    Console.Write(mulNum[i][j]);
                Console.WriteLine();
            }

            Console.WriteLine(
"**************************");
            Console.Write(n1
+"乘以"+n2+"的计算结果是(请稍等...):");
            
//所有层的值相加
            int[] jieguo=new int[mulNum[0].Length];
            
for(int i=0;i<jieguo.Length;i++)
            
{
                progressBar1.Value
=(int)(double.Parse(i.ToString())/(jieguo.Length-1)*100);
                label4.Text
="正在进行计算...总进度已完成:"+(int)(double.Parse(i.ToString())/(jieguo.Length-1)*100)+"%";
                
for(int k=0;k<mulNum.Length;k++)
                
{
                    
if(mulNum.Length>1)
                    
{
                        progressBar2.Value
=(int)(double.Parse(k.ToString())/(mulNum.Length-1)*100);
                        label3.Text
="正在进行计算...子进度已完成:"+(int)(double.Parse(k.ToString())/(mulNum.Length-1)*100)+"%";
                    }

                    
else
                    
{
                        progressBar2.Value
=100;
                        label3.Text
="正在进行计算...子进度已完成:100%";
                    }

                    
try
                    
{

                        jieguo[i]
+=mulNum[k][mulNum[k].Length-(i+1)];
                    }

                    
catch{}
                }

                
if(jieguo[i].ToString().Length==1)
                
{
                    jieguo[i]
=jieguo[i]+jinwei;
                    jinwei
=0;
                }

                
else
                
{
                    jieguo[i]
=jieguo[i]+jinwei;
                    jinwei
=int.Parse(jieguo[i].ToString().Substring(0,1));//获取进位值;
                    jieguo[i]=int.Parse(jieguo[i].ToString().Substring(1));//获取个位值
                }


                mulJieguo
=jieguo[i]+mulJieguo;
            }

            textBox3.Text
=checkData(jinwei.ToString()+mulJieguo);//打印计算结果
            Console.Write(textBox3.Text);
            button1.Enabled
=true;
            button2.Enabled
=false;
            button3.Enabled
=false;
            MessageBox.Show(
"任务执行完毕","蓝天^_^软件工作室",MessageBoxButtons.OK,MessageBoxIcon.Information);
            thread
=new Thread(new ThreadStart(calc));
        }

        
static private string checkData(string s)
        
{
            
try
            
{
                
while(s.Substring(0,1)=="0")
                    s
=s.Substring(1);
            }

            
catch{s="0";}
            
return s;
        }


        
private void button1_MouseEnter(object sender, System.EventArgs e)
        
{
            button1.BackColor
=Color.Yellow;
        }


        
private void button1_MouseLeave(object sender, System.EventArgs e)
        
{
            button1.BackColor
=Color.Linen;
        }

        
private void button2_MouseEnter(object sender, System.EventArgs e)
        
{
            button2.BackColor
=Color.Yellow;
        }


        
private void button2_MouseLeave(object sender, System.EventArgs e)
        
{
            button2.BackColor
=Color.Linen;
        }

        
private void button3_MouseEnter(object sender, System.EventArgs e)
        
{
            button3.BackColor
=Color.Yellow;
        }


        
private void button3_MouseLeave(object sender, System.EventArgs e)
        
{
            button3.BackColor
=Color.Linen;
        }


        
private void Form1_Load(object sender, System.EventArgs e)
        
{
            thread
=new Thread(new ThreadStart(calc));
        }


        
private void button2_Click(object sender, System.EventArgs e)
        
{
            
if(button2.Text=="暂停计算")
            
{
                thread.Suspend();
                button2.Text
="恢复计算";
            }

            
else
            
{
                thread.Resume();
                button2.Text
="暂停计算";
            }

        }


        
private void button3_Click(object sender, System.EventArgs e)
        
{
            
if(MessageBox.Show("您真的要停止正在执行的任务吗?","请选择",MessageBoxButtons.YesNo,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button2)==DialogResult.Yes)
                
try
                
{
                    thread.Abort();
                }

                
catch{}
                
finally
                
{
                    button1.Enabled
=true;
                    button2.Enabled
=false;
                    button3.Enabled
=false;
                    button2.Text
="暂停计算";
                    thread
=new Thread(new ThreadStart(calc));
                }

        }

    }

}

 
原创粉丝点击