๐ก
๋ฐ๋ฐฐ์จ 7.10 ~ ๋ณธ๋ฌธ
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 |