๐Ÿ’ก

๋”ฐ๋ฐฐ์”จ 7.10 ~ ๋ณธ๋ฌธ

๋”ฐ๋ฐฐ์”จ

๋”ฐ๋ฐฐ์”จ 7.10 ~

์•„์˜ณ์ด 2021. 1. 18. 19:12

7.10 ๋ฃจํ”„ ๋„์šฐ๋ฏธ continue์™€ break

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>


int main()
{
	/* continue */
	for (int i = 0; i < 10; i++)
	{
		if (i == 5)
			continue;

		printf("%d ", i);

		/*if (i != 5)
		  printf("%d ", i);*/
	}

	/* break */
	for (int i = 0; i < 10; i++)
	{
		if (i == 5)
			break;

		printf("%d ", i);
	}

	/* while and continue */
	int count = 0;
	while (count < 5)
	{
		int c = getchar();
		if (c == 'a')
			continue;
		putchar(c);
		count++;
	}

	/* for and continue */
	for (int count = 0; count < 5; count++)
	{
		int c = getchar();
		if (c == 'a')
			continue;
		putchar(c);
	}

	/* continue as a placeholder */
	while (getchar() != '\n')		//	๋ฌดํ•œ๋ฃจํ”„
		continue;
		
	/* Need to use continue ? */
	char c;
	while ((c = getchar()) != '\n')
	{
		if (c == 'a')
			continue;
		putchar(c);

		//if (c != 'a')
		//	putchar(c);
	}	

	/* break */

	char c;
	while ((c = getchar()) != '.')
	{
		putchar(c);
	}

	while (1)		//	๋ฌดํ•œ๋ฃจํ”„ ๋๋‚ด๊ธฐ
	{
		char c = getchar();

		if (c == '.')
			break;
	
		putchar(c);
	}

	for (int i = 0; i < 10; i++)	//	๋‹ค์ค‘๋ฃจํ”„ ์ œ์–ด
	{
		for (int j = 0; j < 10; j++)
		{
			if (j == 5)
				break;

			printf("(%d %d)", i, j);
		}

		printf("\n");
	}

	return 0;
}

 

 

 

7.11 ์ตœ๋Œ€, ์ตœ์†Œ, ํ‰๊ท  ๊ตฌํ•˜๊ธฐ ์˜ˆ์ œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <float.h>


int main()
{
	float max = -FLT_MAX;
	float min = FLT_MIN;
	float sum = 0.0f;
	float input;
	int n = 0;
	
	while (scanf("%d", &input) == 1)
	{
		if (input < 0.0f || input > 100.0f)
			continue;
		
		max = (max > input) ? max : input;
		min = (min < input) ? min : input;
		sum += input;

		n++;
	}
	if (n > 0)
		printf("min = %f, max = %f, ave = %f\n", min, max, sum / n);
	else
		printf("No input\n");
	
	return 0;
}

while (scanf("%d", &input) == 1)๋Š” ์ˆซ์ž๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๊ณ„์† ์ž…๋ ฅ๋ฐ›๊ณ 

๋ฌธ์ž๊ฐ€ ๋“ค์–ด์˜ค๋ฉด false๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋น ์ ธ๋‚˜์˜ค๋Š” ๋ฌดํ•œ๋ฃจํ”„์ด๋‹ค.

 

 

 

7.12 ๋‹ค์ค‘ ์„ ํƒ switch์™€ case

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <float.h>


int main()
{
	char c;
	while ((c = getchar()) != '.')
	{
		printf("You love ");

		switch (c)	//Note: integer types only
		{
		case 'a': case 'A':
			printf("apple");
			break;
		case 'b': case 'B':
			printf("baseball");
			break;
		case 'c': case 'C':
			printf("cake");
			break;
		default:
			printf("nothing");
		}
		printf(".\n");

		while (getchar() != '\n')		//	๋‹จ์–ด์˜ ์ฒซ๊ธ€์ž๋ฅผ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ๊ธ€์ž๋Š” ์ „๋ถ€ ๋ฌด์‹œํ•œ๋‹ค
			continue;
	}
	return 0;
}

๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ 

#include <ctype.h>

 

tolower() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค.

 

 

 

7.13 goto๋ฅผ ํ”ผํ•˜๋Š” ๋ฐฉ๋ฒ•

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <float.h>


int main()
{
	//if else
	int size = 15, cost;

	if (size < 10)
		goto a;
	goto b;

a: cost = 50 * size;
b: cost = 100 * size;

	if (size < 10)
		cost = 50 * size;
	cost = 100 * size;

	//loop
	char c;

read: c = getchar();
	putchar(c);
	if (c == '.') goto quit;
	goto read;
quit:

	while (1)
	{
		c = getchar();
		putchar(c);
		if (c == '.') break;
	}

	return 0;
}

ํ˜„์žฌ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ƒฅ ์ด๋Ÿฐ๊ฒŒ ์žˆ๋‹ค ํ•˜๊ณ  ๋„˜์–ด๊ฐ€์ž

 

 

 

8.1 ์ž…์ถœ๋ ฅ ๋ฒ„ํผ

๋ฒ„ํผ๋ž€ ๋“ค์–ด์˜ค๋Š” ์ž…๋ ฅ์„ ์ž„์‹œ์ €์žฅ ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„ ์ •๋„๋กœ ์ •์˜๊ฐ€ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™๋‹ค

 

 

 

8.2 ํŒŒ์ผ์˜ ๋ EOF

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>


int main()
{
	int c;

	while ((c = getchar()) != EOF)	//	End Of File
		putchar(c);

	while (1)
	{
		c = getchar();
		printf("%d\n", c);
		if (c == EOF)
			break;
	}
	return 0;
}

EOF๋Š” ํŒŒ์ผ์˜ ๋์„ ํ‘œํ˜„ํ•˜๋Š” ์ƒ์ˆ˜๋กœ ๋ฌธ์ž์—ด ์ž…๋ ฅ ํ•จ์ˆ˜์˜ ๋์„ ๋‚˜ํƒ€๋‚ผ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋Š” ์˜๋ฏธ

vs์—์„œ EOF์— ๋งˆ์šฐ์Šค๋ฅผ ์˜ฌ๋ ค๋ณด๋ฉด #define EOF(-1)๋กœ ์ง€์ •๋˜์–ด ์žˆ๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ฝ˜์†”์ฐฝ์—์„œ ์ค„์˜ ์ฒ˜์Œ ๋ถ€๋ถ„์—์„œ ctrl + z ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด EOF๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

8.3 ์ž…์ถœ๋ ฅ ๋ฐฉํ–ฅ ์žฌ์ง€์ • Redirection

int main()
{
	/*printf("Programming.\n");*/

	char str[100];
	scanf("%s", str);
	printf("i love %s\n", str);

	return 0;
}

์ฝ˜์†”์ฐฝ์—์„œ

 

- MyCProgramingStart2.exe > output.txt

MyCProgramingStart2.exe์˜ ์ถœ๋ ฅ ๋ฐฉํ–ฅ์„ output.txt๋กœ ์žฌ์ง€์ •

- MyCProgramingStart2.exe >> output.txt

์‹คํ–‰๊ฒฐ๊ณผ๋ฅผ output.txt์— ๋ง๋ถ™์ธ๋‹คappend

- copy MyCProgramingStart2.exe test.exe

MyCProgramingStart2.exe๋ฅผ test.exe๋กœ ๋ณต์‚ฌํ•œ๋‹ค.

- test.exe | MyCProgramingStart2.exe

test.exe์˜ ์‹คํ–‰๊ฒฐ๊ณผ๋ฅผ MyCProgramingStart2.exe๋กœ ๋ณด๋‚ด์ค€๋‹ค

 

 

 

8.5 ์ˆซ์ž์™€ ๋ฌธ์ž๋ฅผ ์„ž์–ด์„œ ์ž…๋ ฅ๋ฐ›๊ธฐ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void display(char cr, int lines, int width);

int main()
{
	char c;
	int rows, cols;

	/*while (1)
	{
		scanf("%c %d %d", &c, &rows, &cols);
		while (getchar() != '\n') continue;
		display(c, rows, cols);
		if (c == 'n')
			break;
	}*/

	printf("Input one character and two integers:\n");
	while ((c = getchar()) != '\n')
	{
		scanf("%d %d", &rows, &cols);
		while (getchar() != '\n') continue;

		display(c, rows, cols);
		printf("Input another character and two integers:\n");
		printf("Press Enter to quit.\n");
	}

	return 0;
}

void display(char cr, int lines, int width)
{
	for (int i = 0; i < lines; i++)
	{
		for (int j = 0; j < width; j++)
			putchar(cr);
		putchar('\n');
	}
}

 

 

 

8.6 ์ž…๋ ฅ ํ™•์ธํ•˜๊ธฐ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

long get_long(void);

int main()
{
	long number;

	while (1)
	{
		printf("Plesas input an integer between 1 and 100.\n");

		number = get_long();

		if (number > 1 && number < 100)
		{
			printf("Ok. Thank you.\n");
			break;
		}
		else
			printf("Wrong input. Pleases tyr again\n");
	}

	printf("Your input %ld is between 1 and 100. Thank you.\n", number);

	return 0;
}

get_long(void)
{
	printf("Please input an integer and press enter.\n");

	long input;
	char c;

	while (scanf("%ld", &input) != 1)
	{
		printf("Your input  - ");

		while ((c = getchar()) != '\n')
			putchar(c);		//	input left in butter

		printf(" - is not an integer. Please try agan.\n");
	}

	printf("Your input %ld is an integer Thank you.\n", input);


}

get_long ํ•จ์ˆ˜์˜

while ((c = getchar()) != '\n')
    putchar(c);

๋กœ ๋ฒ„ํผ์— ๋‚จ์•„์žˆ๋Š” ์ž…๋ ฅ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

8.7 ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ๊ณผ ์ˆซ์ž

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


int main()
{
	//	A 123 1.23 ์ž…๋ ฅ

	char str[255];
	int i, i2;
	double d;

	scanf("%s %d %lf", str, &i, &d);
	printf("%s %d %f\n", str, i, d);	//	A 123 1.23

	//	or	(see the difference)

	scanf("%s %d %d", str, &i, &d);
	printf("%s %d %f\n", str, i, i2);	//	A 123 1

	//	or	(see the difference)

	char c;
	while ((c = getchar()) != '\n')
		putchar(c);						//	.23
	printf("\n");

	return 0;
}

 

 

 

8.8 ๋ฉ”๋‰ด ๋งŒ๋“ค๊ธฐ ์˜ˆ์ œ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


int main()
{
	char c;
	int i;

	
	
	while (1)
	{
		printf("Enter the letter of your choice:\n");
		printf("a. avengers    b. beep\n");
		printf("c. count       q. quit\n");
		
		scanf("%c", &c);
		if (c == 'a')
		{
			printf("Avengers assmele!\n");
			continue;
		}
		else if (c == 'b')
		{
			printf("\a");
			continue;
		}
		else if (c == 'c')
		{
			printf("Enter an integer : \n");
			scanf("%d", &i);
			for (int j = 1; j <= i; j++)
				printf("%d\n", j);
			continue;
		}
		else if (c == 'q')
			break;
	}

	return 0;
}

๋‚ด๊ฐ€ ๋งŒ๋“  ์ฝ”๋“œ while๋ฌธ์ด ์ฒ˜์Œ ๋Œ๋•Œ๋Š” ์ž˜ ๋˜๋Š”๋ฐ ๋‘๋ฒˆ์งธ ๋Œ๋•Œ๋ถ€ํ„ฐ๋Š” 

printf("Enter the letter of your choice:\n");
printf("a. avengers    b. beep\n");
printf("c. count       q. quit\n");

๋ถ€๋ถ„์ด ๋‘๋ฒˆ์”ฉ ๋‚˜์˜จ๋‹ค.

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

char get_choice(void);
char get_first_char(void);
int get_integer(void);
void count(void);

int main()
{
	int user_choice;

	while ((user_choice = get_choice()) != 'q')
	{
		switch (user_choice)
		{
		case 'a':
			printf("Avengersassemble!\n");
			break;
		case 'b':
			printf("\a");
			break;
		case 'c':
			count();
			break;
		default:
			printf("Error with %d.\n", user_choice);
			exit(1);
			break;
		}
	
	
	}
	return 0;
}

void count(void)
{
	int n, i;

	printf("Enter an integer:\n");
	n = get_integer();
	for (i = 1; i <= n; i++)
		printf("%d\n", i);
	while (getchar() != '\n')
		continue;
}


char get_choice(void)
{
	int user_input;
	
	printf("Enter the letter of your choice:\n");
	printf("a. avengers\tb. beep\n");
	printf("c. count\tq. quit\n");

	user_input = get_first_char();

	while ((user_input < 'a' || user_input > 'c') && user_input != 'q')
	{
		printf("Please try again.\n");
		user_input = get_first_char;
	}
	return user_input;
}

char get_first_char(void)
{
	int ch;

	ch = getchar();
	while (getchar() != '\n')
		continue;

	return ch;
}

int get_integer(void)
{
	int input;
	char c;

	while (scanf("%d", &input) != 1)
	{
		while((c= getchar()) != '\n')
		putchar(c);
		printf(" is not an integer.\nPlease try again.");
	}
	return input;
}

๋”ฐ๋ฐฐ์”จ์ฝ”๋“œ

 

ํ•  ๋ง์ด ์—†๋‹ค. ๋งŽ์ด ๋ณด๊ณ  ์ดํ•ดํ•˜๊ณ  ๋…ธ๋ ฅํ•˜์ž

 

 

 

8.9 ํ…์ŠคํŠธ ํŒŒ์ผ ์ฝ๊ธฐ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>	//	exit()

int main()
{
	int c;
	FILE *file = NULL;
	char file_name[] = "my_file.txt";

	file = fopen(file_name, "r");	//	r = read
	if (file == NULL)
	{
		printf("Failed to open file.\n");
		exit(1);
	}

	while ((c = getc(file)) != EOF)
		putchar(c);
	fclose(file);

	return 0;
}

 

'๋”ฐ๋ฐฐ์”จ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๋”ฐ๋ฐฐ์”จ 9.10 ~  (0) 2021.01.21
๋”ฐ๋ฐฐ์”จ 9.1 ~  (0) 2021.01.19
๋”ฐ๋ฐฐ์”จ 7.2 ~  (0) 2021.01.15
๋”ฐ๋ฐฐ์”จ 6.10 ~  (0) 2021.01.12
๋•Œ๋ฐฐ์”จ 6.1 ~  (0) 2021.01.11