2021. 1. 25. 08:21ㆍ개발/C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Diagnostics;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
namespace MenuChoice11 {
class Program {
public static void ChoiceStart(string data) {
Console.WriteLine(Environment.NewLine);
Console.WriteLine(" ---------------------------- " + Environment.NewLine);
Console.WriteLine("먹은 음식의 빈도를 분석합니다. " + Environment.NewLine);
Console.WriteLine(" ---------------------------- " + Environment.NewLine);
if (File.Exists(@"C:\Users\inwoo\Desktop\iwhwang-book-dynamic.xlsx")) {
Console.WriteLine("데이터베이스가 있습니다. 이전에 먹은 메뉴를 참고하여 분석합니다. ");
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Users\inwoo\Desktop\iwhwang-book-dynamic.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
xlWorksheet.Cells[6, 1] = data;
Excel.Range xlRange = xlWorksheet.UsedRange;
xlWorkbook.Save();
xlWorkbook.Application.ActiveWorkbook.Saved = true; // 파일을 읽어와서 저장하고 끌때 저장할지 물어보는 창을 띄우는지 마는지
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
string[] result = new string[10];
string result1;
Console.WriteLine(Environment.NewLine);
for (int i = 1; i <= rowCount; i++) {
if (xlRange.Cells[i, 1] != null && xlRange.Cells[i, 1].Value2 != null) {
//Console.Write(xlRange.Cells[i, j].Value2.ToString() + " ");
result[i - 1] = xlRange.Cells[i, 1].Value2.ToString();
}
}
Console.WriteLine();
xlWorkbook.Close();
xlWorkbook = null;
if (xlApp != null) {
Process[] pProcess;
pProcess = Process.GetProcessesByName("Excel");
pProcess[0].Kill();
}
Console.WriteLine(" ---------------------------- ");
Console.WriteLine("먹은 음식 리스트");
Console.WriteLine(" ---------------------------- " + Environment.NewLine);
foreach (string x in result) {
if (x != null) {
Console.WriteLine(x);
}
}
int bigcnt = 0;
string bigmenu = "";
for (int i = 0; i < result.Length; i++) {
if (result[i] != null) {
int cnt = 0;
for (int j = 0; j < result.Length; j++) {
if (result[i] == result[j]) {
cnt++;
}
}
if (cnt > bigcnt) {
bigcnt = cnt;
bigmenu = result[i];
}
}
}
Console.WriteLine(" ------------------------------------------------------- " + Environment.NewLine);
Console.WriteLine("최근 가장 많이 드신 메뉴는 {0}이며, 총 {1}회 드셨습니다. ", bigmenu, bigcnt);
Console.WriteLine();
Console.WriteLine(" ------------------------------------------------------- " + Environment.NewLine);
Console.WriteLine(Environment.NewLine);
while (true) {
Console.WriteLine(" --------------------------------------------------------------------------------------- " + Environment.NewLine);
Console.WriteLine("{0}를 드시겠습니까? 드시려면 1번을 눌러주시고 랜덤으로 다시 선택하려면 2번을 눌러주세요. ", bigmenu);
Console.WriteLine();
Console.WriteLine(" --------------------------------------------------------------------------------------- " + Environment.NewLine);
result1 = Console.ReadLine();
if (result1 == "1") {
// 셀레니움 코드
Console.WriteLine(" -------------------------------------------------------------- " + Environment.NewLine);
Console.WriteLine("웹페이지에서 해당 메뉴를 검색하여 이미지를 찾습니다." + Environment.NewLine);
Console.WriteLine(" -------------------------------------------------------------- " + Environment.NewLine);
IWebDriver handler = new ChromeDriver();
handler.Url = "http://www.google.co.kr";
handler.FindElement(By.Name("q")).SendKeys(bigmenu);
handler.FindElement(By.XPath("//*[@id=\"tsf\"]/div[2]/div/div[3]/center/input[1]")).Click();
handler.FindElement(By.XPath("//*[@id=\"hdtb-msb-vis\"]/div[2]/a")).Click();
break;
}
else if (result1 == "2") {
string NoMenu = "";
Console.WriteLine(" ----------------------------------------- " + Environment.NewLine);
Console.WriteLine("랜덤으로 메뉴를 골라드립니다. ");
Console.WriteLine("혹시 드시지 않는 메뉴가 있으면 입력해주세요." + Environment.NewLine);
Console.WriteLine(" ----------------------------------------- ");
NoMenu = Console.ReadLine();
int result2 = 0;
List<string> menu = new List<string>() { "한식", "중식", "일식", "김치찌개", "된장찌개", "피자", "짜장면", "햄버거", "국수", "냉면" };
menu.Remove(NoMenu);
Random random = new Random();
result2 = random.Next(0, menu.Count - 1);
Console.WriteLine(" --------------------------------------- " + Environment.NewLine);
Console.WriteLine("오늘의 메뉴는 {0}입니다.", menu[result2]);
Console.ReadLine();
break;
}
else
Console.WriteLine("잘못 선택하셨습니다.");
}
}
else {
Console.WriteLine("데이터베이스가 없습니다. 새로운 데이터베이스를 생성합니다.");
Excel.Application xlApp = new Excel.Application();
xlApp.Workbooks.Add();
Excel._Worksheet workSheet = xlApp.ActiveSheet;
//workSheet.Cells[DateTime.Now.Day, 1] = data; // 셀의 어느 부분에 데이터를 표시할 것인가
workSheet.SaveAs(@"C:\Users\inwoo\Desktop\iwhwang-book-dynamic.xlsx");
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Users\inwoo\Desktop\iwhwang-book-dynamic.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
xlWorksheet.Cells[DateTime.Now.Day, 1] = data;
Excel.Range xlRange = xlWorksheet.UsedRange;
xlWorkbook.Save();
xlWorkbook.Application.ActiveWorkbook.Saved = true; // 파일을 읽어와서 저장하고 끌때 저장할지 물어보는 창을 띄우는지 마는지
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
string[] result = new string[10];
string result1;
Console.WriteLine(Environment.NewLine);
for (int i = 1; i <= rowCount; i++) {
if (xlRange.Cells[i, 1] != null && xlRange.Cells[i, 1].Value2 != null) {
//Console.Write(xlRange.Cells[i, j].Value2.ToString() + " ");
result[i - 1] = xlRange.Cells[i, 1].Value2.ToString();
}
}
Console.WriteLine(result[0].ToString());
xlWorkbook.Close();
xlWorkbook = null;
if (xlApp != null) {
Process[] pProcess;
pProcess = Process.GetProcessesByName("Excel");
pProcess[0].Kill();
}
Console.WriteLine(" ---------------------------- ");
Console.WriteLine("먹은 음식 리스트");
Console.WriteLine(" ---------------------------- " + Environment.NewLine);
foreach (string x in result) {
if (x != null) {
Console.WriteLine(x);
}
}
int bigcnt = 0;
string bigmenu = "";
for (int i = 0; i < 10; i++) {
if (result[i] != null) {
int cnt = 0;
for (int j = 0; j < 10; j++) {
if (result[i] == result[j]) {
cnt++;
}
}
if (cnt > bigcnt) {
bigcnt = cnt;
bigmenu = result[i];
}
}
}
Console.WriteLine(" ------------------------------------------------------- " + Environment.NewLine);
Console.WriteLine("최근 가장 많이 드신 메뉴는 {0}이며, 총 {1}회 드셨습니다. ", bigmenu, bigcnt);
Console.WriteLine();
Console.WriteLine(" ------------------------------------------------------- " + Environment.NewLine);
Console.WriteLine(Environment.NewLine);
while (true) {
Console.WriteLine(" --------------------------------------------------------------------------------------- " + Environment.NewLine);
Console.WriteLine("{0}를 드시겠습니까? 드시려면 1번을 눌러주시고 랜덤으로 다시 선택하려면 2번을 눌러주세요. ", bigmenu);
Console.WriteLine();
Console.WriteLine(" --------------------------------------------------------------------------------------- " + Environment.NewLine);
result1 = Console.ReadLine();
if (result1 == "1") {
// 셀레니움 코드
Console.WriteLine(" -------------------------------------------------------------- " + Environment.NewLine);
Console.WriteLine("웹페이지에서 해당 메뉴를 검색하여 이미지를 찾습니다." + Environment.NewLine);
Console.WriteLine(" -------------------------------------------------------------- " + Environment.NewLine);
IWebDriver handler = new ChromeDriver();
handler.Url = "http://www.google.co.kr";
handler.FindElement(By.Name("q")).SendKeys(bigmenu);
handler.FindElement(By.XPath("//*[@id=\"tsf\"]/div[2]/div/div[3]/center/input[1]")).Click();
handler.FindElement(By.XPath("//*[@id=\"hdtb-msb-vis\"]/div[2]/a")).Click();
break;
}
else if (result1 == "2") {
string NoMenu = "";
Console.WriteLine(" ----------------------------------------- " + Environment.NewLine);
Console.WriteLine("랜덤으로 메뉴를 골라드립니다. ");
Console.WriteLine("혹시 드시지 않는 메뉴가 있으면 입력해주세요." + Environment.NewLine);
Console.WriteLine(" ----------------------------------------- ");
NoMenu = Console.ReadLine();
int result2 = 0;
List<string> menu = new List<string>() { "한식", "중식", "일식", "김치찌개", "된장찌개", "피자", "짜장면", "햄버거", "국수", "냉면" };
menu.Remove(NoMenu);
Random random = new Random();
result2 = random.Next(1, menu.Count);
Console.WriteLine(" --------------------------------------- " + Environment.NewLine);
Console.WriteLine("오늘의 메뉴는 {0}입니다.", menu[result2]);
Console.ReadLine();
break;
}
else
Console.WriteLine("잘못 선택하셨습니다.");
}
}
}
static void Main(string[] args) {
Console.WriteLine("먹은 음식을 입력하세요. ");
string array = Console.ReadLine();
Console.WriteLine("Creating Excel document in new way...");
ChoiceStart(array);
}
}
'개발 > C#' 카테고리의 다른 글
# 여러가지 프로그램을 한개 창에서 선택하여 실행시키는 프로그램 + (CMD 실행 기능) (0) | 2021.01.25 |
---|---|
# 구독자 조회 프로그램 예제 (0) | 2021.01.25 |
# 알람 시계 만들기 (0) | 2021.01.25 |
# 콘솔 창 숨기는 방법 (0) | 2021.01.25 |
# 네이버 크롤러 예제 코드 (0) | 2021.01.25 |