Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- class Complex {
- int re;
- int im;
- public:
- void setRe(int a) {
- re = a;
- }
- void setIm(int a) {
- im = a;
- }
- int getRe() {
- return re;
- }
- int getIm() {
- return im;
- }
- void printNum() {
- char znak;
- if (re == 0) {
- cout << im << "i" << endl;
- }
- else if (re == 0 && im == 0) {
- cout << "0";
- }
- else if (im <= 0) {
- znak = '-';
- cout << re << " " << znak << " " << abs(im) << "i" << endl;
- }
- else {
- znak = '+';
- cout << re << " " << znak << " " << im << "i" << endl;
- }
- }
- Complex zbroj(Complex b) {
- int aReal = getRe();
- int bReal = b.getRe();
- int aIm = getIm();
- int bIm = b.getIm();
- Complex novi;
- novi.setRe(aReal + bReal);
- novi.setIm(aIm + bIm);
- return novi;
- }
- Complex oduzimanje(Complex b) {
- int aReal = getRe();
- int bReal = b.getRe();
- int aIm = getIm();
- int bIm = b.getIm();
- Complex novi;
- novi.setRe(aReal - bReal);
- novi.setIm(aIm - bIm);
- return novi;
- }
- Complex mnozenje(Complex b) {
- int aReal = getRe();
- int bReal = b.getRe();
- int aIm = getIm();
- int bIm = b.getIm();
- Complex novi;
- novi.setRe(aReal * bReal - aIm * bIm);
- novi.setIm(aReal * bIm + bReal * aIm);
- return novi;
- }
- }br1, br2, zbroj, oduzimanje, umnozak;
- int main()
- {
- int unos;
- cout << "Unesite prvi kompleksni broj: " << endl;
- cout << "Realna vrijednost: ";
- cin >> unos;
- br1.setRe(unos);
- cout << "Imaginarna vrijednost: ";
- cin >> unos;
- br1.setIm(unos);
- cout << endl;
- cout << "Unesite drugi kompleksni broj: " << endl;
- cout << "Realna vrijednost: ";
- cin >> unos;
- br2.setRe(unos);
- cout << "Imaginarna vrijednost: ";
- cin >> unos;
- br2.setIm(unos);
- cout << endl;
- zbroj = br1.zbroj(br2);
- cout << "Zbroj: ";
- zbroj.printNum();
- oduzimanje = br1.oduzimanje(br2);
- cout << "Razlika: ";
- oduzimanje.printNum();
- umnozak = br1.mnozenje(br2);
- cout << "Umnozak: ";
- umnozak.printNum();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement