728x90

문자열을 입력 받아 글자수를 세어주세요.

 

입력된 글자 중 알파벳 대문자의 개수

소문자의 개수, 특수문자의 개수

숫자의 개수를 클릭하세요

 

--------------------------------------------------

123%$#ABCDxyz

대문자 :4

소문자 3

숫자 : 3

특수문자 : 3

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
 
namespace Quiz_String
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            char[] chArr = s.ToCharArray();
 
            int big = 0;
            int small = 0;
            int num = 0;
            int m = 0;
 
            foreach(char c in chArr)
            {
                if ((c >= 'A'&& (c <= 'Z'))
                {
                    big += 1;
                }
                else if ((c >= 'a'&& (c <= 'z'))
                {
                    small += 1;
                }
                else if ((c >= '0' && (c < '9')))
                {
                    num += 1;
                }
                else
                {
                    m += 1;
                }
            }
 
            Console.WriteLine($"대문자 : {big}\n소문자 : {small}\n숫자 : {num}\n특수문자 : {m}");
        }
    }
}
cs

 

 

소수구하기

 

1과 자기 자신으로 나누어 떨어지는 수를 소수라고 합니다. 

 

1 ~ 100 사이의 소수를 구해 봅시다.

 

--------------------------------------------------------

 

2 3 5 7 13 17 ... 97

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
using System;
 
namespace Quiz_Prime
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = int.Parse(Console.ReadLine());
            
            for (int i=2;i<=num ;i++ )
            {
                bool primebool = true;
                if (primebool == false)
                    continue;
                for(int j = 2; j < i; j++)
                {
                    if (i % j == 0)
                    {
                        primebool = false;
                        break;
                    }
                }
                if(primebool == true)
                {
                    Console.Write(i+" ");
                }
            }
        }
    }
}
cs

 

 

클래스와 컬렉션 연습

1. 고양이(Cat) 클래스를 만드세요.

  멤버는 name, color

2. 고양이 클래스를 부모 클래스로 하는 페르시안(Persian) 고양이 클래스를 만드세요.

 

3. 강아지(Dog) 클래스를 만드세요.

   멤버는 name, color

4. 강아지 클래스를 부모 클래스로 하는 불독(Bulldog) 강아지 클래스를 만드세요.

 

5. 페르시안 고양이 2마리를 객체를 만들고 초기화 합니다.

   1) kitty, 하얀색

   2) bbomi, 회색

 

6. 불독 강아지 2마리를 객체를 만들고 초기화 합니다.

  1) tomy, 검은색

  2) kaltok, 회백색

 

7. ArrayList animal 을 만들고

   4마리 고양이 객체를 모두 삽입하세요.

 

8. List catList를 만들어 2마리의 고양이를 담으세요.

 

9. Stack dogStack 만들어 2마리 강아지를 담으세요.

 

 

10. ArrayList, List, Stack 모두 for 또는 foreach문을 사용하여 속성(e.g.Name)을 출력해 보세요.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections;
using System.Collections.Generic;
 
namespace Quiz_Class
{
    class Cat
    {
        private string name;
        private string color;
 
        public string Name
        {
            get;
            set;
        }
        public string Color
        {
            get;
            set;
        }
    }
    class Persian : Cat
    {
 
    }
    class Dog
    {
        private string name;
        private string color;
 
        public string Name
        {
            get;
            set;
        }
        public string Color
        {
            get;
            set;
        }
    }
    class Bulldog : Dog
    {
 
    }
    class Program
    {
        static void Main(string[] args)
        {
            Persian cat1 = new Persian();
            cat1.Name = "kitty";
            cat1.Color = "하얀색";
            Cat cat2 = new Cat();
            cat2.Name = "bbommi";
            cat2.Color = "회색";
 
            Bulldog dog1 = new Bulldog();
            dog1.Name = "tommy";
            dog1.Color = "검은색";
            Bulldog dog2 = new Bulldog();
            dog2.Name = "kaltok";
            dog2.Color = "회백색";
 
            ArrayList animal = new ArrayList();
            animal.Add(cat1);
            animal.Add(cat2);
            animal.Add(dog1);
            animal.Add(dog2);
 
            List<Cat> catList = new List<Cat>();
            catList.Add(cat1);
            catList.Add(cat2);
 
            Stack<Dog> dogStack = new Stack<Dog>();
            dogStack.Push(dog1);
            dogStack.Push(dog2);
 
            for (int i = 0; i < animal.Count-2; i++)
                Console.WriteLine(((Cat)animal[i]).Name);
            for (int i = 2; i < animal.Count; i++)
                Console.WriteLine(((Dog)animal[i]).Name);
 
            foreach (Cat c in catList)
                Console.WriteLine(c.Name);
 
            for (int i = 0; i <= dogStack.Count; i++)
                Console.WriteLine((dogStack.Pop()).Name);
 
            while(dogStack.Count>0)
                Console.WriteLine((dogStack.Pop()).Name);
        }
    }
}
cs

 

 

대리자(Delegator)

코드(메소드)를 대신 실행하는 객체

메소드를 호출하듯 사용(즉, 인수를 입력하고 결과를 반환 받음)

단, 대리자가 실행할 코드는 컴파일 시점이 아닌 실행 시점에 결정

 

대리자의 선언과 사용

delegate 키워드를 이용하여 선언

메소드와 같이 대리자 또한 매개변수 목록과 반환 형식을 가짐

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
 
namespace DelegateTest
{
    delegate int MyDelegate(int a, int b); // namespace 안 class 밖
 
    class Calculator
    {
        // 덧셈 메소드
        public int Plus(int a, int b)
        {
            return a + b;
        }
        //뺄셈 메소드
        public int Minus(int a, int b)
        {
            return a - b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Calculator Calc = new Calculator();
            MyDelegate Callback;
 
            // 함수포인터 유사~~!!
            Callback = new MyDelegate(Calc.Plus);
            Console.WriteLine(Callback(34));
 
            Callback = new MyDelegate(Calc.Minus);
            Console.WriteLine(Callback(75));
        }
    }
}
cs

 

 

Quiz) 대리자를 이용하여 곱하기 연산 Multiple(int a, int b)를 만들어보세요

Callback(a, b)를 이용하여 곱셈기능을 대신처리하세요

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
 
namespace Quiz_Delegate
{
    delegate int QuizDelegate(int a, int b);
    class Calculator
    {
        public int Multiple(int a, int b)
        {
            return a * b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Calculator Calc = new Calculator();
            QuizDelegate Callback;
 
            Callback = new QuizDelegate(Calc.Multiple);
            Console.WriteLine(Callback(3,4));
        }
    }
}
cs

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
 
namespace UsingCallback
{
    // 버블정렬코드, 오름차순-내림차순
    delegate int Compare(int a, int b);
    class Program
    {
        static int AscendCompare(int a, int b)
        {
            if (a > b)
            {
                return 1;
            }
            else if(a == b)
            {
                return 0;
            }
            else
            {
                return -1;
            }
        }
 
        static int DescendCompare(int a, int b)
        {
            if (a < b)
            {
                return 1;
            }
            else if (a == b)
            {
                return 0;
            }
            else
            {
                return -1;
            }
        }
 
        static void BubbleSort(int[] DataSet, Compare Comparer)
        {
            int i = 0;
            int j = 0;
            int temp = 0;
 
            for(i=0;i<DataSet.Length - 1; i++)
            {
                for (j = 0; j < DataSet.Length - (i + 1); j++)
                {
                    if(Comparer(DataSet[j], DataSet[j+1]) > 0)
                    {
                        // 변수 교환
                        temp = DataSet[j+1];
                        DataSet[j + 1= DataSet[j];
                        DataSet[j] = temp;
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            int[] array = { 374210 };
 
            Console.WriteLine("오름차순으로...");
            BubbleSort(array, new Compare(AscendCompare));
 
            for(int i = 0; i < array.Length; i++)
            {
                Console.Write($"{array[i]} ");
            }
 
            Console.WriteLine();
 
            Console.WriteLine("내림차순으로...");
            BubbleSort(array, new Compare(DescendCompare));
 
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write($"{array[i]} ");
            }
        }
    }
}
cs

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
 
namespace DelegateChain02
{
    delegate void Notify(string message);
    class Notifier
    {
        // 멤버 변수
        // 1. C#은 멤버변수로 delegate를 가질 수 있다.
        // 2. C#은 delegate의 이름을 Type처럼 사용한다.
        public Notify EventOccured; // delegate를 멤버로 가진 클래스
    }
    class EventListener
    {
        private string name;
 
        // 생성자
        public EventListener(string name)
        {
            this.name = name;
        }
 
        // 멤버 메소드
        public void SomethingHappened(string message)
        {
            Console.WriteLine($"{name}.SomethingHappened : {message}");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Notifier notifier = new Notifier();
            EventListener listener1 = new EventListener("Listener1");
            EventListener listener2 = new EventListener("Listener2");
            EventListener listener3 = new EventListener("Listener3");
 
            notifier.EventOccured += listener1.SomethingHappened;
            notifier.EventOccured += listener2.SomethingHappened;
            notifier.EventOccured += listener3.SomethingHappened;
            notifier.EventOccured("You've got mail");
 
            Console.WriteLine();
        }
    }
}
cs

 

 

델리게이트는 이벤트 여러 개를 매크로처럼 사용할 수 있게 해준다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
 
namespace DelegateChain02
{
    delegate void Notify(string message);
    class Notifier
    {
        // 멤버 변수
        // 1. C#은 멤버변수로 delegate를 가질 수 있다.
        // 2. C#은 delegate의 이름을 Type처럼 사용한다.
        public Notify EventOccured; // delegate를 멤버로 가진 클래스
    }
    class EventListener
    {
        private string name;
 
        // 생성자
        public EventListener(string name)
        {
            this.name = name;
        }
 
        // 멤버 메소드
        public void SomethingHappened(string message)
        {
            Console.WriteLine($"{name}.SomethingHappened : {message}");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Notifier notifier = new Notifier();
            EventListener listener1 = new EventListener("Listener1");
            EventListener listener2 = new EventListener("Listener2");
            EventListener listener3 = new EventListener("Listener3");
 
            notifier.EventOccured += listener1.SomethingHappened;
            notifier.EventOccured += listener2.SomethingHappened;
            notifier.EventOccured += listener3.SomethingHappened;
            notifier.EventOccured("You've got mail");
 
            Console.WriteLine();
 
            // 2번째
            notifier.EventOccured -= listener2.SomethingHappened;
            notifier.EventOccured("Download complete");
 
            Console.WriteLine();
 
            // 3번째
            notifier.EventOccured = new Notify(listener2.SomethingHappened) + new Notify(listener3.SomethingHappened);
            notifier.EventOccured("Nuclear launch detected");
 
            Console.WriteLine();
 
            // 4번째 - Notify 객체 이용
            Notify notify1 = new Notify(listener1.SomethingHappened);
            Notify notify2 = new Notify(listener2.SomethingHappened);
 
            notifier.EventOccured = (Notify)Delegate.Combine(notify1, notify2);
            notifier.EventOccured("Fire");
 
            Console.WriteLine();
 
            // 5번째
            notifier.EventOccured = (Notify)Delegate.Remove(notifier.EventOccured, notify2);
            notifier.EventOccured("RPG!");
        }
    }
}
cs

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
 
namespace AnonymousMethod_basic
{
    delegate int Calculate(int a, int b);
    // Calculate 클래스 생략해 보았다
    class Program
    {
        static void Main(string[] args)
        {
            Calculate Calc;
 
            Calc = delegate (int a, int b)
            {
                return a + b;
            };
 
            Console.WriteLine(Calc(34));
        }
    }
}
cs

 

 

익명, 이너 클래스 등등 2명 이상이면 되도록 쓰지 않기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
 
namespace Quiz_CompleteNum
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = int.Parse(Console.ReadLine());
 
            int result = 0;
            for(int i = 1; i < num; i++)
            {
                if (num % i == 0)
                {
                    result += i;
                }
            }
            if(result == num)
            {
                Console.WriteLine("yes");
            }
            else
            {
                Console.WriteLine("no");
            }
        }
    }
}
cs

 

728x90
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기