* 2020.08.20-007에 이어서
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._08._20_007
{
public partial class Form1 : Form
{
int[] intArray;
int Score; // 카운트 변수
public Form1()
{
InitializeComponent();
}
private void Init()
{
Random aRandom = new Random();
intArray = new int[3] { 100, 100, 100 };
for (int Count = 0; Count < intArray.Length; ++Count)
{
int iTemp = aRandom.Next(0, 10);
int Count2;
for (Count2 = 0; Count2 < intArray.Length; ++Count2)
{
if (iTemp == intArray[Count2])
{
break;
}
}
if (Count2 < intArray.Length)
{
--Count;
continue;
}
intArray[Count] = iTemp;
}
lbRand1.Text = intArray[0].ToString();
lbRand2.Text = intArray[1].ToString();
lbRand3.Text = intArray[2].ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
Init();
}
private void button1_Click(object sender, EventArgs e)
{
int[] InputArray = new int[intArray.Length];
int strikeCount = 0;
int ballCount = 0;
InputArray[0] = Convert.ToInt32(txtinput1.Text);
InputArray[1] = int.Parse(txtinput2.Text);
InputArray[2] = int.Parse(txtinput3.Text);
// 스트라이크, 볼 판정
/*if (InputArray[0] == intArray[0])
{
MessageBox.Show("스트라이크");
}
if (InputArray[0] == intArray[1])
{
MessageBox.Show("볼");
}
if (InputArray[0] == intArray[2])
{
MessageBox.Show("볼");
}*/
for (int Count1 = 0; Count1 < intArray.Length; ++Count1)
{
for (int Count2 = 0; Count2 < intArray.Length; ++Count2)
{ // 숫자 비교 if문
if (InputArray[Count1] == intArray[Count2])
{ // 위치 비교 if문
if (Count1 == Count2)
{
//MessageBox.Show("스트라이크");
++strikeCount;
}
else
{
//MessageBox.Show("볼");
++ballCount;
}
break;
}
}
}
// 스트라이크, 볼 카운트 나타내기
lbStrike.Text = strikeCount.ToString();
lbBall.Text = ballCount.ToString();
// 카운트 증가 나타내기
++Score;
lbCount.Text = Score.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
Init();
lbStrike.Text = "0";
lbBall.Text = "0";
txtinput1.Clear();
txtinput2.Clear();
txtinput3.Clear();
lbCount.Text = "0";
}
}
}
> 조금 더 디테일한 예외처리가 필요하지만 여기서 마친다.
* List<T> 컬렉션
: 배열이 몇 개 생성되는지 알 필요가 없다. 왜냐하면 알아서 여분의 배열 빈 공간을 생성해준다.
List의 장점이다.
- 소스 코드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Security;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._21_001
{
class Program
{
static void Main(string[] args)
{
// int형 List 객체 생성
List<int> a = new List<int>();
Random r = new Random();
PrintValues(a);
for (int i = 0; i < 10; i++)
{
a.Add(r.Next(100));
}
PrintValues(a);
a.Sort();
PrintValues(a);
a.RemoveAt(3);
PrintValues(a);
}
// a = printValues의 지역변수이다. 위의 a와 다른 a다.
private static void PrintValues(List<int> a)
{
Console.WriteLine("Print Values in List<int>");
// 배열 속 사용되고 있는 수 : Count
Console.WriteLine(" Count = {0}", a.Count);
// 데이터의 크기 : Capacity
Console.WriteLine(" Capacity = {0}", a.Capacity);
foreach (var item in a)
{
Console.WriteLine(" {0}", item);
}
Console.WriteLine();
}
}
}
- 결과
* List<T>와 배열의 정렬
- 소스 코드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._21_002
{
class Program
{
static void Main(string[] args)
{
// 리스트 배열 객체 생성
List<string> lstNames = new List<string>();
// 리스트에 값 넣기(Add 메서드를 사용한다)
lstNames.Add("dog");
lstNames.Add("cow");
lstNames.Add("rabbit");
lstNames.Add("goat");
lstNames.Add("sheep");
// 리스트 정렬(sort메서드 사용)
lstNames.Sort();
// 리스트의 값들 출력하기
foreach (var item in lstNames)
{
Console.Write(item + " ");
}
Console.WriteLine();
// 배열 객체 생성
string[] arrNames = new string[5];
// 배열에 값 넣기
arrNames[0] = "dog";
arrNames[1] = "cow";
arrNames[2] = "rabbit";
arrNames[3] = "goat";
arrNames[4] = "sheep";
// 배열 정렬
Array.Sort(arrNames);
// 배열 값들 출력하기
foreach (var item in arrNames)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
}
}
- 결과
* IComparable 인터페이스를 이용한 객체의 정렬
IComparable
* 콤보박스를 이용한 식당 리스트의 추가, 삭제
- Form1.cs[디자인]
- Form1.cs
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._08._21_004
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
label3.Text = "이번 주 모임장소는 : " + cb.SelectedItem.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text != "")
{
comboBox1.Items.Add(comboBox1.Text);
label3.Text = comboBox1.Text + " 추가됨 ! ";
}
}
private void button2_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex >= 0)
{
label3.Text = comboBox1.SelectedItem.ToString() + " 삭제됨 ! ";
comboBox1.Items.Remove(comboBox1.SelectedItem);
}
}
}
}
* 콤보박스 항목을 추가 or 제거
- Form1.cs[디자인]
- Form1.cs
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._08._21_005
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Items.Add(textBox1.Text);
textBox1.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
if(comboBox1.Items.Contains(textBox2.Text) == true)
{
comboBox1.Items.Remove(textBox2.Text);
textBox2.Clear();
}
}
}
}
* DataGridView
- 도서관 관리 (텔레그램 사진을 보고 한 번 해보기!)
- Form1.cs[디자인]
-User.cs
- Form1.cs
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._08._21_007_Book_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//bookBindingSource.Add(new Book() { Isbn = 9791156641452, Name = "IT CookBook 실전에 강한 PLC", Publisher = "한빛아카데미", Page = 384 , UserId = 0, UserName = "",
// BorrowedAt = , isBorrowed = } ) ;
userBindingSource.Add(new User() { Id = 1, Name = "장국빈" });
}
}
}
*LINQ
(Language -INtegrated Query) - 데이터 질의 기능
데이터베이스에서 사용하는 쿼리문을 C#에 도입한 것!
- 소스 코드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace _2020._08._21_008
{
class Program
{
// 리스트 배열들의 값들을 보여주는 메서드
static void Print(List<int> aList)
{
foreach (var Temp in aList)
{
Console.Write($"{Temp} ");
}
Console.WriteLine();
}
static void Main1(string[] args)
{
List<int> aList1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<int> aList2 = new List<int>();
Print(aList1);
foreach (var Temp in aList1)
{ // aList1의 값들을 aList2에 집어넣는 코드
aList2.Add(Temp);
}
Print(aList2);
}
// LINQ 사용 해보기 (DB에서 사용되는 쿼리를 사용하는 것)
static void Main(string[] args)
{
List<int> aList1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<int> aList2;
var Fact = from Temp in aList1 select Temp;
aList2 = Fact.ToList<int>();
//aList2 = (from Temp in aList1 select Temp).ToList<int>();
Print(aList2);
}
}
}
- Linq 사용 소스 코드 2
static void Main(string[] args)
{
List<int> data = new List<int> { 123, 45, 12, 89, 456, 1, 4, 74, 46 };
List<int> lstSortedEven = new List<int>();
foreach (var item in data)
{
if (item % 2 == 0)
{
lstSortedEven.Add(item);
}
}
lstSortedEven.Sort();
Console.WriteLine("Using foreach : ");
foreach (var item in lstSortedEven)
{
Console.Write(item + " ");
}
Console.WriteLine();
var sortedEven = from item in data
where item % 2 == 0
orderby item
select item;
Console.WriteLine("\nUsing Linq : ");
foreach (var item in sortedEven)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
*Linq를 이용한 조건 검색과 정렬
- 소스 코드
static void Main(string[] args)
{
List<int> data = new List<int> { 123, 45, 12, 89, 456, 1, 4, 74, 46 };
Print("data : ", data);
var lstEven = from item in data
where (item > 20 && item % 2 == 0)
orderby item descending
select item;
Print("20보다 큰 짝수 검색결과 : ", lstEven);
}
private static void Print(string s, IEnumerable<int> data)
{
Console.WriteLine(s);
foreach (var i in data)
{
Console.Write(" " + i);
}
Console.WriteLine();
}
- 결과
'C#' 카테고리의 다른 글
2020.08.25(화) - C# 윈폼 (0) | 2020.08.25 |
---|---|
2020.08.24(월) - C# 야구게임 다듬기 (0) | 2020.08.24 |
2020.08.20(목) - C# 윈폼 & C# (0) | 2020.08.20 |
2020.08.19(수) - C# & 윈폼 (0) | 2020.08.19 |
2020.08.18(화) - C# 메서드/클래스 제너릭 & C# 윈폼만들기 (0) | 2020.08.18 |