Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- class Z {
- private:
- double a;
- double b;
- public:
- Z();
- Z(std::string a);
- Z(int a);
- Z(double a);
- Z(double a, double b);
- Z operator+(const Z &z);
- Z operator-(const Z &z);
- Z operator*(const Z &z);
- Z operator/(const Z &z);
- friend std::ostream &operator<<(std::ostream &output, const Z &z);
- friend std::istream &operator>>(std::istream &input, Z &z);
- operator int() const;
- operator double() const;
- double getA() const;
- void setA(double a);
- double getB() const;
- void setB(double b);
- };
- Z::Z() : a(0), b(0) {
- }
- Z::Z(std::string a) {
- }
- Z::Z(int a) : a(a), b(0) {
- }
- Z::Z(double a) : a(a), b(0) {
- }
- Z::Z(double a, double b) : a(a), b(b) {
- }
- Z Z::operator+(const Z &z) {
- return {this->a + z.getA(), this->b + z.getB()};
- }
- Z Z::operator-(const Z &z) {
- return {this->a - z.getA(), this->b - z.getB()};
- }
- Z Z::operator*(const Z &z) {
- return {this->a * z.getA() - this->b * z.getB(), this->a * z.getB() + this->b * z.getA()};
- }
- Z Z::operator/(const Z &z) {
- double ar = this->a; //kompilator pewnie i tak zaora
- double ai = this->b;
- double br = z.getA();
- double bi = z.getB();
- if (br == 0 && bi == 0) throw ("BLAD");
- return {(ar * br + ai * bi) / (br * br + bi * bi), (ai * br - ar * bi) / (br * br + bi * bi)};
- }
- std::ostream &operator<<(std::ostream &output, const Z &z) {
- //if (z.getA()!=0) output << z.getA();
- /*output << (z.getA() != 0 ? z.getA() : '') << (z.getA() != 0 && z.getB() != 0 && z.getB() > 0 ? '+' : '')
- << (z.getB() != 0 ? z.getB() : '') << (z.getB() != 0 ? 'i' : '');*/
- if (z.getA() != 0) output << (z.getA());
- if (z.getA() != 0 && z.getB() != 0 && z.getB() > 0) output << '+';
- if (z.getB() != 0) output << z.getB() << 'i';
- }
- std::istream &operator>>(std::istream &input, Z &z) {
- std::string str;
- input >> str;
- int pos;
- /*int sum = 0;
- int i = 0;
- ///pierwsza liczba
- for (; str[i] > '0' && str[i] < '9'; ++i) {
- sum = sum*10 + str[i] - '0';
- }
- if (str[i] == 'i') {
- z.setB(sum);
- } else {
- z.setA(sum);
- sum=0;
- if (str[i+1] > '0' && str[i+1] < '9'){
- i++;
- for (; str[i] > '0' && str[i] < '9'; ++i) {
- sum = sum*10 + str[i] - '0';
- }
- z.setB(sum);
- }
- }*/
- if (str.find('+') != str.find('-')) {
- std::string num1, num2;
- pos = (str.find('-', 1) == -1) ? str.find('+') : str.find('-', 1);
- num1 = str.substr(0, pos);
- num2 = str.substr(pos);
- if (num1[0] == '-') z.setA(-std::atof(num1.substr(1, num1.length() - 1).c_str()));
- else z.setA(std::atof(num1.c_str()));
- if (num2[0] == '-') z.setB(-std::atof(num2.substr(1, num2.length() - 2).c_str()));
- else z.setB(std::atof(num2.substr(0, num2.length() - 1).c_str()));
- } else {
- if (str[str.length() - 1] == 'i') {
- str[str.length() - 1] = '\0';
- z.setB(std::atof(str.c_str()));
- } else z.setA(std::atof(str.c_str()));
- }
- }
- Z::operator int() const {
- return (int) this->a;
- }
- Z::operator double() const {
- return this->b;
- }
- double Z::getA() const {
- return a;
- }
- void Z::setA(double a) {
- Z::a = a;
- }
- double Z::getB() const {
- return b;
- }
- void Z::setB(double b) {
- Z::b = b;
- }
- int main() {
- Z z1, z2, z3;
- char znak;
- std::cin >> z1;
- std::cin >> znak;
- std::cin >> z2;
- switch (znak) {
- case '+':
- std::cout << (int)(z1 + z2);
- break;
- case '-':
- std::cout << (int)(z1 - z2);
- break;
- case '*':
- std::cout << (int)(z1 * z2);
- break;
- case '/':
- try {
- std::cout << (int)(z1 / z2);
- } catch (const char *msg) {
- std::cout << msg;
- }
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement