Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define N 5
  4.  
  5. struct gshape
  6. {
  7. int type; // 0/1/2
  8. int ID;
  9.  
  10. union
  11. {
  12. struct
  13. {
  14. float radius;
  15. float x,y;
  16.  
  17. }circle;
  18.  
  19. struct
  20. {
  21. float x1,y1,x2,y2,x3,y3;
  22. }triangle;
  23.  
  24. struct
  25. {
  26. float x1,y1; //upper left
  27. float x4,y4; //right bottom
  28. }rectangle;
  29.  
  30. }gshape;
  31.  
  32.  
  33. void (*read_gshape) (struct gshape *gs);
  34.  
  35. void (*write_gshape) (struct gshape *gs);
  36.  
  37. };
  38.  
  39. void read_circle(struct gshape *gs)
  40. {
  41. printf("radius? ");
  42. scanf("%f", &(gs->gshape.circle.radius));
  43. printf("X center? ");
  44. scanf("%f", &(gs->gshape.circle.x));
  45. printf("Y center? ");
  46. scanf("%f", &(gs->gshape.circle.y));
  47. }
  48.  
  49. void write_circle(struct gshape *gs)
  50. {
  51. printf("Printing circle ID=%d: \n", gs->ID);
  52. }
  53.  
  54. void read_triangle(struct gshape *gs)
  55. {
  56. printf("Reading triangle:\n");
  57. }
  58.  
  59. void write_triangle(struct gshape *gs)
  60. {
  61. printf("Triangle (%f,%f) (%f,%f), (%f,%f)\n",gs->gshape.triangle.x1,gs->gshape.triangle.y1,gs->gshape.triangle.x2,gs->gshape.triangle.y2,gs->gshape.triangle.x3,gs->gshape.triangle.y3);
  62. }
  63.  
  64. void read_rectangle(struct gshape *gs)
  65. {
  66. printf("Reading rectangle:\n");
  67. }
  68.  
  69. void write_rectangle(struct gshape *gs)
  70. {
  71. printf("Printing rectangle:\n");
  72. }
  73.  
  74. int main()
  75. {
  76. int i, type;
  77. struct gshape gs[N];
  78.  
  79. printf("Initializing the geometric shapes colletcion:\n");
  80.  
  81. for(i=0;i<N;i++)
  82. {
  83. do
  84. {
  85. printf("Shape ID %d: ",i);
  86. printf("What type of shape is it? (0=circle, 1=triangle, 2=rectangle) ");
  87. scanf("%d", &type);
  88. gs[i].type=type;
  89. gs[i].ID= i;
  90.  
  91. switch(gs[i].type)
  92. {
  93. case 0:
  94. gs[i].read_gshape = &read_circle;
  95. gs[i].write_gshape = &write_circle;
  96. break;
  97.  
  98. case 1:
  99. gs[i].read_gshape = &read_circle;
  100. gs[i].write_gshape = &write_circle;
  101. break;
  102.  
  103. case 2:
  104. gs[i].read_gshape = &read_rectangle;
  105. gs[i].write_gshape = &write_rectangle;
  106. break;
  107.  
  108. default:
  109. printf("Yikes!\n");
  110. }
  111. }while((gs[i].type != 0) && (gs[i].type != 1) && (gs[i].type != 2));
  112. }
  113.  
  114. printf("Printing all the circles from our collection:\n ");
  115.  
  116. for(i=0;i<N;i++)
  117. if(gs[i].type==0)
  118. gs[i].write_gshape(&gs[i]);
  119.  
  120. return 1;
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement