using System;

public class 절대값
{
    public static void Main()
    {
        MakeLine(); // ===========================
        ShowTitle("절대값");
        MakeLine();
        int result = Abs(-10); // result에는 10이 담긴다.
        Console.WriteLine("{0}", result); // 10
        Console.WriteLine("{0}", Abs(-1234) ); // 1234,  Abs수를 출력메서드에 직접 삽입
        MakeLine();
    }
    //[3] Abs() 함수 : 넘겨온 정수의 절대값을 구해서 반환시켜주는 함수
    public static int Abs(int number)
    {
        //int r;
        //if (number > 0)
        //{
        //    r = number;
        //}
        //else
        //{
        //    r = -number; // 넘겨온 값이 마이너스이면 -(음수)기호를 붙여서 +로 바꾼다.
        //}
        //return r;
        return (number > 0) ? number : -number; // 위의 if ~ else문과 결과값 똑같다.
    }
   
    //[1] 매개변수도 없고, 반환값도 없는 함수(메서드)
    public static void MakeLine()
    {
        Console.WriteLine("=================================");
    }

    //[2] 매개변수가 있는 함수
    public static void ShowTitle(string title)
    {
        Console.WriteLine("{0,15}", title);
    }

   
}



// 위에서 if ~ else문을 모두 주석처리 한 것은 바로 아래있는 3항 연산자로(=한줄로) 대체 가능한 것을 보여주기 위해서임.(실행결과는 똑같음)

Posted by holland14
:

using System;

public class 반환값이있는함수
{
    public static void Main()
    {
        //[1] Input
        int firstNumber = 10;
        int secondNumber = 20;
        int hap = 0;
        //[2] Process : Main 함수가 주체
        hap = Sum(firstNumber, secondNumber);

        //[3] Output
        Console.WriteLine("{0} + {1} = {2}", firstNumber, secondNumber, hap);
        Console.WriteLine( Sum(100, 150) ); // 250

    }
    // Sum함수는 Process관점이다... : 두 수의 더한 값을 반환해주면 됨.
    public static int Sum(int a, int b)
    {
        int r;
        r = a + b;
        return r; // a + b한 값을 리턴(반환)
    }
}

Posted by holland14
:

// 매개변수 == 파라미터 == 인자 == 인수 ==
// Paremeter == Argument == 가인수 == 실인수
// 넘겨주고받는 값을 저장하는 변수

using System;

public class 매개변수가있는함수
{
    public static void Main()
    {
        Hello("안녕");// "안녕"
        Hello("방가");// "방가"
        Hello("또봐");// "또봐"
        Say("낼봐", 3);

       
        // Quiz : 아래기능을 하는 함수를 설계해보시오...
        Calc(3, '+', 5); // 8
        Calc(3, '-', 5); // -2
    }

    public static void Calc(int a, char op, int b)
    {
       
//[1] Input
        int result = 0;
       
//[2] Process
        switch (op)
        {
            case '+': result = a + b; break;
            case '-': result = a - b; break;
            default: break;
        }
       
//[3] Output
        Console.WriteLine(result);
    }


    public static void Hello(string msg)
    {
        Console.WriteLine(msg); // 넘겨온 파라미터값을 출력
    }

    public static void Say(string msg, int cnt)
    {
        for (int i = 0; i < cnt; i++)
        {
            Console.WriteLine(msg); // 2번째 [인자]값만큼 출력
        }
           
    }
}

'.NET프로그래밍 > C# 3.5 SP1' 카테고리의 다른 글

20. 절대값 구하기 함수 예제  (0) 2009.08.05
19. 반환값이 있는 함수  (0) 2009.08.05
17. 매개변수가 없는 함수  (0) 2009.08.05
알고리즘 - 5. 최소값(MIN)  (0) 2009.08.05
알고리즘 - 4. 최대값(MAX)  (0) 2009.08.05
Posted by holland14
:

// 함수 : 서브 프로시저, 부 프로그램
using System;

public class 매개변수가없는함수
{
    public static void Main()
    {
        // 매개변수가 없는 함수
        //int num = 1234;
        //string str = num.ToString(); // (int형의)변수의 내용을 문자열로 변환
        //Console.WriteLine(str); // "문자열 형태로 '1234'가 출력된다."
        // 사용자 정의 함수
        Hi(); // "안녕"을 출력하는 함수를 만들어보자.
        Hi(); // 함수 호출(Call)
    }

    // 접근 한정자(Access Modifier)는 오늘까지는
    // public static void 함수명() {}
    // ("Hi"라는)함수 선언(Declaration)
    public static void Hi()
    {
        Console.WriteLine("안녕");
    }
}

'.NET프로그래밍 > C# 3.5 SP1' 카테고리의 다른 글

19. 반환값이 있는 함수  (0) 2009.08.05
18. 매개변수가 있는 함수  (0) 2009.08.05
알고리즘 - 5. 최소값(MIN)  (0) 2009.08.05
알고리즘 - 4. 최대값(MAX)  (0) 2009.08.05
알고리즘 - 3. 평균(AVG)  (0) 2009.08.05
Posted by holland14
:

using System;

public class 최소값
{
    public static void Main()
    {
        //[1] Initialize
        int min = Int32.MaxValue; // 정수형 중 가장 큰 값으로 초기화
        //[2] Input
        int[] data = { -2, -5, -3, -7, -1 };
        //[3] Process : MIN
        for (int i = 0; i < data.Length; i++)
        {
            if (data[i] < min)
            {
                min = data[i]; // MIN
            }
        }
        //[4] Output
        Console.WriteLine("최소값 : {0}", min); // -7
        //[5] Dispose
        min = 0;
    }
}

'.NET프로그래밍 > C# 3.5 SP1' 카테고리의 다른 글

18. 매개변수가 있는 함수  (0) 2009.08.05
17. 매개변수가 없는 함수  (0) 2009.08.05
알고리즘 - 4. 최대값(MAX)  (0) 2009.08.05
알고리즘 - 3. 평균(AVG)  (0) 2009.08.05
알고리즘 - 2. 카운트(COUNT)  (0) 2009.08.05
Posted by holland14
:


using System;

public class 최대값
{
    public static void Main()
    {
        //[1] Initialize
        int max = 0; // 해당 범위 내에서 가장 작은 값으로 초기화
        //[2] Input
        int[] data = { 2, 5, 3, 7, 1 };
        //[3] Process : MAX
        for (int i = 0; i < data.Length; i++)
        {
            if (data[i] > max)
            {
                max = data[i];
            }                         
        }
        //[4] Output
        Console.WriteLine("최대값 : {0}", max); // 7
        //[5] Dispose
        max = 0;
    }
}


==============================================================================================

예제2 --> 배열 안의 값이 모두 "음의 정수"일 때 최대값 구하기

using System;

public class 최대값
{
    public static void Main()
    {
        //[1] Initialize
        int max = Int32.MinValue; // 정수형 데이터 중 작은 값으로 초기화
        //[2] Input
        int[] data = { -2, -5, -3, -7, -1 };
        //[3] Process : MAX
        for (int i = 0; i < data.Length; i++)
        {
            if (data[i] > max)
            {
                max = data[i];
            }                         
        }
        //[4] Output
        Console.WriteLine("최대값 : {0}", max); // -1
        //[5] Dispose
        max = 0;
    }
}

'.NET프로그래밍 > C# 3.5 SP1' 카테고리의 다른 글

17. 매개변수가 없는 함수  (0) 2009.08.05
알고리즘 - 5. 최소값(MIN)  (0) 2009.08.05
알고리즘 - 3. 평균(AVG)  (0) 2009.08.05
알고리즘 - 2. 카운트(COUNT)  (0) 2009.08.05
알고리즘 - 1. 합계(SUM)  (0) 2009.08.05
Posted by holland14
:

using System;

public class 평균
{
    public static void Main()
    {
        //[1] 입력
        int[] data = { 50, 65, 78, 90, 95 };
        int sum = 0;
        int count = 0;
        double avg = 0.0; // 평균이 저장될 변수
        //[2] 처리 : AVG = SUM / COUNT
        for (int i = 0; i < data.Length; i++)
        {
            if (data[i] >= 80 && data[i] <= 95)
            {
                sum += data[i];
                count++;        
            }
        }
        avg = sum / (double)count; // 캐스팅(형식변환) 필요 : 3 -> 3.0
        //[3] 출력
        Console.WriteLine("80점 이상 95점 이하인 자료의 평균 : {0}", avg); // 92.5
    }
}

'.NET프로그래밍 > C# 3.5 SP1' 카테고리의 다른 글

알고리즘 - 5. 최소값(MIN)  (0) 2009.08.05
알고리즘 - 4. 최대값(MAX)  (0) 2009.08.05
알고리즘 - 2. 카운트(COUNT)  (0) 2009.08.05
알고리즘 - 1. 합계(SUM)  (0) 2009.08.05
16. 1차원배열  (0) 2009.08.05
Posted by holland14
:

using System;

public class 카운트
{
    public static void Main()
    {
        //[1] Input
        int[] data = {10, 9, 4, 7, 6, 5 };
        int count = 0; // 카운트 저장
        //[2] Process : COUNT
        for (int i = 0; i < data.Length; i++)
        {
            if (data[i] % 2 == 0) // 짝수
            {
                count ++; // 카운트 1증가
            }

        }
        //[3] Output
        Console.WriteLine("짝수의 건수 : {0}", count); // 3
    }
}

'.NET프로그래밍 > C# 3.5 SP1' 카테고리의 다른 글

알고리즘 - 4. 최대값(MAX)  (0) 2009.08.05
알고리즘 - 3. 평균(AVG)  (0) 2009.08.05
알고리즘 - 1. 합계(SUM)  (0) 2009.08.05
16. 1차원배열  (0) 2009.08.05
15. 컬렉션반복(foreach문 사용)  (0) 2009.08.05
Posted by holland14
:

using System;

public class 합계
{
    public static void Main()
    {
        //[1] Input : n명의 국어 점수로 가정
        int[] score = {100, 75, 50, 37, 90, 95};
        int sum = 0;
        //[2] Process : SUM ==> 주어진 범위에 주어진 조건을 만족해야 함.
        for (int i = 0; i <score.Length; i++)  // 0번째 ~ n-1번째      
        {
            if (score[i] >= 80)
            {
                sum += score[i]; // SUM
            }
        }
        //[3] Output
        Console.WriteLine("{0}명의 점수 중 80점 이상의 총점 : {1}", score.Length, sum); //  총점 : 285
    }
}

'.NET프로그래밍 > C# 3.5 SP1' 카테고리의 다른 글

알고리즘 - 3. 평균(AVG)  (0) 2009.08.05
알고리즘 - 2. 카운트(COUNT)  (0) 2009.08.05
16. 1차원배열  (0) 2009.08.05
15. 컬렉션반복(foreach문 사용)  (0) 2009.08.05
C# 언어 사양  (0) 2009.08.05
Posted by holland14
:

첫번째 예제

using System;

public class 일차원배열
{
    public static void Main()
    {
        //[1] 배열 선언
        int num = 10; // (Variable)변수 선언과 동시에 초기화
        int[] arr; // 배열 선언
        arr = new int[3]; // 배열의 요소수 생성

        //[2] 초기화 : 배열의 인덱스는 n-1규칙에 의해서 0부터 시작
        arr[0] = 10;
        arr[1] = 20;
        arr[2] = 30;
        //[3]참조
        for (int i = 0; i < arr.Length; i++)
        {
            Console.WriteLine("{0}", arr[i]);
        }
        foreach (int i in arr)
        {
            Console.WriteLine("{0}", i);
        }
    }
}


==============================================================================================

두번째 예제

using System;

public class 일차원배열2
{
    public static void Main()
    {
        //[1] 배열 선언과 동시 요소수 생성
        int[] arr = new int[3];
        arr[0] = 10;
        arr[1] = 20;
        arr[2] = 30;
        //[3]참조
        for (int i = 0; i < arr.Length; i++)
        {
            Console.WriteLine("{0}", arr[i]);
        }
        foreach (int i in arr)
        {
            Console.WriteLine("{0}", i);
        }
    }
}


==============================================================================================

세번째 예제

using System;

public class 일차원배열3
{
    public static void Main()
    {
        //[1] 배열 선언과 동시 요소수 생성과 동시 초기화
        //int[] arr = new int[3] {10, 20, 30}; // 'new'라는 이름의 '참조연산자'(가리키는 '화살표' 역할을 한다.)
        //int[] arr = new int[] {10, 20, 30}; // 요소수 생략 가능
        int[] arr = {10, 20, 30}; // 선언동시 초기화(요소수 자동생성)
        string[] 결제방식 = { "카드", "휴대폰", "무통장입금"};
        //[3]참조
        for (int i = 0; i < arr.Length; i++)
        {
            Console.WriteLine("{0}", arr[i]);
        }
        foreach (int i in arr)
        {
            Console.WriteLine("{0}", i);
        }
        foreach (string 하나씩 in 결제방식)
        {
            Console.WriteLine("{0}", 하나씩);
        }
    }
}

'.NET프로그래밍 > C# 3.5 SP1' 카테고리의 다른 글

알고리즘 - 2. 카운트(COUNT)  (0) 2009.08.05
알고리즘 - 1. 합계(SUM)  (0) 2009.08.05
15. 컬렉션반복(foreach문 사용)  (0) 2009.08.05
C# 언어 사양  (0) 2009.08.05
C# 기본 문법 PPT  (0) 2009.08.05
Posted by holland14
: