Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*DIRECTIONS Take the notes for the Complex structure and write regular c-type functions that add, subtract,
- and multiply complex numbers. Use the following function prototypes.
- Complex addComplex(Complex x, Complex y);
- Complex subtractComplex(Complex x, Complex y);
- Complex multiplyComplex(Complex x, Complex y);
- If time allows, write a function that divides complex numbers.
- mathew myers
- Template blank Source document*/
- #include<iostream>
- #include<string>
- #include<sstream>
- #include<cmath>
- //globals
- using namespace std;
- struct Complex{
- // Data members that define a complex number a + bi
- double a,b;
- // function within a data structure = METHOD
- // functions specific to this type of object.
- void printComplex(void);
- };
- Complex addComplex(Complex x, Complex y);
- Complex subtractComplex(Complex x, Complex y);
- Complex multiplyComplex(Complex x, Complex y);
- Complex divideComplex(Complex x, Complex y);
- bool runAgain(void);
- void main()
- {
- //Declarations
- do{
- //inputs
- //outputs
- }while( runAgain() );
- }
- void Complex::printComplex(void){
- if( b >= 0 )
- cout << a << " + " << b << "i" << endl;
- else
- cout << a << " - " << -1*b << "i"<< endl;
- }
- Complex addComplex(Complex x, Complex y)
- {
- Complex z;
- z.a = x.a + y.a;
- z.b = x.b + y.b;
- return z;
- }
- Complex subtractComplex(Complex x, Complex y)
- {
- Complex z;
- z.a = x.a - y.a;
- z.b = x.b - y.b;
- return z;
- }
- Complex multiplyComplex(Complex x, Complex y)
- {
- Complex z;
- z.a = ((x.a*y.a)-(x.b*y.b));
- z.b = ((x.a*y.b)+(x.b*y.a));
- return z;
- }
- Complex divideComplex(Complex x, Complex y)
- {
- Complex z;
- Complex temp1;
- Complex temp2;
- temp1 = y;
- temp1.b = temp1.b * -1;
- temp2 = multiplyComplex(y,temp1);
- temp1 = multiplyComplex(x,temp1);
- z.a = temp1.a / temp2.a;
- z.b = temp1.b / temp2.a;
- return z;
- }
- bool runAgain(void){
- char userResponse;
- cout << "\nWould you like to run again (y or n): ";
- cin >> userResponse;
- if(userResponse == 'y')
- return(true);
- return(false);
- }
- /* lazy copy stuff
- if()
- {
- }
- else if()
- {
- }
- else
- {
- }
- for( int i = 0; i < x;i++)
- {
- }
- switch()
- {
- case :
- break;
- case :
- break;
- default:
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement