fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. // X座標とY座標で構成されるPoint型構造体
  5. typedef struct {
  6. double x;
  7. double y;
  8. } Point;
  9.  
  10. // 関数のプロトタイプ宣言
  11. Point scan_point(void);
  12. double area_of(Point p1, Point p2);
  13. double circumference_of(Point p1, Point p2);
  14.  
  15. int main(void) {
  16. printf("左上隅と右下隅の座標を入力してください。\n");
  17.  
  18. // 座標1(左上隅)と座標2(右下隅)の入力
  19. Point p1 = scan_point();
  20. Point p2 = scan_point();
  21.  
  22. // 面積と周囲の長さの計算
  23. double area = area_of(p1, p2);
  24. double circumference = circumference_of(p1, p2);
  25.  
  26. // 結果の出力(小数点以下2桁まで)
  27. printf("座標1(%.2f, %.2f)\n", p1.x, p1.y);
  28. printf("座標2(%.2f, %.2f)\n", p2.x, p2.y);
  29. printf("面積:%.2f\n", area);
  30. printf("周囲の長さ:%.2f\n", circumference);
  31.  
  32. return 0;
  33. }
  34.  
  35. Point scan_point(void) {
  36. Point p;
  37. scanf("%lf %lf", &p.x, &p.y);
  38. return p;
  39. }
  40.  
  41. double area_of(Point p1, Point p2) {
  42. double width = fabs(p1.x - p2.x);
  43. double height = fabs(p1.y - p2.y);
  44. return width * height;
  45. }
  46.  
  47. double circumference_of(Point p1, Point p2) {
  48. double width = fabs(p1.x - p2.x);
  49. double height = fabs(p1.y - p2.y);
  50. return 2.0 * (width + height);
  51. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
左上隅と右下隅の座標を入力してください。
座標1(0.00, 0.00)
座標2(0.00, 0.00)
面積:0.00
周囲の長さ:0.00