본문 바로가기

C#/공부

C# 실력 늘리기 5일차

129~130. 데이터베이스와 테이블 만들기, ADO.NET 사용하기: StudentDataBase와 Student 테이블을 만들고 학생 목록을 보여주자.

 

생성 과정이 많이 복잡해서 결과만 올림

 

테이블 정의

 

 

쿼리 (다중 INSERT 사용 불가)

 

INSERT INTO Student VALUES
('ID1', 'NAME1', '1111-1111', 'AD1', 'N', GETDATE());
INSERT INTO Student VALUES
('ID2', 'NAME2', '2222-2222', 'AD2', 'N', GETDATE());
INSERT INTO Student VALUES
('ID3', 'NAME3', '3333-3333', 'AD3', 'N', GETDATE());
INSERT INTO Student VALUES
('ID4', 'NAME4', '4444-4444', 'AD4', 'N', GETDATE());

 

테이블 데이터

 

 

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;

namespace Database
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void btnShowAll_Click(object sender, EventArgs e)
        {
            // TODO: 이 코드는 데이터를 'studentDataSet.Student' 테이블에 로드합니다. 필요한 경우 이 코드를 이동하거나 제거할 수 있습니다.
            this.studentTableAdapter.Fill(this.studentDataSet.Student);
        }
    }
}

 

 

 

133. 로또 만들기: 로또를 만들어 보자.

 

교재에서 아이디어만 가져온, 완전 다른 프로그램이다.(콘솔 -> 윈폼, 배열사용 -> 리스트사용, 생략 다수)

 

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

namespace Lotto
{
    public partial class Form1 : Form
    {
        private List<int> lot;
        int result;
        Lotto lotto;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Ball.Visible=false;
            lbBall.Visible=false;
            lotto = new Lotto();
            lot = new List<int>();
            btnChoose.Enabled = true;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Ball.Visible=false;
            lbBall.Visible=false;
            lbNum.Text="";
            lot.Clear();
            btnChoose.Enabled = true;
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            Ball.Visible = false;
            lbBall.Visible = false;
            result = lotto.choose(lot);
            lot.Add(result);
            lbBall.Text = result.ToString();
            lbNum.Text = lbNum.Text + result.ToString()+" ";
            Thread.Sleep(500);
            Ball.Visible = true;
            lbBall.Visible = true;
            if (lot.Count >= 6)
                btnChoose.Enabled = false;
        }
    }
    public class Lotto
    {
        static int SEED=17;
        Random r;
        public Lotto()
        {
            r = new Random(SEED + System.DateTime.Now.Millisecond);
        }
        public int rand(){
            return r.Next(45)+1;
        }
        public int choose(List<int> list)
        {
            int result;
            while (true)
            {
                result = rand();
                if (list.Contains(result))
                    continue;
                else
                    return result;
            }
        }
    }
}

 

 

 

135. 숫자 섞기(shuffle): 배열의 위치를 변경해서 숫자를 섞자.

 

using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main(String[] args)
        {
            int[] cards = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            Shuffle<int> s=new Shuffle<int>();
            Console.WriteLine("Origin");
            s.print(cards);
            Console.WriteLine("Shuffled");
            s.shuffle(cards);
            s.print(cards);
            Console.WriteLine("Reversed");
            s.reverse(cards);
            s.print(cards);
        }
    }
    public class Shuffle<T>
    {
        private static int SEED = 17;
        Random r;
        public Shuffle()
        {
            r = new Random(SEED + System.DateTime.Now.Millisecond);
        }
        public void shuffle(T[] cards)
        {
            int count = cards.Length;
            for (int i = 0; i < count; i++)
            {
                T t = cards[i];
                int next = r.Next(count);
                cards[i] = cards[next];
                cards[next] = t;
            }
        }
        public void reverse(T[] cards)
        {
            int count = cards.Length;
            for (int i = 0; i < count/2; i++)
            {
                T t = cards[i];
                cards[i] = cards[count - 1 - i];
                cards[count - 1 - i] = t;
            }
        }
        public void print(T[] cards)
        {
            foreach (T t in cards)
                Console.Write(t+" ");
            Console.WriteLine();
        }
    }
}

 

 

 

137. 소인수 분해하기: 788059를 소인수 분해하자.

 

알고리즘이 맘에 들어서 함

 

using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main(String[] args)
        {
            PrimeNum prime = new PrimeNum();
            while (true)
            {
                Console.Write("수를 입력하세요(0 입력시 종료): ");
                int num = Int32.Parse(Console.ReadLine());
                if (num == 0)
                    break;
                prime.Factorization(num);
            }
        }
    }
    public class PrimeNum
    {
        public void Factorization(int num)
        {
            int i = 2;
            while (num != 1)
            {
                if (num % i == 0)
                {
                    if (num / i == 1)   //i==num
                        Console.WriteLine(i);
                    else
                        Console.Write(i + " * ");
                    num /= i;
                }
                else
                    i++;
            }
        }
    }
}

 

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

C# 실력 늘리기 7일차  (0) 2022.07.27
C# 실력 늘리기 6일차  (0) 2022.07.26
C# 실력 늘리기 4일차  (0) 2022.07.22
C# 실력 늘리기 3일차  (0) 2022.07.21
C# 실력 늘리기 2일차  (0) 2022.07.20