7-3 复数相加 (70 分)

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

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

输入示例

3+2i

2+3i

0

输出示例

5+5i
代码:

#include<iostream>
#include <cmath>
#include<bits/stdc++.h>
using namespace std ;
#define r 0.5772156649
int gcd(int a, int b)
{
    return b==0?b:gcd(b,a%b);
}
class fushu
{
    string word;
    int one;
    int two;
    int io;
public:
    fushu (int s=0,int g=0):one(s),two(g) {}
    friend ostream& operator <<(ostream&os, fushu &s);
    friend istream& operator >>(istream&is, fushu &s);
    fushu operator +(const fushu& op2);
    friend int  display(fushu &s);
};
int display(fushu &s)
{
    int lent = s.word.size();
    if(lent==1&&s.word[0]=='0')
        return 1;
}
ostream& operator <<(ostream&os, fushu &s)
{
    if(s.one&&s.two)
		if(s.two==1||s.two==-1)
			cout<<s.one<<(s.two>0?"+":"-")<<"i"<<endl;
		else
			cout<<s.one<<(s.two>0?"+":"")<<s.two<<"i"<<endl;
	else if(s.one)
		cout<<s.one<<endl;
	else if (s.two)
		if(s.two==1||s.two==-1)
			cout<<(s.two>0?"":"-")<<"i"<<endl;
		else
			cout<<s.two<<"i"<<endl;
	else
		cout<<0<<endl;
    return os;
}

fushu fushu::operator+(const fushu &op2)     //重载加号运算符
{
    return fushu(one+op2.one,two+op2.two);
}
int main()
{
    fushu a1;
    string text;
    int a=0, b=0;
    while(cin >>text)
    {
         int len = text.length();
    if(text.find('i')==-1)//只有实部
        {
            stringstream ss;
            ss << text;
            ss >> a;
           fushu a2(a,0);
           a1=a1+a2;
        }/*5||-5*/
        else if(text.find('+')!=-1)//加号的情况
        {
            int flag=0;
            int i=text.find('+');
            string b1 (text,i+1);//后者
            int lenb = b1.length();
            stringstream ss;
            ss << text;
            ss >> a;
            if(lenb ==1)
                b = 1;
            else
            {
                stringstream sss;
            sss << b1;
            sss >> b;

            }
            fushu a2(a,b);
            a1=a1+a2;
        }/*1+i||-1+i*/
        else if(text[1]!='i'&&text.find('-',1)!=-1)//负号的情况
        {
            int  i=text.find('-',1);
            string b1 (text,i);//后者
            int lenb = b1.length();
            int flag = 0;
            stringstream ss;
            ss << text;
            ss >> a;
            if(lenb ==1)
                b = 1;
            else if(b1=="-i")
                b = -1;
            else
            {
                stringstream sss;
            sss << b1;
            sss >> b;

            }
            fushu a2(a,b);
            a1=a1+a2;
        }/*-1-i||1+-i*/
        else //只有虚部ac
        {
            if(text =="i")
                b = 1;
            else if(text=="-i")
                b = -1;
            else
            {
                stringstream sss;
            sss << text;
            sss >> b;
            }
            fushu a2(0,b);
            a1=a1+a2;
        }
        if(text=="0")
            break;
    }
    cout <<a1 ;
    return 0;
}
/*
3+2i
2+3i
0
*/

另一种解题思路:
通过找+与i的位置通过位置确定给的虚数的类型,再通过s[0],判断是否是负数
还有一种是记录两段连续的数字再通过数字后面有没有i判断是否是虚数。
点赞,关注,加收藏。
滑稽(手动动态)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值