[์ ๋ณด์ฒ๋ฆฌ๊ธฐ์ฌ] ์ค๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ธ์ด 3๋ฌธ์ (27)
1๏ธโฃ [C์ธ์ด]
2024.2ํ
๋ค์ C์ธ์ด๋ก ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ๋ถ์ํ์ฌ ๊ทธ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์์ค.
#include
void func(char *d, char *s) {
int sum = 0;
while (*s) {
*d = *s;
d++;
s++;
}
*d = '\0';
}
int main() {
char* str1 = "first";
char str2[50] = "teststring";
int result = 0;
func(str2, str1);
for (int i = 0; str2[i] != '\0'; i++) {
result += i;
}
printf("%d", result);
return 0;
}
10
func ํจ์๋ s๊ฐ ๊ฐ๋ฆฌํค๋ ๋ฌธ์์ด์ d๋ก ๋ณต์ฌํ๋ ํจ์์ด๋ค.
main์ ๋ณด๋ฉด func์ d์๋ str2, s์๋ str1์ด ๋ค์ด๊ฐ๋ค.
while ๋ฌธ์ผ๋ก s์ ๋ฌธ์์ด ๊ฐ์๋งํผ ๋ฐ๋ณตํ์ฌ d์ ๋ณต์ฌ๋๋ค.
๊ฒฐ๋ก ์ ์ผ๋ก func ํจ์๊ฐ ์ ์ฉ๋๊ณ ๋ ์ดํ์ str2๋ "first"๊ฐ ๋๋ค.
for๋ฌธ์ ๋ณด๋ฉด str2๊ฐ '\0' ๊ฐ์ด ๋๋ฉด ๋ฐ๋ณต์ ๋ฉ์ถ๋๋ฐ, str2๋
| 0 | 1 | 2 | 3 | 4 |
| f | i | r | s | t |
๋ก ๋์ด์๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ result์ ๋ค์ด๊ฐ๋ ๊ฐ์ 0 + 1 + 2 + 3 + 4 = 10 ์ด๋ค.
2๏ธโฃ [Python]
2020.4ํ
๋ค์์ ํ์ด์ฌ ์์ค ์ฝ๋์ด๋ค. ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์์ค.
lol = [[1,2,3],[4,5],[6,7,8,9]]
print(lol[0])
print(lol[2][1])
for sub in lol:
for item in sub:
print(item, end = '')
print()
[1, 2, 3]
7
123
45
6789
print(lol[0]) => [1, 2, 3]
print(lol[2][1]) => 7
for sub in lol : ์ ๋ฐ๋ณต๋ฌธ์ ํตํด lol์ ๋ฆฌ์คํธ๋ฅผ ๋ฐ๋ณตํ๋ค.
for item in sub : ์ ๋ฐ๋ณต๋ฌธ์ ํตํด sub ๋ฆฌ์คํธ์ ๊ฐ ์์๋ฅผ ๋ฐ๋ณตํ๋ค.
๊ฐ ์์ item์ด ๋ชจ๋ ์ถ๋ ฅ๋๋ฉด (end = '' ์ด๊ธฐ ๋๋ฌธ์ ๋์ด์ฐ๊ธฐ X)
item์ด ๋ค ์ถ๋ ฅ๋ ์ดํ ๋ค์ sub ๋ฐ๋ณต๋ฌธ ์์
3๏ธโฃ [JAVA]
2023.1ํ
๋ค์ Java ์ฝ๋์ ๋ํ ์ถ๋ ฅ ๊ฐ์ ์์ฑํ์์ค.
class Parent {
int x = 100;
Parent() {
this(500);
}
Parent(int x) {
this.x = x;
}
int getX() {
return x;
}
}
class Child extends Parent {
int x = 4000;
Child() {
this(5000);
}
Child(int x) {
this.x = x;
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
System.out.println(obj.getX());
}
}
500
Child์๋ getX() ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ ํ์ง ์์ผ๋ฏ๋ก Parent์ getX() ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค.
์ฌ๊ธฐ์ Parent ํด๋์ค๋ ๋ฉค๋ฒ ๋ณ์ x๋ 100์ ๊ฐ์ง๋๋ค.
๊ทธ ๋ค์ Parent() ๊ธฐ๋ณธ ์์ฑ์์์ this(500); ์ ํธ์ถํ์ฌ, ๋ ๋ฒ์งธ ์์ฑ์ (Parent(int x))๋ฅผ ํธ์ถํ๋ค.
์ด๋ก ์ธํด x ๊ฐ์ด 500์ผ๋ก ์ค์ ๋๋ค.
Parent ํด๋์ค์ getX() ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ return ๊ฐ์ 500์ด ๋๋ค.