2019. 1. 16. 15:01 Windows/Tip

EXERD

http://ko.exerd.com


'Windows > Tip' 카테고리의 다른 글

GitHub 플러그인 설치 연동  (0) 2019.01.16
[WINTIP] 바로가기  (0) 2018.07.30
[WINTIP] 시작 프로그램 등록 하는 법들  (0) 2018.07.30
Posted by 안낭우훗

복붙도 귀찮다 ...


http://www.developer-leby.kim/95

'Windows > Tip' 카테고리의 다른 글

EXERD  (0) 2019.01.16
[WINTIP] 바로가기  (0) 2018.07.30
[WINTIP] 시작 프로그램 등록 하는 법들  (0) 2018.07.30
Posted by 안낭우훗

C#에서 |와 ||, &와 &&의 차이

C#에서 코딩을 하다보면 AND 조건을 위해서는 &&, OR 조건을 위해서는 ||를 쓴다. 하지만 종종 & 또는 | 이런 식으로 하나씩만 쓰는 경우를 볼 때가 있다. 주로 정규식 객체를 초기화하는 경우 혹은 리플렉션을 이용하여 프라이빗 멤버에 접근하려고 하는 경우가 될텐데, 아래 코드를 살짝 들여다 보도록 하자

// Initialiseing a regular expression instance.
var regex = new Regex("pattern", RegexOptions.IgnoreCase | RegexOptions.Compiled);

// Accessing a private method via reflection.
var mi = this.GetType().GetMethod("MethodName", BindingFlags.NonPublic | BindingFlags.Instance);

위의 코드에서 볼 수 있다시피 정규식 초기화 또는 리플렉션을 통해 프라이빗 메소드에 접근하려는 경우에서는 |를 흔히 볼 수 있다. 그렇다면 |||의 차이는 무엇일까? 프로젝트 내 초급 개발자들이 흔히 물어보곤 하는데, 그냥 이걸 Bitwise 연산자이다! 라고만 하면 전공자가 아닌 이상 사실 잘 와닿지 않는다. 그래서 조금 더 풀어 쓰자면 아래와 같다고 할 수 있다.

var result1 = condition1 || condition2 || condition3;
var result2 = condition1 | condition2 | condition3;

result1의 값은 condition1이 참이라면 더이상 condition2condition3를 수행하지 않고 TRUE가 된다. 이미 OR 연산에서 첫번째 조건이 참이 됐기 때문에 더이상 그 이후를 수행할 의미가 없기 때문이다. 반면 result2의 값은 condition1이 참/거짓인것과 상관 없이 condition2, condition3를 모두 확인하고 그 세 결과값을 통해 하나라도 참이면 TRUE를 갖게 된다. 당연히 result2를 수행하는 것이 비용이 높을 것이다.

var result3 = condition1 && condition2 && condition3;
var result4 = condition1 & condition2 & condition3;

마찬가지로 result3의 값은 condition1의 값이 거짓이라면 곧바로 FALSE를 반환하고 condition2, condition3를 수행하지 않는다. 반면에 result4의 값은 모든 condition1, condition2, condition3를 수행하고 하나라도 거짓값이 있으면 FALSE를 반환하게 된다.

결국 아주 특별한 상황이 아니라면 굳이 비용이 높은 | 또는 &를 사용할 필요가 없다. 맨 위의 예제 코드는 언어의 설계가 그렇게 됐기 때문에 쓰는 것일 뿐이다.

참고로 VB.NET 에서는 |Or, ||OrElse, &And, &&AndAlso에 대응한다.


위 글을 생활코딩 페이스북 커뮤니티에 올리고 난 후 많은 피드백을 받았다. 그에 따라 원문을 다시금 보충하고자 한다. 모든 내용은 C#의 스펙에 따른 것이므로 다른 언어와 다를 수 있다는 점을 전제로 하자.

&&||는 조건부 논리 연산자 conditional logical operator1 라고 부른다. 이는 short-circuiting 논리 연산자라고도 불리는데, 여기서 이 short-circuiting의 의미는 앞의 조건을 만족하면 뒤의 조건들은 무시한다는 뜻이다. C# 스펙에 정의되어 있는 불린형 조건부 논리 연산자 boolean conditional logical operator2 로서 이 두 연산자가 작동하는 방식은 다음과 같은 방식을 따른다.

  • condition1 && condition2의 의미는 condition1 ? condition2 : false이다.
  • condition1 || condition2의 의미는 condition1 ? true : condition2이다.

반면 &|는 논리 연산자 logical operator3 라고 부른다. 즉 &&||보다 좀 더 넓은 범위를 가진 연산자인 셈이다. 이 논리 연산자는 크게 세가지 형태의 미리 정의된 형태를 볼 수 있는데, 정수형 논리 연산자 integer logical operator4, 열거형 논리 연산자 enumeration logical operator5, 불린형 논리 연산자 boolean logical operator6가 있다. 정수형 논리 연산자는 비트 연산을 수행하고 열거형 논리 연산자 역시 비트 연산을 수행하는데 이는 열거형 내부적으로 정수형으로 변환이 가능하기 때문이다. 단, 이 경우 열거형은 [Flag] 속성 클라스와 더불어 2^n 형태의 값을 가져야 한다. 마지막으로 불린형 논리 연산자는 제시된 피연산자를 모두 검사한 후 결과를 반환한다.

// integer logical operator
var operand1 = 6;                      // 0110
var operand2 = 10;                     // 1010
var result = operand1 | operand2       // 1110 = 14

// enumeration logical operator
var operand1 = RegexOptions.IgnoreCase // 1 = 0001
var operand2 = RegexOptions.Compiled   // 8 = 1000
var result = operand1 | operand2       // 9 = 1001

// boolean logical operator
var operand1 = true;                   // 1
var operand2 = false;                  // 0
var result1 = operand1 | operand2      // 1
var result2 = operand1 & operand2      // 0

위의 코드에서 알 수 있다시피 &|는 모두 비트 연산을 수행하고 그 결과를 반환시킨다.

그렇다면, &, |&&, || 사이의 관계는 어떠한 것인가? 앞서 불린형 조건부 논리 연산자 항목2에서 언급했다시피 &&||는 단축형 논리 연산자를 제공하는 것이어서, 굳이 첫번째 피연산자가 조건에 들어맞는다면 그 다음 피연산자를 검사할 필요가 없이 그자리에서 결과값을 반환하고 종료하는 것이다. 따라서, & 또는 | 연산자는 불린형 논리 연산에서는 피연산자 모두를 검사할 필요가 없다면 쓰지 않는 것이 좋다.


참고:

Published Feb 17, 2014 by

https://blog.aliencube.org/ko/2014/02/17/difference-between-single-pipe-and-double-pipes-in-c-sharp/

Posted by 안낭우훗

https://www.devexpress.com/Support/Center/Question/Details/T231674/support-for-the-decimal-data-type-on-the-cell

Hello Alex,
The decimal type is not supported in MS Excel. All decimal values are stored as double values in an MS Excel document and our internal model.
Thus, you need to convert decimal values to double before assigning them with the SpreadsheetControl cell values. Alternatively, you can use the Cell.SetValue method:

Posted by 안낭우훗

https://documentation.devexpress.com/CoreLibraries/DevExpress.Spreadsheet.Range.class

// A range that includes cells from the top left cell (A1) to the bottom right cell (B5).
Range rangeA1B5 = worksheet["A1:B5"];

// A rectangular range that includes cells from the top left cell (C5) to the bottom right cell (E7).
Range rangeC5E7 = worksheet["C5:E7"];

// The C4:E7 cell range located in the "Sheet3" worksheet.
Range rangeSheet3C4E7 = workbook.Range["Sheet3!C4:E7"];

// A range that contains a single cell (E7).
Range rangeE7 = worksheet["E7"];

// A range that includes the entire column A.
Range rangeColumnA = worksheet["A:A"];

// A range that includes the entire row 5.
Range rangeRow5 = worksheet["5:5"];

// A minimal rectangular range that includes all listed cells: C6, D9 and E7.
Range rangeC6D9E7 = worksheet.Range.Parse("C6:D9:E7");

// A rectangular range whose left column index is 0, top row index is 0,
// right column index is 3 and bottom row index is 2. This is the A1:D3 cell range.
Range rangeA1D3 = worksheet.Range.FromLTRB(0, 0, 3, 2);

// A range that includes the intersection of two ranges: C5:E10 and E9:G13.
// This is the E9:E10 cell range.
Range rangeE9E10 = worksheet["C5:E10 E9:G13"];

// Create a defined name for the D20:G23 cell range.
worksheet.DefinedNames.Add("MyNamedRange", "Sheet1!$D$20:$G$23");
// Access a range by its defined name.
Range rangeD20G23 = worksheet["MyNamedRange"];

Range rangeA1D4 = worksheet["A1:D4"];
Range rangeD5E7 = worksheet["D5:E7"];
Range rangeRow11 = worksheet["11:11"];
Range rangeF7 = worksheet["F7"];

// Create a complex range using the Range.Union method.
Range complexRange1 = worksheet["A7:A9"].Union(rangeD5E7);

// Create a complex range using the IRangeProvider.Union method.
Range complexRange2 = worksheet.Range.Union(new Range[] { rangeRow11, rangeA1D4, rangeF7 });

// Fill the ranges with different colors.
complexRange1.FillColor = myColor1;
complexRange2.FillColor = myColor2;

// Use the Areas property to get access to a component of a complex range.
complexRange2.Areas[2].FillColor = Color.Beige;

Posted by 안낭우훗

/*문제: 30
1) 0에서 19까지 정수가 저장된 배열의 수를 임의로 섞어라.
int[] numbers = {0, ..., 19}
2) 출력 예:
17 2 1 19 5 ...

3) 숫자를 섞기 위해 임의로 인덱스를 뽑아내기
Math.random() * 20
*/

 

 


class Test{

 public static void main(String[] args) throws Exception {

   int[] numbers = new int[20];
   for (int i = 0; i <= 19; i++) {
     numbers[i] = i;
   }

   int index1, index2, temp;
   for (int i = 0; i < numbers.length; i++) {
     index1 = (int)(Math.random() * 20);
     index2 = (int)(Math.random() * 20);
     temp = numbers[index1];
     numbers[index1] = numbers[index2];
     numbers[index2] = temp;
   }
   for (int i : numbers) {
    System.out.printf("%d \n", i);
  }

  }
}

 

'ALGORITHM > 문제해결' 카테고리의 다른 글

[JAVA] 여러가지 구구단  (0) 2018.08.11
[JAVA] 여러가지 반복문  (0) 2018.08.11
[JAVA] 유니코드 문자  (0) 2018.08.11
[JAVA] 특정 문자를 다른 문자로 교체  (0) 2018.08.11
[JAVA] 분자 분모 계산  (0) 2018.08.11
Posted by 안낭우훗

class Test{

 public static void main(String[] args) throws Exception {

 for (int i = 2; i <= 9; i++) {
   for (int j = 1; j <= 9; j++) {
     System.out.printf("%d * %d = %d    ", i, j, (i * j));
   }
   System.out.println();
 }

 for (int i = 2; i <= 9; i++) {
   for (int j = 1; j <= 9; j++) {
     System.out.printf("%d * %d = %d \n", i, j, (i * j));
   }
   System.out.println();
 }

 for (int i = 9; i >= 2; i--) {
   for (int j = 9; j >= 1; j-- ) {
     System.out.printf("%d * %d = %d \n", i, j, (i * j));
   }
   System.out.println();
 }

 for (int i = 9; i >= 2; i--) {
   for (int j = 1; j <= 9; j++ ) {
     System.out.printf("%d * %d = %d \n", i, j, (i * j));
   }
   System.out.println();
 }

 int i = 2;
 while (i <= 9)  {
   int j = 1;
   while (j <= 9) {
     System.out.printf("%d * %d = %d \n", i, j, (i * j));
     j++;
   }
   System.out.println();
   i++;
 }
 
  }
}

'ALGORITHM > 문제해결' 카테고리의 다른 글

[JAVA] 임의의 섞기 ( Math.random() )  (0) 2018.08.13
[JAVA] 여러가지 반복문  (0) 2018.08.11
[JAVA] 유니코드 문자  (0) 2018.08.11
[JAVA] 특정 문자를 다른 문자로 교체  (0) 2018.08.11
[JAVA] 분자 분모 계산  (0) 2018.08.11
Posted by 안낭우훗

 

 

class Test{

 public static void main(String[] args) throws Exception {
   /*문제: 23
   1) 반복문을 사용하여 5에서 18까지 출력하라!
   2) 출력 예:
   5 6 7 8 ... 18
   */
   for (int i = 5 ; i <= 18; i++) {
     System.out.printf("%d ", i);
   }
   System.out.println();

   /*문제: 24
   1) 반복문을 사용하여 20에서 5까지 출력하라!
   2) 출력 예:
   20 19 18 ... 5
   */
   for (int i = 20; i >= 5; i--) {
     System.out.printf("%d ", i);
   }
   System.out.println();

   /*문제: 25
   1) 반복문을 사용하여 2에서 30까지 3씩 증가한 값을 출력한다.
   2) 출력 예:
   2 5 8 11 ...
   */
   for (int i = 2; i <= 30; i+=3) {
     System.out.printf("%d ", i);
   }
   System.out.println();

   /*문제: 26
   1) 반복문을 사용하여 4에서 30까지 1씩 증가한 값을 출력한다.
      단 10 이상 20 미만인 경우는 출력하지 말아라.
   2) 출력 예:
   2 5 8 11 ...
   */
   for (int i = 4; i <= 30; i++) {
      if (i >= 10 && i < 20) {
        continue;
      }
      System.out.printf("%d ", i);
   }
   System.out.println();

   /*문제: 27
   1) 반복문을 사용하여 0에서 10까지 1씩 증가한 값을 출력한다.
      동시에 0부터 3씩 증가한 값을 출력한다.
   2) 출력 예:
   0 - 0
   1 - 3
   2 - 6
   3 - 9
   */
   for (int i = 0, j = 0; i <= 10; i++, j += 3) {
     System.out.printf("%d - %d\n", i, j);
   }
   System.out.println();

  }
}

'ALGORITHM > 문제해결' 카테고리의 다른 글

[JAVA] 임의의 섞기 ( Math.random() )  (0) 2018.08.13
[JAVA] 여러가지 구구단  (0) 2018.08.11
[JAVA] 유니코드 문자  (0) 2018.08.11
[JAVA] 특정 문자를 다른 문자로 교체  (0) 2018.08.11
[JAVA] 분자 분모 계산  (0) 2018.08.11
Posted by 안낭우훗

  /*문제: 22
 1) 주어진 문자열의 대문자 알파벳의 개수를 센다.
 2) 실행 및 출력 예:
 >java Test circleofnumbers
 b:1
 c:2
 e:2
 f:1
 i:1

 

  int no = Integer.parseInt(args[0]);

    char ch = 'A'; // A 문자의 유니코드 값이 저장. 0x0041 = 65 = A
    System.out.println((char)(ch + no));
    System.out.println(44032); 
    System.out.println((char)44032); // 가

 

 class Test {

 public static void main(String[] args) throws Exception {
   char[] chars = args[0].toUpperCase().toCharArray();
   int[] counts = new int[26];

   for (int i = 0; i < counts.length; i++) {
       counts[i] = 0;
   }

   for(int i = 0; i < chars.length; i++) {
      counts[chars[i] - 'A']++;
   }
   for (int i = 0; i < counts.length; i++) {
     if (chars[i] <= 0) {
       continue;
     }
     System.out.printf("%c: %d \n", (char) (i + 'A'), counts[i]);
   }

  }
}

'ALGORITHM > 문제해결' 카테고리의 다른 글

[JAVA] 여러가지 구구단  (0) 2018.08.11
[JAVA] 여러가지 반복문  (0) 2018.08.11
[JAVA] 특정 문자를 다른 문자로 교체  (0) 2018.08.11
[JAVA] 분자 분모 계산  (0) 2018.08.11
[JAVA] 등비수열인지 판단  (0) 2018.08.11
Posted by 안낭우훗

/*문제
1) 문자 배열이 있다. 특정 문자를 다른 문자로 교체
2) 배열 데이터 예:
char[] chars = {'i', 'n', 't', 'e', 'g', 'e', 'r'};
char ch1 = 't';
char ch2 = 'x';
3) 실행 및 출력 예:
>java Test
결과: inxeger

 

 

 

class Test{

 public static void main(String[] args) throws Exception {

   char[] chars = {'i', 'n', 't', 'e', 'g', 'e', 'r'};
   char ch1 = 't';
   char ch2 = 'x';

   for (int i = 0; i < chars.length; i++) {
     if (chars[i] == ch1) {
       chars[i] = ch2;
     }
   }

   for (char i : chars) {
     System.out.printf("%c \n",  i);
   }
  }
}

Posted by 안낭우훗
이전버튼 1 2 3 4 5 이전버튼

블로그 이미지
좋은싸이트 공유, 재해석 , 공부 정리, 틀린거 알려 주세요~
안낭우훗

태그목록

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.5
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

최근에 올라온 글

최근에 달린 댓글

글 보관함