Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Z {
  5. private:
  6.     double a;
  7.     double b;
  8. public:
  9.     Z();
  10.  
  11.     Z(std::string a);
  12.  
  13.     Z(int a);
  14.  
  15.     Z(double a);
  16.  
  17.     Z(double a, double b);
  18.  
  19.     Z operator+(const Z &z);
  20.  
  21.     Z operator-(const Z &z);
  22.  
  23.     Z operator*(const Z &z);
  24.  
  25.     Z operator/(const Z &z);
  26.  
  27.     friend std::ostream &operator<<(std::ostream &output, const Z &z);
  28.  
  29.     friend std::istream &operator>>(std::istream &input, Z &z);
  30.  
  31.     operator int() const;
  32.  
  33.     operator double() const;
  34.  
  35.  
  36.     double getA() const;
  37.  
  38.     void setA(double a);
  39.  
  40.     double getB() const;
  41.  
  42.     void setB(double b);
  43. };
  44.  
  45. Z::Z() : a(0), b(0) {
  46.  
  47. }
  48.  
  49. Z::Z(std::string a) {
  50.  
  51. }
  52.  
  53. Z::Z(int a) : a(a), b(0) {
  54.  
  55. }
  56.  
  57. Z::Z(double a) : a(a), b(0) {
  58.  
  59. }
  60.  
  61. Z::Z(double a, double b) : a(a), b(b) {
  62.  
  63. }
  64.  
  65. Z Z::operator+(const Z &z) {
  66.     return {this->a + z.getA(), this->b + z.getB()};
  67. }
  68.  
  69. Z Z::operator-(const Z &z) {
  70.     return {this->a - z.getA(), this->b - z.getB()};
  71. }
  72.  
  73. Z Z::operator*(const Z &z) {
  74.     return {this->a * z.getA() - this->b * z.getB(), this->a * z.getB() + this->b * z.getA()};
  75. }
  76.  
  77. Z Z::operator/(const Z &z) {
  78.     double ar = this->a;        //kompilator pewnie i tak zaora
  79.     double ai = this->b;
  80.     double br = z.getA();
  81.     double bi = z.getB();
  82.     if (br == 0 && bi == 0) throw ("BLAD");
  83.     return {(ar * br + ai * bi) / (br * br + bi * bi), (ai * br - ar * bi) / (br * br + bi * bi)};
  84. }
  85.  
  86. std::ostream &operator<<(std::ostream &output, const Z &z) {
  87.     //if (z.getA()!=0) output << z.getA();
  88.     /*output << (z.getA() != 0 ? z.getA() : '') << (z.getA() != 0 && z.getB() != 0 && z.getB() > 0 ? '+' : '')
  89.            << (z.getB() != 0 ? z.getB() : '') << (z.getB() != 0 ? 'i' : '');*/
  90.     if (z.getA() != 0) output << (z.getA());
  91.     if (z.getA() != 0 && z.getB() != 0 && z.getB() > 0) output << '+';
  92.     if (z.getB() != 0) output << z.getB() << 'i';
  93. }
  94.  
  95. std::istream &operator>>(std::istream &input, Z &z) {
  96.     std::string str;
  97.     input >> str;
  98.     int pos;
  99.  
  100.     /*int sum = 0;
  101.     int i = 0;
  102.     ///pierwsza liczba
  103.     for (; str[i] > '0' && str[i] < '9'; ++i) {
  104.         sum = sum*10 + str[i] - '0';
  105.     }
  106.     if (str[i] == 'i') {
  107.         z.setB(sum);
  108.     } else {
  109.         z.setA(sum);
  110.  
  111.         sum=0;
  112.         if (str[i+1] > '0' && str[i+1] < '9'){
  113.             i++;
  114.             for (; str[i] > '0' && str[i] < '9'; ++i) {
  115.                 sum = sum*10 + str[i] - '0';
  116.             }
  117.             z.setB(sum);
  118.         }
  119.     }*/
  120.  
  121.     if (str.find('+') != str.find('-')) {
  122.         std::string num1, num2;
  123.         pos = (str.find('-', 1) == -1) ? str.find('+') : str.find('-', 1);
  124.         num1 = str.substr(0, pos);
  125.         num2 = str.substr(pos);
  126.         if (num1[0] == '-') z.setA(-std::atof(num1.substr(1, num1.length() - 1).c_str()));
  127.         else z.setA(std::atof(num1.c_str()));
  128.         if (num2[0] == '-') z.setB(-std::atof(num2.substr(1, num2.length() - 2).c_str()));
  129.         else z.setB(std::atof(num2.substr(0, num2.length() - 1).c_str()));
  130.     } else {
  131.         if (str[str.length() - 1] == 'i') {
  132.             str[str.length() - 1] = '\0';
  133.             z.setB(std::atof(str.c_str()));
  134.         } else z.setA(std::atof(str.c_str()));
  135.     }
  136. }
  137.  
  138. Z::operator int() const {
  139.     return (int) this->a;
  140. }
  141.  
  142. Z::operator double() const {
  143.     return this->b;
  144. }
  145.  
  146. double Z::getA() const {
  147.     return a;
  148. }
  149.  
  150. void Z::setA(double a) {
  151.     Z::a = a;
  152. }
  153.  
  154. double Z::getB() const {
  155.     return b;
  156. }
  157.  
  158. void Z::setB(double b) {
  159.     Z::b = b;
  160. }
  161.  
  162.  
  163. int main() {
  164.  
  165.     Z z1, z2, z3;
  166.     char znak;
  167.     std::cin >> z1;
  168.     std::cin >> znak;
  169.     std::cin >> z2;
  170.  
  171.     switch (znak) {
  172.         case '+':
  173.             std::cout << (int)(z1 + z2);
  174.             break;
  175.         case '-':
  176.             std::cout << (int)(z1 - z2);
  177.             break;
  178.         case '*':
  179.             std::cout << (int)(z1 * z2);
  180.             break;
  181.         case '/':
  182.             try {
  183.                 std::cout << (int)(z1 / z2);
  184.             } catch (const char *msg) {
  185.                 std::cout << msg;
  186.             }
  187.             break;
  188.     }
  189.     return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement