Switch to full style
C++ code examples
Post a reply

solve the complex numbers and do operations on it

Wed Jan 23, 2013 4:26 pm

C++ program to solve the complex numbers (Addition , Substruction , Multiplication , Division , Contugate , Angle(theta) , Modulus ).
cpp code
//complex numbers .cpp
//
//***** **********::Header files::********** *******

#include<iostream>
#include <windows.h>//for set_color & clear_screen/.
#include<iomanip>// for setw() maipulator .
#include<cmath> //for square root .
#include<windows.h>
using namespace std;

//************::Variables of the program::*************
const double pi=3.14159265358979323846264;// the constant PI
double re1,re2,im1,im2,re3,im3;//(re1,im1)first num(re2,im2)second num(re3,im3)result.
double deno;//for division.
int count;// the counter of your choise (add,subtr,...).
double r1,r2;//the modulus under the root.
char choise,sign1,sign2,i1,i2;//for the question (y/n)/sign of the num +i or -i.

//********* ****::complex structure::*** *******************
struct complex
{
double x,y; //x real , y imagin .
};

//*********::setTextColor fn decleration::*************
void setTextColor(short fgColor, short bgColor);
//******* ***::setCursorPosition fn decleration::******* *******
void setCursorPosition(short row, short col);
//****** *****::clearScreen fn decleration::********* ******
void clearScreen();

int add();
int subtract();
int multiply();
int divide();
int conjugate();
int angle_theta();
int modulus();
int convert_tri();
int convert_pol();
int convert_expon();
int main()
{ //the body of the program.
setTextColor(15,0);
cout<<setw(48)<<"Cairo university\n"<<setw(58)<<"Faculty of compters and information\n";
cout<<"\n"<<setw(58)<<"************************************\n\n";
cout<<setw(55)<<"\"C O M P L E X N U M B E R S\" \n"<<setw(55)<<"*******************************\n\n";
cout<<"this program to solve the complex numbers (Addition , Substruction , Multiplication , Division , Contugate , Angle(theta) , Modulus ).\n\n";
do
{
number1: cout<<"\tEnter the first complex number : ";
cin>>re1>>sign1>>i1>>im1;
if(sign1!='+')
{
if(sign1!='-')
{
cout << "\tOnly + and - are allowed.\n" << endl;
goto number1;
}
}


if(i1!='i')
{
cout << "\tOnly Letter 'i' is allowed.\n" << endl;
goto number1;
}



number2: cout<<"\tEnter the second complex number : ";
cin>>re2>>sign2>>i2>>im2;

if(sign2!='+')
{
if(sign2!='-')
{
cout << "\tOnly + and - are allowed.\n" << endl;
goto number2;
}
}


if(i2!='i')
{
cout << "\tOnly Letter 'i' is allowed.\n" << endl;
goto number2;
}
do
{
menu: clearScreen();
cout<<endl<<setw(45)<<"\"MAIN MENU\"\n";
cout<<setw(45)<<"===========\n";
cout<<"1) Addition"<<setw(45)<<"2) Subtraction\n";
cout<<"3) Multiplication"<<setw(36)<<"4) Division\n";
cout<<"5) Conjugate"<<setw(45)<<"6) Angle(theta)\n";
cout<<"7) Modulus"<<setw(66)<<"8) Transform to trigenometric form\n";
cout<<"9) Transform to polar form"<<setw(49)<<"10) Transform to Exponential form\n";
cout<<"Your choise = ";
cin>>count;
clearScreen();
switch(count)
{
case 1://Addition
cout<<endl<<setw(45)<<"\"Addition\"\n\n";
add();
break;
case 2://Subtraction
cout<<endl<<setw(46)<<"\"Subtraction\"\n\n";
subtract();
break;
case 3://Multiplication
cout<<endl<<setw(48)<<"\"Multiplication\"\n\n";
multiply();
break;
case 4://Division
cout<<endl<<setw(45)<<"\"Division\"\n\n";
divide();
break;
case 5://Conjugate
cout<<endl<<setw(45)<<"\"Conjugate\"\n\n";
conjugate();
break;
case 6://Angle(theta)
cout<<endl<<setw(47)<<"\"Angle(theta)\"\n\n";
angle_theta();
break;
case 7://Modulus
cout<<endl<<setw(45)<<"\"Modulus\"\n\n";
modulus();
break;
case 8://Transform to trigenometric form
cout<<endl<<setw(56)<<"\"Transform to trigenometric form\"\n\n";
convert_tri();
break;
case 9://Transform to polar form
cout<<endl<<setw(52)<<"\"Transform to polar form\"\n\n";
convert_pol();
break;
case 10://Transform to Exponential form
cout<<endl<<setw(55)<<"\"Transform to Exponential form\"\n\n";
convert_expon();
break;
default ://when the user enter anumber more than 10 .
cout<<setw(46)<<"\"Out of Menu\"\n\n";
cout<<"\t\tError ! enter from 1 to 10 only \n\n";
goto menu;
}
cout<<"Do you want to perform another operation on your numbrs?(y/n)";
cin>>choise;//to do another operation or not.
}while(choise=='y'||choise=='Y');
cout<<"Do you want to enter anew numbers?(y/n)";
cin>>choise;//to enter anew numbers or not.
clearScreen();
}while(choise=='y'||choise=='Y');
return 0;
}

//**** ******::setTextColor fn defination::******* *************
void setTextColor(short fgColor, short bgColor)
{
HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute ( hConsole, fgColor | (bgColor << 4) );
}

//*** **::setCursorPosition fn defination::******** **********
void setCursorPosition(short row, short col)
{
HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
COORD pos = {row, col};
SetConsoleCursorPosition ( hConsole, pos );
}

//****** **::clearScreen fn defination::***** *********
void clearScreen()
{
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
COORD topLeft = {0,0};
FillConsoleOutputCharacter(hConsole, ' ', 25*80, topLeft, NULL);
setCursorPosition(0, 0);
}
int add()
{
if(sign1=='-')
im1*=-1;
if(sign2=='-')
im2*=-1;
re3=re1+re2;
im3=im1+im2;
if(im3<0)
cout<<"\n\tZ1 + Z2 = "<<re3<<" - i "<<im3/-1<<endl;
else
cout<<"\n\tZ1 + Z2 = "<<re3<<" + i "<<im3<<endl;
return 0;
}
int subtract()
{
if(sign1=='-')
im1*=-1;
if(sign2=='-')
im2*=-1;
re3=re1-re2;
im3=im1-im2;
if(im3<0)
cout<<"\n\tZ1 - Z2 = "<<re3<<" - i "<<im3/-1<<endl;
else
cout<<"\n\tZ1 - Z2 = "<<re3<<" + i "<<im3<<endl;
return 0;
}
int multiply()
{
if(sign1=='-')
im1*=-1;
if(sign2=='-')
im2*=-1;
re3=(re1*re2)-(im1*im2);
im3=(re1*im2)+(re2*im1);
if(im3<0)
cout<<"\n\tZ1 * Z2 = "<<re3<<" - i "<<im3/-1<<endl;
else
cout<<"\n\tZ1 * Z2 = "<<re3<<" + i "<<im3<<endl;
return 0;
}
int divide()
{
if(sign1=='+')
{
if(sign2=='+')
{
re3=(re1*re2)+(im1*im2);
im3=(-re1*im2)+(re2*im1);
}
if(sign2=='-')
{
re3=(re1*re2)-(im1*im2);
im3=(re1*im2)+(re2*im1);
}
}
if(sign1=='-')
{
if(sign2=='-')
{
re3=(re1*re2)+(im1*im2);
im3=(re1*im2)-(re2*im1);
}
if(sign2=='+')
{
re3=(re1*re2)-(im1*im2);
im3=(-re1*im2)+(-re2*im1);
}
}
deno=(re2*re2)+(im2*im2);

if(im3<0)
cout<<"\n\tZ1 / Z2 = "<<re3<<"/"<<deno<<" - i "<<im3/-1<<"/"<<deno<<endl;
else
cout<<"\n\tZ1 / Z2 = "<<re3<<"/"<<deno<<" + i "<<im3<<"/"<<deno<<endl;
return 0;
}
int conjugate()
{
if(sign1=='-')
{
sign1='+';
cout<<"\tThe conjugate of the first number = "<<re1<<sign1<<im1<<endl;
}
else
{
sign1='-';
cout<<"\tThe conjugate of the first number = "<<re1<<sign1<<i1<<im1<<endl;
}
if(sign2=='-')
{
sign2='+';
cout<<"\tThe conjugate of the first number = "<<re2<<sign2<<i2<<im2<<endl;
}
else
{
sign2='-';
cout<<"\tThe conjugate of the first number = "<<re2<<sign2<<im2<<endl;
}

return 0;
}
int angle_theta()
{
if(sign1=='-')
im1*=-1;
if(sign2=='-')
im2*=-1;
float g,h;
cout<<"\tThe angle of the first number\xE9 = ";
g=atan(im1/re1);
g=(g*180)/pi;
if(g<0)
g*=-1;
if(re1>0&&im1>0)
g=g;
else if(re1<0&&im1>=0)
g=180-g;
else if(re1<0&&im1<0)
g=180+g;
else if(re1>=0&&im1>0)
g=360-g;
cout<<g<<endl;
cout<<"\tThe angle of the second number\xE9 = ";
h=atan(im2/re2);
h=(h*180)/pi;
if(h<0)
h*=-1;
else if(re2>0&&im2>0)
h=h;
else if(re2<0&&im2>0)
h=180-h;
else if(re2<0&&im2<0)
h=180+h;
else if(re2>0&&im2>0)
h=360-h;
cout<<h<<endl;
return 0;
}
int modulus()
{
if(sign1=='-')
im1*=-1;
if(sign2=='-')
im2*=-1;
cout<<"\tThe modulus of the first number (r)= ";
r1=((re1*re1)+(im1*im1));
cout<<"root"<<(r1)<<endl;
cout<<"\tThe modulus of the second number (r)= ";
r2=((re2*re2)+(im2*im2));
cout<<"root"<<(r2)<<endl;
return 0;
}
int convert_tri()
{
float g,h;
if(sign1=='-')
im1*=-1;
if(sign2=='-')
im2*=-1;
r1=((re1*re1)+(im1*im1));
r2=((re2*re2)+(im2*im2));
g=atan(im1/re1);
g=(g*180)/pi;
if(g<0)
g*=-1;
if(re1>0&&im1>0)
g=g;
else if(re1<0&&im1>=0)
g=180-g;
else if(re1<0&&im1<0)
g=180+g;
else if(re1>=0&&im1>0)
g=360-g;
h=atan(im2/re2);
h=(h*180)/pi;
if(h<0)
h*=-1;
else if(re2>0&&im2>0)
h=h;
else if(re2<0&&im2>0)
h=180-h;
else if(re2<0&&im2<0)
h=180+h;
else if(re2>0&&im2>0)
h=360-h;
cout<<"the first number = "<<"root"<<r1<<"(cos "<<g<<" + "<<"i"<<" sin "<<g<<" ).\n";
cout<<"the second number = "<<"root"<<r2<<"(cos "<<h<<" + "<<"i"<<" sin "<<h<<" ).\n";
return 0;
}
int convert_pol()
{
float g,h;
if(sign1=='-')
im1*=-1;
if(sign2=='-')
im2*=-1;
r1=((re1*re1)+(im1*im1));
r2=((re2*re2)+(im2*im2));
g=atan(im1/re1);
g=(g*180)/pi;
if(g<0)
g*=-1;
if(re1>0&&im1>0)
g=g;
else if(re1<0&&im1>=0)
g=180-g;
else if(re1<0&&im1<0)
g=180+g;
else if(re1>=0&&im1>0)
g=360-g;
h=atan(im2/re2);
h=(h*180)/pi;
if(h<0)
h*=-1;
else if(re2>0&&im2>0)
h=h;
else if(re2<0&&im2>0)
h=180-h;
else if(re2<0&&im2<0)
h=180+h;
else if(re2>0&&im2>0)
h=360-h;
cout<<"the first number = "<<"root"<<r1<<"<"<<g<<endl;
cout<<"the second number = "<<"root"<<r2<<"<"<<h<<endl;
return 0;
}
int convert_expon()
{
float g,h;
if(sign1=='-')
im1*=-1;
if(sign2=='-')
im2*=-1;
r1=((re1*re1)+(im1*im1));
r2=((re2*re2)+(im2*im2));
g=atan(im1/re1);
g=(g*180)/pi;
if(g<0)
g*=-1;
if(re1>0&&im1>0)
g=g;
else if(re1<0&&im1>=0)
g=180-g;
else if(re1<0&&im1<0)
g=180+g;
else if(re1>=0&&im1>0)
g=360-g;
h=atan(im2/re2);
h=(h*180)/pi;
if(h<0)
h*=-1;
else if(re2>0&&im2>0)
h=h;
else if(re2<0&&im2>0)
h=180-h;
else if(re2<0&&im2<0)
h=180+h;
else if(re2>0&&im2>0)
h=360-h;
cout<<"the first number = "<<"root"<<r1<<"e^i"<<g<<endl;
cout<<"the second number = "<<"root"<<r2<<"e^i"<<h<<endl;
return 0;
}




Post a reply
  Related Posts  to : solve the complex numbers and do operations on it
 Complex Numbers     -  
 Complex numbers calculator (C++)     -  
 I need you to help me to solve this program by c++ language     -  
 how to solve that isssue i m nt able to connect cam and code     -  
 use one dimensional array to solve sales commissions     -  
 the text file i saved all in one line. How can i solve it?     -  
 Program to solve equations using double linked list     -  
 lesson5: XSD Complex elements     -  
 lesson7: XSD Complex Text-Only Elements     -  
 lesson9: XSD Complex Types Indicators     -  

Topic Tags

C++ Math