Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

A simple program to solve quadratic equations with

2.83/5 (5 votes)
20 May 2010CPOL 12K  
#include #include #include Raiz_Cuadratica.hint main(){ // float xx = 1; float x = 1; float i = -12; RaizCuadratica *lala = new RaizCuadratica( xx, x, i ); RaizCuadratica::sRaiz raiz = lala->GetRaiz(); printf( x1 = %f || x2 = %fnn,...
#include <stdio.h>
#include <stdlib.h>

#include "Raiz_Cuadratica.h"


int main()
{
	//
	float xx = 1;
	float x = 1;
	float i = -12;

	RaizCuadratica *lala = new RaizCuadratica( xx, x, i );
	
	RaizCuadratica::sRaiz raiz = lala->GetRaiz();

	printf( "x1 = %f || x2 = %f\n\n", raiz.r1, raiz.r2 );

	delete lala;
	lala = 0;
	system("PAUSE");
	return 0;
}


////////////////////////////////////////////////////////////////
#ifndef __RAIZ_CUADRATICA_H__
#define __RAIZ_CUADRATICA_H__

/*
 * (C) Copyright 2010 Ramón 'Reymon' Berrutti <ramonberrutti@hotmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * See LICENSE.TXT file for more information.
 *
 */

#include <math.h>

class RaizCuadratica
{
public:
	struct sRaiz
	{
		float r1;
		float r2;

		sRaiz( float x1, float x2 ) : r1(x1), r2(x2) {}
	};

	RaizCuadratica() : m_valI( 0 ), m_valX( 0 ), m_valXX( 0 ) {}
	RaizCuadratica( float xx, float x, float i ) : m_valI( i ), m_valX( x ), m_valXX( xx ) {}

	RaizCuadratica *setI( float i );
	RaizCuadratica *setX( float x );
	RaizCuadratica *setXX( float xx );

	sRaiz GetRaiz();
	void GetRaiz( sRaiz *output );

private:
	float m_valI;
	float m_valX;
	float m_valXX;
};

#endif // __RAIZ_CUADRATICA_H__

//////////////////////////////////////////////////////////////////
#include "Raiz_Cuadratica.h"

RaizCuadratica *RaizCuadratica::setI(float i)
{
	m_valI = i;
	return this;
}

RaizCuadratica *RaizCuadratica::setX(float x)
{
	m_valX = x;
	return this;
}

RaizCuadratica *RaizCuadratica::setXX(float xx)
{
	m_valXX = xx;
	return this;
}

RaizCuadratica::sRaiz RaizCuadratica::GetRaiz()
{
	float mmm = sqrtf( pow(m_valX,2) - ( 4 * m_valXX * m_valI ) );

	return sRaiz( (-m_valX + mmm) / (m_valXX*2) , (-m_valX - mmm) / (m_valXX*2) );
}

void RaizCuadratica::GetRaiz( RaizCuadratica::sRaiz *output )
{
	float mmm = sqrtf( pow(m_valX,2) - ( 4 * m_valXX * m_valI ) );

	output->r1 = (-m_valX + mmm) / (m_valXX*2);
	output->r2 = (-m_valX - mmm) / (m_valXX*2);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)