CHAPTER 10 - 0083tony/Core-C-Programming GitHub Wiki

๊ตฌ์กฐ์ฒด

  • 1๋ฒˆ ๋ฌธ์ œ

๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” *๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋กœ๊ทธ์ธ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ์กฐ์ฒด๋ฅผ ์ด์šฉํ•ด ๋งŒ๋“ค์–ด๋ณด์ž

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
struct Login {
	char ID[20];
	char Password[20];
};
int main() {
	struct Login a;

	printf("ID? ");
	scanf("%s", a.ID);
	printf("Password? ");
	scanf("%s", a.Password);

	size_t n = strlen(a.Password);

	printf("ID: %s\n", a.ID);
	printf("Password: ");

	for (size_t i = 0; i < n; i++) {
		printf("*");
	}
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

ID? Guest
Password? Idontknow
ID: Guest
Password: *********
  • 2๋ฒˆ ๋ฌธ์ œ

๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด ์•„์ด๋””, ํŒจ์Šค์›Œ๋“œ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ์ €์žฅํ•˜๊ณ , ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
struct Login {
	char ID[20];
	char Password[20];
};
void make_lower(char* ID, char* Password) {
	_strlwr(ID); // error C4996๋กœ ์ธํ•ด strlwr ํ•จ์ˆ˜ ์•ž์— '_'์„ ์ถ”๊ฐ€
	_strlwr(Password);
}
void print_login(char* ID, int n) {
	printf("ID: %s\n", ID);
	printf("Password: ");
	for (size_t i = 0; i < n; i++) {
		printf("*");
	}
}
int main(void) {
	struct Login a;

	printf("ID? ");
	scanf("%s", a.ID);
	printf("Password? ");
	scanf("%s", a.Password);

	size_t n = strlen(a.Password);

	make_lower(a.ID, a.Password);
	print_login(a.ID, n);

	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

ID? Guest
Password? Idontknow
ID: guest
Password: *********
  • 3๋ฒˆ ๋ฌธ์ œ

๊ตฌ์กฐ์ฒด ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ๋“ฑ๋กํ•ด๋‘” 5๊ฐœ์˜ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๋น„๊ตํ•ด ๊ฐ™์œผ๋ฉด "๋กœ๊ทธ์ธ ์„ฑ๊ณต"์ด๋ผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
typedef struct Login {
	char ID[20];
	char Password[20];
} Login;
int main() {
	char ID[20];
	char Password[20];

	Login arr[5] = {
	{"C#","C++"},
	{"Node.js","MySQL"},
	{"html","css"},
	{"JQuery","React.js"},
	{"Git","Github"}
	};

	for (;;) {
		printf("ID? ");
		scanf("%s", ID);

		if (*ID == '.')break;

		getchar();

		printf("Password? ");
		scanf("%s", Password);

		for (size_t i = 0; i < 5; i++) {
			if (*ID == *arr[i].ID) {
				if (*Password == *arr[i].Password) {
					printf("๋กœ๊ทธ์ธ ์„ฑ๊ณต\n");
				}
			}
		}
	}
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

ID? Git
Password? Github
๋กœ๊ทธ์ธ ์„ฑ๊ณต
ID? .
  • 4๋ฒˆ ๋ฌธ์ œ

๊ตฌ์กฐ์ฒด๋ฅผ ์ด์šฉํ•ด ์ž…๋ ฅ๋ฐ›์€ ๋‚ ์งœ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct AA {
	int year = 0;
	int month = 0;
	int date = 0;
};
void print_date(int year, int month, int date) {
	printf("%d / %d / %d\n", year, month, date);
}
int main(void) {
	struct AA a;

	printf("์—ฐ? ");
	scanf("%d", &a.year);
	printf("์›”? ");
	scanf("%d", &a.month);
	printf("์ผ? ");
	scanf("%d", &a.date);

	print_date(a.year, a.month, a.date);
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

์—ฐ? 2019
์›”? 12
์ผ? 25
2019 / 12 / 25
  • 5๋ฒˆ ๋ฌธ์ œ

'localtime' ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์˜ค๋Š˜์˜ ๋‚ ์งœ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>// time()ํ•จ์ˆ˜
struct DATE {
	int year;
	int month;
	int date;
};
struct DATE set_as_today(void) {
	time_t t = time(NULL);
	struct tm* today = localtime(&t);
	struct DATE ti;
	ti.year = today->tm_year + 1900;
	ti.month = today->tm_mon + 1;
	ti.date = today->tm_mday;
	return ti;
}
void printf_date(struct DATE ti) {
	printf("์˜ค๋Š˜์˜ ๋‚ ์งœ๋Š” %d / %d / %d ์ž…๋‹ˆ๋‹ค.\n", ti.year, ti.month, ti.date);
}
int main(void) {
	struct DATE ti;

	ti = set_as_today();
	printf_date(ti);

	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

์˜ค๋Š˜์˜ ๋‚ ์งœ๋Š” 2019 / 10 / 30 ์ž…๋‹ˆ๋‹ค.
  • 6๋ฒˆ ๋ฌธ์ œ

๊ตฌ์กฐ์ฒด 2๊ฐœ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์•„ ์ž…๋ ฅ๋œ ๋‚ ์งœ๊ฐ€ ๊ฐ™์€์ง€ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜ ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct DATE {
	int year;
	int month;
	int date;
};
void is_same_date(struct DATE hld, struct DATE input) {
	if ((hld.year == input.year) && (hld.month == input.month) && (hld.date == input.date)) {
		printf("%d/%d/%d์€ ๊ณตํœด์ผ ์ž…๋‹ˆ๋‹ค.\n", input.year, input.month, input.date);
	}
	else printf("%d/%d/%d์€ ๊ณตํœด์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค.\n", input.year, input.month, input.date);
}
int main(void) {
	struct DATE hld = { 2019,1,1 };
	struct DATE input;

	for (;;) {
		printf("๋‚ ์งœ(์—ฐ ์›” ์ผ)? ");
		scanf("%d %d %d", &input.year, &input.month, &input.date);

		if ((input.year == 0) && (input.month == 0) && (input.date == 0)) {
			break;
		}
		is_same_date(hld, input);
	}
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

๋‚ ์งœ(์—ฐ ์›” ์ผ)? 2019 1 1
2019/1/1์€ ๊ณตํœด์ผ ์ž…๋‹ˆ๋‹ค.
๋‚ ์งœ(์—ฐ ์›” ์ผ)? 2019 1 2
2019/1/2์€ ๊ณตํœด์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค.
๋‚ ์งœ(์—ฐ ์›” ์ผ)? 0 0 0
  • 7๋ฒˆ ๋ฌธ์ œ

'localtime' ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์˜ค๋Š˜ ๋‚ ์งœ๋ฅผ ๊ตฌํ•˜๊ณ  "Today is Jan 1 2019" ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <time.h>
struct DATE {
	int year;
	int month;
	int date;
};
struct DATE set_as_today(void) {
	time_t t = time(NULL);
	struct tm* today = localtime(&t);
	struct DATE ti;
	ti.year = today->tm_year + 1900;
	ti.month = today->tm_mon + 1;
	ti.date = today->tm_mday;
	return ti;
}
void printf_date_eng(struct DATE ti) {
	char month[12][5]{
		"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"
	};
	for (size_t i = 0; i < 12; i++) {
		if (ti.month == i + 1) {
			printf("Today is %s %d %d\n", month[i], ti.date, ti.year);
		}
	}
}
int main(void) {
	struct DATE ti;

	ti = set_as_today();
	printf_date_eng(ti);

	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

Today is Oct 30 2019
  • 8๋ฒˆ ๋ฌธ์ œ

๊ฐ ์ œํ’ˆ์˜ ์ด๋ฆ„, ๊ฐ€๊ฒฉ, ์žฌ๊ณ ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ตฌ์กฐ์ฒด๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ตฌ์กฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์•„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct PRODUCT {
	char name[20];
	int price;
	int stock;
};
void printf_product(struct PRODUCT a) {
	printf("[ %s  %d์›  ์žฌ๊ณ :%d ]", a.name, a.price, a.stock);
}
int main(void) {
	struct PRODUCT a;

	printf("์ œํ’ˆ๋ช…? ");
	scanf("%s", a.name);
	printf("๊ฐ€๊ฒฉ? ");
	scanf("%d", &a.price);
	printf("์žฌ๊ณ ? ");
	scanf("%d", &a.stock);

	printf_product(a);

	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

์ œํ’ˆ๋ช…? ์•„๋ฉ”๋ฆฌ์นด๋…ธ
๊ฐ€๊ฒฉ? 4000
์žฌ๊ณ ? 10
[ ์•„๋ฉ”๋ฆฌ์นด๋…ธ  4000์›  ์žฌ๊ณ :10 ]
  • 9๋ฒˆ ๋ฌธ์ œ

๊ตฌ์กฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์•„ ์ฃผ๋ฌธ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์žฌ๊ณ  ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜ ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct PRODUCT {
	char name[20];
	int price;
	int stock;
};
int order(struct PRODUCT a, int n) {
	int price = a.price * n;
	int stock = a.stock - n;

	if (stock <= 0) {
		printf("์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค.\n");
		return 0;
	}
	else {
		printf("๊ฒฐ์ œ ๊ธˆ์•ก: %d ์žฌ๊ณ : %d\n", price, stock);
	}
	return stock;
}
int main(void) {
	struct PRODUCT a;
	int n;

	printf("์ œํ’ˆ๋ช…? ");
	scanf("%s", a.name);
	printf("๊ฐ€๊ฒฉ? ");
	scanf("%d", &a.price);
	printf("์žฌ๊ณ ? ");
	scanf("%d", &a.stock);

	for (;;) {
	star:
		printf("์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰? ");
		scanf("%d", &n);

		if (n == 0) {
			break;
		}
		if (order(a, n) == 0) {
			goto star;
		}

		a.stock = a.stock - n;
	}
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

์ œํ’ˆ๋ช…? ์•„๋ฉ”๋ฆฌ์นด๋…ธ
๊ฐ€๊ฒฉ? 4000
์žฌ๊ณ ? 10
์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰? 3
๊ฒฐ์ œ ๊ธˆ์•ก: 12000 ์žฌ๊ณ : 7
์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰? 20
์žฌ๊ณ ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค.
์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰? 0
  • 10๋ฒˆ ๋ฌธ์ œ

๊ตฌ์กฐ์ฒด ๋ฐฐ์—ด์„ ์ด์šฉํ•ด 5๊ฐ€์ง€ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct PRODUCT {
	char name[50] = "";
	int price;
	int stock;
};
int main(void) {
	struct PRODUCT a[5];
	int n = 0;

	for (;;) {
		printf("์ œํ’ˆ๋ช…? ");
		scanf("%[^\n]s", a[n].name);

		if (*a[n].name == '.') {
			break;
		}

		printf("๊ฐ€๊ฒฉ ์žฌ๊ณ ?  ");
		scanf("%d %d", &a[n].price, &a[n].stock);
		getchar(); // ๋ฒ„ํผ๊ฐ’์„ ๋น„์šฐ์ง€ ์•Š์œผ๋ฉด scanf ๋ฌด์‹œ
		n++;

		if (n == 5) {
			break;
		}
	}

	for (int i = 0; i < n; i++) {
		printf("[ %s  %d์›  ์žฌ๊ณ :%d ]\n", a[i].name, a[i].price, a[i].stock);
	}
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

์ œํ’ˆ๋ช…? ์•„๋ฉ”๋ฆฌ์นด๋…ธ
๊ฐ€๊ฒฉ ์žฌ๊ณ ?  4000 10
์ œํ’ˆ๋ช…? ํ”Œ๋žซํ™”์ดํŠธ
๊ฐ€๊ฒฉ ์žฌ๊ณ ?  5000 10
์ œํ’ˆ๋ช…? .
[ ์•„๋ฉ”๋ฆฌ์นด๋…ธ  4000์›  ์žฌ๊ณ :10 ]
[ ํ”Œ๋žซํ™”์ดํŠธ  5000์›  ์žฌ๊ณ :10 ]
  • 11๋ฒˆ ๋ฌธ์ œ

ํฌ๊ธฐ๊ฐ€ 5์ธ ๊ตฌ์กฐ์ฒด ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด ์žฌ๊ณ  ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ฃผ๋ฌธ ์ฒ˜๋ฆฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ(๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”๋Š” ์ž„์˜์˜ ๊ฐ’)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct PRODUCT {
	char name[50] = "";
	int price;
	int stock;
	int amount;
};
int main(void) {
	struct PRODUCT a[5] = {
		{"์•„๋ฉ”๋ฆฌ์นด๋…ธ",4000,10},{"์นดํŽ˜๋ผ๋–ผ  ",4500,10},
		{"ํ”„๋ผํ‘ธ์น˜๋…ธ",5000,10},{"ํ”Œ๋žซํ™”์ดํŠธ",5000,10},
		{"์น˜์ฆˆ์ผ€์ดํฌ",9900,10}
	};
	struct PRODUCT input;
	int stock[5] = { 10,10,10,10,10 };

	for (;;) {
	start:
		printf("์ œํ’ˆ๋ช…? ");
		scanf("%[^\n]s", input.name);

		if (*input.name == '.') break;
		
		printf("์ฃผ๋ฌธํ•  ์ˆ˜๋Ÿ‰?  ");
		scanf("%d", &input.amount);

		getchar(); // ๋ฒ„ํผ๊ฐ’์„ ๋น„์šฐ์ง€ ์•Š์œผ๋ฉด scanf ๋ฌด์‹œ

		for (int i = 0; i < 5; i++) {

			if (*a[i].name == *input.name) {

				if (a[i].stock >= input.amount) {
					a[i].stock = a[i].stock - input.amount;
					printf("๊ฒฐ์ œ ๊ธˆ์•ก: %d  %s ์žฌ๊ณ : %d\n", input.amount * a[i].price, a[i].name, a[i].stock);
					goto start;
				}
			}
		}
		printf("์ œํ’ˆ๋ช…์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n");
	}

	for (int i = 0; i < 5; i++) {
		printf("[ %s  %d์›  ์žฌ๊ณ :%d ]\n", a[i].name, a[i].price, a[i].stock);
	}
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

์ œํ’ˆ๋ช…? ์•„๋ฉ”๋ฆฌ์นด๋…ธ
์ฃผ๋ฌธํ•  ์ˆ˜๋Ÿ‰?  5
๊ฒฐ์ œ ๊ธˆ์•ก: 20000  ์•„๋ฉ”๋ฆฌ์นด๋…ธ ์žฌ๊ณ : 5
์ œํ’ˆ๋ช…? ์นดํŽ˜๋ผ๋–ผ
์ฃผ๋ฌธํ•  ์ˆ˜๋Ÿ‰?  3
๊ฒฐ์ œ ๊ธˆ์•ก: 13500  ์นดํŽ˜๋ผ๋–ผ   ์žฌ๊ณ : 7
์ œํ’ˆ๋ช…? .
[ ์•„๋ฉ”๋ฆฌ์นด๋…ธ  4000์›  ์žฌ๊ณ :5 ]
[ ์นดํŽ˜๋ผ๋–ผ    4500์›  ์žฌ๊ณ :7 ]
[ ํ”„๋ผํ‘ธ์น˜๋…ธ  5000์›  ์žฌ๊ณ :10 ]
[ ํ”Œ๋žซํ™”์ดํŠธ  5000์›  ์žฌ๊ณ :10 ]
[ ์น˜์ฆˆ์ผ€์ดํฌ  9900์›  ์žฌ๊ณ :10 ]
  • 12๋ฒˆ ๋ฌธ์ œ

๊ณต์šฉ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด 2๋ฐ”์ดํŠธ์˜ ๋ฐ์ดํ„ฐ 2๊ฐœ๋ฅผ 4๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋งŒ๋“œ๋Š” ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
union mydata {
	short low;
	short high;
	int dword = 0;
};
int make_dword(short low, short high) {
	union mydata a;

	a.dword = high << 16;
	a.dword = a.dword + low;

	return a.dword;
}
int main(void) {
	short low;
	short high;

	printf("low word? ");
	scanf("%hi", &low);
	printf("high word? ");
	scanf("%hi", &high);

	printf("dword data: %x\n", make_dword(low, high));

	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

low word? 0x1234
high word? 0xabcd
dword data: abcd1234
  • 13๋ฒˆ ๋ฌธ์ œ

๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด ์ง์‚ฌ๊ฐํ˜• ์ขŒํ•˜๋‹จ์ , ์šฐ์ƒ๋‹จ์ ์˜ ์ขŒํ‘œ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
struct POINT {
	int x1;
	int x2;
	int y1;
	int y2;
};
void print_rect(struct POINT RECT) {
	printf("[RECT ์ขŒํ•˜๋‹จ์ : (%d,%d) ์šฐ์ƒ๋‹จ์ : (%d,%d)]\n", RECT.x1, RECT.y1, RECT.x2, RECT.y2);
}
int main(void) {
	struct POINT RECT;

	printf("์ง์‚ฌ๊ฐํ˜•์˜ ์ขŒํ•˜๋‹จ์ (x,y)? ");
	scanf("%d %d", &RECT.x1, &RECT.y1);
	printf("์ง์‚ฌ๊ฐํ˜•์˜ ์šฐ์ƒ๋‹จ์ (x,y)? ");
	scanf("%d %d", &RECT.x2, &RECT.y2);

	print_rect(RECT);

	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

์ง์‚ฌ๊ฐํ˜•์˜ ์ขŒํ•˜๋‹จ์ (x,y)? 10 20
์ง์‚ฌ๊ฐํ˜•์˜ ์šฐ์ƒ๋‹จ์ (x,y)? 100 200
[RECT ์ขŒํ•˜๋‹จ์ : (10,20) ์šฐ์ƒ๋‹จ์ : (100,200)]
  • 14๋ฒˆ ๋ฌธ์ œ

rand ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด 0~99์˜ ์ž„์˜์˜ ์ขŒํ‘œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ง์‚ฌ๊ฐํ˜• ์ขŒํ•˜๋‹จ์ ๋ณด๋‹ค ์šฐ์ƒ๋‹จ์ ์ด ํฌ๋„๋ก ์ •๊ทœํ™”ํ•œ ํ›„ ์ง์‚ฌ๊ฐํ˜•์˜ ์ค‘์‹ฌ์ ์˜ ์ขŒํ‘œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct POINT {
	int x1;
	int x2;
	int y1;
	int y2;
};
void normalize_rect(int* arr, struct POINT* RECT, int a) {
	int temp;

	for (int i = 0; i < 4; i++) {
		for (int k = 0; k < 3; k++) {
			if (arr[k] > arr[k + 1]) {
				temp = arr[k];
				arr[k] = arr[k + 1];
				arr[k + 1] = temp;
			}
		}
	}
	RECT[a].x1 = arr[0];
	RECT[a].y1 = arr[1];
	RECT[a].x2 = arr[2];
	RECT[a].y2 = arr[3];
}
void center_rect(struct POINT* RECT, int a) {
	int ct1, ct2;

	ct1 = (RECT[a].x1 + RECT[a].x2) / 2;
	ct2 = (RECT[a].y1 + RECT[a].y2) / 2;

	printf("[ RECT์ขŒํ•˜๋‹จ์ :(%d,%d) ์šฐ์ƒ๋‹จ์ :(%d,%d) ]	์ค‘์‹ฌ์ :(%d, %d)\n", RECT[a].x1, RECT[a].y1, RECT[a].x2, RECT[a].y2, ct1, ct2);
}
int main(void) {
	srand(time(NULL));
	struct POINT RECT[3];
	int arr[5];

	for (int a = 0; a < 3; a++) {
		for (int i = 0; i < 4; i++) {
			arr[i] = rand() % 100;
		}
		normalize_rect(arr, RECT, a);
		center_rect(RECT, a);
	}
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

[ RECT์ขŒํ•˜๋‹จ์ :(11,23) ์šฐ์ƒ๋‹จ์ :(33,97) ]       ์ค‘์‹ฌ์ :(22, 60)
[ RECT์ขŒํ•˜๋‹จ์ :(26,58) ์šฐ์ƒ๋‹จ์ :(71,98) ]       ์ค‘์‹ฌ์ :(48, 78)
[ RECT์ขŒํ•˜๋‹จ์ :(30,46) ์šฐ์ƒ๋‹จ์ :(61,66) ]       ์ค‘์‹ฌ์ :(45, 56)
  • 15๋ฒˆ ๋ฌธ์ œ

10๊ฐœ์˜ ์ ์˜ ์ขŒํ‘œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” POINT ๊ตฌ์กฐ์ฒด ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด x์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARRINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct POINT {
	int x;
	int y;
};
void swap_point(struct POINT* point) {
	int temp;
	struct POINT save[10] = {
		{point[0].x,point[0].y},
		{point[1].x,point[1].y},
		{point[2].x,point[2].y},
		{point[3].x,point[3].y},
		{point[4].x,point[4].y},
		{point[5].x,point[5].y},
		{point[6].x,point[6].y},
		{point[7].x,point[7].y},
		{point[8].x,point[8].y},
		{point[9].x,point[9].y},
	};

	for (int i = 0; i < 10; i++) {
		for (int k = 0; k < 9; k++) {

			if (point[k].x > point[k + 1].x) {
				temp = point[k].x;
				point[k].x = point[k + 1].x;
				point[k + 1].x = temp;
			}
		}
	}
	for (int i = 0; i < 10; i++) {
		for (int k = 0; k < 9; k++) {

			if (point[i].x == save[k].x) {
				point[i].y = save[k].y;
			}
		}
	}
}
int main(void) {
	struct POINT point[10];
	srand(time(NULL));

	printf("<<์ •๋ ฌ ์ „>>\n");

	for (int i = 0; i < 10; i++) {
		point[i].x = rand() % 100;
		point[i].y = rand() % 100;
		printf("(%d,%d) ", point[i].x, point[i].y);
	}

	printf("\n<<์ •๋ ฌ ํ›„>>\n");
	swap_point(point);

	for (int i = 0; i < 10; i++) {
		printf("(%d,%d) ", point[i].x, point[i].y);
	}
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

<<์ •๋ ฌ ์ „>>
(87,95) (39,14) (78,43) (90,38) (16,88) (50,46) (47,36) (84,53) (73,3) (20,81)
<<์ •๋ ฌ ํ›„>>
(16,88) (20,14) (39,14) (47,36) (50,46) (73,3) (78,43) (84,53) (87,95) (90,38)
  • 16๋ฒˆ ๋ฌธ์ œ

๊ณก๋ช…, ๊ฐ€์ˆ˜, ์žฅ๋ฅด, ์žฌ์ƒ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๋Š” ๊ตฌ์กฐ์ฒด์™€ ์žฅ๋ฅด์˜ ์—ด๊ฑฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

#define _CRT_SECURE_NO_WARRINGS
#include <stdio.h>
#include <stdlib.h>
struct SONG {
	char title[30];
	char artist[30];
	int genre;
	int playtime;
};
enum Genre {
	ROCK, HIP_HOP, OST, BALLAD, DANCE
};
int main(void) {
	SONG A[8] = {
	{"Rain","SNO",2,243},
	{"TT","TWICE",4,213},
	{"Awake","BTS",1,226},
	{"๋ด„๋น„","์žฅ๋ฒ”์ค€",3,181},
	{"BIB","AC/DC",0,255},
	{"๋ด„๋‚ ","BTS",1,278},
	{"์•„๋‚™๋„ค","BTS",1,241},
	{"NAPA","miwa",2,281}
	};

	enum Genre B[5] = { ROCK, HIP_HOP, OST, BALLAD, DANCE };

	printf("์ œ๋ชฉ \t์•„ํ‹ฐ์ŠคํŠธ \t์žฅ๋ฅด \t์žฌ์ƒ์‹œ๊ฐ„\n");

	for (int i = 0; i < 5; i++) {
		for (int k = 0; k < 8; k++) {
			switch (B[i]) {
			case ROCK:
				if (ROCK == A[k].genre) {
					printf("%s	%s		ROCK	%d์ดˆ\n", A[k].title, A[k].artist, A[k].playtime);
				}
				break;
			case HIP_HOP:
				if (HIP_HOP == A[k].genre) {
					printf("%s	%s		HIP_HOP	%d์ดˆ\n", A[k].title, A[k].artist, A[k].playtime);
				}
				break;
			case OST:
				if (OST == A[k].genre) {
					printf("%s	%s		OST	%d์ดˆ\n", A[k].title, A[k].artist, A[k].playtime);
				}
				break;
			case BALLAD:
				if (BALLAD == A[k].genre) {
					printf("%s	%s		BALLAD	%d์ดˆ\n", A[k].title, A[k].artist, A[k].playtime);
				}
				break;
			case DANCE:
				if (DANCE == A[k].genre) {
					printf("%s	%s		DANCE	%d์ดˆ\n", A[k].title, A[k].artist, A[k].playtime);
				}
				break;
			}
		}
	}
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

์ œ๋ชฉ    ์•„ํ‹ฐ์ŠคํŠธ        ์žฅ๋ฅด    ์žฌ์ƒ์‹œ๊ฐ„
BIB     AC/DC           ROCK    255์ดˆ
Awake   BTS             HIP_HOP 226์ดˆ
๋ด„๋‚     BTS             HIP_HOP 278์ดˆ
์•„๋‚™๋„ค  BTS             HIP_HOP 241์ดˆ
Rain    SNO             OST     243์ดˆ
NAPA    miwa            OST     281์ดˆ
๋ด„๋น„    ์žฅ๋ฒ”์ค€          BALLAD  181์ดˆ
TT      TWICE           DANCE   213์ดˆ
  • 17๋ฒˆ ๋ฌธ์ œ

๊ณก๋ช…, ๊ฐ€์ˆ˜๋ช…์„ ์ž…๋ ฅ๋ฐ›์•„ ํ•ด๋‹น ๋…ธ๋ž˜๋ฅผ ์ „๋ถ€ ์ฐพ์•„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct SONG {
	char title[30];
	char artist[30];
	char genre[30];
	int playtime;
};
int main(void) {
	SONG A[8] = {
	{"Rain","SNO","HIP_HOP",243},
	{"TT","TWICE","DANCE",213},
	{"Awake","BTS","HIP_HOP",226},
	{"๋ด„๋น„","์žฅ๋ฒ”์ค€","BALLAD",181},
	{"BIB","AC/DC","ROCK",255},
	{"๋ด„๋‚ ","BTS","HIP_HOP",278},
	{"์•„๋‚™๋„ค","BTS","HIP_HOP",241},
	{"NAPA","miwa","OST",281}
	};

	char kw[20];

	for (;;) {
		printf("ํ‚ค์›Œ๋“œ(์ œ๋ชฉ/์•„ํ‹ฐ์ŠคํŠธ)? ");
		scanf_s("%[^\n]s", kw, 20);

		if (*kw == '.') {
			break;
		}

		for (int i = 0; i < 8; i++) {
			int N = _strcmpi(A[i].title, kw);
			if (N == 0) {
				printf("%s	%s	%s	%d์ดˆ\n", A[i].title, A[i].artist, A[i].genre, A[i].playtime);
			}
			int M = _strcmpi(A[i].artist, kw);
			if (M == 0) {
				printf("%s	%s	%s	%d์ดˆ\n", A[i].title, A[i].artist, A[i].genre, A[i].playtime);
			}
		}
		getchar();
	}
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

ํ‚ค์›Œ๋“œ(์ œ๋ชฉ/์•„ํ‹ฐ์ŠคํŠธ)? Rain
Rain    SNO     HIP_HOP 243์ดˆ
ํ‚ค์›Œ๋“œ(์ œ๋ชฉ/์•„ํ‹ฐ์ŠคํŠธ)? BTS
Awake   BTS     HIP_HOP 226์ดˆ
๋ด„๋‚     BTS     HIP_HOP 278์ดˆ
์•„๋‚™๋„ค  BTS     HIP_HOP 241์ดˆ
ํ‚ค์›Œ๋“œ(์ œ๋ชฉ/์•„ํ‹ฐ์ŠคํŠธ)? .
  • 18๋ฒˆ ๋ฌธ์ œ

ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•  ๋…ธ๋ž˜๋Š” ๋ฒˆํ˜ธ๋กœ ์„ ํƒํ•˜๊ณ  ์ „์ฒด ๊ณก ์žฌ์ƒ ์‹œ๊ฐ„๊ณผ ๋ชฉ๋ก์„ ํ•ญ์ƒ ์ถœ๋ ฅํ•ด์•ผํ•œ๋‹ค.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct SONG {
	char title[30];
	char artist[30];
	char genre[30];
	int playtime;
};
int main(void) {
	SONG A[8] = {
	{"Rain","SNO","HIP_HOP",243},
	{"TT","TWICE","DANCE",213},
	{"Awake","BTS","HIP_HOP",226},
	{"๋ด„๋น„","์žฅ๋ฒ”์ค€","BALLAD",181},
	{"BIB","AC/DC","ROCK",255},
	{"๋ด„๋‚ ","BTS","HIP_HOP",278},
	{"์•„๋‚™","BTS","HIP_HOP",241},
	{"NAPA","Miwa","OST",281}
	};

	SONG* playlist[5] = { NULL };
	int n = 0, m = 0;

	printf(" ์ œ๋ชฉ\t์•„ํ‹ฐ์ŠคํŠธ\t์žฅ๋ฅด\t์žฌ์ƒ์‹œ๊ฐ„\n");

	for (int i = 0; i < 8; i++) {
		printf("%d:%s\t%s\t\t%s\t%d์ดˆ\n", i + 1, A[i].title, A[i].artist, A[i].genre, A[i].playtime);
	}

	for (;;) {
	start:
		int playtime = 0;

		printf("\nํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•  ๊ณก ๋ฒˆํ˜ธ? ");
		scanf_s("%d", &n);

		if (n == 0) {
			break;
		}
		if (n > 8) {
			printf("์ž˜๋ชป๋œ ๊ณก ๋ฒˆํ˜ธ์ž…๋‹ˆ๋‹ค.\n");
			goto start;
		}

		playlist[m] = &A[n - 1];
		m++;

		printf("<< ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ >>\n");

		for (int i = 0; i < m; i++) {
			printf("%s\t%s\t\t%s\t%d์ดˆ\n", (*playlist[i]).title, (*playlist[i]).artist, (*playlist[i]).genre, (*playlist[i]).playtime);
			playtime = playtime + (*playlist[i]).playtime;
		}

		printf("์ด ์žฌ์ƒ์‹œ๊ฐ„: %d์ดˆ\n", playtime);
	}
	return 0;
}
[์‹คํ–‰ ๊ฒฐ๊ณผ]

 ์ œ๋ชฉ   ์•„ํ‹ฐ์ŠคํŠธ        ์žฅ๋ฅด    ์žฌ์ƒ์‹œ๊ฐ„
1:Rain  SNO             HIP_HOP 243์ดˆ
2:TT    TWICE           DANCE   213์ดˆ
3:Awake BTS             HIP_HOP 226์ดˆ
4:๋ด„๋น„  ์žฅ๋ฒ”์ค€          BALLAD  181์ดˆ
5:BIB   AC/DC           ROCK    255์ดˆ
6:๋ด„๋‚   BTS             HIP_HOP 278์ดˆ
7:์•„๋‚™  BTS             HIP_HOP 241์ดˆ
8:NAPA  Miwa            OST     281์ดˆ

ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•  ๊ณก ๋ฒˆํ˜ธ? 4
<< ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ >>
๋ด„๋น„    ์žฅ๋ฒ”์ค€          BALLAD  181์ดˆ
์ด ์žฌ์ƒ์‹œ๊ฐ„: 181์ดˆ

ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•  ๊ณก ๋ฒˆํ˜ธ? 8
<< ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ >>
๋ด„๋น„    ์žฅ๋ฒ”์ค€          BALLAD  181์ดˆ
NAPA    Miwa            OST     281์ดˆ
์ด ์žฌ์ƒ์‹œ๊ฐ„: 462์ดˆ

ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•  ๊ณก ๋ฒˆํ˜ธ? 9
์ž˜๋ชป๋œ ๊ณก ๋ฒˆํ˜ธ์ž…๋‹ˆ๋‹ค.

ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•  ๊ณก ๋ฒˆํ˜ธ? 0
โš ๏ธ **GitHub.com Fallback** โš ๏ธ