Seversintabi.com Türkiye'nin En Büyük Forumu Bence Seversin Tabi
 

Go Back   Seversintabi.com Türkiye'nin En Büyük Forumu Bence Seversin Tabi > Bilgisayar Dünyası > Programlama Ve Tasarım
Yardım Topluluk Takvim Bugünki Mesajlar Arama

gaziantep escort gaziantep escort
youtube beğeni hilesi
Cevapla

 

LinkBack Seçenekler Stil
  #1  
Alt 16 November 2008, 19:56
Senior Member
 
Kayıt Tarihi: 21 September 2008
Mesajlar: 15,180
Konular:
Aldığı Beğeni: 0 xx
Beğendiği Mesajlar: 0 xx
Post C# asal sayı programı

Kullanılan C# kavramları : Asynchronous Threading, Windows Forms, Components, Exception Handling, etc

Kod:
//  Prime Numbers. © 2006

using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace isPrime
{
    /// <summary>
    /// isPrimeForm : Main and the only Form.
    /// </summary>
    public class isPrimeForm : System.Windows.Forms.Form
    {
        #region Declarations, Ctor & Dispose()

        private System.Threading.Thread PrimeTesterThread;
        private delegate void testCompleteHandler(bool isPrime,ulong catched);
        private event testCompleteHandler testComplete;

        private System.Threading.Thread RangeTesterThread;
        private delegate void rangeTestCompleteHandler();
        private event rangeTestCompleteHandler rangeTestComplete;

        private decimal number = 0m , rangeStart = 0m , rangeEnd = 0m , ct = 0m;
        private string firstLine = "";

        private System.Windows.Forms.GroupBox groupBoxInput;
        private System.Windows.Forms.ProgressBar progressBar;
        private System.Windows.Forms.TextBox textBoxInput;
        private System.Windows.Forms.Button buttonClear;
        private System.Windows.Forms.TabControl tabControl;
        private System.Windows.Forms.TabPage tabPageSingle;
        private System.Windows.Forms.TabPage tabPageRange;
        private System.Windows.Forms.TextBox textBoxRangeStart;
        private System.Windows.Forms.TextBox textBoxRangeEnd;
        private System.Windows.Forms.Label labelRangeTo;
        private System.Windows.Forms.Label labelRangeFrom;
        private System.Windows.Forms.LinkLabel linkLabel;
        private System.Windows.Forms.Label labelInfoSingle;
        private System.Windows.Forms.Button buttonTest;
        private System.Windows.Forms.Button buttonStart;
        private System.Windows.Forms.TextBox textBoxRangeResults;
        private System.Windows.Forms.TextBox textBoxStatusSingle;
        private System.Windows.Forms.Button buttonCancelRange;
        private System.Windows.Forms.ProgressBar progressBarRange;
        private System.Windows.Forms.Button buttonSave;
        private System.Windows.Forms.SaveFileDialog saveFileDialog;
        private System.Windows.Forms.Label labelNumberCount;
        private System.Windows.Forms.CheckBox checkBoxNC;
        private System.Windows.Forms.Button buttonCancel;

        public isPrimeForm()
        {
            InitializeComponent();
            this.testComplete += new testCompleteHandler(this.testHandler);
            this.rangeTestComplete += new rangeTestCompleteHandler(this.rangeTestHandler);
        }

        protected override void Dispose( bool disposing )
        {
            if ( this.PrimeTesterThread != null )
                this.PrimeTesterThread.Abort();
            if ( this.RangeTesterThread != null )
                 this.RangeTesterThread.Abort();
            base.Dispose( disposing );
        }
        #endregion
        #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            // ...
            // InitializeComponent() kısmı.
            // ...
        }
        [STAThread]
        static void Main() 
        {
            Application.Run(new isPrimeForm());
        }
        #endregion

        private void isPrimeForm_Load(object sender, System.EventArgs e)
        {
            this.Size = new Size( 327 , 295 );
        }

        private void buttonTest_Click(object sender, System.EventArgs e)
        {
            string msg;
            if ( validateInput( this.textBoxInput.Text , out this.number , out msg ) ) 
            {
                this.buttonClear.Enabled = this.buttonTest.Enabled = this.textBoxInput.Enabled = false;
                this.buttonCancel.Enabled = true;
                this.CancelButton = this.buttonCancel;
                this.labelInfoSingle.Text = "[ ESC = cancel ]";
                this.tabPageSingle.Cursor = Cursors.WaitCursor;
                this.firstLine = this.number.ToString( "N0" ) + "\r\n\r\n";
                this.textBoxStatusSingle.Text = firstLine + "Testing...";
                this.progressBar.Value = 0;
                this.progressBar.Show();
                this.progressBar.Select();
                this.PrimeTesterThread = new Thread(new ThreadStart(this.primeTestThreadStart));
                this.PrimeTesterThread.Start();
            }
            else
            {
                this.textBoxStatusSingle.Text = msg;
                this.textBoxInput.Focus();
            }
        }

        private void buttonStart_Click(object sender, System.EventArgs e)
        {
            string msg;
            if ( this.validateInput( this.textBoxRangeStart.Text , out this.rangeStart , out msg )
                && this.validateInput( this.textBoxRangeEnd.Text , out this.rangeEnd , out msg ) )
            {
                this.textBoxRangeStart.Enabled = this.textBoxRangeEnd.Enabled = this.buttonSave.Enabled
                    = this.buttonStart.Enabled = this.textBoxRangeResults.Enabled = false;
                this.buttonCancelRange.Enabled = true;
                this.tabPageRange.Cursor = Cursors.WaitCursor;
                this.textBoxRangeResults.Clear();
                this.labelNumberCount.Show();
                this.progressBarRange.Value = 0;
                this.progressBarRange.Show();
                this.progressBarRange.Select();
                this.RangeTesterThread = new Thread(new ThreadStart(this.rangeTestThreadStart));
                this.RangeTesterThread.Start();
            }
            else
                this.textBoxRangeResults.Text = msg;
        }

        private void buttonCancel_Click(object sender, System.EventArgs e)
        {
            this.PrimeTesterThread.Abort();
            this.textBoxStatusSingle.Text = this.firstLine + "Canceled.";
            this.testEnd();
        }
        
        private void buttonCancelRange_Click(object sender, System.EventArgs e)
        {
            this.RangeTesterThread.Abort();
            this.textBoxRangeResults.AppendText( "Canceled.\r\n" );
            this.rangeTestEnd();
        }

        private void buttonClear_Click(object sender, System.EventArgs e)
        {
            this.textBoxInput.Text = "";
            this.textBoxInput.Focus();
        }

        private void testHandler(bool isPrime,ulong catched)
        {
            this.BeginInvoke(new testCompleteHandler(this.test_Complete) ,
                new Object[] { isPrime , catched } );
        }

        private void rangeTestHandler()
        {
            this.BeginInvoke(new rangeTestCompleteHandler(this.rangeTest_Complete));
        }

        private void test_Complete(bool isPrime,ulong catched)
        {
            this.PrimeTesterThread.Abort();
            if ( isPrime )
                this.textBoxStatusSingle.Text = firstLine + "PRIME.";
            else if ( catched > 0 )
                this.textBoxStatusSingle.Text = firstLine + "NOT Prime, divisible to " + catched.ToString("N0");
            else
                this.textBoxStatusSingle.Text = firstLine + "NOT Prime.";
            this.testEnd();
        }

        private void rangeTest_Complete()
        {
            this.RangeTesterThread.Abort();
            this.rangeTestEnd();
        }

        private void testEnd()
        {
            this.PrimeTesterThread = null;
            GC.Collect();
            this.buttonClear.Enabled = this.buttonTest.Enabled = this.textBoxInput.Enabled = true;
            this.buttonCancel.Enabled = false;
            this.CancelButton = this.buttonClear;
            this.tabPageSingle.Cursor = Cursors.Default;
            this.labelInfoSingle.Text = "[ ENTER = test , ESC = clear ]";
            this.progressBar.Hide();
            this.textBoxInput.Focus();
        }

        private void rangeTestEnd()
        {
            this.textBoxRangeResults.AppendText( "Number Count : " + this.ct.ToString( "N0" ) + "\r\n" );
            this.RangeTesterThread = null;
            GC.Collect();
            this.buttonStart.Enabled = this.textBoxRangeStart.Enabled = this.buttonSave.Enabled
                = this.textBoxRangeEnd.Enabled = this.textBoxRangeResults.Enabled = true;
            this.buttonCancelRange.Enabled = false;
            this.labelNumberCount.Hide();
            this.tabPageRange.Cursor = Cursors.Default;
            this.progressBarRange.Hide();
            this.textBoxRangeStart.Focus();
        }

        private void primeTestThreadStart()
        {
            ulong result = this.primeTest( this.number );
            switch ( result ) 
            {
                case 1:
                    this.testComplete( true , 1 );
                    break;
                default:
                    this.testComplete( false , result );
                    break;
            }
        }

        private void rangeTestThreadStart()
        {
            ulong result;
            decimal start = 0m , end = this.ct = 0m;

            if ( this.rangeStart < 2 )
                start = 1;
            else
                start = this.rangeStart;

            if ( this.rangeEnd < 2 )
                end = 1;
            else
                end = this.rangeEnd;

            if ( start > end )
            {
                decimal temp = start;
                start = end;
                end = temp;
            }

            this.textBoxRangeResults.AppendText( "Prime numbers between " + this.rangeStart.ToString("N0") + " and " + this.rangeEnd.ToString("N0") + " :\r\n" );
            decimal numbers = (decimal)Math.Ceiling( ((double)((this.rangeEnd - this.rangeStart)/50)) );
            decimal step_r = ( (numbers)%2 == 0 ? numbers + 1 : numbers );
            start--;
            while ( end > start++ )
            {
                result = this.primeTest(start);
                if ( result == 1 )
                {
                    this.textBoxRangeResults.AppendText( start.ToString( "N0" ) + "\r\n" );
                    this.ct++;
                    if ( this.checkBoxNC.Checked )
                        this.labelNumberCount.Text = "Number Count :\r\n" + ct.ToString("N0");
                }
                if ( start % step_r == 0 )
                    this.progressBarRange.PerformStep();
            }            
            this.rangeTestComplete();
        }

        private ulong primeTest( decimal n )
        {
            if ( n < 2 )
                return 0;
            if  ( n == 2 || n == 3 )
                return 1;
            if ( n % 2 == 0 )
                return 2;
            else
            {
                ulong end = (ulong)Math.Ceiling( Math.Sqrt( (double)n ) ) + 1;
                ulong step = ( (end/60) % 2 == 0 ? end/60 + 1 : end/60 );
                for ( ulong i = 3 ; i <= end ; i += 2 ) 
                {
                    if  ( n % i == 0 )
                        return i;
                    if ( i % step == 0 )
                        this.progressBar.PerformStep();
                }
                return 1;
            }
        }

        private bool validateInput( string s , out decimal n , out string errMsg )
        {
            n = 0m;
            errMsg = null;
            if ( s.Length == 0 )
            {
                errMsg = "\r\nENTER A NUMBER TO TEST";
                return false;
            }
            try
            {
                n = Convert.ToDecimal(s);
            }
            catch ( System.FormatException )
            {
                errMsg  = "ENTER A POSITIVE INTEGER,\r\nNOT A STRING ;)";
                return false;
            }
            catch ( System.OverflowException )
            {
                errMsg = "ENTER AN INTEGER\r\nBETWEEN 2^96 and - (2^96)";
                return false;
            }
            return true;
        }

        private void linkLabel_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("mailto:[email protected]?subject=about isPrime");
        }

        private void tabControl_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if ( this.tabControl.SelectedIndex == 0 )
            {
                this.Size = new Size( 327 , 295 );
                this.textBoxInput.Focus();
            }
            else
            {
                this.Size = new Size( 336 , 368 );
                this.textBoxRangeStart.Focus();
            }
        }

        private void buttonSave_Click(object sender, System.EventArgs e)
        {
            this.saveFileDialog.ShowDialog();
        }

        private void saveFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            StreamWriter sw = new StreamWriter( this.saveFileDialog.FileName );
            sw.Write( this.textBoxRangeResults.Text );
            sw.Close();
        }

        private void textBoxes_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text)) 
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void textBoxes_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            ((TextBox)sender).Text = e.Data.GetData(DataFormats.Text).ToString();
        }

    }
 }
Alıntı ile Cevapla
Cevapla




Saat: 13:03


Telif Hakları vBulletin® v3.8.9 Copyright ©2000 - 2024, ve
Jelsoft Enterprises Ltd.'e Aittir.
gaziantep escort bayan gaziantep escort
antalya haber sex hikayeleri Antalya Seo tesbih aresbet giriş vegasslotguncel.com herabetguncel.com ikili opsiyon bahis vegasslotyeniadresi.com vegasslotadresi.com vegasslotcanli.com getirbett.com getirbetgir.com
ankara escort ankara escort ankara escort bayan escort ankara ankara escort çankaya escort ankara otele gelen escort eryaman escort eryaman escort eryaman escort kızılay escort çankaya escort kızılay escort ankara eskort
mecidiyeköy escort

Search Engine Friendly URLs by vBSEO 3.6.0 PL2