Developersland

Teknoloji ve Yazılım Blogu

  • Yazıtipi boyutunu arttır
  • Varsayılan yazıtipi boyutu
  • Yazıtipi boyutunu azaltır

Determine whether the string of numbers graphical or not

graph algorthmI write a program with C# to determine whether string of numbers is graphical or not. There is a textbox where you must enter numbers. Don't forgot to seperate this numbers with commas.Then you can press on run button and see drawing of graph step by step or you can press to show button and see the final graph.



Download Source Code (50.7 KB)


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        List<int> sayilar = new List<int>();
        List<CizimSayilar> sayilar2 = new List<CizimSayilar>();
        System.Drawing.Graphics graphicsObj;
        Font myFont = new System.Drawing.Font("Helvetica", 28, FontStyle.Regular);
        Brush myBrush = new SolidBrush(System.Drawing.Color.DarkSlateGray);
        Pen myPen = new Pen(System.Drawing.Color.Black, 5);

        public Form1()
        {
            InitializeComponent();
        }

        private static int compareCizimSayilar(CizimSayilar a, CizimSayilar b)
        {
            if (a.deger == b.deger)
            {
                return 0;
            }
            else if (a.deger > b.deger)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String[] rakamlar = textBox1.Text.Split(',');
            foreach (string s in rakamlar)
            {
                sayilar.Add(Convert.ToInt32(s));
            }

            graphicsObj = this.CreateGraphics();
            int yatay = 100;
            int dikey = 100;

           
            for (int i = 1; i <= sayilar.Count; i++)
            {
                Rectangle myRectangle = new Rectangle(yatay, dikey, 5, 5);
                graphicsObj.DrawString(i.ToString(), myFont, myBrush, yatay, dikey);
                Point p = new Point(yatay, dikey);
                CizimSayilar cs = new CizimSayilar(p.X,p.Y,sayilar[i-1],i);
                sayilar2.Add(cs);
               
                yatay += 100;

                graphicsObj.DrawEllipse(myPen, myRectangle);

                if ((i % 3) == 0)
                {
                    dikey += 100;
                    yatay = 100;
                }

                if ((i % 2) == 0)
                {
                    dikey -= 20;
                }
            }
           
            bool sonuc = true;

            while (sayilar2.Count != 0)
            {
                sayilar2.Sort(compareCizimSayilar);
                sayilar2.Reverse();

                string temp = "";
                foreach (CizimSayilar c in sayilar2)
                {
                    temp += c.deger.ToString();
                }
                MessageBox.Show(temp);

                foreach (CizimSayilar sayi in sayilar2)
                {
                    if (sayi.deger < 0)
                    {
                        sonuc = false;
                        break;
                    }
                }

                if (hepsiSifir(sayilar2))
                {
                    break;
                }
                else
                {
                    if (sayilar2[0].deger > sayilar2.Count)
                    {
                        sonuc = false;
                        break;
                    }

                    for(int t=1;t<=sayilar2[0].deger;t++)
                    {
                       
                            MessageBox.Show("From " + sayilar2[0].numarasi + "\nTo " + sayilar2[t].numarasi);
                            graphicsObj.DrawLine(myPen, sayilar2[0].p, sayilar2[t].p);
                            myPen.Color = GetRandomColor();
                       
                    }

                    for (int i = 1; i <= sayilar2[0].deger; i++)
                    {
                        sayilar2[i].deger--;
                    }
                   
                    sayilar2.RemoveAt(0);
                }
            }

            if (sonuc)
            {
                MessageBox.Show("Doğru sonuç!");
                label1.Visible = false;
            }
            else
            {
                MessageBox.Show("Yanlış sonuç!");
                label1.Visible = true;
                graphicsObj.Clear(Color.Red);

            }
            sayilar2.Clear();
            sayilar.Clear();

        }
        Random random=new Random();
        private Color GetRandomColor()
        {
            return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
        }

        private bool hepsiSifir(List<CizimSayilar> liste)
        {
            bool a = true;

            foreach (CizimSayilar sayi in liste)
            {
                if (sayi.deger != 0)
                {
                    a = false;
                }
            }

            return a;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            String[] rakamlar = textBox1.Text.Split(',');
            foreach (string s in rakamlar)
            {
                sayilar.Add(Convert.ToInt32(s));
            }

            graphicsObj = this.CreateGraphics();
            int yatay = 100;
            int dikey = 100;


            for (int i = 1; i <= sayilar.Count; i++)
            {
                Rectangle myRectangle = new Rectangle(yatay, dikey, 5, 5);
                graphicsObj.DrawString(i.ToString(), myFont, myBrush, yatay, dikey);
                Point p = new Point(yatay, dikey);
                CizimSayilar cs = new CizimSayilar(p.X, p.Y, sayilar[i - 1], i);
                sayilar2.Add(cs);

                yatay += 100;

                graphicsObj.DrawEllipse(myPen, myRectangle);

                if ((i % 3) == 0)
                {
                    dikey += 100;
                    yatay = 100;
                }

                if ((i % 2) == 0)
                {
                    dikey -= 20;
                }
            }

            bool sonuc = true;

            while (sayilar2.Count != 0)
            {
                sayilar2.Sort(compareCizimSayilar);
                sayilar2.Reverse();

                string temp = "";
                foreach (CizimSayilar c in sayilar2)
                {
                    temp += c.deger.ToString();
                }
               
                foreach (CizimSayilar sayi in sayilar2)
                {
                    if (sayi.deger < 0)
                    {
                        sonuc = false;
                        break;
                    }
                }

                if (hepsiSifir(sayilar2))
                {
                    break;
                }
                else
                {
                    if (sayilar2[0].deger > sayilar2.Count)
                    {
                        sonuc = false;
                        break;
                    }

                    for (int t = 1; t <= sayilar2[0].deger; t++)
                    {
                        graphicsObj.DrawLine(myPen, sayilar2[0].p, sayilar2[t].p);
                        myPen.Color = GetRandomColor();

                    }

                    for (int i = 1; i <= sayilar2[0].deger; i++)
                    {
                        sayilar2[i].deger--;
                    }

                    sayilar2.RemoveAt(0);
                }
            }

            if (sonuc)
            {
                label1.Visible = false;
            }
            else
            {
                label1.Visible = true;
            }
            sayilar2.Clear();
            sayilar.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            timer1.Stop();
        }
    }
}
--------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace WindowsFormsApplication4
{
    class CizimSayilar
    {
        public CizimSayilar(int x, int y, int number,int num)
        {
            p.X = x;
            p.Y = y;
            deger = number;
            numarasi = num;
        }
        public Point p;
        public int deger;
        public int numarasi;
    }
}



 

Yorum ekle


Güvenlik kodu
Yenile