Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define PI 3.14
- float calcDistance(int x1, int x2, int y1, int y2);
- float calcHypotenuse(int side1, int side2);
- float calcCircleInfo(int radius, char info);
- int calcRectangleArea(int length, int width);
- int calcSquareArea(int length);
- enum CalcOption {
- distance = 1,
- hypotenuse,
- circleInfo,
- rectangleArea,
- squareArea,
- nothingSelected
- };
- int main()
- {
- int input = 0, working = 1, num1 = 0, num2 = 0;
- while (working == 1)
- {
- printf("Welcome to my calculator!\n");
- printf("Choose option:\n");
- printf("1 - Calc distance between 2 points\n"
- "2 - Calc hypotenuse of triangle\n"
- "3 - Calc area and permieter of circle\n"
- "4 - Calc area of rectangle\n"
- "5 - Calc area of square\n"
- "6 - Exit\n");
- scanf("%d", &input);
- switch (input)
- {
- case distance:
- {
- int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
- printf("Enter point1 coordinates: ");
- scanf("%d %d", &x1, &y1);
- printf("Enter point2 coordinates: ");
- scanf("%d %d", &x2, &y2);
- printf("Distance is %.2f\n", calcDistance(x1, x2, y1, y2));
- break;
- }
- case hypotenuse:
- {
- int side1 = 0, side2 = 0;
- printf("Enter 2 sides of the triangle: ");
- scanf("%d %d", &side1, &side2);
- printf("Hypotenuse is: %.2f\n", calcHypotenuse(side1, side2));
- break;
- }
- case circleInfo:
- {
- int radius = 0;
- printf("Enter circle radius: ");
- scanf("%d", &radius);
- printf("Perimeter: %.2f\n", calcCircleInfo(radius, 'p'));
- printf("Area: %.2f\n", calcCircleInfo(radius, 'a'));
- break;
- }
- case rectangleArea:
- {
- int length = 0, width = 0;
- printf("Enter rectangle length: ");
- scanf("%d", &length);
- printf("Enter rectangle width: ");
- scanf("%d", &width);
- printf("The area of the recrangle is %d", calcRectangleArea(length, width));
- break;
- }
- case squareArea:
- {
- int length = 0;
- printf("Enter length of square side: ");
- scanf("%d", &length);
- printf("The area of the square is %d", calcSquareArea(length));
- break;
- }
- case nothingSelected:
- printf("Goodbye!\n");
- working = 0;
- break;
- }
- }
- }
- /*
- calculates distance between two given points
- input: x1,y1 x2,y2
- output: distance between this points (x1, y1 - x2, y2)
- */
- float calcDistance(int x1, int x2, int y1, int y2)
- {
- return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
- }
- /*
- calculates hypotenuse in triangle according the both sides
- input: two sides
- output: hypotenuse according the given sides
- */
- float calcHypotenuse(int side1, int side2)
- {
- return sqrt(pow(side1, 2) + pow(side2, 2));
- }
- /*
- calculates circle perimeter and area according the given radius
- input: radius (radius of the circle) & info (info what return: perimeter/area)
- output: circle perimeter/area
- */
- float calcCircleInfo(int radius, char info)
- {
- if (info == 'p')
- {
- return 2 * PI * radius;
- }
- if (info == 'a')
- {
- return PI * pow(radius, 2);
- }
- }
- /*
- calculates rectangle area according the given length & width
- input: length & width
- output: Rectangle area (length * width)
- */
- int calcRectangleArea(int length, int width)
- {
- return length * width;
- }
- /*
- calculates square area according the given side length
- input: square length
- output: square area (length^2)
- */
- int calcSquareArea(int length)
- {
- return length * length;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement