C++实现复数类complex以“a+bi“形式的读入与输出

#include<bits/stdc++.h>
using namespace std;

int n;
complex<double> c;

void inputComplex(complex<double> &cplx){
	bool sgn, inpnum;	//0:pos, 1:neg
	double num, dec;	//num:读入的数(不含符号位), dec:decimal小数点后的
	cplx.real(0);		//先初始化实部虚部为0
	cplx.imag(0);
	char x = getchar();	//开始读取
	while (x!='-'&&x!='i'&&(x<'0'||x>'9')) x = getchar();	//不读取无关符号
	while (x=='-'||x=='+'||x=='i'||(x>='0'&&x<='9')){	//当读取到了有关符号,若读取到无关符号再退出循环
		sgn = 0;		//一开始符号位为正
		if (x=='-'){	//读取符号位
			sgn = 1;
			x = getchar();
		}else if (x=='+')	x = getchar();
		num = 0;		//一开始数字位为0
		inpnum = false;	//用于判断是否输入了数字,用于判断"i"前无数字的特殊情况
		while (x>='0'&&x<='9'){	//读取数字
			inpnum = true;
			num *= 10;
			num += x-'0';
			x = getchar();
		}
		if (x=='.'){			//读取小数点后数字
			x = getchar();
			dec = 1;
			while (x>='0'&&x<='9'){
				dec /= 10;
				num += dec*(x-'0');
				x = getchar();
			}
		}
		if (x=='i'){			//通过结尾是不是"i"来判断读取的是实数还是虚数
			if (!inpnum) cplx.imag(cplx.imag()+(sgn?-1:1));//若"i"前无数字,系数为1
			else cplx.imag(cplx.imag()+(sgn?-num:num));
			x = getchar();
		}else	cplx.real(cplx.real()+(sgn?-num:num));
	}
}

void outputComplex(complex<double> cplx){
	if (cplx.imag() == 0) cout<<cplx.real();	//使用输出流,输出最简小数
	else{
		if (cplx.real() == 0){
			if (cplx.imag() == 1) cout<<'i';
			else if (cplx.imag() == -1) cout<<"-i";
			else cout<<cplx.imag()<<'i';
		}else{
			if (cplx.imag() == 1) cout<<cplx.real()<<"+i";
			else if (cplx.imag() == -1) cout<<cplx.real()<<"-i";
			else cout<<cplx.real()<<showpos<<cplx.imag()<<noshowpos<<'i';//第二个复数保留前导'+'
		}
	}
}

int main(){
	scanf("%d",&n);
	while (n--){
		inputComplex(c);
		cout<<c<<"  ";
		outputComplex(c);
		cout<<endl;
	}
	return 0;
}

测试:在这里插入图片描述

主要是因为STL自带的complex类不支持按这种方式读入,又不想重新写一遍整个类,于是就写了这两个读入与输出函数。
有BUG欢迎反馈

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
复数是由实部和虚部组成的,因此在设计复数类complex时,我们需要考虑如何表示实部和虚部以及加法运算。 首先,定义一个complex,其中包括两个私有成员变量real和imag,分别表示复数的实部和虚部。同时,需要定义一个无参构造函数和一个有参构造函数,用于构造复数对象。 其次,需要实现输入和输出函数,可以通过重载流运算符实现输出函数将复数表示为“实部+虚部i”的形式,输入函数则将输入的字符串解析为实部和虚部。 最后,需要实现加法运算。由于是复数的加法运算,需要同时计算实部和虚部,计算公式如下: (a+bi)+(c+di) = (a+c) + (b+d)i 具体实现时,可以定义一个重载运算符+,将两个complex对象相加,返回一个新的complex对象。 下面是一个具体的实现代码示例: ```c++ #include <iostream> class complex { friend std::ostream& operator<<(std::ostream&, const complex&); friend std::istream& operator>>(std::istream&, complex&); public: complex(): real(0), imag(0) {} complex(double r, double i): real(r), imag(i) {} complex operator+(const complex& other) const { return complex(real + other.real, imag + other.imag); } private: double real; // 实部 double imag; // 虚部 }; std::ostream& operator<<(std::ostream& os, const complex& c) { os << c.real << "+" << c.imag << "i"; return os; } std::istream& operator>>(std::istream& is, complex& c) { is >> c.real >> c.imag; return is; } int main() { complex c1(1, 2); std::cout << c1 << std::endl; complex c2; std::cin >> c2; std::cout << c2 << std::endl; complex c3 = c1 + c2; std::cout << c3 << std::endl; return 0; } ``` 在这个示例中,我们定义了一个complex,并且实现输出运算符<<、输入运算符>>和加法运算符+。在main函数中,我们构造了两个复数对象c1和c2,输出它们的值,然后对它们进行加法运算并输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值