Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / C++
Alternative
Tip/Trick

A simple program to solve quadratic equations with

Rate me:
Please Sign up or sign in to vote.
2.83/5 (5 votes)
20 May 2010CPOL 11K   2   4
#include #include #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",...
#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)


Written By
Software Developer (Junior) RB Argentine Software
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 2 I would never consider writing or us... Pin
Rick York21-Nov-10 15:57
mveRick York21-Nov-10 15:57 
GeneralReason for my vote of 2 Overly complicated code. Keep it sim... Pin
S.H.Bouwhuis9-Nov-10 4:47
S.H.Bouwhuis9-Nov-10 4:47 
GeneralReason for my vote of 2 Is OOP really welcome here ? Pin
YvesDaoust8-Nov-10 23:29
YvesDaoust8-Nov-10 23:29 
GeneralReason for my vote of 1 Many problems: unnecessailly complic... Pin
Andrew Phillips7-Nov-10 15:22
Andrew Phillips7-Nov-10 15:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.