📄 complexmodified.cpp
字号:
/*******************PROGRAM TO DO FOLLOWINGS**************************
* TAKE INPUT OF TWO COMPLEX NUMBERS *
* ASKING TO ADD,SUBTRACT OR TAKE CONJUGATE OF THEM *
* FINDING THEIR MAGNITUDE AND CALCULATING ANGLE IN DEGREES *
* MULTIPLY TWO COMPLEX NUBERS *
*********************************************************************/
#include <iostream.h>
#include <math.h>
class Complex
{
private:
double real,imaginary;
public:
Complex add(Complex c);
Complex subtract(Complex c);
Complex conjugate();
Complex multiply(Complex);
double magnitude();
double angle();
//Setters functions
void set_real(double r);
void set_imaginary(double i);
//Getters functions
double get_real();
double get_imaginary();
};
/******************Defining add function****************************/
Complex Complex :: add(Complex c)
{
class Complex c1;
c1.real = real + c.real;
c1.imaginary = imaginary + c.imaginary;
return c1;
}
/******************Defining subtract function********************/
Complex Complex :: subtract(Complex c)
{
class Complex c1;
c1.real = real - c.real;
c1.imaginary = imaginary - c.imaginary;
return c1;
}
/******************Defining conjugate function********************/
Complex Complex :: conjugate()
{
Complex c;
if((imaginary < 0) || (imaginary > 0))
imaginary = -imaginary;
c.real = real;
c.imaginary = imaginary;
return c;
}
/*****************Defining multiply functions*******************/
Complex Complex :: multiply(Complex c)
{
Complex c3;
c3.real = (real*c.real) - (imaginary*c.imaginary);
c3.imaginary = (real*c.imaginary) + (imaginary*c.real);
return c3;
}
/*****************Defining magnitude function*****************/
double Complex :: magnitude()
{
double mag;
mag = sqrt((pow(real,2))+ (pow(imaginary,2)));
return mag;
}
/*****************Defining angle function********************/
double Complex :: angle()
{
double ang;
ang = atan2(real,imaginary);
ang *= 180/3.14;
return ang;
}
/*****************Defining set_real function*****************/
void Complex :: set_real(double r)
{
real = r;
}
/****************Defining set_imaginary function************/
void Complex :: set_imaginary(double i)
{
imaginary = i;
}
/****************Defining get_real fumction****************/
double Complex :: get_real()
{
return real;
}
/****************Defining get_imaginary function***********/
double Complex :: get_imaginary()
{
return imaginary;
}
/*****************Defining main******************************/
int main (void)
{
class Complex c1,c2,c3;
double r1,r2,i1,i2;
char c,op = ' ' ,judge='0';
//Taking input
cout <<"Please Enter Your Two Complex Numbers (format:- real imaginary)" << endl;
cin >> r1 >> i1 >> r2 >> i2;
//Setting values
c1.set_real(r1);
c1.set_imaginary(i1);
c2.set_real(r2);
c2.set_imaginary(i2);
//Asking for task to perform
cout << "Press small 'a' for addition,'s' for subtraction and 'c' for conjugate" <<endl;
cin >> c;
if(c == 'a')
{
c3 = c1.add(c2);
}
else if (c == 's')
{
c3 = c1.subtract(c2);
}
else if(c == 'c')
{
c3 = c1.conjugate();
}
else
{
cout <<"KHAYA PIYA KUCH NAI, GLASS TORA BARA ANA" <<endl;
judge = '1';
}
if (c3.get_imaginary() >= 0)
op = '+';
if(judge == '0')
cout << "The output of your input gives" << endl<< c3.get_real() << op << c3.get_imaginary() << "i"<<endl;
//Displying magnitude and angle (in degrees)
cout << "The magnitude and angle of 1st comlplex number are\n" << c1.magnitude() << "\n" << c1.angle()<<endl;
cout << "The magnitude and angle of 2nd comlplex number are\n" << c2.magnitude()<< "\n" << c2.angle()<<endl;
c3 = c1.multiply(c2);
if(c3.get_imaginary() >=0)
op = '+';
cout << "The multiplication of your numbers give\n" << c3.get_real() << op<<c3.get_imaginary()<<"i"<<endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -