Advertisement
100rads

kompleksni brojevi

Apr 5th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Complex {
  7.     int re;
  8.     int im;
  9. public:
  10.     void setRe(int a) {
  11.         re = a;
  12.     }
  13.  
  14.     void setIm(int a) {
  15.         im = a;
  16.     }
  17.  
  18.     int getRe() {
  19.         return re;
  20.     }
  21.  
  22.     int getIm() {
  23.         return im;
  24.     }
  25.  
  26.     void printNum() {
  27.         char znak;
  28.         if (re == 0) {
  29.             cout << im << "i" << endl;
  30.         }
  31.         else if (re == 0 && im == 0) {
  32.             cout << "0";
  33.         }
  34.         else if (im <= 0) {
  35.             znak = '-';
  36.             cout << re << " " << znak << " " << abs(im) << "i" << endl;
  37.         }
  38.         else {
  39.             znak = '+';
  40.             cout << re << " " << znak << " " << im << "i" << endl;
  41.         }
  42.     }
  43.  
  44.     Complex zbroj(Complex b) {
  45.         int aReal = getRe();
  46.         int bReal = b.getRe();
  47.         int aIm = getIm();
  48.         int bIm = b.getIm();
  49.         Complex novi;
  50.         novi.setRe(aReal + bReal);
  51.         novi.setIm(aIm + bIm);
  52.  
  53.         return novi;
  54.     }
  55.  
  56.     Complex oduzimanje(Complex b) {
  57.         int aReal = getRe();
  58.         int bReal = b.getRe();
  59.         int aIm = getIm();
  60.         int bIm = b.getIm();
  61.         Complex novi;
  62.         novi.setRe(aReal - bReal);
  63.         novi.setIm(aIm - bIm);
  64.  
  65.         return novi;
  66.     }
  67.  
  68.     Complex mnozenje(Complex b) {
  69.         int aReal = getRe();
  70.         int bReal = b.getRe();
  71.         int aIm = getIm();
  72.         int bIm = b.getIm();
  73.         Complex novi;
  74.         novi.setRe(aReal * bReal - aIm * bIm);
  75.         novi.setIm(aReal * bIm + bReal * aIm);
  76.  
  77.         return novi;
  78.     }
  79.  
  80. }br1, br2, zbroj, oduzimanje, umnozak;
  81.  
  82. int main()
  83. {
  84.     int unos;
  85.     cout << "Unesite prvi kompleksni broj: " << endl;
  86.     cout << "Realna vrijednost: ";
  87.     cin >> unos;
  88.     br1.setRe(unos);
  89.     cout << "Imaginarna vrijednost: ";
  90.     cin >> unos;
  91.     br1.setIm(unos);
  92.  
  93.     cout << endl;
  94.  
  95.     cout << "Unesite drugi kompleksni broj: " << endl;
  96.     cout << "Realna vrijednost: ";
  97.     cin >> unos;
  98.     br2.setRe(unos);
  99.     cout << "Imaginarna vrijednost: ";
  100.     cin >> unos;
  101.     br2.setIm(unos);
  102.  
  103.     cout << endl;
  104.  
  105.     zbroj = br1.zbroj(br2);
  106.     cout << "Zbroj: ";
  107.     zbroj.printNum();
  108.  
  109.     oduzimanje = br1.oduzimanje(br2);
  110.     cout << "Razlika: ";
  111.     oduzimanje.printNum();
  112.  
  113.     umnozak = br1.mnozenje(br2);
  114.     cout << "Umnozak: ";
  115.     umnozak.printNum();
  116.        
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement