numeros complejos en una funcion cuadratica

Hola como estan tengo esta funcion cuadratica.
puedo insertar datos para a,b,c. como enteros 1,2,3, o 1.2 3.5 5.4 etc..
y esto da 2 resultados para la funcion cuadratica.
Ahora necesito ingresar al programa numeros imaginarios para a,b,c y que esto me de los 2 resultados para x.
Como puedo hacer que el progrmama me reciba numeros imaginarios o complejos.
gracias.

funcion cuadratica:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{

/* Este programa permite el calculo de las raices
de una ecuacion cuadratica */

/* Declaración de variables*/
double a, b, c, x_1, x_2;
double x_1r, x_1i, x_2r, x_2i;

/* Entrada de datos */
cout<< " coeficientes a, b y c de la ecuacion cuadratica \n";
cin>>a >> b >> c;

/* Procesamiento de datos y Salida de Resultados */
if ( (pow(b,2.0) - 4.0 * a * c) > 0)
{
x_1 = ( -b + sqrt( pow(b,2.0) - 4.0 * a * c) ) / (2.0 * a);
x_2 = ( -b - sqrt( pow(b,2.0) - 4.0 * a * c) ) / (2.0 * a);

cout<<"\n";
cout<<"La primera raiz es "<< x_1 << "\n";
cout<<"La segunda raiz es "<< x_2 << "\n";
cout<<"\n";
}
else
{
x_1r = -b / (2.0 * a);
x_1i = sqrt( fabs(pow(b,2.0) - 4.0 * a * c) ) / (2.0 * a);
x_2r = -b / (2.0 * a);
x_2i = -sqrt( fabs(pow(b,2.0) - 4.0 * a * c) ) / (2.0 * a);

cout<<"\n";
cout<<"La primera raiz es "<< x_1r <<" + " <<x_1i << " i \n";
cout<<"La segunda raiz es "<< x_2r <<" " <<x_2i << " i \n";
cout<<"\n";
}

return 0;
}
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

#include <iostream>
#include <math.h>

struct Complex {
	double real;
	double imag;
	
	Complex(double x = 0) {
		real = x;
		imag = 0;
	}
};

Complex* raices(double a, double b, double c) {
	Complex *r = new Complex[2]; //raices
	double discriminante = -b*b - 4*a*c, //-b*b == -b[sub]2[/sup]
		dosA = 2*a;
	if( discriminante >= 0 ) {
		double sqrtDisc = sqrt(discriminante);
		r[0] = (b + sqrtDisc) / dosA;
		r[1] = (b - sqrtDisc) / dosA;
	} else {
		double sqrtNegDisc = sqrt(-discriminante);
		r[0].real = r[1].real = b / (2*a);
		r[0].imag = sqrtNegDisc / (2*a);
		r[1].imag = -r[0].imag;
	}
	return r;
}
Hello how are you I have this quadratic function.
I can insert data for a, b, c. as integers 1,2,3, or 1.2 3.5 4.5 etc. ..
and this gives 2 results for the quadratic function.
Now to enter the the program need imaginary numbers for a,b ,c and this gives me the 2 results for x.
As I can make me get progrmama imaginary or complex numbers.
thanks.

It's kind of hard to understand.. Maybe you could translate better than google? Anyway, my guess is that you should thake a look at std::complex
http://www.cplusplus.com/reference/std/complex/
Ahora necesito ingresar al programa numeros imaginarios para a,b,c y que esto me de los 2 resultados para x.
Como puedo hacer que el progrmama me reciba numeros imaginarios o complejos.
Translation: He wants to know how to enter complex coefficients for the polynomial at run time.

My suggestion is that you enter the real and imaginary parts separately. Otherwise, you'd have to do text parsing, which is always a bother.
Why would you want to find the roots of a quadratic function with complex coeficiants anyway? But yea, something like this should work:
1
2
3
Complex a, b, c;
cin >> a.real >> a.imag
  >> b.real >> ...


PS: As for actually finding the roots, you would need a sqrt function that can take the root of a complex number.
Last edited on
closed account (D80DSL3A)
@Mathhead200 Fortunately, std::complex contains just such an overloaded version of sqrt().

However, writing such a function isn't too hard either.
Let a complex # z be represented in polar notation as z = r*ei*a where r = |z| and a is the phase angle in the complex plane. We get sqrt(z) = sqrt(r)*ei*a/2 or
sqrt(z) = sqrt(r)*( cos(a/2) + i*sin(a/2) )
Here's a little program to demonstrate (which is relevant to OP's problem because it shows the use of methods he needs).
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
#include <iostream>
#include <cmath>
#include <complex>
using namespace std;

int main()
{	
	double PI = 3.14159265;	
	double x, y, r, a;// for our representation of a complex #
	cout << "Zr = "; cin >> x;// user enters real
	cout << "Zi = "; cin >> y;// and imaginary parts for a complex #

	// using the complex # library to find sqrt(z)
	complex<double> z(x,y);// construct z from user entered values
	cout << "z = " << z << endl;// echo input
	cout << "Using complex sqrt(z)    = " << sqrt(z) << endl;

	// now by our own method
	r = sqrt(x*x+y*y);// r = |z|

	// find a = phase angle in complex plane
	if( x != 0.0 )// z has a real pert
	{
		a = atan(y/x);// returns a = -PI/2 to +PI/2 ( for z in 1st or 4th quadrant)
		if(x < 0.0)
			a += PI;// for z in 2nd or 3rd quadrant
	}
	else if(y > 0.0 )// z is purely imaginary
		a = 0.5*PI;// z = i*r
	else
		a = 1.5*PI;// z = -i*r
	
	cout << "Own method gives sqrt(z) = (" << sqrt(r)*cos(a/2.0) << "," << sqrt(r)*sin(a/2.0) << ")" << endl;
	return 0;
}

I thought you might like this, considering your username.
Last edited on
You know I worked with complex numbers expressed in polar (r cis θ) form a while back in a high school trig course, but I haven't really needed them since.

Alos doesn't <math.h> have an atan2 function?
Last edited on
Thanks, my problem is:

I need imaginary numbers add to the variables a, b, c
This program solves a quadratic equation, only asks that I insert the data for the variables a, b, c. these variables can be integers like 2, 3, 56, 13, etc. .. 56.4 or 2.3 etc ... Now I need to know how to insert a, b, c imaginary numbers: 34i, 2i, 56i or negative roots.

You have already been given an answer.

Use the complex<> class to store your values, and ask for the values individually. You can parse it easily enough if you want to allow your user to enter something like "3.5 - 2.9i", but you can also just ask for both real and imaginary parts for each variable.

(Remember, that "3.5" is the same as "3.5 + 0i", and "-2.9i" is the same as "0 - 2.9i".)

Hope this helps.
Topic archived. No new replies allowed.