* MessageMap
- delegate
- property
(분석해보기)
using System;
class Program
{
// 델리게이트
delegate int Facto(int i1, int i2);
// 메세지맵
static MessageMap[] aMessageMap = new MessageMap[]
{
new MessageMap("더하기", add),
new MessageMap("빼기", sub),
new MessageMap("곱하기", mul),
new MessageMap("나누기", div)
};
class MessageMap
{
// 프로퍼티
public string cmd { get; set; }
public Facto Method { get; set; }
public MessageMap()
{
}
public MessageMap(string _cmd, Facto _Method)
{
cmd = _cmd;
Method = _Method;
}
}
static public int add(int T1, int T2)
{
return T1 + T2;
}
static public int mul(int T1, int T2)
{
return T1 * T2;
}
static public int sub(int T1, int T2)
{
return T1 - T2;
}
static public int div(int T1, int T2)
{
return T1 / T2;
}
static int step1(string command, int iNum1, int iNum2)
{
switch (command)
{
case "더하기":
return add(iNum1, iNum2);
case "빼기":
return sub(iNum1, iNum2);
case "곱하기":
return mul(iNum1, iNum2);
case "나누기":
return div(iNum1, iNum2);
default:
return 0;
}
}
static int step2(string command, int iNum1, int iNum2)
{
Facto aFacto;
switch (command)
{
case "더하기":
aFacto = add;
break;
case "빼기":
aFacto = sub;
break;
case "곱하기":
aFacto = mul;
break;
case "나누기":
aFacto = div;
break;
default:
return 0;
}
return aFacto(iNum1, iNum2);
}
static int step3(string command, int iNum1, int iNum2)
{
foreach (MessageMap Temp in aMessageMap)
{
if (Temp.cmd.Equals(command))
{
return Temp.Method(iNum1, iNum2);
}
}
return 0;
}
static int step4(string command, int iNum1, int iNum2)
{
foreach (MessageMap Temp in aMessageMap)
{
if (Temp.cmd.Equals(command))
{
return Temp.Method(iNum1, iNum2);
}
}
return 0;
}
// 명령어 리스트 출력
static void CmdList()
{
Console.WriteLine("사용할 수 있는 연산 리스트");
Console.WriteLine("============================");
foreach (MessageMap Temp in aMessageMap)
{
Console.WriteLine(Temp.cmd);
}
Console.WriteLine("============================");
}
// step4를 2개로 나누어서 DelegateSearch , RunCacul
// Delegate찾기
static Facto DelegateSearch(string command)
{
foreach (MessageMap Temp in aMessageMap)
{
if (Temp.cmd.Equals(command))
{
return Temp.Method;
}
}
return null;
}
// 실행
static int RunCacul(string command, int iNum1, int iNum2)
{
Facto aFacto = DelegateSearch(command);
if (null == aFacto)
{
return 0;
}
return aFacto(iNum1, iNum2);
}
static string InputCmd()
{
string command;
while (true)
{
Console.Write("수행할 연산을 한글로 입력하세요(도움말 : ?) ");
command = Console.ReadLine();
if (null == command)
{
Console.WriteLine("잘 못 입력하셨습니다");
continue;
}
if ('?' == command[0])
{
CmdList();
continue;
}
foreach (MessageMap Temp in aMessageMap)
{
if (Temp.cmd.Equals(command))
{
return command;
}
}
Console.WriteLine("잘 못 입력하셨습니다");
}
}
static void Main(string[] args)
{
Console.Write("정수1을 입력하세요 : ");
int iNum1 = Int32.Parse(Console.ReadLine());
Console.Write("정수2를 입력하세요 : ");
int iNum2 = Int32.Parse(Console.ReadLine());
string command = InputCmd();
int Result = step4(command, iNum1, iNum2);
Console.WriteLine(command + $"의 결과는 {Result}입니다.");
}
}
* 거듭제곱 추가
using System;
class Program
{
// 델리게이트
delegate int Facto(int i1, int i2);
// 메세지맵
static MessageMap[] aMessageMap = new MessageMap[]
{
new MessageMap("더하기", add),
new MessageMap("빼기", sub),
new MessageMap("곱하기", mul),
new MessageMap("나누기", div),
new MessageMap("거듭제곱", squar)
};
class MessageMap
{
// 프로퍼티
public string cmd { get; set; }
public Facto Method { get; set; }
public MessageMap()
{
}
public MessageMap(string _cmd, Facto _Method)
{
cmd = _cmd;
Method = _Method;
}
}
static public int add(int T1, int T2)
{
return T1 + T2;
}
static public int mul(int T1, int T2)
{
return T1 * T2;
}
static public int sub(int T1, int T2)
{
return T1 - T2;
}
static public int div(int T1, int T2)
{
return T1 / T2;
}
// 제곱
static public int squar(int T1, int T2)
{
int Temp = 1;
while (T2 != 0)
{
Temp = Temp * T1;
--T2;
}
return Temp;
}
static int step1(string command, int iNum1, int iNum2)
{
switch (command)
{
case "더하기":
return add(iNum1, iNum2);
case "빼기":
return sub(iNum1, iNum2);
case "곱하기":
return mul(iNum1, iNum2);
case "나누기":
return div(iNum1, iNum2);
default:
return 0;
}
}
static int step2(string command, int iNum1, int iNum2)
{
Facto aFacto;
switch (command)
{
case "더하기":
aFacto = add;
break;
case "빼기":
aFacto = sub;
break;
case "곱하기":
aFacto = mul;
break;
case "나누기":
aFacto = div;
break;
default:
return 0;
}
return aFacto(iNum1, iNum2);
}
static int step3(string command, int iNum1, int iNum2)
{
foreach (MessageMap Temp in aMessageMap)
{
if (Temp.cmd.Equals(command))
{
return Temp.Method(iNum1, iNum2);
}
}
return 0;
}
static int step4(string command, int iNum1, int iNum2)
{
foreach (MessageMap Temp in aMessageMap)
{
if (Temp.cmd.Equals(command))
{
return Temp.Method(iNum1, iNum2);
}
}
return 0;
}
// 명령어 리스트 출력
static void CmdList()
{
Console.WriteLine("사용할 수 있는 연산 리스트");
Console.WriteLine("============================");
foreach (MessageMap Temp in aMessageMap)
{
Console.WriteLine(Temp.cmd);
}
Console.WriteLine("============================");
}
// step4를 2개로 나누어서 DelegateSearch , RunCacul
// Delegate찾기
static Facto DelegateSearch(string command)
{
foreach (MessageMap Temp in aMessageMap)
{
if (Temp.cmd.Equals(command))
{
return Temp.Method;
}
}
return null;
}
// 실행
static int RunCacul(string command, int iNum1, int iNum2)
{
Facto aFacto = DelegateSearch(command);
if (null == aFacto)
{
return 0;
}
return aFacto(iNum1, iNum2);
}
static string InputCmd()
{
string command;
while (true)
{
Console.Write("수행할 연산을 한글로 입력하세요(도움말 : ?) ");
command = Console.ReadLine();
if ("" == command)
{
Console.WriteLine("잘 못 입력하셨습니다");
continue;
}
if ('?' == command[0])
{
CmdList();
continue;
}
foreach (MessageMap Temp in aMessageMap)
{
if (Temp.cmd.Equals(command))
{
return command;
}
}
Console.WriteLine("잘 못 입력하셨습니다");
}
}
static void Main(string[] args)
{
Console.Write("정수1을 입력하세요 : ");
int iNum1 = Int32.Parse(Console.ReadLine());
Console.Write("정수2를 입력하세요 : ");
int iNum2 = Int32.Parse(Console.ReadLine());
string command = InputCmd();
int Result = step4(command, iNum1, iNum2);
Console.WriteLine(command + $"의 결과는 {Result}입니다.");
}
}
* 리스트의 정렬방법
(정렬하는 기준을 메서드로 만들어줘야 한다.)
(델리게이트, 무명델리게이터)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2020._09._16_002
{
class Program
{
// 리스트의 정렬
class Product
{
public string Name { get; set; }
public int Price { get; set; }
}
// 리스트 value들을 정렬하는 기준1(가격)
static int ArraySortPrice(Product T1, Product T2)
{
return T1.Price.CompareTo(T2.Price);
// return T1.Price.CompareTo(T2.Price) * -1; > 역순정렬
// 또는 Main에서 products.Reverse(ArraySortPrice);
}
// 리스트 value들을 정렬하는 기준2(이름)
static int ArraySortName(Product T1, Product T2)
{
return T1.Name.CompareTo(T2.Name);
}
delegate int ArraySort(Product T1, Product T2);
static void Main(string[] args)
{
List<Product> products = new List<Product>()
{
new Product() { Name = "감자", Price = 500 },
new Product() { Name = "사과", Price = 700 },
new Product() { Name = "고구마", Price = 400 },
new Product() { Name = "배추", Price = 600 },
new Product() { Name = "상추", Price = 300 }
};
// 무명 델리게이터
/*products.Sort(delegate (Product T1, Product T2)
{
return T1.Price.CompareTo(T2.Price);
});*/
// 무명 델리게이터 > 람다식으로
//products.Sort(ArraySortPrice);
//products.Sort(ArraySortName);
foreach (var item in products)
{
Console.WriteLine("{0} : {1}", item.Name, item.Price);
}
}
}
}
* 온/습도 데이터를 불러와서 차트 그리기
using MySql.Data.MySqlClient;
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 _2020._09._16_003
{
public partial class Form1 : Form
{
private static string TName = "tblSensorDHT11";
private string QueryLogin = "SERVER=192.168.0.165;DATABASE=mydb;UID=root;PASSWORD=1234;";
private static string QueryTable = "SELECT * FROM " + TName + " ORDER BY TIME DESC LIMIT ";
private static string QueryTable12 = QueryTable + 12;
private static string QueryTable1 = QueryTable + 1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataSet aDataSet = new DataSet();
using (MySqlConnection aMySqlConnection = new MySqlConnection(QueryLogin))
{
aMySqlConnection.Open();
//string strQuery = QueryTable;
MySqlDataAdapter aMySqlDataAdapter = new MySqlDataAdapter(QueryTable12, aMySqlConnection);
aMySqlDataAdapter.Fill(aDataSet, TName);
}
List<int> TempData = new List<int>();
List<int> HumiData = new List<int>();
foreach (DataRow Temp in aDataSet.Tables[TName].Rows)
{
TempData.Add(Convert.ToInt32(Temp["TEMP"].ToString()));
HumiData.Add(Convert.ToInt32(Temp["HUMI"].ToString()));
}
//MessageBox.Show(TempData.Count.ToString());
chart1.Series[0].Points.DataBindY(TempData);
chart1.Series[1].Points.DataBindY(HumiData);
}
private void button1_Click(object sender, EventArgs e)
{
DataSet aDataSet = new DataSet();
using (MySqlConnection aMySqlConnection = new MySqlConnection(QueryLogin))
{
aMySqlConnection.Open();
string strQuery = QueryTable;
MySqlDataAdapter aMySqlDataAdapter = new MySqlDataAdapter(QueryTable1, aMySqlConnection);
aMySqlDataAdapter.Fill(aDataSet, TName);
}
DataRow Temp = aDataSet.Tables[TName].Rows[0];
chart1.Series[0].Points.Add(Convert.ToInt32(Temp["TEMP"].ToString()));
chart1.Series[1].Points.Add(Convert.ToInt32(Temp["HUMI"].ToString()));
if (chart1.Series[0].Points.Count > 12)
{
chart1.Series[0].Points.RemoveAt(0);
chart1.Series[1].Points.RemoveAt(0);
}
}
}
}
-2020.09.16 - 003
'C#' 카테고리의 다른 글
Excel Export, Serch, Insert, Upload - 초간단 버전(MS-SQL) (0) | 2022.01.04 |
---|---|
2020.09.17 - 온/습도 모듈 센서를 이용, 차트 작성 (0) | 2020.09.17 |
2020.09.15 - c# review (0) | 2020.09.15 |
2020.09.11 - 온/습도 측정하기 / 리눅스 (0) | 2020.09.11 |
2020.09.10 - (0) | 2020.09.10 |