๐16์ฅ Structures, Unions, and Enumerations | Exercises and Programming Projects
C Programming, A Modern Approach - K.N.KING ์ Exercises์ Programming Projects๋ฅผ ํผ ๋ด์ฉ์ ๋๋ค.
๐ Exercises
- ๊ฐ๋ฅํ๋ค. ๊ตฌ๋์ (.)์ ์ด์ฉํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
- (a)
struct {
double real;
double imaginary;
} c1, c2, c3;
(b)
struct {
double real;
double imaginary;
} c1{ 0.0, 1.0 }, c2{ 1.0, 0.0 }, c3;
(c)
c1 = c2
(d)
c3.real = c1.real + c2.real;
c3.imaginary = c3.imaginary + c2.imaginary;
- (a)
struct complex {
double real;
double imaginary;
};
(b)
struct complex c1, c2, c3;
(c)
struct complex make_complex(double real, double imaginary) {
return (struct complex) { real, imaginary };
}
(d)
struct complex add_complex(struct complex a, struct complex b) {
return (struct complex) { a.real + b.real, a.imaginary + b.imaginary };
}
typedef struct {
double real;
double imaginary;
} complex;
complex c1, c2, c3;
complex make_complex(double real, double imaginary) {
return (complex) { real, imaginary };
}
complex add_complex(complex a, complex b) {
return (complex) { a.real + b.real, a.imaginary + b.imaginary };
}
- (a)
struct date {
int month;
int day;
int year;
};
int day_of_year(struct date d)
{
int day = 0;
int days_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0))
day++;
for (int i = 0; i < d.month; i++)
day += days_month[i];
return day + d.day;
}
(b)
int compare_dates(struct date d1, struct date d2) {
int day_of_d1 = day_of_year(d1);
int day_of_d2 = day_of_year(d2);
if (day_of_d1 > day_of_d2)
return -1;
else if (day_of_d1 < day_of_d2)
return 1;
else
return 0;
}
struct time split_time(long total_seconds)
{
struct time t;
t.hours = total_seconds / 3600;
total_seconds %= 3600;
t.minutes = total_seconds / 60;
total_seconds %= 60;
t.seconds = total_seconds;
return t;
}
- (a) (์ฝ๋ถํ๊ธฐ)
struct fraction reduce_fraction(struct fraction f) {
int n = f.numerator;
int d = f.denominator;
int temp;
while (n % d != 0) {
temp = d;
d = n % d;
n = temp;
}
f.numerator /= d;
f.denominator /= d;
return f;
}
(b)
struct fraction add_fraction(struct fraction f1, struct fraction f2) {
struct fraction f;
f.denominator = f1.denominator * f2.denominator;
f.numerator = f1.numerator * f2.denominator + f1.denominator * f2.numerator;
f = reduce_fraction(f);
return f;
}
(c)
struct fraction sub_fraction(struct fraction f1, struct fraction f2) {
struct fraction f;
f.denominator = f1.denominator * f2.denominator;
f.numerator = f1.numerator * f2.denominator - f1.denominator * f2.numerator;
f = reduce_fraction(f);
return f;
}
(d)
struct fraction multi_fraction(struct fraction f1, struct fraction f2) {
struct fraction f;
f.denominator = f1.denominator * f2.denominator;
f.numerator = f1.numerator * f2.numerator;
f = reduce_fraction(f);
return f;
}
(e)
struct fraction multi_fraction(struct fraction f1, struct fraction f2) {
struct fraction f;
f.denominator = f1.denominator * (1/f2.denominator);
f.numerator = f1.numerator * (1/f2.numerator);
f = reduce_fraction(f);
return f;
}
- (a)
struct color MAGENTA = {255, 0, 255};
(b)
struct color MAGENTA = {.red = 255, .blue = 255};
- (a)
struct color make_color(int red, int green, int blue) {
if (red < 0)
red = 0;
else if (red > 255)
red = 255;
if (blue < 0)
blue = 0;
else if (blue > 255)
blue = 255;
if (green < 0)
green = 0;
else if (green > 255)
green = 255;
return (struct color) { red, green, blue };
}
(b)
int getRed(struct color c) {
return c.red;
}
(c)
bool equal_color(struct color color1, struct color color2) {
if (color1.red == color2.red && color1.green == color2.green && color1.blue == color2.blue)
return true;
}
(d)
struct color brighter(struct color c) {
if (c.red + c.green + c.blue == 0)
return (struct color) { 3, 3, 3 };
if (c.red > 0 && c.red < 3)
c.red = 3;
if (c.green > 0 && c.green < 3)
c.green = 3;
if (c.blue > 0 && c.blue < 3)
c.blue = 3;
c.red /= 0.7;
c.green /= 0.7;
c.blue /= 0.7;
if (c.red > 255)
c.red = 255;
if (c.green > 255)
c.green = 255;
if (c.blue > 255)
c.blue = 255;
return c;
}
(e)
struct color darker(struct color c) {
c.red *= 0.7;
c.green *= 0.7;
c.blue *= 0.7;
return c;
}
- (a)
int area = (r.lower_right.x - r.upper_left.x) * (r.lower_right.y - r.upper_left.y);
(b)
struct point rectangle_center(struct rectangle r) {
struct point p;
p.x = (r.lower_right.x - r.upper_left.x) / 2;
p.y = (r.lower_right.y - r.upper_left.y) / 2;
return p;
}
(c)
struct rectangle move_rect(struct rectangle r, int x, int y) {
r.upper_left.x += x;
r.upper_left.y += y;
r.lower_right.x += x;
r.lower_right.y += y;
return r;
}
(d)
bool is_within_rect(struct rectangle r, struct point p) {
if (p.x > r.upper_left.x && p.x < r.lower_right.x && p.y > r.upper_left.y && p.y < r.lower_right.y)
return true;
else
return false;
}
- double 8 byte + union 8 byte (for double) + char 4 byte
์ด 20byte๊ฐ ํ ๋น๋๋ค.
-
struct๊ฐ 4+8+4๋ก 16byte๋ฅผ ํ ๋น๋ฐ์ผ๋ฏ๋ก union์๋ 16byte๊ฐ ํ ๋น๋๋ค.
-
(a), (b), (d) ๊ฐ ์ณ์ ํํ์ด๋ค.
(c) s.u.rectangle.height = 25;
(e) s.u.circle.radius = 5;
(f) s.u.circle.radius = 5;
- (a)
double shape_area(struct shape s) {
if (s.shape_kind == RECTANGLE)
return s.u.rectangle.height * s.u.rectangle.width;
else
return 3.1415 * s.u.circle.radius * s.u.circle.radius;
}
(b)
struct shape shape_move(struct shape s, int x, int y) {
s.center.x += x;
s.center.y += y;
return s;
}
(c)
struct shape shape_scale(struct shape s, double c) {
if (s.shape_kind == RECTANGLE) {
s.u.rectangle.height *= c;
s.u.rectangle.width *= c;
}
else
s.u.circle.radius *= c;
return s;
}
- (a)
enum days = { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };
(b)
typedef enum { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } days;
- (a), (c), (e) ๊ฐ ์ณ์ ํํ์ด๋ค.
- (b), (c)๋ ๋ฒ์ ๋ฐ์ ์ ์๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋ ์ํ์ด ์๋ค.
- (a)
typedef enum {EMPTY, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING} Piece;
typedef enum {BLACK, WHITE} Color;
(b)
typedef struct { Piece; Color; } Square;
(c)
Square board[8][8];
struct pinball_machine {
char[40] name;
int year;
enum { EM, SS } type;
int players;
};
switch (direction) {
case NORTH: y--;
break;
case SOUTH: y++;
break;
case EAST: x++;
break;
case WEST: x--;
break;
default:
break;
}
- (a) 0, 1, 2, 3
(b) 11, 12, 13
(c) 14, 15, 16, 24, 25
(d) 45, 46, 47, 37, 38, 39
- (a)
enum chess_pieces {KING = 200, QUEEN = 9, ROOK = 5, BISHOP = 3, KNIGHT = 3, PAWN = 1};
(b)
const int piece_value[] = {
[KING] = 200,
[QUEEN] = 9,
[ROOK] = 5,
[BISHOP] = 3,
[KNIGHT] = 3,
[PAWN] = 1
};
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(๐16์ฅ Structures, Unions, and Enumerations | Exercises and Programming Projects), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@mi_pine/16์ฅ-Structures-Unions-and-Enumerations-Exercises-and-Programming-Projects์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค