using System;
using System.Diagnostics;

public class 스톱워치클래스 // 교재385p
{
    public static void Main()
    {
        Stopwatch st = new Stopwatch();
        st.Start();
        for (int i = 0; i < 100000; i++)
        {
            Console.Write(i + ".");
        }
        st.Stop();
        Console.WriteLine("\n총 경과시간 = {0}", st.Elapsed);       
    }
}

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

알고리즘 - 9. 선택정렬  (0) 2009.08.07
알고리즘 - 8. 순위(RANK)  (0) 2009.08.07
35. 랜덤클래스  (0) 2009.08.07
34. 환경변수  (0) 2009.08.07
수학관련함수예제 - ROUND(반올림)함수  (0) 2009.08.07
Posted by holland14
:


using System;

public class 랜덤클래스
{
    public static void Main()
    {
        // 임의의 수 출력

        // Random 클래스의 인스턴스 생성
        Random r = new Random();
        for (int i = 0; i < 50; i++)
        {
            Console.Write("{0} ", r.Next(100)); // 0~n미만의 정수(여기서는 100미만의 정수)   
        } Console.WriteLine();

        for (int i = 0; i < 10; i++)
        {
            Console.Write("{0} ", r.NextDouble()); // 0.0~1.0 실수
        } Console.WriteLine();

        // Random 클래스를 사용해서 1~45까지의 수를 6개 출력 기능 : 로또
        Console.WriteLine("금주의 로또 : ");
        for (int i = 0; i < 6; i++)
        {
            Console.Write("{0} ", r.Next(45) + 1 ); // 1~45
        } Console.WriteLine();
        // 중복제거해서 출력
        Console.Write("중복제거 : ");

        Random ran = new Random();
        int[] arr = new int[6]; // 6개 데이터
        int temp = 0;
        for (int i = 0; i < 6; i++)
        {
            temp = ran.Next(45) + 1; // 1~45까지
            bool flag = false;
            if (i > 0 && i < 6)
            {
                for (int j = 0; j <= i; j++)
                {
                    if (arr[j] == temp) // 이전 자료와 중복되면 제거
                    {
                        flag = true; // 중복되면, true로 설정
                    }
                }
            }

            if (flag)
            {
                i--; // 중복되었다면, 현재 인덱스를 재반복
            }
            else
            {
                arr[i] = temp; // 중복된 데이터가 없다면 저장
            }
        }
        for (int i = 0; i < 6; i++)
        {
            Console.Write("{0} ", arr[i]);
        } Console.WriteLine();
       
    }
}

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

알고리즘 - 8. 순위(RANK)  (0) 2009.08.07
36. 스톱워치클래스  (0) 2009.08.07
34. 환경변수  (0) 2009.08.07
수학관련함수예제 - ROUND(반올림)함수  (0) 2009.08.07
33. 수학관련함수  (0) 2009.08.07
Posted by holland14
:

using System;

public class 환경변수
{
    public static void Main()
    {
        Console.WriteLine( Environment.SystemDirectory ); // 시스템폴더
        Console.WriteLine( Environment.Version ); // 닷넷기준버전:2.0.50727
        Console.WriteLine( Environment.OSVersion); // 운영체제 버전
        Console.WriteLine( Environment.MachineName); // 컴퓨터명
        Console.WriteLine( Environment.UserName); // 사용자명
        Console.WriteLine( Environment.CurrentDirectory ); // 현재 폴더
        Console.WriteLine( Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) ); // 내문서 폴더
    }

}

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

36. 스톱워치클래스  (0) 2009.08.07
35. 랜덤클래스  (0) 2009.08.07
수학관련함수예제 - ROUND(반올림)함수  (0) 2009.08.07
33. 수학관련함수  (0) 2009.08.07
32. 스트링포맷(String.Format)  (0) 2009.08.07
Posted by holland14
:

using System;

public class 반올림
{
    public static void Main()
    {
        double d = 1234.5678;
        Console.WriteLine( Math.Round(d, 2) ); // 1234.57
        Console.WriteLine( MyRound(d, 3) ); // 1234.568
        //double temp = (int)((d + 0.05) * 10) / 10.0; // xxx.x
        double temp = (int)((d + 0.005) * 100) / 100.0; // xxx.xx
        Console.WriteLine("{0}", temp );
    }

    /// <summary>
    /// 내가 만든 반올림 함수
    /// </summary>
    /// <param name="num">실수형</param>
    /// <param name="pos">자릿수 : 반올림되어질 자리</param>
    /// <returns>반올림된 수</returns>
    public static double MyRound(double num, int pos)
    {
        //[1]
        double result = 0.0;
        double half = 0.5;
        double factor = 1;
        //[2]
        for (int i = 0; i < pos; i++)
        {
            half *= 0.1;
            factor *= 10;
        }
        result = (int)((num + half) * factor) / (double)factor;
        //[3]
        return result;
    }

}

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

35. 랜덤클래스  (0) 2009.08.07
34. 환경변수  (0) 2009.08.07
33. 수학관련함수  (0) 2009.08.07
32. 스트링포맷(String.Format)  (0) 2009.08.07
31. 파일명 추출  (0) 2009.08.07
Posted by holland14
:

using System;

public class 수학관련함수
{
    public static void Main()
    {
        Console.WriteLine( Math.E); // 자연로그
        Console.WriteLine( Math.PI); // 3.1415926535

        Console.WriteLine( Math.Abs(-10) ); // 절대값
        Console.WriteLine( Math.Pow(2, 10) ); //2 ^ 10 = 1024
        Console.WriteLine( Math.Round(1234.5678, 2) ); //1234.57
        Console.WriteLine( Math.Max(3, 5) ); // 5
        Console.WriteLine( Math.Min(3, 5) ); // 3
    }
}

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

34. 환경변수  (0) 2009.08.07
수학관련함수예제 - ROUND(반올림)함수  (0) 2009.08.07
32. 스트링포맷(String.Format)  (0) 2009.08.07
31. 파일명 추출  (0) 2009.08.07
30. 스트링(string)클래스  (0) 2009.08.07
Posted by holland14
:

// String.Format() 메서드 : 문자열 연결 관련 주요 기능 제공
using System;

public class 스트링포맷
{
    public static void Main()
    {
        int i = 1234;
        double d = 1234.5678;
        string s = "1234";

        // 서로 다른 데이터형식을 묶을 때 문자열로 묶어준다.
        string result = String.Format("{0} {1} {2}", i, d, s);
        Console.WriteLine("{0}", result);
        // 정수 또는 실수형 자릿수 표현 가능
        result = String.Format("{0:###,###}", i);
        Console.WriteLine(result); // 1,234
        Console.WriteLine(String.Format("{0:###,###.##}", d)); // 1,234.57
        Console.WriteLine(String.Format("{0:###,###.##0000}", d)); // 1,234.567800
        Console.WriteLine(String.Format("{0:000,###}", i)); // 001,234
        Console.WriteLine(String.Format("{0:000,###}", s)); // 1234
        Console.WriteLine(String.Format("{0:000,###}", Convert.ToInt32(s))); // 001,234
        // 긴문자열 연결
        result = String.Format(
            "{0}\n{1}\n{2}"
            , "<script type='text/css'>"
            , String.Format("window.alert(\"{0}\")", "안녕하세요.")
            , "</script>");
        Console.WriteLine(result);
        // 긴문자열 연결 : @기호 : 마치 HTML의 <pre> 태그처럼...
        result = @"
            <script type='text/javascript'>
            window.alert('반갑습니다.');
            </script>
        ";
        Console.WriteLine(result);
        // 긴문자열 연결: +연산자
        result =
            "<script>"
            + "alert('안녕');"
            + "</script>";
        Console.WriteLine(result);
        //채우기
        string data = "1234";
        Console.WriteLine("{0}", data.PadLeft(10, '*')); // ******1234
        Console.WriteLine("{0}", data.PadRight(10, '*')); // 1234******
    }
}

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

수학관련함수예제 - ROUND(반올림)함수  (0) 2009.08.07
33. 수학관련함수  (0) 2009.08.07
31. 파일명 추출  (0) 2009.08.07
30. 스트링(string)클래스  (0) 2009.08.07
알고리즘 - 수열예제  (0) 2009.08.06
Posted by holland14
:

using System;

public class 파일명추출
{
    public static void Main()
    {
        //[Q] 아래 전체 경로가 입력되었을 때 파일명과 확장자 추출
       
        //[1] Input
        string dir = "C:\\Website\\RedPlus\\images\\test.gif";
        string fullName = String.Empty;
        string name = "";
        string ext = name;
       
        //[2] Process
        // 전체파일명 : 마지막 \\ 위치값 +1한 위치부터 마지막까지 검색
        fullName = dir.Substring(dir.LastIndexOf('\\') + 1);
        name = fullName.Substring(0, fullName.LastIndexOf('.'));
        ext = fullName.Substring(fullName.LastIndexOf('.') + 1);

        //[3] Output
        Console.WriteLine("전체 파일명 : {0}", fullName);
        Console.WriteLine("순수 파일명 : {0}", name);
        Console.WriteLine("확장자 : {0}", ext);

        //[A]
        // 전체 파일명 : test.gif
        // 순수 파일명 : test
        // 확장자 : gif
    }
}

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

33. 수학관련함수  (0) 2009.08.07
32. 스트링포맷(String.Format)  (0) 2009.08.07
30. 스트링(string)클래스  (0) 2009.08.07
알고리즘 - 수열예제  (0) 2009.08.06
알고리즘 - 7. 최빈값  (0) 2009.08.06
Posted by holland14
:

using System;

public class 스트링클래스
{
    public static void Main()
    {
        string s = String.Empty; // 빈문자열 저장(빈문자열로 초기화)
        s = ""; // 일반적으로 많이 쓰는 표현(NULL값이 아니다.)
        s = " Abc Def Fed Cba "; // 테스트용 문자열 저장

        Console.WriteLine("{0}", s); // 전체 출력
        Console.WriteLine( s[6-1] ); // 5번째 인덱스의 문자 하나 출력 ==> 'D'가 출력됨.
        Console.WriteLine( s.Length ); // 길이
        Console.WriteLine( s.ToUpper() ); // 대문자(로 화면에 출력함. 원래 값은 변하지 않는다.)
        Console.WriteLine( s.ToLower() ); // 소문자
        Console.WriteLine( s.TrimStart() ); // 앞에 있는 공백 제거
        Console.WriteLine( s.TrimEnd() ); //  뒤에 있는 공백 제거
        Console.WriteLine( s.Trim() ); // 양쪽 공백 제거
        Console.WriteLine( s.Replace("Def", "deF").Replace('F', 'f') ); // 치환

        // 검색기능관련
        Console.WriteLine( s.IndexOf('z') ); // 실행결과 -1 반환 : 조건만족하는 데이타가 없다는 것을 의미
        Console.WriteLine( s.IndexOf("e") ); // 문자 e의 위치(인덱스)값? 앞에서부터 6번째
        Console.WriteLine( s.LastIndexOf("e") ); // 문자 e의 위치(인덱스)값? 뒤에서부터 10번째
        Console.WriteLine( s.Substring(5, 3) ); // 5번째 인덱스부터 3자
        Console.WriteLine( s.Substring(5) ); // 5번째 인덱스 이후로 모두
        Console.WriteLine( s.Remove(5, 3) ); // 5번째 인덱스부터 3자 삭제

        // 구분자(공백, 콤마)를 사용해서 분리해서 저장
        string[] arr = s.Trim().Split(' '); // ???
        // arr[0] = "Abc";
        // arr[1] = "Def";
        // arr[2] = "Fed";
        // arr[3] = "Cba";
        foreach (string one in arr)
        {
            Console.WriteLine(one);
        }

        string url = "https://www.dotnetkorea.com/";
        if ( !url.StartsWith("http://") ) // http://로 시작되지 않는다면
        {
            Console.WriteLine("URL은 http://로 시작해야 됩니다.");
     }

        if (url.Substring(url.Length - 1) == "/") // 마지막에 '/'가 있으면 제거
        {
            string temp = url.Remove(url.Length - 1, 1); // '/'제거된 임시...
            if (temp.EndsWith(".com")) // .com으로 끝나는지 검사
            {
                Console.WriteLine(".com으로 끝나는군요.");
            }
        }
    }
}

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

32. 스트링포맷(String.Format)  (0) 2009.08.07
31. 파일명 추출  (0) 2009.08.07
알고리즘 - 수열예제  (0) 2009.08.06
알고리즘 - 7. 최빈값  (0) 2009.08.06
알고리즘 - 6. 가까운값(NEAR)  (0) 2009.08.06
Posted by holland14
:

1번째 문제

/*
아래 수열의 결과값을 구하는 프로그램을 작성하시오.
1 - 2 + 3 - 4 + 5 - ... + 99 - 100 = ?
패턴 : 홀수는 +, 짝수는 -
*/
using System;

public class 간단수열
{
    public static void Main()
    {
        //[1] Input
        int sum = 0;
        //[2] Process
        for (int i = 1; i <= 100; i++)
        {
            if (i % 2 != 0)
            {
                sum += i;
            }
            else
            {
                sum -= i;
            }
        }   
        //[3] Output
        Console.WriteLine("Result : {0}", sum);
    }
}


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

2번째 문제


// 아래 수열의 결과값을 구하는 프로그램을 작성하시오.
// 1/2 - 2/3 + 3/4 - ... - 98/99 + 99/100 = ?
using System;

public class 수열문제
{
    public static void Main()
    {
        //[1] Input
        double sum = 0.0;
        //[2] Process
        for (int i = 1; i <= 99; i++)
        {
            if (i % 2 != 0)
            {
                sum += i / (double)(i + 1);
            }
            else
            {
                sum -= i / (double)(i + 1);
            }
        }

        //[3] Output
        Console.WriteLine(sum);
    }
}

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

3번째문제

// 아래 수열의 결과값을 구하는 프로그램을 작성하시오.
// 1 + 2 + 4 + 7 + 11 + 16 + 22 + ... +
// 위 패턴대로 20번 반복했을 때의 결과값은? 얼마?
using System;

public class 수열문제2
{
    public static void Main()
    {
        //[1] Input
        int data = 1;
        int sum = 0;
        //[2] Process
        for (int i = 0; i < 20; i++) // 20번 반복
        {
            Console.Write("{0} + ", data);
            sum += data; // 1누적
            data = data + (i + 1);
        }
        //[3] Output
        Console.WriteLine("\n{0}", sum); // 1350
    }
}

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

31. 파일명 추출  (0) 2009.08.07
30. 스트링(string)클래스  (0) 2009.08.07
알고리즘 - 7. 최빈값  (0) 2009.08.06
알고리즘 - 6. 가까운값(NEAR)  (0) 2009.08.06
29. 구조체를 이용한 카운트 알고리즘  (0) 2009.08.06
Posted by holland14
:

// 최빈값(MODE): 가장 많이 나타난 값
// -> 데이터의 인덱스(0점~100점)의 카운트(COUNT)값의 최대값(MAX)
using System;

public class 최빈값
{
    public static void Main()
    {
        //[1] Input : 범위 : 0부터 100점까지의 점수만 들어온다고 가정
        int[] score = {1, 3, 4, 3, 5}; // 0~5까지만 들어온다고 가정
        int mode = 0; // 최빈값이 담길 그릇
        int[] index = new int[5 + 1]; // 0점부터 5점까지 : 인덱스의 카운터를 구하기 위해서
        int max = Int32.MinValue;
        //[2] Process
        for (int i = 0; i < score.Length; i++)
        {
            index[score[i]]++; // COUNT
        }
        for (int i = 0; i < index.Length; i++)
        {
            if (index[i] > max)
            {
                max = index[i]; // MAX
                mode = i; // MODE
            }
        }

        //[3] Output
        Console.WriteLine("최빈값 : {0} , {1}번", mode, max); // 3, 2
    }
}

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

30. 스트링(string)클래스  (0) 2009.08.07
알고리즘 - 수열예제  (0) 2009.08.06
알고리즘 - 6. 가까운값(NEAR)  (0) 2009.08.06
29. 구조체를 이용한 카운트 알고리즘  (0) 2009.08.06
28. 열거형  (0) 2009.08.06
Posted by holland14
: