Lab 4 Mark_Roche

Dependencies:   mbed

Committer:
mark_roche
Date:
Wed May 11 17:29:08 2022 +0000
Revision:
1:5a6e04b6795b
Parent:
0:2217986be753
Lab 4 Mark Roche

Who changed what in which revision?

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