鱼C论坛

 找回密码
 立即注册
查看: 1890|回复: 1

[已解决]求大神解救c++的一个复数加和输出程序

[复制链接]
发表于 2017-4-6 20:55:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
//定义描述复数的类型类型Complex,编写加法成员函数Add()完成两个复数的加法运算。

//在主函数中定义复数类型变量sum与数组c[5]。用成员函数Input()给数组c的5个元素输入复数值,

//并调用Add()函数完成sum=c[0]+c[1]+c[2]+c[3]+c[4]的操作,最后利用成员函数display()输出数组c的5个复数值,

//及其复数sum的值。
#include<iostream>
using namespace std;
class Complex
{
public:
    void Input();//输入复数函数的声明
    void Add();//加和函数的声明
    void Display();//输复数函数的声明
private:
    double c[5];
    double real;//实部
    double image;//虚部
};
void Complex::Input()//类外定义公有输入函数
{
    cin>>real>>image;
}
void Complex::Add()//类外定义公有加和函数
{
    int sum1,sum2;//定义实部的总和um1和虚部的总加和sum2
    sum1=c[o].real+c[1].real+c[2].real+c[3].real+c[4].real;
    sum2==c[o].image+c[1].image+c[2].image+c[3].image+c[4].image;
}
void Complex::Display()
{
    cout<<c[1]<<endl;

}
int main()
{
cout<<"请输入复数的数字部分(不输入i)"<<endl;
    int i;
    int sum1,sum2;
    Complex c[5];
    Complex t;
    for(i=0;i<5;i++)//
    {
        c[i].Input();
    }

         t.Add();

     cout<<"结果如下:"<<endl;
         for(i=0;i<5;i++)
      {
          c[i].Display();
      }
      cout<<"sum ="<<sum1<<"+"<<sum2<<"i"<<endl;//输出复数的加和结果
      return 0;

}
最佳答案
2017-4-7 11:21:54
#include<iostream>
using namespace std;

class  Complex
{
        float Real, Image;
public:
        Complex(float r = 0, float i = 0)        { Real = r; Image = i; }
        void Show()
        {
                cout << "Real=" << Real << '\t' << "Image=" << Image << '\n';
        }
        friend Complex operator *(Complex &, Complex &);
        Complex operator /(Complex &);                        //重载运算符
        friend Complex operator -(Complex &, Complex &);
        Complex operator +(Complex &);
        void display();
};
Complex operator *(Complex &c1, Complex  &c2)
{
        Complex  t;
        t.Real = c1.Real * c2.Real - c1.Image * c2.Image;
        t.Image = c1.Image*c2.Real + c1.Real* c2.Image;
        return t;
}

Complex Complex::operator /(Complex &c)
{
        Complex t;
        t.Real = (Real *c.Real + Image * c.Image) / (c.Real*c.Real + c.Image * c.Image);
        t.Image = (Image *c.Real - Real * c.Image) / (c.Real*c.Real + c.Image * c.Image);
        return t;
}

Complex operator -(Complex &c1, Complex &c2)
{
        Complex t;
        t.Real = c1.Real - c2.Real;
        t.Image = c1.Image - c2.Image;
        return t;
}

Complex Complex::operator +(Complex &c)
{
        Complex t;
        t.Real = c.Real + Real;
        t.Image = c.Image + Image;
        return t;
}

void Complex::display()
{
        cout << Real << "+" << Image << "i" << endl;
}

int main()
{
        Complex c1(1, 2), c2(3, 4), c3, c4, c5, c6;
        cout << "设c1=1+2i,c2=3+4i\n";
        c3 = c1 + c2;
        cout << "c3 = c1 + c2 = ";
        c3.display();
        c4 = c1 - c2;
        cout << "c4 = c1 - c2 = ";
        c4.display();
        c5 = c1*c2;
        cout << "c5 = c1 * c2 = ";
        c5.display();
        c6 = c1 / c2;
        cout << "c6 = c1 / c2 = ";
        c6.display();
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-7 11:21:54 | 显示全部楼层    本楼为最佳答案   
#include<iostream>
using namespace std;

class  Complex
{
        float Real, Image;
public:
        Complex(float r = 0, float i = 0)        { Real = r; Image = i; }
        void Show()
        {
                cout << "Real=" << Real << '\t' << "Image=" << Image << '\n';
        }
        friend Complex operator *(Complex &, Complex &);
        Complex operator /(Complex &);                        //重载运算符
        friend Complex operator -(Complex &, Complex &);
        Complex operator +(Complex &);
        void display();
};
Complex operator *(Complex &c1, Complex  &c2)
{
        Complex  t;
        t.Real = c1.Real * c2.Real - c1.Image * c2.Image;
        t.Image = c1.Image*c2.Real + c1.Real* c2.Image;
        return t;
}

Complex Complex::operator /(Complex &c)
{
        Complex t;
        t.Real = (Real *c.Real + Image * c.Image) / (c.Real*c.Real + c.Image * c.Image);
        t.Image = (Image *c.Real - Real * c.Image) / (c.Real*c.Real + c.Image * c.Image);
        return t;
}

Complex operator -(Complex &c1, Complex &c2)
{
        Complex t;
        t.Real = c1.Real - c2.Real;
        t.Image = c1.Image - c2.Image;
        return t;
}

Complex Complex::operator +(Complex &c)
{
        Complex t;
        t.Real = c.Real + Real;
        t.Image = c.Image + Image;
        return t;
}

void Complex::display()
{
        cout << Real << "+" << Image << "i" << endl;
}

int main()
{
        Complex c1(1, 2), c2(3, 4), c3, c4, c5, c6;
        cout << "设c1=1+2i,c2=3+4i\n";
        c3 = c1 + c2;
        cout << "c3 = c1 + c2 = ";
        c3.display();
        c4 = c1 - c2;
        cout << "c4 = c1 - c2 = ";
        c4.display();
        c5 = c1*c2;
        cout << "c5 = c1 * c2 = ";
        c5.display();
        c6 = c1 / c2;
        cout << "c6 = c1 / c2 = ";
        c6.display();
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-28 02:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表