Question

EXPLAIN HOW THIS PROGRAM WORKS, USING DEV C++ #include <iostream> #include <cmath> #define PI 3.1415926535897 using...

EXPLAIN HOW THIS PROGRAM WORKS, USING DEV C++

#include <iostream>
#include <cmath>

#define PI 3.1415926535897

using namespace std;

typedef struct ComplexRectStruct {
   double real;
   double imaginary;
} ComplexRect;

typedef struct ComplexPolarStruct {
   double magnitude;
   double angle;
} ComplexPolar;

double radToDegree (double rad) {
   return (180.00 * rad) / PI;
}

double degToRadian (double deg) {
   return (deg * PI) / 180;
}


ComplexPolar toPolar (ComplexRect r) {
   ComplexPolar p;
   p.magnitude = round(sqrt(pow(r.real, 2.0) + pow(r.imaginary, 2.0)));
   p.angle = round(radToDegree(atan(r.imaginary / r.real)));
  
   return p;
}


ComplexRect toRect (ComplexPolar p) {
   ComplexRect r;
   r.real = round(p.magnitude * cos(degToRadian(p.angle)));
   r.imaginary = round(p.magnitude * sin(degToRadian(p.angle)));
  
   return r;
}

void displayRect (ComplexRect r) {
   cout << r.real << " + " << r.imaginary << "i";
}

void displayPolar (ComplexPolar p) {
   cout << p.magnitude << "(cos(" << p.angle << (char) 248 << ") + isin(" << p.angle << (char) 248 << "))";
}

void addComplexRect(ComplexRect *pC1, ComplexRect *pC2, ComplexRect *pSum) {
   pSum->real = pC1->real + pC2->real;
   pSum->imaginary = pC1->imaginary + pC2->imaginary;
}

void multComplexRect(ComplexRect *pC1, ComplexRect *pC2, ComplexRect *pProd) {
   pProd->real = (pC1->real * pC2->real) - (pC1->imaginary * pC2->imaginary);
   pProd->imaginary = (pC1->real * pC2->imaginary) + (pC1->imaginary * pC2->real);
}

int main () {
   ComplexRect r0, r1, r2;
   ComplexPolar p0, p1, p2;
   int operation_done = 0, choice0, is_polar = 0;
  
   r0.real = 1;
   r0.imaginary = 2;
  
   r1.real = 3;
   r1.imaginary = 4;
  
   r2.real = 1;
   r2.imaginary = 1;
  
   p0 = toPolar(r0);
   p1 = toPolar(r1);
   p2 = toPolar(r2);
  
   do {
       system("cls");
       cout << "1 Edit the First Complex Number" << endl;
       cout << "2 Edit the Second Complex Number" << endl;
       cout << "3 Addition" << endl;
       cout << "4 Multiplication" << endl;
       cout << "5 Display All" << endl;
       if (is_polar)
           cout << "[6] Change Form: Rectangular" << endl;
       else
           cout << "[6] Change Form: Polar" << endl;
       cout << "[7] Exit" << endl;
       cout << ">> ";
       cin >> choice0;
      
       cout << endl;
       switch (choice0) {
           case 1:
               if (!is_polar) {
                   cout << "Real Part (Current: " << r0.real << "): ";
                   cin >> r0.real;
              
                   cout << "Imaginary Part (Current: " << r0.imaginary << "): ";
                   cin >> r0.imaginary;
                  
                   p0 = toPolar(r0);
               } else {
                   cout << "Magnitude Part (Current: " << p0.magnitude << "): ";
                   cin >> p0.magnitude;
              
                   cout << "Angle Part (Current: " << p0.angle << (char) 248 << "): ";
                   cin >> p0.angle;
                  
                   r0 = toRect(p0);
               }
              
               cout << "Note: Succesfully updated the first complex number." << endl;
               system("pause");
               break;
           case 2:
               if (!is_polar) {
                   cout << "Real Part (Current: " << r1.real << "): ";
                   cin >> r1.real;
              
                   cout << "Imaginary Part (Current: " << r1.imaginary << "): ";
                   cin >> r1.imaginary;
                  
                   p1 = toPolar(r1);
               } else {
                   cout << "Magnitude Part (Current: " << p1.magnitude << "): ";
                   cin >> p1.magnitude;
              
                   cout << "Angle Part (Current: " << p1.angle << (char) 248 << "): ";
                   cin >> p1.angle;
                  
                   r1 = toRect(p1);
               }
              
               cout << "Note: Succesfully updated the second complex number." << endl;
               system("pause");
               break;
           case 3:
               operation_done = 1;
               addComplexRect(&r0, &r1, &r2);
               p2 = toPolar(r2);
              
               cout << "Note: Succesfully added the first and second complex number." << endl;
               system("pause");
               break;
           case 4:
               operation_done = 1;
               multComplexRect(&r0, &r1, &r2);
               p2 = toPolar(r2);
              
               cout << "Note: Succesfully multiplied the first and second complex number." << endl;
               system("pause");
               break;
           case 5:
               cout << "Complex Number #1: ";
               if (!is_polar)
                   displayRect(r0);
               else
                   displayPolar(p0);
               cout << endl;
                  
               cout << "Complex Number #2: ";
               if (!is_polar)
                   displayRect(r1);
               else
                   displayPolar(p1);
               cout << endl;
              
               if (operation_done) {
                   cout << "Complex Number #3: ";
               if (!is_polar)
                   displayRect(r2);
               else
                   displayPolar(p2);
                   cout << endl;
               }
              
               system("pause");
               break;
           case 6:
               is_polar = !is_polar;
               break;
           case 7: break;
           default:
               cout << "Invalid Option" << endl;
               system("pause");
       }
   } while (choice0 != 7);
  
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This program is basically used to calculate operation on Complex number using object oriented way - I have added comment through the code for your explaination pls check the code via comments

#include <iostream>

#include <cmath>

//declare the constant value of pie

#define PI 3.1415926535897

using namespace std;

//complex number rectangle structure define

typedef struct ComplexRectStruct {

double real;

double imaginary;

} ComplexRect;

//complex number polar structure define

typedef struct ComplexPolarStruct {

double magnitude;

double angle;

} ComplexPolar;

//function to convert radian to degree

double radToDegree (double rad) {

return (180.00 * rad) / PI;

}

//function to convert degree to radian

double degToRadian (double deg) {

return (deg * PI) / 180;

}

//function converts complex number rectangle to complex number polar

//this take ComplexRect struct as input parameter and return ComplexPolar number

ComplexPolar toPolar (ComplexRect r) {

//variable declare complex polar

ComplexPolar p;

//calculate the magnitude and angle

p.magnitude = round(sqrt(pow(r.real, 2.0) + pow(r.imaginary, 2.0)));

p.angle = round(radToDegree(atan(r.imaginary / r.real)));

//return the calculate value

return p;

}


//function converts complex number polar to complex number rectangle

//this take ComplexPolar struct as input parameter and return ComplexRect number

ComplexRect toRect (ComplexPolar p) {

ComplexRect r;

r.real = round(p.magnitude * cos(degToRadian(p.angle)));

r.imaginary = round(p.magnitude * sin(degToRadian(p.angle)));

return r;

}

//function to display complex rect this take complex rect as input parameter

void displayRect (ComplexRect r) {

cout << r.real << " + " << r.imaginary << "i";

}

//function to display complex polar this take complex polar as input parameter

void displayPolar (ComplexPolar p) {

cout << p.magnitude << "(cos(" << p.angle << (char) 248 << ") + isin(" << p.angle << (char) 248 << "))";

}

//function to add 2 complex number rect

void addComplexRect(ComplexRect *pC1, ComplexRect *pC2, ComplexRect *pSum) {

pSum->real = pC1->real + pC2->real;

pSum->imaginary = pC1->imaginary + pC2->imaginary;

}

//function to multiply 2 complex number rect

void multComplexRect(ComplexRect *pC1, ComplexRect *pC2, ComplexRect *pProd) {

pProd->real = (pC1->real * pC2->real) - (pC1->imaginary * pC2->imaginary);

pProd->imaginary = (pC1->real * pC2->imaginary) + (pC1->imaginary * pC2->real);

}

//driver function , execution start from here

int main () {

//decalre ComplexRect struct variable

ComplexRect r0, r1, r2;

//decalre ComplexPolar struct variable

ComplexPolar p0, p1, p2;

int operation_done = 0, choice0, is_polar = 0;

//declare initial value to ComplexRect struct variable

r0.real = 1;

r0.imaginary = 2;

r1.real = 3;

r1.imaginary = 4;

r2.real = 1;

r2.imaginary = 1;

//converting to polar complex struct using toPolar method

p0 = toPolar(r0);

p1 = toPolar(r1);

p2 = toPolar(r2);

//menu driven program

do {

//ask user for his choice to perform operation

system("cls");

cout << "1 Edit the First Complex Number" << endl;

cout << "2 Edit the Second Complex Number" << endl;

cout << "3 Addition" << endl;

cout << "4 Multiplication" << endl;

cout << "5 Display All" << endl;

if (is_polar)

cout << "[6] Change Form: Rectangular" << endl;

else

cout << "[6] Change Form: Polar" << endl;

cout << "[7] Exit" << endl;

cout << ">> ";

cin >> choice0;

cout << endl;

//put user choice in a switch case

switch (choice0) {

//case 1 Edit the First Complex Number

case 1:

if (!is_polar) {

//take the value from user and replace it

cout << "Real Part (Current: " << r0.real << "): ";

cin >> r0.real;

cout << "Imaginary Part (Current: " << r0.imaginary << "): ";

//take the value from user and replace it

cin >> r0.imaginary;

p0 = toPolar(r0);

} else {

cout << "Magnitude Part (Current: " << p0.magnitude << "): ";

//take the value from user and replace it

cin >> p0.magnitude;

cout << "Angle Part (Current: " << p0.angle << (char) 248 << "): ";

//take the value from user and replace it

cin >> p0.angle;

r0 = toRect(p0);

}

cout << "Note: Succesfully updated the first complex number." << endl;

system("pause");

break;

//this is to Edit the Second Complex Number

case 2:

if (!is_polar) {

cout << "Real Part (Current: " << r1.real << "): ";

//take the value from user and replace it

cin >> r1.real;

cout << "Imaginary Part (Current: " << r1.imaginary << "): ";

//take the value from user and replace it

cin >> r1.imaginary;

p1 = toPolar(r1);

} else {

cout << "Magnitude Part (Current: " << p1.magnitude << "): ";

//take the value from user and replace it

cin >> p1.magnitude;

cout << "Angle Part (Current: " << p1.angle << (char) 248 << "): ";

//take the value from user and replace it

cin >> p1.angle;

r1 = toRect(p1);

}

cout << "Note: Succesfully updated the second complex number." << endl;

system("pause");

break;

//perform adddition on complex numver

case 3:

operation_done = 1;

//call function to perform addition

addComplexRect(&r0, &r1, &r2);

//convert addition to polar form

p2 = toPolar(r2);

cout << "Note: Succesfully added the first and second complex number." << endl;

system("pause");

break;

//perform multiplicaiton on complex numbers

case 4:

operation_done = 1;

//call function to perform multiplicaiton

multComplexRect(&r0, &r1, &r2);

//convert to polar form

p2 = toPolar(r2);

cout << "Note: Succesfully multiplied the first and second complex number." << endl;

system("pause");

break;

//diplay all the complex numbers

case 5:

cout << "Complex Number #1: ";

if (!is_polar)

displayRect(r0);

else

displayPolar(p0);

cout << endl;

cout << "Complex Number #2: ";

if (!is_polar)

displayRect(r1);

else

displayPolar(p1);

cout << endl;

if (operation_done) {

cout << "Complex Number #3: ";

if (!is_polar)

displayRect(r2);

else

displayPolar(p2);

cout << endl;

}

system("pause");

break;

//this is change polar to rect and rect to polar

case 6:

is_polar = !is_polar;

break;

case 7: break;

default:

cout << "Invalid Option" << endl;

system("pause");

}

} while (choice0 != 7);

return 0;

}


Add a comment
Know the answer?
Add Answer to:
EXPLAIN HOW THIS PROGRAM WORKS, USING DEV C++ #include <iostream> #include <cmath> #define PI 3.1415926535897 using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Flow chart of this program #include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0,...

    Flow chart of this program #include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0, ave-ee, int locmx, locmn int n; cout <<"Enter the number of students: "<<endl; cin >> ni double listln]; double max-0; double min-e; for (int i-e ; i<n ;i++) cout<s enter the gpa: "<cendli cin>>listli]; while (listlile i listli1>4) cout<s error,Try again "<cendl; cin>listlil: sun+=list[i]; N1 if (listli]>max) for(int isin itt) max=list [i]; 10cmx=1+1 ; else if (list [i] min) min=list [i]; locmn-i+1; ave sum/n;...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

  • Complete the functions for the program complex.c (Down) . The program adds, subtracts,and multiplies complex numbers....

    Complete the functions for the program complex.c (Down) . The program adds, subtracts,and multiplies complex numbers. The C program will prompt the user for the choice of operations,read in the complex numbers, perform the operations, and print the results. The main programrepeatedly prints the menu, reads in the user’s selection, and performs the operation. We use pointers only for those arguments that the function intends to change. In all thefunctions, r represents the real component and i represents the imaginary...

  • This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to...

    This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example listed below) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked...

  • C++

    /*Colesha PearmanCIS247CATM ApplicationMay 4,2020*///Bring in our libaries#include"stdafx.h" #include<iostream>#include<conio.h>#include<string>#include<fstream>//read/write to files#include<ctime>//time(0)#include<iomanip>//setpresision stdusing namespace std; //create constant vaules-- cannot be changedconst int EXIT_VALUE = 5;const float DAILY_LIMIT = 400.0f;const string FILENAME = "Account.txt"; //create balance variabledouble balance = 0.0; //prototypesvoid deposit(double* ptrBalance);void withdrawal(double* ptrBalance, float dailyLimit);  //overloaded method-this verision does not take withdrawal amount void withdrawal(double* ptrBalance, float dailyLimit, float amount);  //overloaded method that takes withdrawal amount ///Enrty point to the application int main(){                 //look for the starting balance; otherwise generate a random balance                 ifstream...

  • PLEASE HELP WITH THE FIX ME'S #include #include #include #include "CSVparser.hpp" using namespace std; //==...

    PLEASE HELP WITH THE FIX ME'S #include #include #include #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; //============================================================================ // Linked-List class definition //============================================================================ /** * Define a class containing data members and methods to *...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType...

    This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *next;    double value; }; void createList(nodeType*& first, nodeType*& last, ifstream& inf); void printList(nodeType* first); int main() {    nodeType *first, *last;    int num;    ifstream infile;    infile.open("InputIntegers.txt");    createList(first, last, infile);    printList(first);    infile.close();    system("pause");    return 0; } void createList(nodeType*& first, nodeType*& last, ifstream& infile) {    int number;...

  • //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;   ...

    //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;    double mpg;    Warranty warranty;    static int numOfVehicles; public:    Vehicle();    Vehicle(string s, int y, double m, Warranty warranty);    Vehicle(Vehicle& v);    ~Vehicle();    string getMake();    int getYear();    double getGasMileage();    void setMake(string s);    void setYear(int y);    void setYear(string y);    void setGasMileage(double m);    void setGasMileage(string m);    void displayVehicle();    static int getNumVehicles();    Warranty getWarranty();    void setWarranty(Warranty& ); }; //Vehicle.cpp #include "Vehicle.h" #include <string> Vehicle::Vehicle() {    make = "unknown";    year =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT