Advertisement
Adytzu04

lab7p1

Dec 17th, 2012
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. //h
  2. #ifndef                 POLI_H
  3. #define                 POLI_H
  4. #pragma warning(disable : 4996)
  5.  
  6. #include <iostream>
  7. #include <math.h>
  8.  
  9. using namespace std;
  10.  
  11. class Figura
  12. {
  13.     public:
  14.         virtual ~Figura(){}
  15.         virtual float arie()=0;
  16.         virtual void afisare()=0;
  17. };
  18. class Dreptunghi : public Figura
  19. {
  20.     private:
  21.         int x1,y1,x2,y2;
  22.     public:
  23.         Dreptunghi(int x1,int y1,int x2,int y2);
  24.         float arie();
  25.         void afisare();
  26. };
  27. class Cerc : public Figura
  28. {
  29.     private:
  30.         int x,y,r;
  31.     public:
  32.         Cerc(int x, int y, int r);
  33.         float arie();
  34.         void afisare();
  35. };
  36.  
  37. //Figura *figCuArieMax(Figura **figuri, int n);
  38.  
  39. class Triunghi : public Figura
  40. {
  41.     private:
  42.         int x1,y1,x2,y2,x3,y3;
  43.     public:
  44.         Triunghi(int x1,int y1,int x2,int y2,int x3,int y3);
  45.         float arie();
  46.         void afisare();
  47. };
  48.  
  49. #endif
  50.  
  51. //main
  52.  
  53. #include"poli.h"
  54.  
  55. /*
  56. int main(int)
  57. {
  58.     const int n = 3;
  59.     Figura *figuri[n];
  60.     figuri[0] = new Dreptunghi(0,0,2,5);
  61.     figuri[1] = new Dreptunghi(0,0,2,2);
  62.     figuri[2] = new Cerc(0,0,3);
  63.     figuri[2] = new Triunghi(1,4,3);
  64.     Figura *figMax = figCuArieMax(figuri, n);
  65.  
  66.         cout << " Dintre figurile:"<<endl;
  67.  
  68.     for(int i=0; i<3; i++)
  69.     {
  70.         cout << i << ". ";
  71.         figuri[i]->afisare();
  72.     }
  73.         cout << endl<<" aria maxima o are:"<<endl;
  74.  
  75.     figMax->afisare();
  76.     for(int i=0; i<n; i++)
  77.     {
  78.         delete figuri[i];
  79.     }
  80.    
  81.     return 0;
  82. }*/
  83.  
  84. int main()
  85. {
  86.     int f;
  87.     Figura *dr = new Dreptunghi(1,2,4,4);
  88.     Figura *cerc = new Cerc(1,1,3);
  89.     Figura *tr = new Triunghi(0,2,7,0,-2,0);
  90.     //urmatoarea linie ar genera eroare:
  91.     //Figura *fig = new Figura();
  92.     dr->afisare();
  93.     cerc->afisare();
  94.     tr->afisare();
  95.     delete dr;
  96.     delete cerc;
  97.     delete tr;
  98.  
  99.     cin>>f;
  100.    
  101.     return 0;
  102. }
  103.  
  104. //dreptunghi.cpp
  105.  
  106. #include"poli.h"
  107.  
  108. Dreptunghi::Dreptunghi(int x1, int y1, int x2, int y2)
  109. {
  110.     this->x1 = x1;
  111.     this->y1 = y1;
  112.     this->x2 = x2;
  113.     this->y2 = y2;
  114. }
  115.  
  116. float Dreptunghi::arie()
  117. {
  118.     return (float)(x2 - x1)*(y2 - y1);
  119. }
  120. void Dreptunghi::afisare()
  121. {
  122.     cout << "Dreptunghi cu coordonatele (" <<x1<<","<<y1<<")-("<<x2<<","<<y2<<"), si aria " << arie()<<endl;
  123. }
  124.  
  125. //cerc.cpp
  126.  
  127. #include "poli.h"
  128.  
  129. Cerc::Cerc(int x, int y, int r)
  130. {
  131.     this->x = x;
  132.     this->y = y;
  133.     this->r = r;
  134. }
  135. float Cerc::arie()
  136. {
  137.     const float PI = 3.14F;
  138.     return PI * r * r;
  139. }
  140. void Cerc::afisare()
  141. {
  142. cout << "Cerc cu coordonatele (" <<x<<","<<y <<"), raza " << r<<" si aria " << arie()<<endl;
  143. }
  144.  
  145. //triunghi.cxpp
  146.  
  147. #include "poli.h"
  148.  
  149. Triunghi::Triunghi(int x1,int y1,int x2,int y2,int x3,int y3)
  150. {
  151.     this->x1 = x1;
  152.     this->y1 = y1;
  153.     this->x2 = x2;
  154.     this->y2 = y2;
  155.     this->x3 = x3;
  156.     this->y3 = y3;
  157. }
  158. void Triunghi::afisare()
  159. {
  160.     cout<<"triunghi cu coordonatele (" <<x1<<","<<y1<<")-("<<x2<<","<<y2<<")-("<<x3<<","<<y3<<"), si aria " << arie()<<endl;
  161. }
  162. float Triunghi::arie()
  163. {
  164.     float p,s;
  165.     float a,b,c;
  166.     a= sqrt((float)(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
  167.     b= sqrt((float)(x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
  168.     c= sqrt((float)(x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
  169.     p= (a+b+c)/2;
  170.     s = sqrt(p*(p-a)*(p-b)*(p-c));
  171.     return s;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement