forked from Salamander0/Progtest
/
homework01.c
90 lines (64 loc) · 1.76 KB
/
homework01.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <math.h>
// prompts for side a and calculates area and perimeter of square
int square()
{
double a;
printf("Enter the side of the square:\n");
if (scanf("%lf", &a) != 1 || a <= 0) {
return 1;
}
printf("Square area is: %f\n", a * a);
printf("Square perimeter is %f\n", 4 * a);
return 0;
}
// prompts for side a and b and calculates area and perimeter of rectangle
int rectangle()
{
double a, b;
printf("Enter the sides of the rectangle:\n");
if (scanf("%lf %lf", &a, &b) != 2 || a <= 0 || b <= 0) {
return 1;
}
printf("Rectangle area is: %f\n", a * b);
printf("Rectangle perimeter is: %f\n", 2 * a + 2 * b);
return 0;
}
// prompts for radius r and calculates area and circumvence of circle
int circle()
{
double r;
printf("Enter the radius of the circle:\n");
if (scanf("%lf", &r) != 1 || r <= 0) {
return 1;
}
printf("Circle area is: %f\n", M_PI * pow(r, 2));
printf("Circumference is: %f\n", 2 * M_PI * r);
return 0;
}
int main()
{
int result; // holds return value of the functions above
char option = '\0';
printf("Enter a 2D shape to compute area and perimeter: a - square, b - rectangle, c -circle\n");
scanf("%c", &option); // read single char
switch(option) {
case 'a':
result = square();
break;
case 'b':
result = rectangle();
break;
case 'c':
result = circle();
break;
default:
result = 1; // error
break;
}
if (result == 1) {
printf("Invalid input.\n");
return 1;
}
return 0;
}