본문 바로가기

C#/공부

2. 연산자와 배열

1. 수식 연산자, 증감 연산자

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 100, b = 10;
            //수식 연산자
            Console.WriteLine("{0} + {1} = {2}", a, b, a + b);
            Console.WriteLine("{0} - {1} = {2}", a, b, a - b);
            Console.WriteLine("{0} * {1} = {2}", a, b, a * b);
            Console.WriteLine("{0} / {1} = {2}", a, b, a / b);
            Console.WriteLine("{0} % {1} = {2}", a, b, a % b);
            Console.WriteLine();

            a = 1;
            //증감 연산자
            Console.WriteLine("++a={0}",++a); // a의 값을 1 증가시키고, a의 값을 출력 (a = 2)
            Console.WriteLine("a++={0}", a++); // a의 값을 출력한 뒤, a의 값을 1 증가 (a = 3)
            Console.WriteLine("--a={0}", --a); // a의 값을 1 감소시키고, a의 값을 출력 (a = 2)
            Console.WriteLine("a--={0}", a--); // a의 값을 출력한 뒤, a의 값을 1 감소 (a = 1)
        }
    }
}

 

 

 

2. 관계, 할당, 논리 연산자

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10, b = 20;
            //관계 연산자
            Console.WriteLine("a < b {0}",a < b);
            Console.WriteLine("a > b {0}", a > b);
            Console.WriteLine("a == b {0}", a == b);
            Console.WriteLine("a != b {0}", a != b);
            Console.WriteLine("a >= b {0}", a >= b);
            Console.WriteLine("a <= b {0}", a <= b);
            Console.WriteLine();
            //할당 연산자
            a += b;
            Console.WriteLine(a);
            a -= b;
            Console.WriteLine(a);
            a *= b;
            Console.WriteLine(a);
            a /= b;
            Console.WriteLine(a);
            a %= b;
            Console.WriteLine(a);
            Console.WriteLine();
            //논리 연산자
            Console.WriteLine("a > 5 && b > 10 {0}",a > 5 && b > 10);    //AND
            Console.WriteLine("a > 5 || b > 30 {0}", a > 5 || b > 30);    //OR
            Console.WriteLine("!(a < b) {0}", !(a < b));            //NOT  
        }
    }
}

 

 

 

3. 비트 연산자, 시프트 연산자

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 15, b = 8;      //a=1111, b=1000      
            //비트 연산자
            Console.WriteLine("a & b={0}",a & b);   //1111 & 1000 = 1000
            Console.WriteLine("a | b={0}", a | b);   //1111 | 1000 = 1111
            Console.WriteLine("a ^ b={0}", a ^ b);   //1111 ^ 1000 = 0111
            Console.WriteLine();
            //시프트 연산자
            Console.WriteLine("a << b={0}", a << 2);  //111100
            Console.WriteLine("a >> b={0}", a >> 2);  //0011
        }
    }
}

 

 

 

4. 배열

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[6] { 10, 20, 30, 40, 50, 60 };

            for (int i = 0; i < array.Length; i++)
                Console.Write("array[" + i + "]={0}, ", array[i]);

            Console.WriteLine("\r\n배열의 차원={0}, 길이={1}", array.Rank, array.Length);

            Console.WriteLine("\r\nClear 메소드");
            Array.Clear(array, 0, 5);
            for (int i = 0; i < array.Length; i++)
                Console.Write("array[" + i + "]={0}, ", array[i]);
            Console.WriteLine("\r\n\r\nResize 메소드");
            Array.Resize(ref array, array.Length + 3);
            for (int i = 0; i < array.Length; i++)
                Console.Write("array[" + i + "]={0}, ", array[i]);
            Console.WriteLine();
        }
    }
}

 

 

 

5. 다차원 배열

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] array = new int[3, 6] { { 0, 10, 20, 30, 40, 50 }, { 5, 15, 25 , 35, 45,55 }, { 0, 1, 2, 3, 4, 5 } };
            Console.WriteLine("다차원 배열 출력");
            for (int i = 0; i < array.GetLength(0); i++)
            {                
                for (int j = 0; j < array.GetLength(1); j++)
                    Console.Write("[" + i + ", " + j + "] = {0}, ", array[i, j]);
                Console.WriteLine();
            }
        }
    }
}

 

'C# > 공부' 카테고리의 다른 글

C# 실력 늘리기 1일차  (0) 2022.07.19
시리얼 통신  (0) 2022.07.11
4. 클래스와 메소드  (0) 2022.07.06
3. 조건문 / 반복문 / 제어문  (0) 2022.07.06
1. C# 시작 - Hello, world! 와 변수, 자료형, 서식 지정자  (0) 2022.07.05