设计一个c++程序,实现两个复数相加。
3个回答
展开全部
#include "iostream.h"
typedef struct
{
float real;
float imag;
}complex;\\先定义一个复数型变量
float a=0,b=0;
void complex_add()
{\\定义复数相加的函数
complex z1,z2;
cout<<"请输入第一个复数:"<<endl;
cin>>z1.real>>z1.imag;
if(z1.imag<0)
cout<<"第一个复数为z1="<<z1.real<<z1.imag<<"j"<<endl;
if(z1.imag==0)
cout<<"第一个复数为z1="<<z1.real<<endl;
if(z1.imag>0)
cout<<"第一个复数为z1="<<z1.real<<"+"<<z1.imag<<"j"<<endl;
cout<<endl;
cout<<endl;
cout<<"请输入第二个复数:"<<endl;
cin>>z2.real>>z2.imag;
if(z2.imag<0)
cout<<"第二个复数为z2="<<z2.real<<z2.imag<<"j"<<endl;
if(z2.imag==0)
cout<<"第二个复数为z2="<<z2.real<<endl;
if(z2.imag>0)
cout<<"第二个复数为z2="<<z2.real<<"+"<<z2.imag<<"j"<<endl;
a=z1.real+z2.real;
b=z1.imag+z2.imag;
}
void main()
{
complex_add();
cout<<"两个复数之和为:"<<endl;
if(b<0)
cout<<"z="<<a<<b<<"j"<<endl;
if(b==0)
cout<<"z="<<a<<endl;
if(b>0)
cout<<"z="<<a<<"+"<<b<<"j"<<endl;
}
typedef struct
{
float real;
float imag;
}complex;\\先定义一个复数型变量
float a=0,b=0;
void complex_add()
{\\定义复数相加的函数
complex z1,z2;
cout<<"请输入第一个复数:"<<endl;
cin>>z1.real>>z1.imag;
if(z1.imag<0)
cout<<"第一个复数为z1="<<z1.real<<z1.imag<<"j"<<endl;
if(z1.imag==0)
cout<<"第一个复数为z1="<<z1.real<<endl;
if(z1.imag>0)
cout<<"第一个复数为z1="<<z1.real<<"+"<<z1.imag<<"j"<<endl;
cout<<endl;
cout<<endl;
cout<<"请输入第二个复数:"<<endl;
cin>>z2.real>>z2.imag;
if(z2.imag<0)
cout<<"第二个复数为z2="<<z2.real<<z2.imag<<"j"<<endl;
if(z2.imag==0)
cout<<"第二个复数为z2="<<z2.real<<endl;
if(z2.imag>0)
cout<<"第二个复数为z2="<<z2.real<<"+"<<z2.imag<<"j"<<endl;
a=z1.real+z2.real;
b=z1.imag+z2.imag;
}
void main()
{
complex_add();
cout<<"两个复数之和为:"<<endl;
if(b<0)
cout<<"z="<<a<<b<<"j"<<endl;
if(b==0)
cout<<"z="<<a<<endl;
if(b>0)
cout<<"z="<<a<<"+"<<b<<"j"<<endl;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询