๋”ฐ๋ฐฐ์”จ

๋”ฐ๋ฐฐ์”จ 7.2 ~

์•„์˜ณ์ด 2021. 1. 15. 19:08

7.2 ํ‘œ์ค€ ์ž…์ถœ๋ ฅ ํ•จ์ˆ˜๋“ค getchar(), putchar()

ํ•œ ๊ธ€์ž์”ฉ ์ž…๋ ฅ์„ ๋ฐ›์•„๋“ค์—ฌ ๋ฒ„ํผ์— ์ €์žฅ ํ›„ ํ•œ ๊ธ€์ž์”ฉ ์ถœ๋ ฅํ•จ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
	/*
		1. Introduce getchar(), putchar
		2. Use while loop to process character sequences
		3. Filter a sepcific character
		4. Convert number to asterisks
		5. Lower characters to Upper characters
	*/

	char ch;

	//	1
	ch = getchar();
	putchar(ch);

	//	2, 3
	while((ch = getchar()) != '\n')	//	Use '\n' to find the end of a sentence
	{
		if (ch == 'f' || ch =='F')
			ch = 'X';
		//else if (ch == 'F')
		//	ch = 'X';
		
		putchar(ch);
	}

	//	4
	while ((ch = getchar()) != '\n')
	{
		//for (int i = '0'; i <= 9; ++i)
		if (ch >= '0' && ch <= '9')
			ch = '*';

		putchar(ch);
	}

	//	5
	while ((ch = getchar()) != '\n')
	{
		if (ch >= 'a' && ch <= 'z')
			ch -= 'a' - 'A';
		else if (ch >= 'A' && ch <= 'Z')
			ch += 'a' - 'A';
		putchar(ch);
	}

	putchar(ch);		//	'\n'

	return 0;
}

 

 

 

7.3 ctype.h ๋ฌธ์ž ํ•จ์ˆ˜๋“ค

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

/*
	Reference link
	https://tutorialspoint.com/c_standard_library/ctype_h.htm
*/

int main()
{
	char ch;

	while ((ch = getchar()) != '\n')
	{
		if (islower(ch))
			ch = toupper(ch);
		else if (isupper(ch))
			ch = tolower(ch);

		if (isdigit(ch) != 0)
			ch = '*';

		putchar(ch);
	}

	putchar(ch);

	return 0;
}

7.2์˜ 4๋ฒˆ 5๋ฒˆ ์˜ˆ์ œ๋ฅผ <ctype.h>ํ—ค๋”ํŒŒ์ผ์˜ ํ•จ์ˆ˜๋“ค์„ ์ด์šฉํ•˜์—ฌ ๋ฐ”๊ฟ” ๋ณธ ๊ฒƒ๋“ค

์˜ˆ์ œ์˜ ํ•จ์ˆ˜ ๋ง๊ณ ๋„

isalnum() ๋ฌธ์ž๋‚˜ ์ˆซ์ž์ธ์ง€

isalpha() ์ˆซ์ž์ธ์ง€

isxdigit() 16์ง„์ˆ˜ ์ˆซ์ž์ธ์ง€

๋“ฑ์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

 

 

 

7.4 ๋‹ค์ค‘ ์„ ํƒ else if

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

//	assessment standard tax base
#define BASE1  12000000.0
#define BASE2  46000000.0
#define BASE3  88000000.0
#define BASE4 150000000.0
#define BASE5 300000000.0
#define BASE6 500000000.0

#define RATE1 (6.0 / 100.0)
#define RATE2 (15.0 / 100.0)
#define RATE3 (24.0 / 100.0)
#define RATE4 (35.0 / 100.0)
#define RATE5 (38.0 / 100.0)
#define RATE6 (40.0 / 100.0)
#define RATE7 (42.0 / 100.0)

#define BASIC_DEDUCTION 1500000.0

int main()
{
	double income = 0.0;
	double tax = 0.0;

	scanf("%lf", &income);

	income -= BASIC_DEDUCTION;

	if (income <= BASE1)
	{
		tax = income * RATE1;
	}
	else if (income <= BASE2)
	{
		tax = BASE1 * RATE1 + (income - BASE1) * RATE2;
	}
	else if (income <= BASE3)
	{
		tax = BASE1 * RATE1 + (BASE2 - BASE1) * RATE2 + (income - BASE2) * RATE3;
	}
	else if (income <= BASE4)
	{
		tax = BASE1 * RATE1 + (BASE2 - BASE1) * RATE2 + (BASE3 - BASE2) * RATE3 + (income - BASE3) * RATE4;
	}
	else if (income <= BASE5)
	{
		tax = BASE1 * RATE1 + (BASE2 - BASE1) * RATE2 + (BASE3 - BASE2) * RATE3 + (BASE4 - BASE3) * RATE4 
			+ (income - BASE4) * RATE5;
	}
	else if (income <= BASE6)
	{
		tax = BASE1 * RATE1 + (BASE2 - BASE1) * RATE2 + (BASE3 - BASE2) * RATE3 + (BASE4 - BASE3) * RATE4
			+ (BASE5 - BASE4) * RATE5 + (income - BASE5) * RATE6;
	}
	else
	{
		tax = BASE1 * RATE1 + (BASE2 - BASE1) * RATE2 + (BASE3 - BASE2) * RATE3 + (BASE4 - BASE3) * RATE4
			+ (BASE5 - BASE4) * RATE5 + (BASE6 - BASE5) * RATE6 + (income - BASE6) * RATE7;
	}
	
	printf("Tax is = %f\n", tax);
	printf("Your icome after tax deduction = %f", income - tax);


	return 0;
}

์†Œ๋“๊ณต์ œ ๊ณ„์‚ฐ๊ธฐ ์˜ˆ์ œ

 

 

 

7.5 else์™€ if ์ง์ง“๊ธฐ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
	int number;
	scanf("%d", &number);

	if (number == 1)
		printf("One");
	else if (number == 2)
		printf("Two");
	else if (number == 3)
		printf("Tree");

	/*
	if (number == 1)
		printf("One");
	else
		if (number == 2)
			printf("Two");
		else
			if (number == 3)
			printf("Tree");
	*/

	if (number > 5)	//	{}๋กœ block ๋งŒ๋“ค์–ด์ฃผ๊ธฐ
	{
		if (number < 10)
			printf("Larger than 5 smaller than 10");
		else
			printf("Larger than 10");
	}
	else
		printf("Less than or equal to 5");


	return 0;
}

 ๋งˆ์ง€๋ง‰ ์˜ˆ์ œ

if (number > 5) 
    if (number < 10)
        printf("Larger than 5 smaller than 10");
else
    printf("Less than or equal to 5");

 

์ด๋ ‡๊ฒŒ ๋งŒ๋“ค๋ฉด else๋Š” ์ฒซ ๋ฒˆ์งธ if์™€ ์ง์ธ ๊ฑฐ ๊ฐ™์•„ ๋ณด์ด์ง€๋งŒ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” indenting์„ ๋ฌด์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์—

๋‘ ๋ฒˆ์งธ if์™€ ์ง์ด ๋˜์–ด 5๋ณด๋‹ค ์ž‘์€ ์ˆ˜๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

 

 

7.6 ์†Œ์ˆ˜ ํŒ๋‹จ ์˜ˆ์ œ

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

int main()
{
	unsigned num;
	bool isprime = 0;//	flag, try bool type

	scanf("%u", &num);

	isprime = true;

	for (unsigned div = 2; (div*div) <= num; div++)
	{
		if (num % div == 0)
		{
			isprime = false;
			
			if (num == div * div)
				printf("%u is divisible by %u.\n", num, div);
			else
				printf("%u is divisible by %u and %u.\n", num, div, num/div);
		}
	}

	if (isprime)
		printf("%u is a prime number.\n", num);
	else
		printf("%u is not a prime number.\n", num);
	
	return 0;
}

 

 

 

7.7 ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž Logical operators

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>		//	islower()
#include <iso646.h>		//	and, or, not

#define PERIOD '.'

int main()
{
	/*
		Lofical operators

		&& : and
		|| : or
		!  : not
	*/

	bool test1 = 3 > 2 || 5 > 6;	//	true
	bool test2 = 3 > 2 && 5 > 6;	//	false
	bool test3 = !(5 > 6);			//	true, equivalent to 5 <= 6

	printf("%d %d %d\n", test1, test2, test3);



	char ch;
	int count = 0;

	while ((ch = getchar()) != PERIOD)
	{
		if (ch != '\n' && ch != ' ')		
			count++;
	}

	printf("%d", count);



	int a = 1, b = 2, c = 3, d = 4;

	a > b && b > c || b > d;
	((a > b) && (b > c)) || (b > d);



	/*
		Short-circuit Evaluation

		- Logical expressions evaluated from left to right
		- && ans || are sequence points
	*/

	int temp = (1 + 2) * (3 + 4);

	printf("Before : %d\n", temp);

	if (temp == 0 && (++temp == 1024)) {
		// do nothing
	};

	printf("After : %d\n", temp);



	int x = 1, y = 2;
	if (x++ > 0 && x + y == 4)
		printf("%d %d\n", x, y);



	/* Ranges */

	for (int i = 0; i < 100; ++i)
		if (i >= 10 && i <= 20)
			printf("%d ", i);

	printf("\n");

	for (int i = 0; i < 100; ++i)
		if (10 <= i <= 20)			//	Note : if((10 <= i) <= 20)
			printf("%d ", i);



	for (char c = -128; c < 127; ++c)
		if (c >= 'a' && c <= 'z')
			printf("%c ", c);

	printf("\n");

	for (char c = -128; c < 127; ++c)
		if (islower(c))
			printf("%c ", c);

	return 0;
}

 

 

 

7.8 ๋‹จ์–ด ์„ธ๊ธฐ ์˜ˆ์ œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>

#define STOP '.'

int main()
{
	int chars = 0;
	int words = 0;
	int lines = 0;
	char c;
	bool word_flag = false;
	bool line_flag = false;

	printf("Enter text : \n");

	while ((c = getchar()) != STOP)
	{
		if (!isspace(c))
			chars++;
		
		if (!isspace(c) && !word_flag)
		{
			words++;
			word_flag = true;
		}
		
		if (isspace(c))
			word_flag = false;

		if (!isspace(c) && !line_flag)
		{
			lines++;
			line_flag = true;
		}
			
		if (c == '\n')
			line_flag = false;
	}

	printf("Characters = %d, Words = %d, Lines = %d", chars, words, lines);

	return 0;
}

bool์ž๋ฃŒํ˜• true์™€ false๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์„ ์ง‘์ค‘ํ•ด์„œ ๋ด๋ณด์ž ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์› ๋‹ค.

 

 

 

7.9 ์กฐ๊ฑด ์—ฐ์‚ฐ์ž Conditional Operator ? :

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>


int main()
{
	int temp;
	temp = true ? 1024 : 7;	//	true๋ฉด ์™ผ์ชฝ false๋ฉด ์˜ค๋ฅธ์ชฝ
	printf("%d\n", temp);

	temp = false ? 1024 : 7;
	printf("%d", temp);

	
	int number;
	scanf("%d", &number);

	bool is_even;

	/*
	if (number % 2 == 0)
		is_even = true;
	else
		is_even = false;
	*/

	is_even = (number % 2 == 0) ? true : false;

	if (is_even)
		printf("Even");
	else
		printf("Odd");

	//(number % 2 == 0) ? printf("Even") : printf("Odd");

	int a = 1, b = 2;
	int max = (a > b) ? a : b;

	return 0;
}