zoukankan      html  css  js  c++  java
  • 流插入运算符和流提取运算符的重载

    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    class Complex {
    public:
        double real, imag;
    public:
        Complex(double r = 0, double i = 0): real(r), imag(i){};
        friend ostream& operator<<(ostream& os, const Complex& c);
        friend istream& operator>>(istream& is, const Complex& c);
    };
    
    ostream& operator<<(ostream& os, const Complex& c) {
        os << c.real << "+" << c.imag << "i";
        return os;
    }
    
    istream& operator>>(istream& is, Complex& c) {
        string s;
        is >> s;
        int pos = s.find("+", 0);
        string sTmp = s.substr(0, pos);
        c.real = atof(sTmp.c_str());
        sTmp = s.substr(pos+1, s.length()-pos-2);
        c.imag = atof(sTmp.c_str());
        return is;
    }
    
    int main()
    {
        Complex c;
        int n;
        cin >> c >> n;
        cout << c << "," << n;
        return 0;
    }
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    lsof命令详解
    nmap命令详解
    ss命令详解
    netstat命令详解
    ifup,ifdown命令详解
    playbook部署nginx
    cento7忘记root密码怎么办
    正确关闭selinux
    使用ssh-agent管理私钥
    gitlab中的CI
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10165786.html
Copyright © 2011-2022 走看看