虽然代码有点长,但是胜在清晰,^_^ #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; }
#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; }
#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; }
#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; } }
#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; }
#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")
#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")
#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(); } }
// 其实不定义类也能通过吧,哈哈哈哈 #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")
#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; }
#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); } } } }
#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; }
#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; }
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); } }
#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; }