C# PGM) 로또 중복 번호 찾기

guk (Guk)·2021년 10월 29일
0

C Sharp PGM

목록 보기
1/1

엑셀로 로또 당첨 번호를 불러와서 충복된 번호가 나온 적이 있는지 확인하는 프로그램

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.IO;

namespace ConsoleApp_study
{

    class study1
    {
        static Excel.Application excelApp = null; 
        static Excel.Workbook workBook = null; 
        static Excel.Worksheet workSheet = null; 
        static void Main(string[] args)
        {
            string[] num = new string[1000];//각 번호가 들어갈 변수
            string[] num2 = new string[8];
            int i = 0;
            int c = 0;

            try
            {
                string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // 바탕화면 경로
                string path = Path.Combine(desktopPath, "lotto.xlsx"); // 엑셀 파일 저장 경로
                excelApp = new Excel.Application(); // 엑셀 어플리케이션 생성
                workBook = excelApp.Workbooks.Open(path); // 워크북 열기
                workSheet = workBook.Worksheets.get_Item(1) as Excel.Worksheet; // 엑셀 첫번째 워크시트 가져오기
                Excel.Range range = workSheet.UsedRange; // 사용중인 셀 범위를 가져오기

                for (int row = 4; row <= range.Rows.Count; row++) // 가져온 열 만큼 반복 세로
                {
                    for (int column = 2; column <= 9; column++) // 가져온 행 만큼 반복 가로
                    {
                        num2[i] = Convert.ToString((range.Cells[row, column]).Value2);
                        if (i < 7)
                        {
                            i++;
                        }
                        else
                        {
                            i = 0;
                        }
                    }
                    num[c] = num2[0] + "회 " + num2[1] + ", " + num2[2] + ", " + num2[3] + ", " + num2[4] + ", " + num2[5] + ", " + num2[6] + " 보너스 " + num2[7];
                    c++;
                }

                string sub1, sub2;//번호를 비교하기 위한 저장 변수
                int index, bo_index;//회차와 보너스 번호를 지우기 위한 변수

                for (i = 0; i < 985; i++)//각 로또번호 비교하기 위한 반복문
                {
                    index = Convert.ToInt32(num[i].IndexOf("회"));
                    bo_index = Convert.ToInt32(num[i].IndexOf("보"));
                    sub1 = num[i].Substring(index + 2, bo_index - 5);//회차와 보너스 번호 지우기

                    for (int b = i + 1; b < 985; b++)
                    {
                        index = Convert.ToInt32(num[b].IndexOf("회"));
                        bo_index = Convert.ToInt32(num[b].IndexOf("보"));
                        sub2 = num[b].Substring(index + 2, bo_index - 5);//회차와 보너스 번호 지우기

                        if (sub1 == sub2)//번호를 비교해서 동일하면 출력
                        {
                            Console.WriteLine(num[i]);
                            Console.WriteLine(num[b]);
                        }
                    }
                }
                workBook.Close(true); // 워크북 닫기
                excelApp.Quit(); // 엑셀 어플리케이션 종료
            } 
            finally 
            {   ReleaseObject(workSheet); 
                ReleaseObject(workBook); 
                ReleaseObject(excelApp); 
            } 
        } 
        static void ReleaseObject(object obj)
        { 
            try 
            { 
                if (obj != null) 
                { 
                    Marshal.ReleaseComObject(obj); // 액셀 객체 해제
                    obj = null; 
                } 
            } catch (Exception ex) 
            { 
                obj = null; 
                throw ex; 
            } finally 
            { 
                GC.Collect(); // 가비지 수집
            } 
        } 
    }
}
profile
개발자 블로그 및 이것저것

0개의 댓글