首页 > 试题广场 >

复数

[编程题]复数
  • 热度指数:4221 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
编写一个复数类,有构造函数,能对复数初始化重载加法操作符并按a+bi 的形式输出。

输入描述:
输入第一行表示测试用例的个数m,接下来m行每行有4个用空格隔开的整数,分别表示2个复数的实部和虚部。


输出描述:
输出m行。按a+bi或者a-bi的格式输出,表示两个复数相加的和。
示例1

输入

1
3 4 1 -2

输出

4+2i

备注:
注意虚部为负数时的输出。
虽然代码有点长,但是胜在清晰,^_^
#include<iostream>
using namespace std;

class Complex{
public:
    Complex(double r, double i)
    {
        this->real = r;
        this->imag = i;
    }
    friend Complex operator+(const Complex&, const Complex&);
    void display();
private:
    double real;
    double imag;
};

Complex operator+(const Complex& c1, const Complex& c2)
{
    return Complex(c1.real+c2.real, c1.imag+c2.imag);
}

void Complex::display()
{
    if(imag >= 0)
        cout<<real<<'+'<<imag<<'i'<<endl;
    else
        cout<<real<<imag<<'i'<<endl;
}

int main()
{
    int m;
    cin>>m;
    while(m-- > 0)
    {
        double a, b, c, d;
        cin>>a>>b>>c>>d;
        Complex c1(a, b), c2(c, d), c3(0, 0);
        c3 = c1 + c2;
        c3.display();
    }
    return 0;
}

发表于 2019-12-20 09:56:30 回复(1)
#include <stdio.h>
using namespace std;
class complex{
    public:
    int a,b;
    complex(int x,int y):a(x),b(y){}
    complex(){};
    complex operator + (const complex &e) const{
        complex tmp(a+e.a,b+e.b);
        return tmp;
    }
};
int main()
{
    freopen("in.txt","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int a1,b1,a2,b2;
        for(int i=0;i<n;i++){
            complex A,B,C;
            scanf("%d%d%d%d",&A.a,&A.b,&B.a,&B.b);
            C=A+B;
            if(C.b>0) printf("%d+%di\n",C.a,C.b);
            else if(C.b<0) printf("%d%di\n",C.a,C.b);
            else printf("%d\n",C.a);
        }
    }
    return 0;
}

发表于 2019-03-03 17:11:51 回复(0)
#include<stdio.h>
int main()
{
    int m,a,b,c,d;
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);
        int p=a+c,q=b+d;
        if(q>0)
            printf("%d+%di\n",p,q);
        else
            printf("%d%di\n",p,q);
    }
}

发表于 2020-05-09 16:53:12 回复(0)
#include<iostream>
using namespace std;
class Complex
{
    public:
    double a,b;
    Complex(double a,double b)//构造函数
    {
        this->a = a;
        this->b = b;
    }
    Complex operator+=(Complex temp)//我重载的是+=,个人喜好
    {
        this->a += temp.a;
        this->b += temp.b;
        return Complex(this->a,this->b);
    }
    Complex operator-(Complex temp)
    {
        this->a -= temp.a;
        this->b -= temp.b;
        return Complex(this->a,this->b);
    }
    void cal()
    {
        if(this->b >= 0)
            cout << this->a << "+" << this->b << "i" << endl;
        else
            cout << this->a << this->b << "i" << endl;
    }
    ~Complex(){}//析构函数
};
int main(void)
{
    int m;
    while(cin >> m)
    {
        while(m--)
        {
            double a_1,b_1,a_2,b_2;
            cin >> a_1 >> b_1 >> a_2 >> b_2;
            Complex x(a_1,b_1);
            Complex y(a_2,b_2);
            x += y;
            x.cal();
        }
    }
    return 0;
}

编辑于 2022-02-15 21:52:18 回复(0)
#include<iostream>
using namespace std;
int main(){
    int m;
    cin>>m;
    for(int i=0;i<m;i++){
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        if(b+d>0)
            cout<<a+c<<"+"<<b+d<<"i"<<endl;
        else
            cout<<a+c<<b+d<<"i"<<endl;
    }
}

发表于 2019-02-25 13:51:45 回复(0)
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;

class Complex { //复数类
  private:
    int realPart, imagPart; //实部和虚部

  public:
    //构造函数
    Complex(int r = 0, int i = 0): realPart(r), imagPart(i) {}

    //重载加法操作符
    Complex operator+(const Complex& c);

    //重载输入流运算符
    friend istream& operator>>(istream& in, Complex& c);

    //重载输出流运算符
    friend ostream& operator<<(ostream& out, const Complex& c);
};

Complex Complex::operator+(const Complex& c) {
    return {realPart + c.realPart, imagPart + c.imagPart};
}

istream& operator>>(istream& in, Complex& c) {
    in >> c.realPart >> c.imagPart;
    return in;
}

ostream& operator<<(ostream& out, const Complex& c) {
    out << c.realPart;
    if (c.imagPart >= 0) {
        out << '+';
    }
    out << c.imagPart << "i";
    return out;
}

int main() {
    int m;
    cin >> m;
    while (m--) {
        Complex c1, c2;
        cin >> c1 >> c2;
        cout << c1 + c2 << endl;
    }
    return 0;
}

发表于 2024-02-03 10:08:40 回复(0)
#include <iostream>
using namespace std;

class fuShu{
public:
    fuShu operator+(const fuShu & f){
        this->a += f.a;
        this->b += f.b;
        return * this;
    }
    int a,b;
};

int main() {
    int m;
    while (cin >> m) {
        fuShu x,y;int a1,b1,a2,b2;
        for (int i = 0; i < m; ++i) {
            cin >> a1 >> b1 >> a2 >> b2;
            x.a = a1;x.b = b1;
            y.a = a2;y.b = b2;

            fuShu newNum = x + y;
            if(newNum.b < 0){
                cout << newNum.a << newNum.b << "i" << endl;
            }else if(newNum.b == 0){
                cout << newNum.a << endl;
            }else{
                cout << newNum.a << "+" << newNum.b << "i" << endl;
            }
        }
    }
}
// 64 位输出请用 printf("%lld")

发表于 2023-03-29 16:25:33 回复(0)
#include <iostream>
using namespace std;
struct fNum{
    int head;
    int off;
};
int main() {
    fNum a,b;
    int m;
    while(cin>>m){
        for(int i=0;i<m;i++){
            cin>>a.head>>a.off>>b.head>>b.off;
            a.head+=b.head;
            a.off+=b.off;
            cout<<a.head;
            if(a.off>0){
                cout<<"+"<<a.off<<"i"<<endl;
            }
            else if(a.off<0){
                cout<<a.off<<"i"<<endl;
            }
            else{
                cout<<endl;
            }
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

发表于 2023-03-22 14:55:03 回复(0)
#include <cstdio>

int main(){
    int m;
    scanf("%d",&m);
    for(int i = 0; i < m; ++i){
        int a1,b1,a2,b2;
        scanf("%d %d %d %d",&a1,&b1,&a2,&b2);
        if((b1+b2)>0){
            printf("%d+%di\n",a1+a2,b1+b2);//补一个正号
        }else{
            printf("%d%di\n",a1+a2,b1+b2); 
        }
    }
    return 0;
}

发表于 2023-03-22 11:51:15 回复(0)
#include <iostream>
using namespace std;

class complex{
public:
    int real;
    int fake;

    complex(int a ,int b){
        this->real = a;
        this->fake = b;
    }

    void show(){
        if(fake >=0){
            cout << real <<'+'<< fake <<'i'<<endl;
        }
        else{

            cout << real << fake <<'i'<<endl;
        }
    }

};

complex operator +(const complex &x,const complex &y){

    return complex(x.real+y.real,x.fake+y.fake);
}


int main() {
    int m;
    cin >> m;

    while(m--){

        int a,b,c,d;

        cin >> a >> b >> c >> d;

        complex x1(a,b),x2(c,d),x3(0,0);

        x3 = x1 + x2;

        x3.show();

    }
}


发表于 2023-03-18 10:32:59 回复(0)
// 其实不定义类也能通过吧,哈哈哈哈
#include <iostream>
using namespace std;

class Plura{
public:
    int real;
    int vir;
    Plura operator+(Plura b){
        Plura p(0,0);
        p.real = this->real+b.real;
        p.vir = this->vir+b.vir;
        return p;
    }

    Plura(int a,int b){
        this->real = a;
        this->vir = b;
    }

    void show(){
        if(vir >0)
        cout << real << "+" << vir << "i"<<endl;
        else {
             cout << real << vir << "i"<<endl;
        }
    }

};

int main() {
    int a;
    while(cin >> a){
        for (int i = 0; i < a; ++i) {
            int x,y,z,h;
            cin >> x>>y>>z>>h;
            Plura p1(x,y);
            Plura p2(z,h);

            p1= p1+p2;
            p1.show();
        }
    }
}
// 64 位输出请用 printf("%lld")

发表于 2023-03-04 10:18:17 回复(0)
#include<iostream>
#include<cstdio>

struct Complex{
    int real;
    int imag;
    
    Complex(int a, int b) : real(a), imag(b) {}
    
    Complex operator+(Complex c) const{
        return Complex(real + c.real, imag + c.imag);  
    }    
};

int main(){
    int n;
    scanf("%d", &n);
    while(n--){
        int c1_r, c1_i, c2_r, c2_i;
        scanf("%d%d%d%d", &c1_r, &c1_i, &c2_r, &c2_i);
        Complex c1 = Complex(c1_r, c1_i);
        Complex c2 = Complex(c2_r, c2_i);
        Complex c = c1 + c2;
        if(c.imag > 0){
            printf("%d+%di\n", c.real, c.imag);
        }
        else if(c.imag < 0){
            printf("%d%di\n", c.real, c.imag);
        }
        else{
            printf("%d\n", c.real);
        }
        
    }
    return 0;
}

发表于 2022-01-21 12:16:51 回复(0)
#include<iostream>
#include<math.h>
using namespace std;

class complex {
public:
    int a, b;
    complex(int x, int y) { a = x; b = y; }
    complex(){}
    complex operator + (const complex &C) {
        complex addc(a + C.a, b + C.b);
        return addc;
    }
};

int main() {
    int m;
    while (scanf("%d", &m) != EOF) {
        for (int i = 0; i < m; i++) {
            int a1, b1, a2, b2;
            cin >> a1 >> b1 >> a2 >> b2;
            complex c1(a1, b1);
            complex c2(a2, b2);
            complex c = c1 + c2;
            if (c.b > 0) {
                printf("%d+%di\n", c.a, c.b);
            }
            else if(c.b==0){
                printf("%d\n", c.a);
            }
            else {
                printf("%d%di\n", c.a, c.b);
            }
        }
    }
}
编辑于 2021-02-04 10:31:32 回复(0)
#include <bits/stdc++.h>
using namespace std;
class ComplexNumber
{
private:
    int Real,Vir;
public:
    ComplexNumber(int real,int vir) : Real(real),Vir(vir) {}
    ComplexNumber operator+(const ComplexNumber &other) const
    {
        return ComplexNumber(this->Real+other.Real,this->Vir+other.Vir);
    }
    void Print()
    {
        cout<<this->Real;
        if(this->Vir>0) cout<<"+"<<this->Vir<<"i\n";
        else  if(this->Vir<0)   cout<<this->Vir<<"i\n";
    }
};
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int r1,r2,v1,v2;
        cin>>r1>>v1>>r2>>v2;
        ComplexNumber a=ComplexNumber(r1,v1), b=ComplexNumber(r2,v2);
        ComplexNumber c=a+b;
        c.Print();
    }
    return 0;
}

发表于 2020-04-30 16:59:22 回复(0)
#include <stdio.h>

int main()
{
    int m;
    while(~scanf("%d", &m))
        while(m--)
        {
            int a1, b1, a2, b2;
            scanf("%d%d%d%d", &a1, &b1, &a2, &b2);
            int a = a1 + a2, b = b1 + b2;
            printf("%d", a);
            if(b >= 0)
                putchar('+');
            printf("%di\n", b);
        }
}

发表于 2020-04-03 14:38:40 回复(0)
搞不懂北理为什么那么喜欢用类
#include<iostream>
using namespace std;
class Main{
    private:
    int a;//实部
    int b;//虚部
    public:
    Main(int a,int b){
        this->a=a;
        this->b=b;
    }
    Main operator +(Main m2){
        a=a+m2.get_a();
        b=b+m2.get_b();
        return *this;
    }
    int get_a(){
        return a;
    }
    int get_b(){
        return b;
    }
};
int main(){
    int k;
    int a,b,c,d;
    cin>>k;
    while(k-- >0){
        cin>>a>>b>>c>>d;
        Main m1(a,b);
        Main m2(c,d);
        Main m3 = m1+m2;
        if(m3.get_b()>0)
            cout<<m3.get_a()<<"+"<<m3.get_b()<<"i"<<endl;
        else if(m3.get_b()==0)
            cout<<m3.get_a()<<endl;
        else
            cout<<m3.get_a()<<m3.get_b()<<"i"<<endl;
    }
    return 0;
}


发表于 2020-04-02 11:14:54 回复(0)
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            int ar = getNum(scanner.next());
            int ai = getNum(scanner.next());
            int br = getNum(scanner.next());
            int bi = getNum(scanner.next());
            int sumi = ai+bi;
            int sumr = ar+br;
            System.out.println(sumi>=0?sumr+"+"+sumi+"i":sumr+""+sumi+"i");
        }
    }

    public static int getNum(String s){
        return s.startsWith("-")?-Integer.parseInt(s.substring(1)):Integer.parseInt(s);
    }
}


发表于 2020-03-17 11:01:26 回复(0)
#include<math.h>
(865)#include <stdio.h>
int main(void)
{
	int m,i;
	int a,b,c,d;
	while(!scanf("%d",&m))
	{
		while (getchar() != '\n')
			continue;
	}
	for (i = 0; i < m; i++)
	{
		scanf("%d %d %d %d", &a,&b,&c,&d);
		if ((b + d)>0)
		{
			printf("%d+%di\n",a+c,b+d);
		}
		else
		{
			printf("%d%di\n",a +c,b + d);
		}
	}
	return 0;
}

发表于 2020-03-16 23:04:40 回复(0)
#include<stdio.h>
int main()
{
    int m;
    while(scanf("%d\n",&m)!=EOF)
    {
        while(m--)
        {
            int a,b,c,d;
            scanf("%d %d %d %d",&a,&b,&c,&d);
            if(b+d>0)
                printf("%d+%di\n",a+c,b+d);
            else
                printf("%d%di\n",a+c,b+d);
        }
    }
    return 0;
}

发表于 2020-03-07 16:36:40 回复(0)
import java.util.Scanner;
public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int N=sc.nextInt();
            for (int i = 0; i < N; i++) {
                int a=sc.nextInt();
                int b=sc.nextInt();
                int c=sc.nextInt();
                int d=sc.nextInt();
                fushu f1=new fushu(a,b);
                fushu f2=new fushu(c,d);
                String s=fushu.complex(f1,f2);
                System.out.println(s);
            }
        }
}
class fushu{
    int shubu;
    int xubu;
    String op;
    public fushu(int i, int j) {
        if (j>0) {
            this.setOp("+");
         }
         if (j<0) {
             this.setOp("-");
         }
         this.setShubu(i);
        this.setXubu(j);
    }
    public static String complex(fushu f1,fushu f2){
         StringBuffer sb=new StringBuffer();
         sb.append(f1.shubu+f2.shubu);
         if (f1.xubu+f2.xubu>0) {
            sb.append("+").append(f1.xubu+f2.xubu).append("i");
         }else {
            sb.append(f1.xubu+f2.xubu).append("i");
        }
         return sb.toString();
    }
    public int getShubu() {
        return shubu;
    }
    public void setShubu(int shubu) {
        this.shubu = shubu;
    }
    public int getXubu() {
        return xubu;
    }
    public void setXubu(int xubu) {
        this.xubu = xubu;
    }
    public String getOp() {
        return op;
    }
    public void setOp(String op) {
        this.op = op;
    }
}     
发表于 2019-11-06 12:38:39 回复(0)

问题信息

上传者:小小
难度:
30条回答 2948浏览

热门推荐

通过挑战的用户

查看代码