Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

A simple program to solve quadratic equations with

, 20 May 2010
Rate this:
Please Sign up or sign in to vote.
#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)

Share

About the Author

ReymonARG
Software Developer (Junior) RB Argentine Software
Argentina Argentina
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 May 2010
Article Copyright 2010 by ReymonARG
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid