The code below should follow the inputs entered by the user, however when I "*" I get a different value for the Current Value is. I think this is because of the void multi but I'm a not 100% sure. Any suggestions?
Examples (user input to the right of :)
Option?(C,L,S,+,-,*,/): C
Please input real part: 2
Please input imag part 4
Current Value is 2.00 + 4.00i
Option?(C,L,S,+,-,*,/): +
Please input real part: 4
Please input imag part: 1
Current Value is 6.00 + 5.00i
Option?(C,L,S,+,-,*,/):-
Please input real part: 3
Please input imag part: 1
Current Value is 3.00 + 4.00i
Option?(C,L,S,+,-,*,/): L
Length of current Value is 5.00
Option?(C,L,S,+,-,*,/): *
Please input real part: 0.5
Please input imag part: 1.5
Current Value is -4.50 + 6.50i
Option?(C,L,S,+,-,*,/): /
Please input real part: 0
Please input imag part: 0
Illegal attempt to divide by 0
Current Value is -4.50 + 6.50i
Option?(C,L,S,+,-,*,/): /
Please input real part: 3
Please input imag part: 4
Current Value is 0.50+ 1.50i
Option?(C,L,S,+,-,*,/): S
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#include <iostream>
#include <iomanip>
#include<string>
#define _USE_MATH_DEFINES
#include<math.h>
using namespace std;
void input(double& real, double& imag);
void output(double real, double imag);
double mag(double real, double imag);
void add(double &a, double &b, double c, double d);
void sub(double &a, double &b, double c, double d);
void multi(double &a, double &b, double c, double d);
bool div(double &a, double &b, double c, double d);
int main(void)
{
char choice;
double a, b, c, d;
do
{
cout << "Option?(C, L, S, +, -, *, /): ";
cin >> choice;
if (choice == 'C')
{
input(a, b);
output(a, b);
}
else if (choice == 'L')
{
cout << "Length current Value is " << fixed << setprecision(2) << mag(a, b) << endl;
}
else if (choice == '+')
{
input(c, d);
add(a, b, c, d);
}
else if (choice == '-')
{
input(c, d);
sub(a, b, c, d);
}
else if (choice == '*')
{
input(c, d);
multi(a, b, c, d);
}
else if (choice == '/')
{
input(c, d);
if (!div(a, b, c, d))
cout << "Illegal attempt to divide by 0 " << endl;
}
} while (choice != 'S');
return 0;
}
void input(double& real, double& imag)
{
cout << "Please input real part: ";
cin >> real;
cout << "Please input imag part: ";
cin >> imag;
}
void output(double real, double imag)
{
cout << fixed << setprecision(2) << "Current value is " << real << " + " << imag << "i" << endl;
}
double mag(double real, double imag)
{
return sqrt((real*real) + (imag*imag));
}
void add(double &a, double &b, double c, double d)
{
a = (a + c);
b = (b + d);
output(a, b);
}
void sub(double &a, double &b, double c, double d)
{
a = (a - c);
b = (b - d);
output(a, b);
}
void multi(double &a, double &b, double c, double d)
{
a = (a*c) - (b*d);
b = (a*d) + (b*c);
output(a, b);
}
bool div(double &a, double &b, double c, double d)
{
if (c == 0 && d == 0)
return false;
a = (a*c) - (b*d) / (c*c) + (d*d);
b = (a*d) + (b*c) / (c*c) + (d*d);
output(a, b);
return true;
}
|