Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Zadaci
- // 1. zadatak
- class Contact {
- private:
- string mName;
- string mEmail;
- int mNumber;
- public:
- Contact();
- Contact(string, string, int);
- ~Contact();
- friend ostream& operator<< (ostream&, const Contact&);
- };
- Contact::Contact() : mName(""), mEmail(""), mNumber(0) {}
- Contact::Contact(string name, string email, int number) : mName(name), mEmail(email), mNumber(number) {}
- Contact::~Contact() {}
- ostream& operator<< (ostream& OutputStream, const Contact& rhs)
- {
- OutputStream << rhs.mName << "\t" << rhs.mEmail << "\t" << rhs.mNumber;
- return OutputStream;
- }
- int main() {
- Contact N[3]={{ "Enio Dordan", "edordan@etfos.hr", 992051444 }, { "Petar Biocic", "pbiocic@etfos.hr", 992317847 }, {"Matej Petric", "mpetric@etfos.hr", 993422373}};
- int i;
- string name;
- getline(cin, name);
- fstream outputFile(name, ios::out);
- for (i = 0; i < 3; i++) {
- outputFile << N[i] << endl;
- cout << N[i] << endl;
- }
- outputFile.close();
- return 0;
- }
- // 2. zadatak
- class Vector {
- private:
- int mI;
- int mJ;
- int mK;
- public:
- Vector();
- Vector(int, int, int);
- ~Vector();
- Vector& operator= (const Vector&);
- friend Vector operator+ (const Vector& lhs, const Vector& rhs);
- friend bool operator== (const Vector&, const Vector&);
- friend ostream& operator<< (ostream&, const Vector&);
- };
- Vector::Vector() : mI(0), mJ(0), mK(0) {}
- Vector::Vector(int i, int j, int k) : mI(i), mJ(j), mK(k) {}
- Vector::~Vector() {}
- Vector& Vector::operator=(const Vector& Ref)
- {
- if (&Ref == this) return *this;
- this->mI = Ref.mI;
- this->mJ = Ref.mJ;
- this->mK = Ref.mK;
- return *this;
- }
- Vector operator+ (const Vector& lhs, const Vector& rhs)
- {
- return Vector(lhs.mI + rhs.mI, lhs.mJ + rhs.mJ, lhs.mK + rhs.mK);
- }
- bool operator== (const Vector& lhs, const Vector& rhs)
- {
- return lhs.mI == rhs.mI && lhs.mJ == rhs.mJ && lhs.mK == rhs.mK;
- }
- ostream& operator<< (ostream& OutputStream, const Vector& rhs)
- {
- OutputStream << rhs.mI << "i";
- if (rhs.mJ >= 0)
- {
- OutputStream << "+";
- }
- OutputStream << rhs.mJ << "j";
- if (rhs.mK >= 0)
- {
- OutputStream << "+";
- }
- OutputStream << rhs.mK << "k";
- return OutputStream;
- }
- int main() {
- Vector *A, *B;
- A = new Vector (4, 5, 3);
- B = new Vector (4, 5, 3);
- Vector C = *A;
- Vector D = *B;
- cout << (C + D) << endl;
- if (C == D) {
- cout << "Vectors are same!" << endl;
- }
- return 0;
- }
- // 3. zadatak
- class Vector {
- private:
- int mI;
- int mJ;
- int mK;
- public:
- Vector();
- Vector(int, int, int);
- ~Vector();
- Vector& operator= (const Vector&);
- friend bool operator< (const Vector&, const Vector&);
- };
- Vector::Vector() : mI(0), mJ(0), mK(0) {}
- Vector::Vector(int i, int j, int k) : mI(i), mJ(j), mK(k) {}
- Vector::~Vector() {}
- Vector& Vector::operator=(const Vector& Ref)
- {
- if (&Ref == this) return *this;
- this->mI = Ref.mI;
- this->mJ = Ref.mJ;
- this->mK = Ref.mK;
- return *this;
- }
- bool operator< (const Vector& lhs, const Vector& rhs)
- {
- return lhs.mI < rhs.mI && lhs.mJ < rhs.mJ && lhs.mK < rhs.mK;
- }
- int indexofSmallestElement(int array[], int size) {
- int index = 0;
- int n = array[0];
- for (int i = 1; i < size; ++i)
- {
- if (array[i] < n)
- {
- n = array[i];
- index = i;
- }
- }
- return index;
- }
- int indexofSmallestElement(double array[], int size) {
- int index = 0;
- double n = array[0];
- for (int i = 1; i < size; ++i)
- {
- if (array[i] < n)
- {
- n = array[i];
- index = i;
- }
- }
- return index;
- }
- int indexofSmallestElement(Vector array[], int size) {
- int index = 0;
- Vector n = array[0];
- for (int i = 1; i < size; ++i)
- {
- if (array[i] < n)
- {
- n = array[i];
- index = i;
- }
- }
- return index;
- }
- int indexofSmallestElement(string array[], int size) {
- int index = 0;
- string n = array[0];
- for (int i = 1; i < size; ++i)
- {
- if (array[i].size() < n.size())
- {
- n = array[i];
- index = i;
- }
- }
- return index;
- }
- int main() {
- int array1[2] = { 2, 3 };
- double array2[2] = { 5.2, 2.4 };
- Vector array3[2] = { { 2, 4, 5 }, { 6, 7, 8 } };
- string array4[2] = { "enio", "dordan" };
- cout << indexofSmallestElement(array1, (sizeof(array1)/sizeof(array1[0]))) << endl;
- cout << indexofSmallestElement(array2, (sizeof(array2) / sizeof(array2[0]))) << endl;
- cout << indexofSmallestElement(array3, (sizeof(array3) / sizeof(array3[0]))) << endl;
- cout << indexofSmallestElement(array4, (sizeof(array4) / sizeof(array4[0]))) << endl;
- return 0;
- }
- // Analiza
- //1. Napišite program koji ispisuje vlastiti izvorni kod (quine).
- #include<cstdio>
- int main(){char n[]=R"(#include<cstdio>
- int main(){char n[]=R"(%s%c";printf(n,n,41);})";printf(n,n,41);}
- //2. Napišite funkciju povrsina() koja računa površinu kruga, a zatim tu funkciju
- //preopteretite da računa površinu pravokutnika i trokuta(razlike su u broju
- //parametara).U glavnoj funkciji pozovite funkciju površina za svaki od likova.
- #include <iostream>
- #define PI 3.14159
- using namespace std;
- float Povrsina(float r) { return PI * (r * r); }
- float Povrsina(float a, float b) { return a * b; }
- float Povrsina(float a, float b, float c) { return (a * b) / 2; }
- int main() {
- cout << "Povrsina kruga polumjera " << 4.5 << "cm iznosi: " << Povrsina(4.5) << "cm" << endl;
- cout << "Povrsina pravokutnika stranica a: " << 3.2 << "cm i b: " << 2.3 << "cm iznosi: " << Povrsina(3.2, 2.3) << "cm" << endl;
- 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;
- return 0;
- }
- //3. Napišite klasu polinom2 koja predstavlja polinom drugog stupnja.Uz
- //podatkovne članove i konstruktore potrebno je preopteretiti po jedan
- //operator za U / I, aritmetičku operaciju i usporedbu.
- #include <iostream>
- using namespace std;
- class Polinom2 {
- friend ostream& operator<< (ostream&, const Polinom2&);
- friend Polinom2 operator+ (const Polinom2& lhs, const Polinom2& rhs);
- friend bool operator< (const Polinom2&, const Polinom2&);
- private:
- float mA;
- float mB;
- float mC;
- public:
- Polinom2();
- Polinom2(float, float, float);
- ~Polinom2();
- };
- Polinom2::Polinom2() : mA(0), mB(0), mC(0) {}
- Polinom2::Polinom2(float a, float b, float c) : mA(a), mB(b), mC(c) {}
- Polinom2::~Polinom2() {}
- ostream& operator<< (ostream& OutputStream, const Polinom2& rhs) {
- OutputStream << rhs.mA << "x^2 ";
- if (rhs.mB >= 0)
- {
- OutputStream << "+ ";
- }
- OutputStream << rhs.mB << "x ";
- if (rhs.mC >= 0)
- {
- OutputStream << "+ ";
- }
- OutputStream << rhs.mC;
- return OutputStream;
- }
- Polinom2 operator+ (const Polinom2& lhs, const Polinom2& rhs) {
- return Polinom2(lhs.mA + rhs.mA, lhs.mB + rhs.mB, lhs.mC + rhs.mC);
- }
- bool operator< (const Polinom2& lhs, const Polinom2& rhs)
- {
- return lhs.mA < rhs.mA && lhs.mB < rhs.mB && lhs.mB < rhs.mB;
- }
- int main() {
- Polinom2 A(4, 3, 2);
- Polinom2 B(2, 7, 1);
- cout << A << endl;
- cout << B << endl;
- cout << A + B << endl;
- if (A < B) {
- cout << A << " < " << B << endl;
- }
- else {
- cout << B << " < " << A << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement