Lab 4 Mark Roche

Dependencies:   mbed

Committer:
mark_roche
Date:
Wed May 11 17:27:20 2022 +0000
Revision:
0:2217986be753
Lab 4 Mark Roche

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mark_roche 0:2217986be753 1 #include "mbed.h"
mark_roche 0:2217986be753 2
mark_roche 0:2217986be753 3 #define Invalid 0
mark_roche 0:2217986be753 4 #define Square 1
mark_roche 0:2217986be753 5 #define Rectangle 2
mark_roche 0:2217986be753 6 #define Circle 3
mark_roche 0:2217986be753 7
mark_roche 0:2217986be753 8 class Shape{
mark_roche 0:2217986be753 9 public:
mark_roche 0:2217986be753 10 Shape(int a, int b) {
mark_roche 0:2217986be753 11 _a = a;
mark_roche 0:2217986be753 12 _b = b;
mark_roche 0:2217986be753 13 _r = -1;
mark_roche 0:2217986be753 14 _shap = typeis();
mark_roche 0:2217986be753 15
mark_roche 0:2217986be753 16 }
mark_roche 0:2217986be753 17
mark_roche 0:2217986be753 18 Shape(int r){
mark_roche 0:2217986be753 19 _a = -1;
mark_roche 0:2217986be753 20 _b = -1;
mark_roche 0:2217986be753 21 _r = r;
mark_roche 0:2217986be753 22 _shap = typeis();
mark_roche 0:2217986be753 23
mark_roche 0:2217986be753 24 }
mark_roche 0:2217986be753 25
mark_roche 0:2217986be753 26 int area(){
mark_roche 0:2217986be753 27 int perimeter();
mark_roche 0:2217986be753 28 float res = 0;
mark_roche 0:2217986be753 29 switch (_shap){
mark_roche 0:2217986be753 30 case (Square):
mark_roche 0:2217986be753 31 case (Rectangle):
mark_roche 0:2217986be753 32 res = _a*_b;
mark_roche 0:2217986be753 33 break;
mark_roche 0:2217986be753 34 case (Circle):
mark_roche 0:2217986be753 35 res = 3.17 * _r * _r;
mark_roche 0:2217986be753 36 break;
mark_roche 0:2217986be753 37 default:
mark_roche 0:2217986be753 38 res = -1;
mark_roche 0:2217986be753 39 break;
mark_roche 0:2217986be753 40 }
mark_roche 0:2217986be753 41 return res;
mark_roche 0:2217986be753 42 }
mark_roche 0:2217986be753 43 int perimeter(){
mark_roche 0:2217986be753 44 int res = 0;
mark_roche 0:2217986be753 45 switch(_shap){
mark_roche 0:2217986be753 46 case (Square):
mark_roche 0:2217986be753 47 res = 4 * _a;
mark_roche 0:2217986be753 48 break;
mark_roche 0:2217986be753 49 case (Rectangle):
mark_roche 0:2217986be753 50 res = 2 *(_a + _b);
mark_roche 0:2217986be753 51 break;
mark_roche 0:2217986be753 52 case (Circle):
mark_roche 0:2217986be753 53 res = 2 * 3.17 * _r ;
mark_roche 0:2217986be753 54 break;
mark_roche 0:2217986be753 55 default:
mark_roche 0:2217986be753 56 res = -1;
mark_roche 0:2217986be753 57 break;
mark_roche 0:2217986be753 58 }
mark_roche 0:2217986be753 59 return res;
mark_roche 0:2217986be753 60 }
mark_roche 0:2217986be753 61
mark_roche 0:2217986be753 62
mark_roche 0:2217986be753 63
mark_roche 0:2217986be753 64 void type() {
mark_roche 0:2217986be753 65 switch(_shap){
mark_roche 0:2217986be753 66 case (Square):
mark_roche 0:2217986be753 67 printf("The Shape is a Square \r\n");
mark_roche 0:2217986be753 68 break;
mark_roche 0:2217986be753 69 case (Rectangle):
mark_roche 0:2217986be753 70 printf("The Shape is a Rectangle \r\n");
mark_roche 0:2217986be753 71 break;
mark_roche 0:2217986be753 72 case (Circle):
mark_roche 0:2217986be753 73 printf("The Shape is a Circle \r\n");
mark_roche 0:2217986be753 74 break;
mark_roche 0:2217986be753 75 default:
mark_roche 0:2217986be753 76 printf("The Shape is Invalid \r\n");
mark_roche 0:2217986be753 77 break;
mark_roche 0:2217986be753 78 }
mark_roche 0:2217986be753 79 }
mark_roche 0:2217986be753 80
mark_roche 0:2217986be753 81 private:
mark_roche 0:2217986be753 82 int _a;
mark_roche 0:2217986be753 83 int _b;
mark_roche 0:2217986be753 84 int _r;
mark_roche 0:2217986be753 85 int _shap;
mark_roche 0:2217986be753 86
mark_roche 0:2217986be753 87 int typeis(){
mark_roche 0:2217986be753 88 if(_r != 0)
mark_roche 0:2217986be753 89 return (Circle);
mark_roche 0:2217986be753 90 else
mark_roche 0:2217986be753 91 if ((_a>0.0) && (_b>0.0)){
mark_roche 0:2217986be753 92 if(_a == _b)
mark_roche 0:2217986be753 93 return (Square);
mark_roche 0:2217986be753 94 else
mark_roche 0:2217986be753 95 return (Rectangle);
mark_roche 0:2217986be753 96 }else
mark_roche 0:2217986be753 97 return (Invalid);
mark_roche 0:2217986be753 98 }
mark_roche 0:2217986be753 99 };
mark_roche 0:2217986be753 100
mark_roche 0:2217986be753 101 int main() {
mark_roche 0:2217986be753 102 printf("---------- Square 3X3 ---------- \r\n");
mark_roche 0:2217986be753 103 Shape sqr(-3, 3);
mark_roche 0:2217986be753 104 sqr.type();
mark_roche 0:2217986be753 105 float a1 = sqr.area();
mark_roche 0:2217986be753 106 printf("The area is %f\r\n", a1);
mark_roche 0:2217986be753 107 float p1 = sqr.perimeter();
mark_roche 0:2217986be753 108 printf("The perimeter is %f\r\n", p1);
mark_roche 0:2217986be753 109
mark_roche 0:2217986be753 110 printf("\r\n---------- Rectangle 5X7 ---------- \r\n");
mark_roche 0:2217986be753 111 Shape rec(5, 0);
mark_roche 0:2217986be753 112 rec.type();
mark_roche 0:2217986be753 113 float a2 = rec.area();
mark_roche 0:2217986be753 114 printf("The area is %f\r\n", a2);
mark_roche 0:2217986be753 115 float p2 = rec.perimeter();
mark_roche 0:2217986be753 116 printf("The perimeter is %f\r\n", p2);
mark_roche 0:2217986be753 117
mark_roche 0:2217986be753 118 printf("\r\n---------- Circle 6 ---------- \r\n");
mark_roche 0:2217986be753 119 Shape cir(6);
mark_roche 0:2217986be753 120 cir.type();
mark_roche 0:2217986be753 121 float a3 = cir.area();
mark_roche 0:2217986be753 122 printf("The area is %f\r\n", a3);
mark_roche 0:2217986be753 123 float p3 = cir.perimeter();
mark_roche 0:2217986be753 124 printf("The perimeter is %f\r\n", p3);
mark_roche 0:2217986be753 125
mark_roche 0:2217986be753 126 {
mark_roche 0:2217986be753 127 }
mark_roche 0:2217986be753 128 }