๐Ÿ“—16์žฅ Structures, Unions, and Enumerations | Exercises and Programming Projects

C Programming, A Modern Approach - K.N.KING ์˜ Exercises์™€ Programming Projects๋ฅผ ํ‘ผ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

๐Ÿ“ Exercises


  1. ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ตฌ๋‘์ (.)์„ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. (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;
  1. (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 };
}
  1. (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;
}
  1. (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;
}

  1. (a)
struct color MAGENTA =  {255, 0, 255};

(b)

struct color MAGENTA =  {.red = 255, .blue = 255};
  1. (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;
}
  1. (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;
}
  1. double 8 byte + union 8 byte (for double) + char 4 byte
    ์ด 20byte๊ฐ€ ํ• ๋‹น๋œ๋‹ค.

  1. struct๊ฐ€ 4+8+4๋กœ 16byte๋ฅผ ํ• ๋‹น๋ฐ›์œผ๋ฏ€๋กœ union์—๋Š” 16byte๊ฐ€ ํ• ๋‹น๋œ๋‹ค.

  2. (a), (b), (d) ๊ฐ€ ์˜ณ์€ ํ‘œํ˜„์ด๋‹ค.

(c) s.u.rectangle.height = 25;
(e) s.u.circle.radius = 5;
(f) s.u.circle.radius = 5;


  1. (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;
}

  1. (a)
enum days = { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };

(b)

typedef enum { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } days;

  1. (a), (c), (e) ๊ฐ€ ์˜ณ์€ ํ‘œํ˜„์ด๋‹ค.

  1. (b), (c)๋Š” ๋ฒ”์œ„ ๋ฐ–์˜ ์ •์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋  ์œ„ํ—˜์ด ์žˆ๋‹ค.

  1. (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;
}

  1. (a) 0, 1, 2, 3
    (b) 11, 12, 13
    (c) 14, 15, 16, 24, 25
    (d) 45, 46, 47, 37, 38, 39

  1. (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
};

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ