复数的加法(改错)

题目:一个复数类,运算符重载 + ,实现复数和复数的相加。输入一组复数,每行一个复数,直到输入0结束。 输出这组复数的结果。

提示: 复数的输入和输出符合数学书写规范

输入示例

3+2i

2+3i

0

输出示例

5+5i

代码:

#include<iostream>
using namespace std;

class complex
{
    int r,v;
    public:
    void set()
    {
        r=0;
        v=0;
    }
    complex()
    {
        r=0;
        v=0;
    }
    void display()
        {
            int i=v;
            if (i > 0)
            {
                if (i == 1)
                {
                    if (r == 0)
                        cout << "i" << endl;
                    else
                        cout << r << "+" << "i" << endl;
                }
                else
                {
                    if (r == 0)
                        cout << i << "i" << endl;
                    else
                        cout << r << "+" << i << "i" << endl;
                }
            }
            else if (i < 0)
            {
                if (i == -1)
                {
                    if (r != 0)
                        cout << r;
                    cout << "-i" << endl;
                }
                else
                {
                    if (r == 0)
                        cout << i << "i" << endl;
                    else
                        cout << r << i << "i" << endl;
                }
            }
            else
                cout << r << endl;
        }
    friend void mul(complex &,string);
    friend complex operator+(complex &a,complex &b);
};
complex operator+(complex &a,complex &b)
{
    complex c;
    c.r=a.r+b.r;
    c.v=a.v+b.v;
    return c;
}
void mul(complex &a,string s)
{
    int k;
    k=s.length();
    int f[3]={0};
    int num=0,fi=0;
            for(int i=0;i<k;i++)
            {
                if(s[i]=='+') f[num]=1,num++;
                else if(s[i]=='-') f[num]=-1,num++;
                else if(s[i]=='i') fi=1;
            }
            if(num==0)
            {
                if(fi==0) 
                {
                    for(int i=0;i<k;i++)
                        a.r=a.r*10+s[i]-'0';
                    a.v=0;
                }
                else if(fi==1)
                {
                    if(s=="i") a.v=1;
                    else 
                    {
                        for(int i=0;i<k-1;i++)
                            a.v=a.v*10+s[i]-'0';
                    }
                    a.r=0;
                }
            }
            else if(num==1)
            {
                if(fi==0)
                {
                    if(s=="-i")
                        a.v=-1;
                    else
                    {
                        for(int i=0;i<k-1;i++)
                            a.v=a.v*10+s[i]-'0';     
                    }
                    a.v=a.v*(-1);
                    a.r=0;
                }
                else if(f[0]==1)
                {
                    int j=0;
                    while(s[j]!='+') 
                    {
                        a.r=a.r*10+s[j]-'0';
                        j++;
                    }
                    j++;
                    if(s[j]=='i') a.v=1;
                    else 
                    {
                        while(s[j]!='i')
                        {
                            a.v=a.v*10+s[j]-'0';
                            j++;
                        }
                    }
                }
                else if(s[0]=='-')
                {
                    if(s=="-i") a.v=-1;
                    else
                    {
                        int j=1;
                        while(s[j]!='i')
                        {
                            a.v=a.v*10+s[j]-'0'; 
                            j++;
                        }
                        a.v=a.v*(-1);
                    }
                    a.r=0;
                }
                else
                {
                    int j=0;
                    while(s[j]!='-')
                    {
                        a.r=a.r*10+s[j]-'0';
                        j++;
                    }
                    j++;
                    if(s[j]=='i') a.v=-1;
                    else
                    {
                    
                        while(s[j]!='i')
                        {
                            a.v=a.v*10+s[j]-'0';
                            j++;
                        }
                        a.v=a.v*(-1);
                    }
                }
            }
            else if(num==2)
            {
                int j=1;
                if(s[k-2]=='-') 
                {
                        while(s[j]!='-')
                    {
                        a.r=a.r*10+s[j]-'0';
                        j++;
                    }
                    a.r=-a.r;
                    a.v=-1;
                }
                else if(s[k-2]=='+')
                {
                    while(s[j]!='+')
                    {
                        a.r=a.r*10+s[j]-'0';
                        j++;
                    }
                    a.r=-a.r;
                    a.v=1;
                }
                else
                {
                    while(s[j]!='-'&&s[j]!='+')
                    {
                        a.r=a.r*10+s[j]-'0';
                        j++;
                    }
                    j++;
                    while(s[j]!='i')
                    {
                        a.v=a.v*10+s[j]-'0';
                        j++; 
                    }
                    a.r=a.r*(-1);
                    a.v=a.v*f[1];
                }
        }
}

int main()
{
    complex a,b;
    string str1,str2;
    while(1)
    {
        cin>>str1;
        if(str1=="0") break;
        mul(a,str1);
        b=a+b;
        a.set();
    }
    b.display();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值