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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <ostream>
using namespace std;
struct cmplxNumber{
double dReal, dImaginary;
} ;
bool ComputeRoots( double dCoefA, double dCoefB, double dCoefC,
cmplxNumber & Root1, cmplxNumber & Root2);
ostream & PrintComplex( ostream & outDevice, cmplxNumber Root );
int main()
{
const int value = 40; // array size
ifstream fin; // input file stream object
// Need some variable storage for parameters
double dCoefA = 0.0, dCoefB = 0.0, dCoefC = 0.0;
int complex[value];
int c = 0;
// values for the menu option
int option;
// display for the menu complex numbers
do
{
cout<<"\t\t MENU COMPLEX NUMBERS\n\n";
cout<<"\tPlease choose the following numbers\n\n";
cout<<"Press 1 for reading values\n";
cout<<"Press 2 for display the result\n";
cout<<"Press 3 for quit\n\n";
cout<<"Enter your option : ";
cin >> option;
// for formatting numeric output
cout << fixed << showpoint << setprecision(2);
// menu loop
while ( option < 1 || option > 3)
{
cout <<" please enter 1, 2 or 3:";
cin >> option;
}
// respond to the users menu
switch ( option )
{
case 1:
fin.open("RAW_DATA.TXT");
for (c = 0; c < value ; c++)
fin >> complex[c];
fin.close();
break;
case 2:
cmplxNumber Root1, Root2;
ComputeRoots( dCoefA, dCoefB, dCoefC, Root2, Root1 );
cout << "\n\nThe first root is " << Root1.dReal;
if( Root1.dImaginary < 0.0 ) {
cout << " - " << abs(Root1.dImaginary) << " j" << endl;
}
else if( Root1.dImaginary > 0.0 ) {
cout << " + " << Root1.dImaginary << " j" << endl;
}
else
cout << endl;
cout << "\n\nThe second root is ";
PrintComplex( cout, Root2 );
break;
case 3:
cout<< " program is ending\n";
break;
}
}while (option != 3);
system("pause");
return 0;
}
bool ComputeRoots( double dCoefA, double dCoefB, double dCoefC,
cmplxNumber & Root1, cmplxNumber & Root2)
{
double dInter1, dInter2;
dInter1 = dCoefB * dCoefB; // get the square of B
dInter2 = 4.0 * dCoefA * dCoefC;
if(dInter1 < dInter2 ) {
Root1.dImaginary = pow(abs(dInter1 - dInter2), 0.5)/ (2.0 * dCoefA);
Root2.dImaginary = -pow(abs(dInter1 - dInter2), 0.5)/ (2.0 * dCoefA);
Root1.dReal = Root2.dReal = -dCoefB / (2.0 * dCoefA);
} else {
Root1.dReal = (-dCoefB + pow(dInter1 - dInter2, 0.5))/ (2.0 * dCoefA);
Root2.dReal = (-dCoefB - pow(dInter1 - dInter2, 0.5))/ (2.0 * dCoefA);
}
return true;
}
// declaration of function using 'things' from the stream devices is
// fairly exacting. MUST use references to the device variables.
ostream & PrintComplex( ostream & outDevice, cmplxNumber Root )
{
outDevice << Root.dReal;
if( Root.dImaginary < 0.0 ) {
outDevice << " - " << abs(Root.dImaginary) << " j" << endl;
}
else if( Root.dImaginary > 0.0 ) {
outDevice << " + " << Root.dImaginary << " j" << endl;
}
else
cout << endl;
return outDevice;
}
|