文章

34

粉丝

89

获赞

3

访问

20.5k

头像
复数 题解:c++struct 和 运算符重载实现
P1021 贵州大学机试题
发布于2024年3月24日 19:40
阅读数 453

#include<bits/stdc++.h>
using namespace std;
struct Complex{
    double real;
    double image;
    Complex operator + (const Complex &c) const{
        Complex cpx;
        cpx.real = this->real + c.real;
        cpx.image = this->image + c.image;
        return cpx;
    }
    Complex operator * (const Complex &c) const{
        Complex cpx;
        cpx.real = (this->real * c.real) - (this -> image * c.image);
        cpx.image = (this->real * c.image) + (this -> image * c.real);
        return cpx;
    }

};
int main(){
    double a,b,c,d;
    char ch;
    cin >> a >> b >> c >> d >> ch;
    Complex c1 = {a,b};
    Complex c2 = {c,d};
    if(ch == '*'){
        Complex c3 = c1 * c2;
        printf("%.1lf %.1lf",c3.real,c3.image);
    }else{
        Complex c3 = c1 + c2;
        printf("%.1lf %.1lf",c3.real,c3.image);
    }
    return 0;
}
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发