File: calculate_areas.tiny

package info (click to toggle)
pyparsing 3.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,200 kB
  • sloc: python: 30,867; ansic: 422; sh: 112; makefile: 24
file content (103 lines) | stat: -rw-r--r-- 2,876 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
/* Compute the area of a selected shape in TINY.
   Interaction:
   - First, prompt for the shape type: enter one of "s", "c", "r", or "t"
     for square, circle, rectangle, or triangle, respectively.
   - Then, prompt only for the dimensions required for that shape.
   - All dimensions must be integers > 0 (non-positive values are rejected).
   - The program prints the area for the selected shape and exits.
*/

float area_square(float side) {
    return side * side;
}

float area_circle(float radius) {
    float pi := 3.141592653589793;
    return pi * radius * radius;
}

float area_rectangle(float width, float height) {
    return width * height;
}

float area_triangle(float base, float height) {
    return 0.5 * base * height;
}

int main() {
    string shape;

    /* Prompt for shape type */
    write "Enter shape (s=square, c=circle, r=rectangle, t=triangle): ";
    read shape;

    /* Square */
    if shape = "s" then
        int side;
        write "Enter side (integer > 0): ";
        read side;
        if side <= 0 then
            write "Error: side must be an integer > 0"; write endl;
            return 1;
        end
        write "Square area: "; write area_square(side); write endl;
        return 0;
    end

    /* Circle */
    if shape = "c" then
        int radius;
        write "Enter radius (integer > 0): ";
        read radius;
        if radius <= 0 then
            write "Error: radius must be an integer > 0"; write endl;
            return 1;
        end
        write "Circle area: "; write area_circle(radius); write endl;
        return 0;
    end

    /* Rectangle */
    if shape = "r" then
        int width;
        int height;
        write "Enter width (integer > 0): ";
        read width;
        if width <= 0 then
            write "Error: width must be an integer > 0"; write endl;
            return 1;
        end
        write "Enter height (integer > 0): ";
        read height;
        if height <= 0 then
            write "Error: height must be an integer > 0"; write endl;
            return 1;
        end
        write "Rectangle area: "; write area_rectangle(width, height); write endl;
        return 0;
    end

    /* Triangle */
    if shape = "t" then
        int base;
        int height;
        write "Enter base (integer > 0): ";
        read base;
        if base <= 0 then
            write "Error: base must be an integer > 0"; write endl;
            return 1;
        end
        write "Enter height (integer > 0): ";
        read height;
        if height <= 0 then
            write "Error: height must be an integer > 0"; write endl;
            return 1;
        end
        write "Triangle area: "; write area_triangle(base, height); write endl;
        return 0;
    end

    /* Unknown shape */
    write "Error: unknown shape; enter one of s, c, r, t"; write endl;
    return 1;
}