fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct{
  4. double x;
  5. double y;
  6. }Point;
  7.  
  8. double absolute(double val){
  9. if(val<0){
  10. return -val;
  11. }
  12. return val;
  13. }
  14.  
  15. Point scan_point(void){
  16. Point p;
  17. printf("X座標を入力:");
  18. fflush(stdout);
  19. scanf("%lf",&p.x);
  20. printf("Y座標を入力:");
  21. scanf("%lf",&p.y);
  22. return p;
  23. }
  24.  
  25. double area_of(Point p1, Point p2){
  26. double width=absolute(p1.x-p2.x);
  27. double height=absolute(p1.y-p2.y);
  28. return width*height;
  29. }
  30.  
  31. double circumference_of(Point p1, Point p2){
  32. double width=absolute(p1.x-p2.x);
  33. double height=absolute(p1.y-p2.y);
  34. return 2.0*(width+height);
  35. }
  36.  
  37. int main(void){
  38. printf("左上隅の座標を入力してください\n");
  39. Point p1=scan_point();
  40.  
  41. printf("\n右下隅の座標を入力してください\n");
  42. Point p2=scan_point();
  43.  
  44. double area=area_of(p1, p2);
  45. double circumference=circumference_of(p1, p2);
  46.  
  47. printf("\n面積:%.2f\n",area);
  48. printf("周囲の長さ:%.2f\n",circumference);
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 5288KB
stdin
0
0
1
1
stdout
左上隅の座標を入力してください
X座標を入力:Y座標を入力:
右下隅の座標を入力してください
X座標を入力:Y座標を入力:
面積:1.00
周囲の長さ:4.00