Total members 11890 |It is currently Fri Apr 19, 2024 7:33 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





This Program for Complex numbers operations
    1. Addition .
    2. Substruction .
    3. Multiplication .
    4. Division .
    5. Conjugate.
    6. Angle(theta) .
    7. Modulus.

any problems please send to me a mail :mrgreen: :mrgreen: :mrgreen:
cpp code
//complex numbers .cpp
//infinity group-fci-cu.
//**********************::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<<"\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<<" 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;
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Complex Numbers
 Complex numbers calculator (C++)     -  
 solve the complex numbers and do operations on it     -  
 lesson5: XSD Complex elements     -  
 lesson9: XSD Complex Types Indicators     -  
 lesson6: XSD Complex Empty Elements     -  
 lesson7: XSD Complex Text-Only Elements     -  
 lesson8: XSD Complex Type Mixed Content     -  
 add two 24-bit numbers (sum two numbers)     -  
 The harmonic mean of two numbers is given by:     -  
 The harmonic mean of two numbers is given by     -  



Topic Tags

C++ Math
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com