๋ฐ๋ฐฐ์จ 7.2 ~
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;
}