Advertisement
eniodordan

[OOP] LV2 - Zadaci + Analiza

Oct 29th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. // Zadaci
  2. // 1. zadatak
  3. class Contact {
  4. private:
  5.     string mName;
  6.     string mEmail;
  7.     int mNumber;
  8. public:
  9.     Contact();
  10.     Contact(string, string, int);
  11.     ~Contact();
  12.     friend ostream& operator<< (ostream&, const Contact&);
  13. };
  14.  
  15. Contact::Contact() : mName(""), mEmail(""), mNumber(0) {}
  16.  
  17. Contact::Contact(string name, string email, int number) : mName(name), mEmail(email), mNumber(number) {}
  18.  
  19. Contact::~Contact() {}
  20.  
  21. ostream& operator<< (ostream& OutputStream, const Contact& rhs)
  22. {
  23.     OutputStream << rhs.mName << "\t" << rhs.mEmail << "\t" << rhs.mNumber;
  24.  
  25.     return OutputStream;
  26. }
  27.  
  28. int main() {
  29.     Contact N[3]={{ "Enio Dordan", "edordan@etfos.hr", 992051444 }, { "Petar Biocic", "pbiocic@etfos.hr", 992317847 }, {"Matej Petric", "mpetric@etfos.hr", 993422373}};
  30.  
  31.     int i;
  32.     string name;
  33.  
  34.     getline(cin, name);
  35.     fstream outputFile(name, ios::out);
  36.  
  37.     for (i = 0; i < 3; i++) {
  38.         outputFile << N[i] << endl;
  39.         cout << N[i] << endl;
  40.     }
  41.  
  42.     outputFile.close();
  43.  
  44.     return 0;
  45. }
  46.  
  47. // 2. zadatak
  48. class Vector {
  49. private:
  50.     int mI;
  51.     int mJ;
  52.     int mK;
  53. public:
  54.     Vector();
  55.     Vector(int, int, int);
  56.     ~Vector();
  57.     Vector& operator= (const Vector&);
  58.     friend Vector operator+ (const Vector& lhs, const Vector& rhs);
  59.     friend bool operator== (const Vector&, const Vector&);
  60.     friend ostream& operator<< (ostream&, const Vector&);
  61. };
  62.  
  63. Vector::Vector() : mI(0), mJ(0), mK(0) {}
  64.  
  65. Vector::Vector(int i, int j, int k) : mI(i), mJ(j), mK(k) {}
  66.  
  67. Vector::~Vector() {}
  68.  
  69. Vector& Vector::operator=(const Vector& Ref)
  70. {
  71.     if (&Ref == this) return *this;
  72.     this->mI = Ref.mI;
  73.     this->mJ = Ref.mJ;
  74.     this->mK = Ref.mK;
  75.  
  76.     return *this;
  77. }
  78.  
  79. Vector operator+ (const Vector& lhs, const Vector& rhs)
  80. {
  81.     return Vector(lhs.mI + rhs.mI, lhs.mJ + rhs.mJ, lhs.mK + rhs.mK);
  82. }
  83.  
  84. bool operator== (const Vector& lhs, const Vector& rhs)
  85. {
  86.     return lhs.mI == rhs.mI && lhs.mJ == rhs.mJ && lhs.mK == rhs.mK;
  87. }
  88.  
  89. ostream& operator<< (ostream& OutputStream, const Vector& rhs)
  90. {
  91.     OutputStream << rhs.mI << "i";
  92.     if (rhs.mJ >= 0)
  93.     {
  94.         OutputStream << "+";
  95.     }
  96.     OutputStream << rhs.mJ << "j";
  97.     if (rhs.mK >= 0)
  98.     {
  99.         OutputStream << "+";
  100.     }
  101.     OutputStream << rhs.mK << "k";
  102.  
  103.     return OutputStream;
  104. }
  105.  
  106.  
  107. int main() {
  108.     Vector *A, *B;
  109.     A = new Vector (4, 5, 3);
  110.     B = new Vector (4, 5, 3);
  111.     Vector C = *A;
  112.     Vector D = *B;
  113.  
  114.     cout << (C + D) << endl;
  115.  
  116.     if (C == D) {
  117.         cout << "Vectors are same!" << endl;
  118.     }
  119.  
  120.     return 0;
  121. }
  122.  
  123. // 3. zadatak
  124. class Vector {
  125. private:
  126.     int mI;
  127.     int mJ;
  128.     int mK;
  129. public:
  130.     Vector();
  131.     Vector(int, int, int);
  132.     ~Vector();
  133.     Vector& operator= (const Vector&);
  134.     friend bool operator< (const Vector&, const Vector&);
  135. };
  136.  
  137. Vector::Vector() : mI(0), mJ(0), mK(0) {}
  138.  
  139. Vector::Vector(int i, int j, int k) : mI(i), mJ(j), mK(k) {}
  140.  
  141. Vector::~Vector() {}
  142.  
  143. Vector& Vector::operator=(const Vector& Ref)
  144. {
  145.     if (&Ref == this) return *this;
  146.     this->mI = Ref.mI;
  147.     this->mJ = Ref.mJ;
  148.     this->mK = Ref.mK;
  149.  
  150.     return *this;
  151. }
  152.  
  153. bool operator< (const Vector& lhs, const Vector& rhs)
  154. {
  155.     return lhs.mI < rhs.mI && lhs.mJ < rhs.mJ && lhs.mK < rhs.mK;
  156. }
  157.  
  158. int indexofSmallestElement(int array[], int size) {
  159.     int index = 0;
  160.     int n = array[0];
  161.  
  162.     for (int i = 1; i < size; ++i)
  163.     {
  164.         if (array[i] < n)
  165.         {
  166.             n = array[i];
  167.             index = i;
  168.         }
  169.     }
  170.  
  171.     return index;
  172. }
  173.  
  174. int indexofSmallestElement(double array[], int size) {
  175.     int index = 0;
  176.     double n = array[0];
  177.  
  178.     for (int i = 1; i < size; ++i)
  179.     {
  180.         if (array[i] < n)
  181.         {
  182.             n = array[i];
  183.             index = i;
  184.         }
  185.     }
  186.  
  187.     return index;
  188. }
  189.  
  190. int indexofSmallestElement(Vector array[], int size) {
  191.     int index = 0;
  192.     Vector n = array[0];
  193.  
  194.     for (int i = 1; i < size; ++i)
  195.     {
  196.         if (array[i] < n)
  197.         {
  198.             n = array[i];
  199.             index = i;
  200.         }
  201.     }
  202.  
  203.     return index;
  204. }
  205.  
  206. int indexofSmallestElement(string array[], int size) {
  207.     int index = 0;
  208.     string n = array[0];
  209.  
  210.     for (int i = 1; i < size; ++i)
  211.     {
  212.         if (array[i].size() < n.size())
  213.         {
  214.             n = array[i];
  215.             index = i;
  216.         }
  217.     }
  218.  
  219.     return index;
  220. }
  221.  
  222. int main() {
  223.     int array1[2] = { 2, 3 };
  224.     double array2[2] = { 5.2, 2.4 };
  225.     Vector array3[2] = { { 2, 4, 5 }, { 6, 7, 8 } };
  226.     string array4[2] = { "enio", "dordan" };
  227.     cout << indexofSmallestElement(array1, (sizeof(array1)/sizeof(array1[0]))) << endl;
  228.     cout << indexofSmallestElement(array2, (sizeof(array2) / sizeof(array2[0]))) << endl;
  229.     cout << indexofSmallestElement(array3, (sizeof(array3) / sizeof(array3[0]))) << endl;
  230.     cout << indexofSmallestElement(array4, (sizeof(array4) / sizeof(array4[0]))) << endl;
  231.  
  232.     return 0;
  233. }
  234.  
  235. // Analiza
  236. //1. Napišite program koji ispisuje vlastiti izvorni kod (quine).
  237.  
  238. #include<cstdio>
  239. int main(){char n[]=R"(#include<cstdio>
  240. int main(){char n[]=R"(%s%c";printf(n,n,41);})";printf(n,n,41);}
  241.  
  242. //2. Napišite funkciju povrsina() koja računa površinu kruga, a zatim tu funkciju
  243. //preopteretite da računa površinu pravokutnika i trokuta(razlike su u broju
  244. //parametara).U glavnoj funkciji pozovite funkciju površina za svaki od likova.
  245.  
  246. #include <iostream>
  247.  
  248. #define PI 3.14159
  249.  
  250. using namespace std;
  251.  
  252. float Povrsina(float r) { return PI * (r * r); }
  253.  
  254. float Povrsina(float a, float b) { return a * b; }
  255.  
  256. float Povrsina(float a, float b, float c) { return (a * b) / 2; }
  257.  
  258. int main() {
  259.     cout << "Povrsina kruga polumjera " << 4.5 << "cm iznosi: " << Povrsina(4.5) << "cm" << endl;
  260.     cout << "Povrsina pravokutnika stranica a: " << 3.2 << "cm i b: " << 2.3 << "cm iznosi: " << Povrsina(3.2, 2.3) << "cm" << endl;
  261.     cout << "Povrsina trokuta stranica a: " << 5.4 << "cm i b: " << 4.3 << "cm i c: " << 2.4 << "cm iznosi: " << Povrsina(5.4, 4.3, 2.4) << "cm" << endl;
  262.  
  263.     return 0;
  264. }
  265.  
  266. //3. Napišite klasu polinom2 koja predstavlja polinom drugog stupnja.Uz
  267. //podatkovne članove i konstruktore potrebno je preopteretiti po jedan
  268. //operator za U / I, aritmetičku operaciju i usporedbu.
  269.  
  270. #include <iostream>
  271.  
  272. using namespace std;
  273.  
  274. class Polinom2 {
  275.     friend ostream& operator<< (ostream&, const Polinom2&);
  276.     friend Polinom2 operator+ (const Polinom2& lhs, const Polinom2& rhs);
  277.     friend bool operator< (const Polinom2&, const Polinom2&);
  278. private:
  279.     float mA;
  280.     float mB;
  281.     float mC;
  282. public:
  283.     Polinom2();
  284.     Polinom2(float, float, float);
  285.     ~Polinom2();
  286. };
  287.  
  288. Polinom2::Polinom2() : mA(0), mB(0), mC(0) {}
  289.  
  290. Polinom2::Polinom2(float a, float b, float c) : mA(a), mB(b), mC(c) {}
  291.  
  292. Polinom2::~Polinom2() {}
  293.  
  294. ostream& operator<< (ostream& OutputStream, const Polinom2& rhs) {
  295.     OutputStream << rhs.mA << "x^2 ";
  296.     if (rhs.mB >= 0)
  297.     {
  298.         OutputStream << "+ ";
  299.     }
  300.     OutputStream << rhs.mB << "x ";
  301.     if (rhs.mC >= 0)
  302.     {
  303.         OutputStream << "+ ";
  304.     }
  305.     OutputStream << rhs.mC;
  306.  
  307.     return OutputStream;
  308. }
  309.  
  310. Polinom2 operator+ (const Polinom2& lhs, const Polinom2& rhs) {
  311.     return Polinom2(lhs.mA + rhs.mA, lhs.mB + rhs.mB, lhs.mC + rhs.mC);
  312. }
  313.  
  314. bool operator< (const Polinom2& lhs, const Polinom2& rhs)
  315. {
  316.     return lhs.mA < rhs.mA && lhs.mB < rhs.mB && lhs.mB < rhs.mB;
  317. }
  318.  
  319. int main() {
  320.     Polinom2 A(4, 3, 2);
  321.     Polinom2 B(2, 7, 1);
  322.  
  323.     cout << A << endl;
  324.     cout << B << endl;
  325.  
  326.     cout << A + B << endl;
  327.  
  328.     if (A < B) {
  329.         cout << A << " < " << B << endl;
  330.     }
  331.     else {
  332.         cout << B << " < " << A << endl;
  333.     }
  334.  
  335.     return 0;
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement