#include <iostream>
#include<ostream>
#include<cstdlib>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
class CStudent{
public: int nAge;
};
ostream& operator<<(ostream& o,const CStudent& s){
o<<s.nAge;
return o;
}
class Complex{
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, 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;//将a+bi 作为字符串读入 a+bi中间不可
int pos = s.find("+",0);
string sTmp = s.substr(0,pos);//分离出实部的字
//atof库函数能将const char*指针 指向 的内容转换成 float
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(int argc, char** argv) {
CStudent s;
s.nAge = 5;
cout<< s<<"hello";
Complex c;
int n;
//1+2i 87
cin>>c >> n;
cout<<c <<","<<n;
return 0;
}
#include<ostream>
#include<cstdlib>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
class CStudent{
public: int nAge;
};
ostream& operator<<(ostream& o,const CStudent& s){
o<<s.nAge;
return o;
}
class Complex{
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, 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;//将a+bi 作为字符串读入 a+bi中间不可
int pos = s.find("+",0);
string sTmp = s.substr(0,pos);//分离出实部的字
//atof库函数能将const char*指针 指向 的内容转换成 float
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(int argc, char** argv) {
CStudent s;
s.nAge = 5;
cout<< s<<"hello";
Complex c;
int n;
//1+2i 87
cin>>c >> n;
cout<<c <<","<<n;
return 0;
}