Jump to content

Quadratic solver

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Jepcats

Jepcats

    Newbie

  • Members
  • Pip
  • 3 posts
I had a working quadratic solver that just took in the values for a, b then c which took like a minute to do, but I didn't like it working like that and I wanted to be able to just type in the equation and get an answer that way. It's probably the while loop where I take in the characters for the equation, but anyway this is what I came up with that doesn't work at all:

void quadratic_solver()

{

	std::vector<char> equation;

	double a = 0, b = 0, c = 0;

	double answer1 = 0, answer2 = 0;

	double inRoot = 0;

	char character;


	std::cout << "Enter the equation (e.g. 2x^2 - 5x + 3|): ";

	while( ( std::cin >> character ) && ( character != '|' ) )

	{

		equation.push_back(character);

	}


	for( int i = 0; i < equation.size(); i++ )

	{

		if( equation[i] == 'x' )

		{

			if( equation[i + 1] == '^' )

			{

				if( equation[i + 2] == '2' )

				{

					a = equation[i - 1];

				}

			}

			else if( equation[i + 1] != '^' )

			{

				b = equation[i - 1];

			}

		}

	}


	c = equation[equation.size()];


	inRoot = pow( b, 2 ) - ( 4 * a * c );


	if( inRoot < 0 )

	{

		std::cout << "Error: Number inside of square root is negative.";

	}


	else if( ( a == 0 ) || ( b == 0 ) || ( c == 0 ) )

	{

		std::cout << "Error: Not a quadratic equation.";

	}


	else

	{

		answer1 = ( -b + sqrt( inRoot ) ) / ( 2 * a );

		answer2 = ( -b - sqrt( inRoot ) ) / ( 2 * a );


		std::cout << "x = " << answer1 << ", x = " << answer2;

	}


	wait_continue();

}

If anyone can suggest a better way of doing this and/or where I'm going wrong that'd be great, thanks :)

#2
DRK

DRK

    Newbie

  • Members
  • PipPip
  • 10 posts
double a, b, c;
char bsign, csign;
if (scanf("%lfx^2 %c %lf %c %lf", &a, &bsign, &b, &csign, &c) == 5)
{
    if (bsign == '-')
        b = -b;
    if (csign == '-')
        c = -c;
}
Note: Ending '|' no longer needed.

#3
RichardM

RichardM

    Newbie

  • Members
  • PipPip
  • 21 posts
You want to do more input than less?!?!
Seems like you are making more work for yourself. Personally, I'd rather type just the coefficients instead of the coefficients AND the variables AND the operators.
Is this a school assignment, or something you are working on for your own reasons?

Jepcats said:

. . . but anyway this is what I came up with that doesn't work at all:
. . .



The whole thing doesn't work? Just the input block? The computation block? Does it compile? Does it run but give the wrong results?
What kinds of errors are you getting?

A search for "Quadratic Equation Solver" turns up thousands of results.
Here is a good one, from the first results page:

Quadratic Equation Solver

It handles a negative discriminant AND does a good job of dealing with unfriendly input data.
If nothing else, it should give you something to compare the results of your program against.

#4
Jepcats

Jepcats

    Newbie

  • Members
  • Pip
  • 3 posts
I'm working on creating one console window application where you can do a load of mathematical things like matrices, rearranging functions and other stuff.
The reason I wanted to make it work like this is I thought it might come in handy for something else later on.

It's just something I'm working on for fun. Thanks for the help (:

The error I was getting was:
[ATTACH=CONFIG]4444[/ATTACH]

#5
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
This:


else if( ( a == 0 ) || ( b == 0 ) || ( c == 0 ) ) 	{ 		std::cout << "Error: Not a quadratic equation."; 	}

Is wrong. You can have a quadratic equation where b = 0 or c = 0.

x^2 is a quadratic equation.
x^2 + 3 is a quadratic equation.

You, however, cannot have a quadratic equation where a = 0 or a = 0 and b = 0 and c = 0.

#6
Jepcats

Jepcats

    Newbie

  • Members
  • Pip
  • 3 posts
Whoops, that's embarrassing. Fixed.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users