Vienna
4일차 본문
오늘도 디카페인 커피를 타 왔다. 화이팅
1. 실습_Calculator
using System;
namespace Method
{
class Calculator
{
public static int Plus (int a, int b)
{
return a + b;
}
public static int Minus(int a, int b)
{
return a - b;
}
}
}
namespace _6장_Calculator_188page
{
class Program
{
static void Main(string[] args)
{
int result = Method.Calculator.Plus(3, 4);
Console.WriteLine(result);
result = Method.Calculator.Minus(5, 2);
Console.WriteLine(result);
}
}
}
2. 실습_Return
using System;
namespace _6장_Return_189page
{
class Program
{
static int Fibonacci(int n)
{
if (n < 2) return n;
else return Fibonacci(n - 1) + Fibonacci(n - 2);
}
static void PrintProfile(string name, string phone)
{
if (name == "")
{
Console.WriteLine("이름을 입력해주세요.");
return;
}
Console.WriteLine($"Name: {name}, Phone: {phone}");
}
static void Main(string[] args)
{
Console.WriteLine($"10번째 피보나치 수: {Fibonacci(10)}");
PrintProfile("", "123-4567");
PrintProfile("김팁", "456-1230");
}
}
}
3. 실습_SwapByValue
using System;
namespace _6장_SwapByValue_195page
{
class Program
{
public static void Swap(int a, int b)
{
int temp = b;
b = a;
a = temp;
}
static void Main(string[] args)
{
int x = 3, y = 4;
Console.WriteLine($"x:{x}, y:{y}");
Swap(x, y);
Console.WriteLine($"x:{x}, y:{y}");
}
}
}
4. 실습_SwapByRef
using System;
namespace _6장_SwapByRef_197page
{
class Program
{
public static void Swap(ref int a, ref int b)
{
int temp = b;
b = a;
a = temp;
}
static void Main(string[] args)
{
int x = 3, y = 4;
Console.WriteLine($"x:{x}, y:{y}");
Swap(ref x, ref y);
Console.WriteLine($"x:{x}, y:{y}");
}
}
}
5. 실습_RefReturn
using System;
namespace RefReturn
{
class Product
{
private int price = 100;
public ref int GetPrice()
{
return ref price;
}
public void PrintPrice()
{
Console.WriteLine($"Price: {price}");
}
}
}
namespace _6장_RefReturn_199page
{
class Program
{
static void Main(string[] args)
{
RefReturn.Product carrot = new RefReturn.Product();
ref int ref_local_price = ref carrot.GetPrice();
int normal_local_price = carrot.GetPrice();
carrot.PrintPrice();
Console.WriteLine($"Ref Local Price: {ref_local_price}");
Console.WriteLine($"Normal Local Price: {normal_local_price}");
ref_local_price = 200;
carrot.PrintPrice();
Console.WriteLine($"Ref Local Price: {ref_local_price}");
Console.WriteLine($"Normal Local Price: {normal_local_price}");
}
}
}
6. 실습_UsingOut
using System;
namespace _6장_UsingOut_202page
{
class Program
{
static void Divide(int a, int b, out int quotient, out int remainder)
{
quotient = a / b;
remainder = a % b;
}
static void Main(string[] args)
{
int a = 20;
int b = 3;
Divide(a, b, out int c, out int d);
Console.WriteLine($"a:{a}, b:{b}, a/b:{c}, a%b:{d}");
}
}
}
7. 실습_Overloading
using System;
namespace _6장_Overloading_205page
{
class Program
{
static int Plus(int a,int b)
{
Console.WriteLine("Calling int Plus(int a,int b)...");
return a + b;
}
static int Plus(int a, int b, int c)
{
Console.WriteLine("Calling int Plus(int a,int b, int c)...");
return a + b + c;
}
static double Plus(double a, double b)
{
Console.WriteLine("Calling double Plus(double a,double b)...");
return a + b;
}
static double Plus(int a, double b)
{
Console.WriteLine("Calling double Plus(int a,double b)...");
return a + b;
}
static void Main(string[] args)
{
Console.WriteLine(Plus(1, 2));
Console.WriteLine(Plus(1, 2,3));
Console.WriteLine(Plus(1.0, 2.4));
Console.WriteLine(Plus(1, 2.4));
}
}
}
8. 실습_UsingParams
using System;
namespace _6장_UsingParams_207page
{
class Program
{
static int Sum(params int[] args)
{
Console.Write("Summing... ");
int sum = 0;
for (int i = 0; i < args.Length; i++)
{
if (i > 0) Console.Write(", ");
Console.Write(args[i]);
sum += args[i];
}
Console.WriteLine();
return sum;
}
static void Main(string[] args)
{
int sum = Sum(3, 4, 5, 6, 7, 8, 9, 10);
Console.WriteLine($"Sum: {sum}");
}
}
}
진짜 편하다... 감탄이 절로 나온다. 왜 C#이 쉽다고 하는지 알 수 있을 것 같다.
9. 실습_NamedParameter
using System;
namespace _6장_NamedParameter_209page
{
class Program
{
static void PrintProfile(string name, string phone)
{
Console.WriteLine($"Name: {name}, Phone: {phone}");
}
static void Main(string[] args)
{
PrintProfile(name: "박찬호", phone: "010-123-1234");
PrintProfile(phone: "010-987-9876", name: "박지성");
PrintProfile("박세리", "010-222-2222");
PrintProfile("박상현", phone: "010-567-5678");
}
}
}
10. 실습_OptionalParameter
using System;
namespace _6장_OptionalParameter_211page
{
class Program
{
static void PrintProfile(string name, string phone="")
{
Console.WriteLine($"Name: {name}, Phone: {phone}");
}
static void Main(string[] args)
{
PrintProfile("중근");
PrintProfile("관순", "010-123-1234");
PrintProfile(name: "봉길");
PrintProfile(name: "동주", phone: "010-789-7890");
}
}
}
11. 실습_LocalFunction
using System;
namespace _6장_LocalFunction_214page
{
class Program
{
static string ToLowerString(string input)
{
var arr = input.ToCharArray();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = ToLowerChar(i);
}
char ToLowerChar(int i)
{
if (arr[i] < 65 || arr[i] > 90) // A~Z의 ASCII 값: 65~90
return arr[i];
else // a~z의 ASCII 값: 97~122
return (char)(arr[i] + 32);
}
return new string(arr);
}
static void Main(string[] args)
{
Console.WriteLine(ToLowerString("Hello!"));
Console.WriteLine(ToLowerString("Good Morning."));
Console.WriteLine(ToLowerString("This is C#"));
}
}
}
12. 연습문제1
using System;
namespace 연습문제1_216page
{
class Program
{
static double Square(double arg)
{
return arg * arg;
}
static void Main(string[] args)
{
Console.Write("수를 입력하세요: ");
string input = Console.ReadLine();
double arg = Convert.ToDouble(input);
Console.WriteLine("결과: {0}", Square(arg));
}
}
}
12. 연습문제2
using System;
namespace 연습문제2_216page
{
class Program
{
static void Main(string[] args)
{
double mean = 0;
Mean(1, 2, 3, 4, 5, ref mean);
Console.WriteLine("평균: {0}", mean);
}
public static void Mean(double a, double b, double c, double d, double e, ref double mean)
{
mean = (a + b + c + d + e) / 5;
}
}
}
using System;
namespace 연습문제2_216page
{
class Program
{
static void Main(string[] args)
{
Mean(1, 2, 3, 4, 5, out double mean);
Console.WriteLine("평균: {0}", mean);
}
public static void Mean(double a, double b, double c, double d, double e, out double mean)
{
mean = (a + b + c + d + e) / 5;
}
}
}
연습 겸 ref, out 둘다 써봤다.
13. 연습문제3
using System;
namespace 연습문제3_126page
{
class Program
{
static void Main(string[] args)
{
int a = 3;
int b = 4;
int resultA = 0;
Plus(a, b, out resultA);
Console.WriteLine("{0} + {1} = {2}", a, b, resultA);
double x = 2.4;
double y = 3.1;
double resultB = 0;
Plus(x, y, out resultB);
Console.WriteLine("{0} + {1} = {2}", x, y, resultB);
}
public static void Plus(int a, int b, out int c)
{
c = a + b;
}
public static void Plus(double a, double b, out double c)
{
c = a + b;
}
}
}
Comments