Advertisement
Yonka2019

Untitled

Jan 12th, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define PI 3.14
  5.  
  6. float calcDistance(int x1, int x2, int y1, int y2);
  7. float calcHypotenuse(int side1, int side2);
  8. float calcCircleInfo(int radius, char info);
  9. int calcRectangleArea(int length, int width);
  10. int calcSquareArea(int length);
  11.  
  12. enum CalcOption {
  13.     distance = 1,
  14.     hypotenuse,
  15.     circleInfo,
  16.     rectangleArea,
  17.     squareArea,
  18.     nothingSelected
  19.    
  20. };
  21. int main()
  22. {
  23.     int input = 0, working = 1, num1 = 0, num2 = 0;
  24.  
  25.     while (working == 1)
  26.     {
  27.         printf("Welcome to my calculator!\n");
  28.         printf("Choose option:\n");
  29.         printf("1 - Calc distance between 2 points\n"
  30.                 "2 - Calc hypotenuse of triangle\n"
  31.                 "3 - Calc area and permieter of circle\n"
  32.                 "4 - Calc area of rectangle\n"
  33.                 "5 - Calc area of square\n"
  34.                 "6 - Exit\n");
  35.  
  36.         scanf("%d", &input);
  37.         switch (input)
  38.         {
  39.             case distance:
  40.             {
  41.                 int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
  42.  
  43.                 printf("Enter point1 coordinates: ");
  44.                 scanf("%d %d", &x1, &y1);
  45.  
  46.                 printf("Enter point2 coordinates: ");
  47.                 scanf("%d %d", &x2, &y2);
  48.  
  49.                 printf("Distance is %.2f\n", calcDistance(x1, x2, y1, y2));
  50.                 break;
  51.             }
  52.             case hypotenuse:
  53.             {
  54.                 int side1 = 0, side2 = 0;
  55.  
  56.                 printf("Enter 2 sides of the triangle: ");
  57.                 scanf("%d %d", &side1, &side2);
  58.  
  59.                 printf("Hypotenuse is: %.2f\n", calcHypotenuse(side1, side2));
  60.                 break;
  61.             }
  62.             case circleInfo:
  63.             {
  64.                 int radius = 0;
  65.  
  66.                 printf("Enter circle radius: ");
  67.                 scanf("%d", &radius);
  68.  
  69.                 printf("Perimeter: %.2f\n", calcCircleInfo(radius, 'p'));
  70.                 printf("Area: %.2f\n", calcCircleInfo(radius, 'a'));
  71.                 break;
  72.             }
  73.             case rectangleArea:
  74.             {
  75.                 int length = 0, width = 0;
  76.    
  77.                 printf("Enter rectangle length: ");
  78.                 scanf("%d", &length);
  79.  
  80.                 printf("Enter rectangle width: ");
  81.                 scanf("%d", &width);
  82.                
  83.                 printf("The area of the recrangle is %d", calcRectangleArea(length, width));
  84.                 break;
  85.             }
  86.             case squareArea:
  87.             {
  88.                 int length = 0;
  89.  
  90.                 printf("Enter length of square side: ");
  91.                 scanf("%d", &length);  
  92.                
  93.                 printf("The area of the square is %d", calcSquareArea(length));
  94.                 break;
  95.             }
  96.             case nothingSelected:
  97.                 printf("Goodbye!\n");
  98.                 working = 0;
  99.                 break;
  100.         }
  101.     }
  102. }
  103. /*
  104. calculates distance between two given points
  105. input: x1,y1 x2,y2
  106. output: distance between this points (x1, y1 - x2, y2)
  107. */
  108. float calcDistance(int x1, int x2, int y1, int y2)
  109. {
  110.     return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
  111. }
  112. /*
  113. calculates hypotenuse in triangle according the both sides
  114. input: two sides
  115. output: hypotenuse according the given sides
  116. */
  117. float calcHypotenuse(int side1, int side2)
  118. {
  119.     return sqrt(pow(side1, 2) + pow(side2, 2));    
  120. }
  121. /*
  122. calculates circle perimeter and area according the given radius
  123. input: radius (radius of the circle) & info (info what return: perimeter/area)
  124. output: circle perimeter/area
  125. */
  126. float calcCircleInfo(int radius, char info)
  127. {
  128.     if (info == 'p')
  129.     {
  130.         return 2 * PI * radius;
  131.  
  132.     }    
  133.     if (info == 'a')
  134.     {
  135.         return PI * pow(radius, 2);
  136.     }
  137. }
  138. /*
  139. calculates rectangle area according the given length & width
  140. input: length & width
  141. output: Rectangle area (length * width)
  142. */
  143. int calcRectangleArea(int length, int width)
  144. {
  145.     return length * width;
  146. }
  147. /*
  148. calculates square area according the given side length
  149. input: square length
  150. output: square area (length^2)
  151.  */
  152. int calcSquareArea(int length)
  153. {
  154.     return length * length;
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement