73~74. 기본형 캐스팅을 이용한 윈도우 적금 만들기
결과 화면에는 연복리라 되어 있는데 코드는 월복리이고 초기값 기준으로만 계산되는게 아니라 월마다 10만원씩 추가되는 이상한 조건도 추가되어 있는 등 오류가 너무 많아 그냥 초기값에 연복리로 프로그래밍함
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 Bank
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalc_Click(object sender, EventArgs e)
{
this.lbResult.Text = "" + Calc(Decimal.ToDouble(this.nudAmount.Value),
Decimal.ToDouble(this.nudRate.Value), Decimal.ToInt32(this.nudYear.Value)).ToString();
}
private void btnCancle_Click(object sender, EventArgs e)
{
this.nudAmount.Value = (decimal)(double)200000;
this.nudRate.Value = (decimal)4.5M;
this.nudYear.Value = (decimal)2M;
this.lbResult.Text = "";
}
public static int Calc(double a, double r, int n)
{
double result = a;
double tmp = a;
for (int i = 0; i < n; i++)
{
result*=(100.0 + r)/100.0;
}
return (int)result;
}
}
}
81~83. IO 이해하기: 파일 읽기, 파일 쓰기, using 사용하기
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(String[] args)
{
FileIO fi = new FileIO();
fi.FileWrite("../../test.txt");
fi.FileRead("../../test.txt");
}
}
public class FileIO
{
public void FileRead(string fname)
{
if (!File.Exists(fname))
{
Console.WriteLine("{0}이 없습니다.", fname);
return;
}
using (FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fs, Encoding.Default))
{
string message = string.Empty;
while ((message = sr.ReadLine()) != null)
{
Console.WriteLine(message);
}
}
}
}
public void FileWrite(string fname){
using (FileStream fs = new FileStream(fname, FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
{
string message = string.Empty;
while ((message = Console.ReadLine()) != null) //ctrl+c 로 종료
{
sw.WriteLine(message);
sw.Flush();
}
}
}
}
}
}
using 구문은 IO를 사용한 다음 연결을 자동으로 닫기 때문에 쓰기와 읽기를 따로 실행해야 함
84~85. 구조체: 지뢰게임을 위한 구조체를 만들자.
한 파일에 모두 코딩하는 대신 필요없는 것들은 생략했다
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(String[] args)
{
MineConsole mc = new MineConsole();
mc.setMine();
mc.CalMine();
mc.Print();
}
}
public class MineConsole
{
public enum BombState
{
EMPTY = 0, ONE = 1, TWO, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, END = 10, FLAGEND = 11, BOMB = 12
};
public enum OpenState { CLOSE = 0, OPEN, FLAG, QUESTION };
public struct Block
{
public BombState bState;
public OpenState oState;
public bool down;
};
public struct Position
{
public int row;
public int col;
}
Block[,] board;
Position[] pos;
int boardSize = 15;
int mine = 20;
public MineConsole()
{
board = new Block[boardSize, boardSize];
pos = new Position[mine];
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
board[i, j].bState = BombState.EMPTY;
}
}
public void setMine()
{
Random r = new Random();
int count = 0;
int row = 0;
int col = 0;
while (true)
{
if (count == mine) break;
row = r.Next(boardSize);
col = r.Next(boardSize);
if (board[row, col].bState != BombState.BOMB)
{
pos[count].row = row;
pos[count].col = col;
count++;
board[row, col].bState = BombState.BOMB;
}
}
}
public void CalMine()
{
for (int t = 0; t < mine; t++)
{
int x = pos[t].row;
int y = pos[t].col;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (i >= 0 && j >= 0 && i <= (boardSize - 1) && j <= (boardSize - 1) && board[i, j].bState != BombState.BOMB)
board[i, j].bState++;
}
}
}
}
public void Print()
{
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
Console.Write("{0,2} ", (int)(board[i, j].bState));
Console.WriteLine();
}
}
}
}
109~111. 쓰레드: 작업순서 변경하기, 조인
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(String[] args)
{
ThreadCompete tcomp = new ThreadCompete();
tcomp.Going();
}
}
public class ThreadT
{
public void Print()
{
for (int i = 0; i < 1000; i++)
{
Console.Write("T");
}
}
public void Go()
{
Thread tt = new Thread(new ThreadStart(Print));
tt.Start();
tt.Join();
for (int i = 0; i < 1000; i++)
Console.Write("H");
}
}
public class ThreadS
{
public void Go()
{
for (int i = 0; i < 1000; i++)
{
Console.Write("S");
}
}
}
public class ThreadM
{
public void Go()
{
for (int i = 0; i < 1000; i++)
{
Console.Write("M");
}
}
}
public class ThreadCompete
{
public void Going()
{
ThreadT t = new ThreadT();
ThreadS s = new ThreadS();
ThreadM m = new ThreadM();
Thread t1 = new Thread(new ThreadStart(t.Go));
Thread t2 = new Thread(new ThreadStart(s.Go));
Thread t3 = new Thread(new ThreadStart(m.Go));
t1.Start();
t1.Join();
t3.Priority = ThreadPriority.Highest;
t2.Start();
t3.Start();
for (int i = 0; i < 1000; i++)
Console.Write("M");
}
}
}
Join을 쓴 T와 H가 먼저 완료되긴 했는데...Priority로 순서가 바뀌는진 잘 모르겠다
'C# > 공부' 카테고리의 다른 글
C# 실력 늘리기 5일차 (0) | 2022.07.25 |
---|---|
C# 실력 늘리기 4일차 (0) | 2022.07.22 |
C# 실력 늘리기 2일차 (0) | 2022.07.20 |
C# 실력 늘리기 1일차 (0) | 2022.07.19 |
시리얼 통신 (0) | 2022.07.11 |