一道C++课本上的编程题:
编写一个程序完成下列功能:
a)创建用户自定义类型Complex,使其拥有private整形数据成员real和imaginary,并声明重载的流插入和流提取运算符函数为该类的友元。
b)定义流插入和流提取运算符函数。流提取运算符函数应该判断输入的数据是否有效;如果不是,它应该设置failbit为来表示有错误输入。输入的格式应该是
3 + 8i
c)这两个数字可以是正的,也可以是负的,并且可能两个数字中缺少一个。如果缺少一个值,对应的数据成员应当被设置为零。当输入发生错误时,流插如运算符不能显示该数。当输入的imaginary值为负时,应当显示减号而不是加号。
d)编写一个main函数,检查用户自定义类Complex的输入和输出,其中使用重载的流提取和流插入运算符。
可以用正则表达式检验一下,没有说输出的格式要求,我就按0输出了,如果需要省略,改一下就好了
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <regex>
using namespace std;
class Complex {
private:
int real, imaginary;
public:
friend istream& operator>>(istream&, Complex&);
friend ostream& operator<<(ostream&, Complex&);
};
int main(){
Complex c;
while (true) {
if (cin >> c)
cout << c;
else
cout << "error" << endl;
}
return 0;
}
istream& operator>>(istream&in, Complex&complex) {
string exp;
getline(in, exp);
smatch result;
regex_match(exp, result, regex("([+-]{0,1}\\d+) *([+-]) *(\\d+)i"));
if (regex_match(exp, result, regex("([+-]{0,1}\\d+) *([+-]) *(\\d+)i"))) { //实部虚部都有
complex.real = stoi(result[1].str());
complex.imaginary = stoi(result[2].str() + result[3].str());
}
else if (regex_match(exp, result, regex("([+-]{0,1}\\d+)"))) { //只有实部
complex.real = stoi(result[1].str());
complex.imaginary = 0;
}
else if (regex_match(exp, result, regex("(([+-]{0,1}) *(\\d+)i)"))) { //只有虚部
complex.real = 0;
complex.imaginary = stoi(result[1].str() + result[2].str());
}
else
in.setstate(ios::failbit);
return in;
}
ostream& operator<<(ostream&out, Complex&complex)
{
out << complex.real << ' ' << (complex.imaginary > 0 ? '+' : '-') << ' ' << abs(complex.imaginary) << 'i' << endl;
return out;
}
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/*6. 更加完整建立一个复数类 Complex,要求
a. 含两个私有数据成员:real, imaginary: float
b. 具有三种构造函数:a.不带参数构造: 0+0i
b.以实部和虚部构造
c.拷贝构造函数
c.公有成员函数包括如下功能:
输出这个复数
与另一复数的加法函数,要求函数原型为 complex add(complex c);
判断与另一复数是否相等 要求函数原型为bool isEqual(complex c);
在main函数中,输入两个复数,判断两个复数是否相等(实部和虚部要分别相等),然后两者相加之和赋值给一个新的复数,并输出
思考:基于上面的Complex类,有如下main函数能否通过运行,有那些是错误的,为什么?
int main()
{
Complex a,b;
Complex c=a+b;
Complex d=a;
Complex e;
e=a;
return 0;
}*/
class Complex{
public:
void getRI(float R,float I); //设置复数的两个私有成员
void output(); //输出这个复数
void add(Complex c); //与另一复数的加法函数 原型为 add (Complex c);
void isEqual(Complex c); //判断与另一复数是否相等
Complex(){ //不带参数的构造函数
real=0;
imaginary=0;
}
Complex(float R,float I); //以实部和虚部构造
Complex(Complex &c); //复制构造函数
Complex operator+(Complex& b)
{
real += b.real;
imaginary += b.imaginary;
return *this;
}
private:
float real,imaginary;
};
Complex::Complex(float R,float I) //带参数构造函数的实现
{
real=R;
imaginary=I;
}
Complex::Complex(Complex &c){
real=c.real;
imaginary=c.imaginary;
cout<<"calling the copy constructor"<<endl;
}
void Complex::getRI(float R,float I){
real=R;
imaginary=I;
}
void Complex::output(){
cout<<real<<"+"<<imaginary<<"i"<<endl;
}
void Complex::add(Complex c){
cout<<"两复数相加为:";
cout<<real+c.real<<"+"<<imaginary+c.imaginary<<"i"<<endl;
}
void Complex::isEqual(Complex c){
if(real==c.real)
{
if(imaginary==c.imaginary)
cout<<"They are equal."<<endl;
}
else
cout<<"They are not equal."<<endl;
}
int main(int argc, char** argv) {
Complex c1;
Complex c2(1,2); //调用带参数的构造函数
Complex c3(3,4); //用对象c2初始化对象c3,复制构造函数被调用
float R1,I1,R2,I2;
cin>>R1>>I1;
cin>>R2>>I2;
c2.getRI(R1,I1);
c3.getRI(R2,I2);
cout<<"复数c2为:";
c2.output();
cout<<"复数c3为:";
c3.output();
c2.add(c3);
c2.isEqual(c3);
/*函数调用的时候不用
也不可以包含两类型名,
应该写成myComplex.add(c); */
Complex a,b;
Complex c=a+b;
Complex d=a;
Complex e;
e=a;
return 0;
}