1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include "my.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>
#define MAX_WIDTH 22 #define MAX_RADIUS 11
#ifdef __unix__ #define SYSTEM "unix" #elif __linux__ #define SYSTEM "linux" #elif _WIN32 #define SYSTEM "windows" #elif _WIN64 #define SYSTEM "windows" #else #error "unkonw system" #endif
#define MALLOC(type, name, len) \ type* name = (type*)malloc(sizeof(type) * len); \ name = memset(name, 0, sizeof(type) * len) \
#define CREATE_CIRCLE(type, name, len) \ MALLOC(type, name, len); \ name->radius = 0; \ name->width = 0; \ name->printCircle = print##type; \ printf("type " #type " has been created in %s\n", SYSTEM) \
struct Position { int x; int y; };
typedef struct _Circle { int radius; int width; struct Position point; void (*printCircle)(struct _Circle*); } Circle;
void printCircle(Circle* circle) { struct Position* position = &circle->point;
int w, h = 0; int i = w = 0; do { while(1) { for(h = 0; h < circle->width; h += 1) { for(w = 0; w < circle->width; w = w + 1) { int distance = pow(h - position->x, 2) + pow(w - position->y, 2); int r = pow(circle->radius, 2); char result = distance <= pow(circle->radius, 2) ? '*' : ' '; switch (result) { case '*': printf("%c",result); break; case ' ': default: printf("%c",' '); break; } } printf("\n"); }
if (w != circle->width && h != circle->width) { continue; } else if(w == circle->width && h == circle->width) { break; } } break; } while (++i); }
Circle* createCircle(int x, int y, int radius, int width);
int main(int argc, char** argv) { int width[2] = {22, 18}; int* w = width; int* radius = (int*)malloc(sizeof(int) * 2);
*(radius + 1) = 8; radius[0] = 10;
Circle* circle; for(int i = 0; i < 2; i++) { circle = createCircle( w[i] / 2, *(w + i) / 2, radius[i], w[i]); void (*print)(Circle*) = circle->printCircle; (*print)(circle);
free(circle); }
return 0; }
Circle* createCircle(int x, int y, int radius,int width) { CREATE_CIRCLE(Circle, circle, 1); struct Position position; position.x = x; position.y = y; circle->point = position; (*circle).radius = radius; circle->width = width; return circle; }
|