본문 바로가기

C#/공부

C# 실력 늘리기 2일차

30~32. 1차원 배열(버블 정렬), ref, foreach: 6, 3, 8, 9, 2, 1, 4, 7, 5를 크기순으로 정렬하자.

 

정렬이 끝나면 반복을 빠져나가는 기능 추가

 

using System;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void swap(ref int a, ref int b)
        {
            int tmp = a;
            a = b;
            b = tmp;
        }
        public static void Main(String[] args)
        {
            int[] num = { 6, 3, 8, 9, 2, 1, 4, 7, 5 };
            int flag;
            for (int i = 0; i < num.Length; i++)
            {
                flag = 0;
                for (int j = 0; j < num.Length - 1; j++)
                {
                    if (num[j] > num[j + 1])
                    {
                        swap(ref num[j], ref num[j + 1]);
                        flag++;
                    }
                }
                if (flag == 0)
                    break;
                Console.Write("{0}차 정렬 결과: ", i + 1);
                foreach (int k in num)
                {
                    Console.Write("{0}   ", k);
                }
                Console.WriteLine();
            }
        }
    }
}

 

 

 

46~47. ArrayList와 IEnumerator: 서로 다른 카드 52장을 만들어보자. 

 

괜히 풀었다

 

using System;
using System.Collections;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main(String[] args){
            ArrayList list = new ArrayList();
            string []deck={"S","D","H","C"};
            string [] suit={"A","1","2","3","4","5","6","7","8","9","T","J","Q","K"};
            
            foreach(string d in deck){
                foreach(string s in suit)
                    list.Add(string.Concat(d,s));
            }

            IEnumerator iter = list.GetEnumerator();
            int i = 0;
            while(iter.MoveNext()){
                Console.Write("{0}    ",iter.Current as string);
                if ((i++ + 1) % suit.Length == 0)
                    Console.WriteLine();
            }
        }
    }
}

 

 

 

55~58. 상속(inheritance)과 protected, this() / base() 생성자, 오버라이드(override), 다형성(polymorphism): Shape를 상속한 CableCar 클래스를 만들고 시간에 따라 이동하게 만들자.

 

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

namespace Graphic
{
    public partial class Form1 : Form
    {
        Cablecar Car;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Car = new Cablecar(30, this.Height/2, Color.Black);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.TranslateTransform(0, 0);

            g.FillRectangle(new SolidBrush(Color.Wheat), 0, 0, this.Width, this.Height);

            Liner liner = new Liner(20, this.Height / 2 - 16, this.Width - 20, this.Height / 2 - 16, Color.Red);
            liner.Draw(g);
            
            Car.Draw(g);
            Car.go(10, 0);

            base.OnPaint(e);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Invalidate();
        }
    }
    public class Cablecar : Shape
    {
        Shape[] sh;
        public Cablecar(float x, float y, Color color)
        {
            sh = new Shape[5];
            sh[0] = new Rectangle(x, y, 40, 20, color);
            sh[1] = new Circle(x + 8, y - 16, 10, 10, color);
            sh[2] = new Circle(x + 24, y - 16, 10, 10, color);
            sh[3] = new Shape(x + 10, y - 16, 6, 6, color);
            sh[4] = new Shape(x + 26, y - 16, 6, 6, color);
        }
        public override void Draw(Graphics g)
        {
            foreach (Shape bus in sh)
                bus.Draw(g);
        }
        public void go(float mx, float my)
        {
            foreach (Shape bus in sh)
                bus.Move(mx, my);
        }
    }
    public class Liner : Shape
    {
        public Liner(float x, float y, float width, float height)
            : base(x, y, width, height) { }

        public Liner(float x, float y, float width, float height, Color color)
            : base(x, y, width, height, color) { }

        public override void Draw(Graphics g)
        {
            Pen xPen = new Pen(color, 1);
            g.DrawLine(xPen, new Point((int)x, (int)y), new Point((int)width, (int)y));
            xPen.Dispose();
        }
    }
    public class Shape
    {
        protected Color color;
        protected float x;
        protected float y;
        protected float width;
        protected float height;

        public Shape():this(0.0f, 0.0f, 10f, 10f, Color.Black) { }

        public Shape(float x, float y, float width, float height)
            :this(x, y, width, height, Color.Black) { }

        public Shape(float x, float y, float width, float height, Color color)
        {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.color = color;
        }
        public virtual void Draw(Graphics g)
        {
            SolidBrush axisXBrush = new SolidBrush(color);
            g.FillEllipse(axisXBrush, x, y, width, height);
        }
        public void Move(float mx, float my)
        {
            this.x += mx;
            this.y += my;
        }
    }
    public class Circle : Shape
    {
        public Circle(float x, float y, float width, float height)
            : base(x, y, width, height) { }

        public Circle(float x, float y, float width, float height, Color color)
            : base(x, y, width, height, color) { }

        public override void Draw(Graphics g)
        {
            Pen xPen = new Pen(color, 1);
            g.DrawArc(xPen, new RectangleF(x, y, width, height), 0.0f, 360.0f);
            xPen.Dispose();
        }
    }
    public class Rectangle : Shape
    {
        public Rectangle(float x, float y, float width, float height)
            : base(x, y, width, height) { }

        public Rectangle(float x, float y, float width, float height, Color color)
            : base(x, y, width, height, color) { }

        public override void Draw(Graphics g)
        {
            Pen xPen = new Pen(color, 1);
            g.DrawRectangle(xPen, x, y, width, height);
            xPen.Dispose();
        }
    }
}

 

보고 있으면 눈아프다

 

 

 

64~65. 객체지향 프로그래밍 짜보기: 메모리, 다형성, 객체지향 프로그래밍의 특징을 적용시켜 원, 사각형을 그려보자.

 

생략을 많이 해서 교재 코드와 차이가 있음

 

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

namespace Graphic
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.TranslateTransform(0, 0);

            g.FillRectangle(new SolidBrush(Color.White), 0, 0, this.Width, this.Height);

            MakeShape ms = new MakeShape(this.Width, this.Height);
            ms.Make();
            ms.PrintShape(g);

            base.OnPaint(e);
        }

        private void btnReDraw_Click(object sender, EventArgs e)
        {
            this.Invalidate();
        }

    }
    public class MakeShape
    {
        byte[,] colors ={{255,0,0},{0,255,0},{0,0,255},{255,255,0},{255,0,255},{0,255,255},
                        {0,128,0},{0,255,128},{255,0,128},{128,0,255},{255,128,0},{128,255,0},};
        List<Shape> shlist = new List<Shape>();
        Random r;
        static int SEED = 37;
        int maxWidth, maxHeight;
        public MakeShape(int maxWidth, int maxHeight)
        {
            r = new Random(SEED++ + DateTime.Now.Millisecond);
            this.maxWidth = maxWidth;
            this.maxHeight = maxHeight;
        }
        public void Make()
        {
            for (int i = 0; i < 30; i++)
            {
                Shape s = new Shape(r.Next(maxWidth), r.Next(maxHeight),
                    r.Next(1, 50), r.Next(1, 50), RGB(r.Next(0, colors.GetLength(0))));
                Circle c = new Circle(r.Next(maxWidth), r.Next(maxHeight),
                    r.Next(1, 50), r.Next(1, 50), RGB(r.Next(0, colors.GetLength(0))));
                Rectangle re = new Rectangle(r.Next(maxWidth), r.Next(maxHeight),
                    r.Next(1, 50), r.Next(1, 50), RGB(r.Next(0, colors.GetLength(0))));
                shlist.Add(s);
                shlist.Add(c);
                shlist.Add(re);
            }
        }
        private Color RGB(int m)
        {
            return Color.FromArgb(colors[m, 0], colors[m, 1], colors[m, 2]);
        }
        public void PrintShape(Graphics g){
            foreach(Shape sh in shlist){
                sh.Draw(g);
            }
        }
    }
            
    public class Shape
    {
        protected Color color;
        protected float x;
        protected float y;
        protected float width;
        protected float height;

        public Shape() : this(0.0f, 0.0f, 10f, 10f, Color.Black) { }

        public Shape(float x, float y, float width, float height)
            : this(x, y, width, height, Color.Black) { }

        public Shape(float x, float y, float width, float height, Color color)
        {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.color = color;
        }
        public virtual void Draw(Graphics g)
        {
            SolidBrush axisXBrush = new SolidBrush(color);
            g.FillEllipse(axisXBrush, x, y, width, height);
        }
        public void Move(float mx, float my)
        {
            this.x += mx;
            this.y += my;
        }
    }
    public class Circle : Shape
    {
        public Circle(float x, float y, float width, float height)
            : base(x, y, width, height) { }

        public Circle(float x, float y, float width, float height, Color color)
            : base(x, y, width, height, color) { }

        public override void Draw(Graphics g)
        {
            Pen xPen = new Pen(color, 1);
            g.DrawArc(xPen, new RectangleF(x, y, width, height), 0.0f, 360.0f);
            xPen.Dispose();
        }
    }
    public class Rectangle : Shape
    {
        public Rectangle(float x, float y, float width, float height)
            : base(x, y, width, height) { }

        public Rectangle(float x, float y, float width, float height, Color color)
            : base(x, y, width, height, color) { }

        public override void Draw(Graphics g)
        {
            Pen xPen = new Pen(color, 1);
            g.DrawRectangle(xPen, x, y, width, height);
            xPen.Dispose();
        }
    }
}

 

 

'C# > 공부' 카테고리의 다른 글

C# 실력 늘리기 4일차  (0) 2022.07.22
C# 실력 늘리기 3일차  (0) 2022.07.21
C# 실력 늘리기 1일차  (0) 2022.07.19
시리얼 통신  (0) 2022.07.11
4. 클래스와 메소드  (0) 2022.07.06