R7-2 复数相加分数 10作者 余春艳单位 福州大学题目:一个复数类,运算符重载 + ,实现复数和复数的相加。输入一组复数,每行一个复数,直到输入0结束。 输出这组复数的结果。

文章描述了一个C++程序,定义了一个Complex类来处理和解析用户输入的复数,包括从字符串中提取实部和虚部,以及复数的加法运算和输出格式化。
摘要由CSDN通过智能技术生成

按数学规范输入输出复数:

a+bi ;

注意:

i, -i,

a+i, a-i,

a, bi

完整代码展示

#include<iostream>
#include<cmath>
using namespace std;
const int Max = 100;
class Complex{
    double real, image;
    public:
    Complex(double r=0,double i=0):real(r),image(i){}
    ~Complex(){}
    void setRI(double r,double i);
    void display();
    Complex operator+(Complex &b);
    friend Complex getC(char *pch);
};
Complex getC(char *pch);
int main(){
    char ch[Max]="\0";
    Complex a;
    while(cin>>ch){
        Complex b=getC(ch);
        a=a+b;
    }
    a.display();
    return 0;
}
Complex getC(char *pch){
    int index = 0, one=0, two=0, signOne=0, signTwo=0, signPoint=0;
    Complex c;
    if(pch[0]=='i') c.setRI(0,1);
    else{
        if(pch[0]=='-'){
            signOne=1, index++;
            if(pch[1]=='i') c.setRI(0,-1);
        }
        for(int i=index; pch[i]>='0'&&pch[i]<='9'||pch[i]=='.'; i++, index++){
            if(pch[i]=='.'){ signPoint=1; continue; }
            if(signPoint){ one=one+pow(0.1,signPoint)*int(pch[i]-48); signPoint++; }
            else one=one*10+int(pch[i]-48);
        }
        if(signOne) one=-one;
        if(one){
            if(pch[index]=='i') c.setRI(0,one) ;
            else if(pch[index]=='+'||pch[index]=='-'){
                if(pch[index]=='-') signTwo=1 ;
                index++, signPoint=0;
                if(pch[index]=='i'){
                    if(signTwo) c.setRI(one,-1);
                    else c.setRI(one,1);
                }
                for(int i=index; pch[i]>='0'&&pch[i]<='9'||pch[i]=='.'; i++, index++){
                    if(pch[i]=='.'){ signPoint=1; continue; }
                    if(signPoint){ two=two+pow(0.1,signPoint)*int(pch[i]-48); signPoint++; }
                    else two=two*10+int(pch[i]-48);
                }
                if(signTwo) two=-two;
                if(two) c.setRI(one,two);
            }else c.setRI(one,0);
        }
    }
    return c ;
}
void Complex::setRI(double r,double i){ real=r, image=i; }
Complex Complex::operator+(Complex &b){
    real=real+b.real;
    image=image+b.image;
    return *this;
}
void Complex::display(){
    if(real==0){
        if(image==1) cout<<'i';
        else if(image==-1) cout<<"-i";
        else if(image)cout<<image<<'i';
        else cout<<image;
    }else{
        if(image==1) cout<<real<<"+i";
        else if(image==-1) cout<<real<<"-i";
        else if(image>0) cout<<real<<'+'<<image<<'i';
        else if(image<0)cout<<real<<image<<'i';
        else cout<<real ;
    }
}

输入数据函数

Complex getC(char *pch){
    int index = 0, one=0, two=0, signOne=0, signTwo=0, signPoint=0;
    Complex c ;
    if(pch[0]=='i') c.setRI(0,1);
    else{
        if(pch[0]=='-'){
            signOne=1, index++;
            if(pch[1]=='i') c.setRI(0,-1);
        }
        for(int i=index; pch[i]>='0'&&pch[i]<='9'||pch[i]=='.'; i++, index++){
            if(pch[i]=='.'){ signPoint=1; continue; }
            if(signPoint){ one=one+pow(0.1,signPoint)*int(pch[i]-48); signPoint++; }
            else one=one*10+int(pch[i]-48);
        }
        if(signOne) one=-one;
        if(one){
            if(pch[index]=='i') c.setRI(0,one) ;
            else if(pch[index]=='+'||pch[index]=='-'){
                if(pch[index]=='-') signTwo=1 ;
                index++, signPoint=0;
                if(pch[index]=='i'){
                    if(signTwo) c.setRI(one,-1);
                    else c.setRI(one,1);
                }
                for(int i=index; pch[i]>='0'&&pch[i]<='9'||pch[i]=='.'; i++, index++){
                    if(pch[i]=='.'){ signPoint=1; continue; }
                    if(signPoint){ two=two+pow(0.1,signPoint)*int(pch[i]-48); signPoint++; }
                    else two=two*10+int(pch[i]-48);
                }
                if(signTwo) two=-two;
                if(two) c.setRI(one,two);
            }else c.setRI(one,0);
        }
    }
    return c ;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值