#include #include #include /* may still need gcc shapes1.c -lm */ #define PI 3.1415927 struct circle { int x; int y; double radius; }; typedef struct circle circle; circle make_circle(int x, int y, double r) { struct circle C; C.x = x; C.y = y; C.radius = r; return C; // copying elided by modern compilers upon return } double area_c(circle* this) { return this->radius*this->radius*PI; } double circumference_c(circle* self) { return self->radius*2*PI; } double distance_c(circle* this, circle *other) { int dx = this->x - other->x; int dy = this->y - other->y; return sqrt((double)(dx*dx + dy*dy)); } struct rectangle { int x; int y; double width; double height; }; typedef struct rectangle rectangle; void make_rectangle(rectangle *this, int x, int y, double w, double h) { this->x=x; this->y=y; this->width=w; this->height=h; }// old C style, allocate memory "down stack" double area_r(rectangle* this) { return this->width*this->height; } double circumference_r(rectangle* self) { return self->width*2 + self->height*2; } double distance_r(rectangle* this, rectangle *other) { int dx = this->x - other->x; int dy = this->y - other->y; return sqrt(dx*dx + dy*dy); } int main() { circle C[2] = {make_circle(3,4,5), make_circle(50,60,10)}; rectangle R[2]; make_rectangle(R,3,4,5,6); make_rectangle(R+1,30,40,50,60); double totalarea = 0; int i = 0; for(;i<2;i++) { totalarea += area_c(C+i); totalarea+= area_r(R+i); } printf("totalarea = %.2f\n",totalarea); return 0; }//main