Advertisement
sdee3

OBP1 - Vezba2

Mar 24th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. //Tacka.h
  2.  
  3. #pragma once
  4.  
  5. class Tacka {
  6. private:
  7.     int *x;
  8.     int *y;
  9. public:
  10.     Tacka();
  11.  
  12.     Tacka(int a, int b);
  13.  
  14.     void setX(int a);
  15.     void setY(int b);
  16.  
  17.     int getX();
  18.     int getY();
  19.  
  20.     void ispisiTacku();
  21.  
  22.     bool uporedi(Tacka);
  23. };
  24.  
  25. //Tacka.cpp
  26.  
  27. #include "Tacka.h"
  28. #include<iostream>
  29.  
  30. using namespace std;
  31.  
  32. Tacka::Tacka() {
  33.     x = new int(0);
  34.     y = new int(0);
  35. }
  36.  
  37. Tacka::Tacka(int a, int b) {
  38.     x = new int(a);
  39.     y = new int(b);
  40. }
  41.  
  42. void Tacka::setX(int a) {
  43.     x = new int(a);
  44. }
  45.  
  46. void Tacka::setY(int b) {
  47.     y = new int(b);
  48. }
  49.  
  50. int Tacka::getX() {
  51.     return *x;
  52. }
  53.  
  54. int Tacka::getY() {
  55.     return *y;
  56. }
  57.  
  58. void Tacka::ispisiTacku() {
  59.     cout<<"\n("<<*x<<", "<<*y<<")"<<endl;
  60. }
  61.  
  62. bool Tacka::uporedi(Tacka tacka) {
  63.     return ((*x == *tacka.x) && (*y == *tacka.y));
  64. }
  65.  
  66. //Trougao.h
  67.  
  68. #include "Tacka.h"
  69.  
  70. class Trougao {
  71.  
  72. private:
  73.     Tacka a;
  74.     Tacka b;
  75.     Tacka c;
  76.     double povrsina;
  77. public:
  78.     Trougao();
  79.     Trougao(Tacka, Tacka, Tacka);
  80.     Trougao(int, int, int, int, int, int);
  81.     void povrsinaT();
  82.     void ispisiT();
  83.  
  84. };
  85.  
  86. //Trougao.cpp
  87.  
  88. #include "Trougao.h"
  89. #include<iostream>
  90. #include<cmath>
  91.  
  92. using namespace std;
  93.  
  94. Trougao::Trougao() {
  95.     a = Tacka(0, 0);
  96.     b = Tacka(0, 5);
  97.     c = Tacka(5, 0);
  98. }
  99.  
  100. Trougao::Trougao(Tacka tacka, Tacka tacka1, Tacka tacka2) {
  101.     a = tacka;
  102.     b = tacka1;
  103.     c = tacka2;
  104. }
  105.  
  106. Trougao::Trougao(int x1, int y1, int x2, int y2, int x3, int y3) {
  107.     a = Tacka(x1, y1);
  108.     b = Tacka(x2, y2);
  109.     c = Tacka(x3, y3);
  110. }
  111.  
  112. void Trougao::povrsinaT() {
  113.     double AB, BC, AC, s;
  114.     AB = sqrt(pow(a.getX() - b.getX(), 2) + pow(a.getY() - b.getY(), 2));
  115.     BC = sqrt(pow(b.getX() - c.getX(), 2) + pow(b.getY() - c.getY(), 2));
  116.     AC = sqrt(pow(a.getX() - c.getX(), 2) + pow(a.getY() - c.getY(), 2));
  117.  
  118.     s = (AB + BC + AC) / 2;
  119.  
  120.     povrsina = sqrt(s * (s - AB) * (s - BC) * (s - AC));
  121. }
  122.  
  123. void Trougao::ispisiT() {
  124.     cout << "\n{A(" << a.getX() << ", " << a.getY()
  125.     << "), B(" << b.getX() << ", " << b.getY()
  126.     << "), C(" << c.getX() << ", " << c.getY()
  127.     << ") : Povrsina = " << povrsina << " }" << endl;
  128. }
  129.  
  130. //Pravougaonik.h
  131.  
  132. #include "Tacka.h"
  133.  
  134. class Pravougaonik {
  135.  
  136. private:
  137.     Tacka t1;
  138.     Tacka t2;
  139.     Tacka t3;
  140.     Tacka t4;
  141. public:
  142.     Pravougaonik(Tacka a, Tacka b, Tacka c, Tacka d);
  143.     Pravougaonik();
  144.     Pravougaonik(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);
  145.  
  146.     double Povrsina();
  147.     void ispisP();
  148.  
  149. };
  150.  
  151. //Pravougaonik.cpp
  152.  
  153. #include "Pravougaonik.h"
  154. #include <iostream>
  155. #include <cmath>
  156.  
  157. using namespace std;
  158.  
  159. Pravougaonik::Pravougaonik(Tacka a, Tacka b, Tacka c, Tacka d){
  160.     t1 = a;
  161.     t2 = b;
  162.     t3 = c;
  163.     t4 = d;
  164. }
  165.  
  166. Pravougaonik::Pravougaonik() {
  167.     t1.setX(0); t1.setY(0);
  168.     t2.setX(0); t2.setY(6);
  169.     t3.setX(5); t3.setY(6);
  170.     t4.setX(5); t4.setY(0);
  171. }
  172.  
  173. Pravougaonik::Pravougaonik(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
  174.     t1.setX(x1); t1.setY(y1);
  175.     t2.setX(x2); t2.setY(y2);
  176.     t3.setX(x3); t3.setY(y3);
  177.     t4.setX(x4); t4.setY(y4);
  178. }
  179.  
  180. double Pravougaonik::Povrsina() {
  181.     double T12, T14, T23, T24, T34, s1, s2, p1, p2;
  182.  
  183.     T12 = sqrt(pow(t1.getX() - t2.getX(),2) + pow(t1.getY() - t2.getY(),2));
  184.     T14 = sqrt(pow(t1.getX() - t4.getX(),2) + pow(t1.getY() - t4.getY(),2));
  185.     T23 = sqrt(pow(t2.getX() - t3.getX(),2) + pow(t2.getY() - t3.getY(),2));
  186.     T24 = sqrt(pow(t2.getX() - t4.getX(),2) + pow(t2.getY() - t4.getY(),2));
  187.     T34 = sqrt(pow(t3.getX() - t4.getX(),2) + pow(t3.getY() - t4.getY(),2));
  188.  
  189.     s1 = (T12 + T14 + T24)/2;
  190.     s2 = (T23 + T34 + T24)/2;
  191.     p1 = sqrt(s2 * (s1 - T12) * (s1 - T14) * (s1 - T24));
  192.     p2 = sqrt(s2 * (s2 - T23) * (s2 - T34) * (s2 - T24));
  193.  
  194.     return p1+p2;
  195. }
  196.  
  197. void Pravougaonik::ispisP() {
  198.     cout<<"\nPovrsina je "<<this->Povrsina()<<endl;
  199. }
  200.  
  201. //Main.cpp
  202.  
  203. #include <iostream>
  204. #include "Tacka.h"
  205. #include "Trougao.h"
  206. #include "Pravougaonik.h"
  207.  
  208. using namespace std;
  209.  
  210. int main() {
  211.     Tacka t1;
  212.     Tacka t2 = Tacka(3,4);
  213.  
  214.     int x1;
  215.     int y1;
  216.  
  217.     cout<<"Tacke - pocetno stanje:"<<endl;
  218.     t1.ispisiTacku();
  219.     t2.ispisiTacku();
  220.  
  221.     t1.setX(5);
  222.     t2.setY(9);
  223.  
  224.     cout<<"Nakon izmene:"<<endl;
  225.     t1.ispisiTacku();
  226.     t2.ispisiTacku();
  227.  
  228.     x1 = t1.getX();
  229.     y1 = t1.getY();
  230.  
  231.     cout<<"\nx1: "<<x1<<endl<<"y1: "<<y1<<endl;
  232.  
  233.     if(t1.uporedi(t2) == true)
  234.         cout<<"Iste"<<endl;
  235.     else
  236.         cout<<"Razlicite"<<endl;
  237.  
  238.     //Drugi zadatak
  239.  
  240.     Tacka tacka1 = Tacka(4,2);
  241.     Tacka tacka2 = Tacka(3,4);
  242.     Tacka tacka3 = Tacka(2,2);
  243.  
  244.     Trougao trougao1 = Trougao(tacka1, tacka2, tacka3);
  245.     Trougao trougao2;
  246.  
  247.     trougao1.povrsinaT();
  248.     trougao2.povrsinaT();
  249.  
  250.     cout<<"Prvi trougao: "<<endl;
  251.     trougao1.ispisiT();
  252.     cout<<"Drugi trougao: "<<endl;
  253.     trougao2.ispisiT();
  254.  
  255.     //Treci zadatak
  256.  
  257.     Pravougaonik pravougaonik;
  258.  
  259.     pravougaonik.ispisP();
  260.  
  261.     return 0;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement