📗第16章構造、ユニットと顧客|ExecisesとProgramming Projects
C Programming,A ModernApproach-K.N.KINGのExecisesとProgramming Projects.
📍 Exercisesでいいです.靴屋(.)利用可能 (a) (a)
(a)
(a)(約分) (a) (a) (a) double 8 byte + union 8 byte (for double) + char 4 byte
合計20バイトを割り当てます.
structは4+8+4で16バイトを割り当てるので、unionは16バイトを割り当てる.
(a)、(b)、(d)は正しい言い方です.
(c) s.u.rectangle.height = 25;
(e) s.u.circle.radius = 5;
(f) s.u.circle.radius = 5; (a) (a) (a)、(c)、(e)は正しい言い方です. (b)、(c)は範囲外の整数を指す危険がある. (a)
Square board[8][8];
(a) 0, 1, 2, 3
(b) 11, 12, 13
(c) 14, 15, 16, 24, 25
(d) 45, 46, 47, 37, 38, 39 (a)
📍 Exercises
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;
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 };
}
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;
}
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;
}
struct color MAGENTA = {255, 0, 255};
(b) struct color MAGENTA = {.red = 255, .blue = 255};
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;
}
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;
}
合計20バイトを割り当てます.
structは4+8+4で16バイトを割り当てるので、unionは16バイトを割り当てる.
(a)、(b)、(d)は正しい言い方です.
(e) s.u.circle.radius = 5;
(f) s.u.circle.radius = 5;
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;
}
enum days = { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };
(b)typedef enum { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } days;
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;
}
(b) 11, 12, 13
(c) 14, 15, 16, 24, 25
(d) 45, 46, 47, 37, 38, 39
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
};
Reference
この問題について(📗第16章構造、ユニットと顧客|ExecisesとProgramming Projects), 我々は、より多くの情報をここで見つけました https://velog.io/@mi_pine/16장-Structures-Unions-and-Enumerations-Exercises-and-Programming-Projectsテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol