함수
using System;
namespace ConsoleApp1 {
internal class ex08 {
static void Main() {
myFunc();
myFunc2(10);
string result = myFunc3();
Console.WriteLine(result);
float dimension = myFunc4(10, 20);
Console.WriteLine(dimension);
}
static void myFunc() {
Console.WriteLine("myFunc() 호출됨");
}
static void myFunc1() {
Console.WriteLine("myFunc1() 호출됨");
}
static void myFunc2(int param) {
Console.WriteLine("myFunc1() 호출됨");
Console.WriteLine($"매개변수{param}");
}
static string myFunc3() {
Console.WriteLine("myFunc() 호출됨");
return "하이~";
}
static float myFunc4(int width, int height) {
return width * height;
}
}
}
연습문제
using System;
namespace ConsoleApp1 {
internal class ex09 {
static void Main() {
double result = pie(10);
Console.WriteLine(result);
int a = 10;
int b = 20;
int c = 30;
int maxValue = max_num(a, b, c);
Console.WriteLine("\n<문제2>");
Console.WriteLine("가장 큰 숫자 : " + maxValue);
Console.WriteLine("\n<문제3>");
CompressString("aaabbcccc");
}
static double pie(int half) {
Console.WriteLine("<문제1>");
return half * 2 * 3.14;
}
static int max_num(int a, int b, int c) {
if (a > b && a > c) {
return a;
} else if (b > a && b > c) {
return b;
} else {
return c;
}
}
static void CompressString(string input) {
int[] alphbet = new int[26];
char[] cArray = input.ToCharArray();
for (int i = 0; i < cArray.Length; i++) {
for(int j = 0; j < alphbet.Length; j++) {
if(cArray[i] == 'a' + j) {
alphbet[j]++;
}
}
}
Console.WriteLine(string.Join(',',alphbet));
for(int n = 0; n < alphbet.Length; n++) {
}
}
}
}
배열, 큐, stack
using System;
namespace ConsoleApp1 {
internal class ex10 {
static void Main(string[] args) {
int[] arrayNum = { 10, 20, 30 };
int[] arrayNum2 = { 10, 20, 30 };
int[] arrayNum3 = new int[3];
arrayNum3[0] = 10;
int[][] array2DNum = [[10, 20], [30, 40]];
int[][] array2DNum2 = new int[3][];
array2DNum2[0] = [10, 20, 30];
array2DNum2[1] = [40, 50, 60];
array2DNum2[2] = [70, 80, 90];
List<int> list = new List<int>();
list.Add(10);
list.Add(20);
foreach (int n in list) {
Console.WriteLine(n);
}
for (int i = 0; i < list.Count; i++) {
Console.WriteLine(list.ElementAt(i));
}
ArrayList arrayList = new ArrayList();
arrayList.Add(10);
arrayList.Add("20");
arrayList.Add(3.14f);
foreach (var item in arrayList) {
if (typeof(int) == item.GetType()) {
Console.WriteLine("int타입입니다.");
int num = Convert.ToInt32(item);
Console.WriteLine(num);
}
if (typeof(string) == item.GetType()) {
Console.WriteLine("String타입입니다.");
}
Console.WriteLine(item);
}
Queue<int> queue = new Queue<int>();
queue.Enqueue(10);
queue.Enqueue(20);
queue.Enqueue(30);
while (queue.Count > 0) {
Console.WriteLine(queue.Dequeue());
}
Stack<int> stack = new Stack<int>();
stack.Push(10);
stack.Push(20);
stack.Push(30);
while (stack.Count > 0) {
Console.WriteLine(stack.Pop());
}
Console.Write("나눌 숫자를 입력하세요");
int divider = int.Parse(Console.ReadLine());
try {
Console.WriteLine(10 / divider);
} catch (Exception e) {
Console.WriteLine("예외 상황 : " + e.Message);
}
}
}
}
클래스
using System;
using System.Collections.Generic;
namespace ConsoleApp1 {
class Dog {
public string name = "까미";
public void eat() {
Console.WriteLine("먹는다");
}
}
internal class ex11 {
static void Main() {
Dog dog = new Dog();
Console.WriteLine(dog.name);
dog.eat();
Student student = new Student();
student.study();
Console.WriteLine(student.age);
Farm farm = new Farm();
farm.plant(); farm.plant(); farm.plant(); farm.plant(); farm.plant();
Console.WriteLine(farm.getCarrotCount());
Calc calc = new Calc();
calc.sum(10, 20);
Console.WriteLine(calc.result);
}
}
class Student {
public int age = 20;
public void study() {
Console.WriteLine("공부한다.");
}
}
class Farm() {
private int carrot = 0;
public int getCarrotCount() {
return carrot;
}
public void setCarrotCount(int carrot) {
this.carrot = carrot;
}
public void plant() {
carrot++;
}
}
class Calc {
public double result = 0.0;
public void sum(double x, double y) {
this.result = x + y;
}
public void sub(double x, double y) {
this.result = x - y;
}
public void mul(double x, double y) {
this.result = x * y;
}
public void div(double x, double y) {
this.result = x / y;
}
}
}
클래스 상속
using System;
using System.Collections.Generic;
namespace ConsoleApp1 {
internal class ex12 {
static void Main(string[] args) {
CoffeeRobot cRobot = new CoffeeRobot();
cRobot.work();
Console.WriteLine(cRobot.price);
cRobot.make();
}
}
class Robot {
public int price = 1000;
public void work() {
Console.WriteLine("일한다");
}
}
class CoffeeRobot : Robot {
public string name = "커피로봇";
public void make() {
Console.WriteLine("커피를 만든다.");
}
}
class DeliveryRobot : Robot {
public string name = "배달로봇";
public void delivery() {
Console.WriteLine("배달한다.");
}
}
}
생성자 함수
using System;
namespace ConsoleApp1 {
internal class ex13 {
static void Main() {
Book book = new Book();
Console.WriteLine(book.price);
Book book2 = new Book(2000);
Console.WriteLine(book2.price);
}
}
class Book {
public int price = 1000;
public Book() {
Console.WriteLine("생성자함수 호출됨.");
}
public Book(int price) {
this.price = price;
}
void read() {
}
}
}