πŸ‘©‍πŸ’» Language/C, C++

1κ°•. [주말에 λ°°μš°λŠ” C/C++] Cμ–Έμ–΄ - μ—°μ‚°μž

kongmi 2022. 12. 31. 19:51

1. μ—°μ‚°μž(Operator)

 (1) μ‚°μˆ μ—°μ‚°μž

#include <stdio.h>

int main() {
	int a;
	int b;

	a = 1;
	b = 2;
    
    //1번
	int ret = a / b;
	printf("%d / %d = %d\n", a, b, ret);

   //2번
	float fret = a / b;
	printf("%d / %d = %f\n", a, b, fret);

   //3번
	fret = a / (float)b;   // (float) ν˜•λ³€ν™˜ μ—°μ‚°μž
	printf("%d / %d = %f\n", a, b, fret);

	return 0;
}

* 1을 2둜 λ‚˜λˆ„λ©΄ 0.5 μ΄μ§€λ§Œ μ»΄ν“¨ν„°λŠ” μ •μˆ˜ / μ •μˆ˜ = μ •μˆ˜ 둜 λ‚˜νƒ€λ‚Έλ‹€.

λ”°λΌμ„œ, 3번과 같이 float둜 ν˜•λ³€ν™˜ ν›„ 좜λ ₯ν•΄μ•Ό ν•œλ‹€.

데이터 νƒ€μž… 쀑 크기가 더 큰 κ²ƒμœΌλ‘œ μžλ™ ν˜•λ³€ν™˜ 되기 λ•Œλ¬Έμ— λ‘˜ 쀑 ν•˜λ‚˜λ§Œ ν•΄μ€˜λ„ λœλ‹€.

 

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main() {
	srand(time(NULL));   // λ‚œμˆ˜λ°œμƒμš© μ”¨λ“œκ°’μ„ μ„€μ •

	// μž„μ˜μ˜κ°’μ„ μΌμ •ν•œ λ²”μœ„μ˜ κ°’μœΌλ‘œ λ§Œλ“€λ•Œ μ‚¬μš©ν•©λ‹ˆλ‹€.
	// λ‚œμˆ˜(λžœλ€κ°’)
	int randvalue = rand(); // λžœλ€κ°’μ„ ꡬ함.

	int direction = randvalue % 4;

	// North: 0, South: 1, West: 2, East 3
	printf("randvalue = %d, direction = %d\n", randvalue, direction);

	return 0;
}

μ‚°μˆ  μ—°μ‚°μž 쀑 '%'λŠ” μž„μ˜μ˜ 값을 μΌμ •ν•œ λ²”μœ„μ˜ κ°’μœΌλ‘œ λ§Œλ“€λ•Œ μ‚¬μš©.

 - λ‚œμˆ˜ λ°œμƒ ν›„ λ‚˜μ˜€λŠ” 값에 따라 '동,μ„œ,남,뢁'으둜 μ§€μ •ν•˜κ³ μž 함.

 - 뢁:0, 남:1, μ„œ:2, 동:3

 - 즉, 0~3 λ²”μœ„μ˜ μˆ«μžκ°€ λ‚˜μ˜€κ²Œ ν•΄μ•Όν•˜λ―€λ‘œ "λ‚œμˆ˜κ°’ / 4의 λ‚˜λ¨Έμ§€ κ°’"으둜 λ‚˜νƒ€λ‚Έλ‹€.

 => λ‚œμˆ˜κ°’ % 4 

 

 (2) 관계 μ—°μ‚°μž

#include <stdio.h>

int main() {
    int a = 20;

    a = -a;

    printf("a = %d\n", a);

    // κ΄€κ³„μ—°μ‚°μž
    // <, >, <=, >= , ==, != 

    int left = 20;
    int right = 30;

    int ret = left < right;   // κ΄€κ³„μ—°μ‚°μžμ˜ μ—°μ‚°μ˜ 결과값은 데이타 νƒ€μž…μ΄ λ…Όλ¦¬νƒ€μž…
    printf("%d < %d = %d\n", left, right, ret);

    ret = left > right;
    printf("%d > %d = %d\n", left, right, ret);

    ret = left <= right;
    printf("%d <= %d = %d\n", left, right, ret);

    ret = left >= right;
    printf("%d >= %d = %d\n", left, right, ret);

    ret = left == right;
    printf("%d == %d = %d\n", left, right, ret);

    ret = left != right;
    printf("%d != %d = %d\n", left, right, ret);

    return 0;
}

* 논리값은 true, false
  ν—ˆλ‚˜ Cμ–Έμ–΄μ—λŠ” 논리 νƒ€μž…μ΄ μ—†μŠ΅λ‹ˆλ‹€.
  λ”°λΌμ„œ, Cμ–Έμ–΄λŠ” μ •μˆ˜κ°’μ„ κ°€μ§€κ³  λ…Όλ¦¬νƒ€μž…μ„ μ‚¬μš©ν•©λ‹ˆλ‹€.
  => 0은 κ±°μ§“, 0μ΄μ™Έμ˜ μˆ«μžλŠ” μ°Έ 으둜 λ°›μ•„λ“œλ¦½λ‹ˆλ‹€.

 

 (3) 논리 μ—°μ‚°μž

#include <stdio.h>

int main() {

	int TRUE = 1;
	int FALSE = 0;

	int ret = TRUE && TRUE;
	printf("TRUE && TRUE = %d\n", ret);

	ret = TRUE && FALSE;
	printf("TRUE && FALSE = %d\n", ret);

	ret = FALSE && TRUE;
	printf("FALSE && TRUE = %d\n", ret);

	ret = FALSE && FALSE;
	printf("FALSE && FALSE = %d\n", ret);


	ret = TRUE || TRUE;
	printf("TRUE || TRUE = %d\n", ret);

	ret = TRUE || FALSE;
	printf("TRUE || FALSE = %d\n", ret);

	ret = FALSE || TRUE;
	printf("FALSE && TRUE = %d\n", ret);

	ret = FALSE || FALSE;
	printf("FALSE && FALSE = %d\n", ret);


	ret = !TRUE;
	printf("!TRUE = %d\n", ret);

	ret = !FALSE;
	printf("!FALSE = %d\n", ret);


	return 0;
}

1.  && (논리And), ||(논리 OR), ! (논리 not)
  * λ…Όλ¦¬μ—°μ‚°μžμ˜ ν”Όμ—°μ‚°μž 데이타 νƒ€μž…μ€ ? λ…Όλ¦¬νƒ€μž…
    Cμ–Έμ–΄μ—λŠ” λ…Όλ¦¬νƒ€μž…μ΄ μ—†μŠ΅λ‹ˆλ‹€.
   μ •μˆ˜κ°’μ„ κ°€μ§€κ³  λ…Όλ¦¬νƒ€μž…μ„ μ²˜λ¦¬ν•©λ‹ˆλ‹€.
   => 0은 κ±°μ§“, 0이외 숫자 μ°Έ

2. && (논리 And)
true && true = true;
false && true = false;
true && false = false;
false && false = false;

3. || (논리 OR)
true || true = true;
true || false = true;
false || true = true;
false || false = false;

4. !(논리 not)
!true = false;
!false = true;

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

// κ΄€κ³„μ—°μ‚°μžν•˜κ³  λ…Όλ¦¬μ—°μ‚°μž
// κ΄€κ³„μ—°μ‚°μžλŠ” μ—°μ‚°μ˜ κ²°κ³Όκ°’μ˜ 데이타 νƒ€μž…? λ…Όλ¦¬νƒ€μž…
// λ…Όλ¦¬μ—°μ‚°μžλŠ” ν”Όμ—°μ‚°μžμ˜ 데이타 νƒ€μž…? λ…Όλ¦¬νƒ€μž…

int main() {
	int value;

	printf("μ •μˆ˜κ°’μ„ μž…λ ₯ν•˜μ„Έμš”: ");
	scanf("%d", &value);   // ν‚€λ³΄λ“œμ—μ„œ μ •μˆ˜κ°’μ„ μž…λ ₯λ°›μ•„μ„œ λ³€μˆ˜ value에 μ €μž₯ν•©λ‹ˆλ‹€.

	int ret = value > 0 && value < 10;

	printf("value:%dκ°€ 0κ³Ό 10사이에 %d\n", value, ret);


	return 0;
}

1. scanfλŠ” ν‚€λ³΄λ“œλ‘œ λΆ€ν„° 값을 μž…λ ₯ λ°›μ„λ•Œ μ‚¬μš©ν•©λ‹ˆλ‹€.
  μ •μˆ˜μž…λ ₯: %d
  μ‹€μˆ˜μž…λ ₯: %f, %lf
  λ¬Έμžμž…λ ₯: %c
  λ¬Έμžμ—΄ μž…λ ₯: %s