Solve Quadratic Equations 

  

 Print  

  Email  

  

 

 

 

 Submitted on: 10/17/2000 3:36:40 PM

By: 

Level: Beginner

User Rating:     By 1 Users

Compatibility:C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++

Users have accessed this code 1668 times.

   

(About the author) 

 

  

     Check this cool Quadratic equation solving tool ! It even supports complex roots, all variables from 'a' to 'z'. Try this simple code and don't forget to vote for it.

 

 

 

  

 

code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

//**************************************

//     

// Name: Solve Quadratic Equations

// Description:Check this cool Quadratic

//     equation solving tool !

It even supports complex roots, all variables from 'a' to 'z'. try this simple code and don't forget to vote for it.

// By: 

//

//This code is copyrighted and has// limited warranties.Please see http://

//     www.1CPlusPlusStreet.com/xq/ASP/txtCodeI

//     d.804/lngWId.3/qx/vb/scripts/ShowCode.ht

//     m//for details.//**************************************

//     

/* 220 lines - Updated 24th September 2000.

 EQUATION SOLVERby ANKIT ROHATGI

 ~~~~~~~~ ~~~~~~~~~~~ ~~~~~~~

this is a program to solve simple quadratic equations.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Way of entering the equation (SYNTAX):-

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1) When the coefficient of a variable has been typed , a '*' has to

be further typed to indicate that the coefficient is multiplied

by the variable.

2) The equation input is ended by a '!'.

3) this WILL support complex answers.

4) All coefficients MUST be integers(Decimals not currently supported).

5) Type with CAPS LOCK OFF.

EXAMPLES :-

~~~~~~~~~~~

1) the equation 4xy+4x+1=0 has to be typed as:

4*x2+4*x+1*=0*!

2) the equation xy+4x+4=0 has to be typed as:

1*x2+4*x+4*=0*!

3) the equation xy+1=0 will be:

1*x2+0*x+1*=0*!

NOTE: The equations will be displayed on the screen (while typing) as:

~~~~ 4xy+4x+1=0!the character '*' will NOT be shown.

 if it is displayed ,there is something wrong in your syntax.

 THE 'i' in the complex answer means iota

*/

#include

#include

#include

#include

float getno(int fn=0);

void showsign(float no=0);

void main()

    { clrscr();

    textcolor(14);

    cprintf("This is the Equation solver by Ankit Rohatgi");

    cout<<"\n";

    textcolor(12);

    cprintf("Press any key to input the equation");

    cout<<"\n";

    cprintf("GENERAL FORM 4xy+2x+3=0 ик 4*x2+2*x+3*=0*!");

    cout<<"\a";

    getch();

    clrscr();

    textcolor(14);

    cprintf("Enter the Equation: \n");

    textcolor(11);

    char input='o',var,variable='x';

    int a=0,c=0,c2,sign=1,pow,b;

    float digit,A=0,B=0,C=0;

    while(input!='!')

        { input=getche();

        begin:

        if(isdigit(input)||input=='-')

            { if(isdigit(input))

                { digit=getno((input-48));}

                else

                    { digit=(0-getno());

                }

                a=a+1;

                if(a==1)

                    {A=digit;}

                    else if(a==2)

                        {B=digit;}

                        else if(a==3)

                            {C=digit;}

                            else if(a==4)

                                {C=C-digit;}

                            }

                            else if((input>='a')&&(input<='z'))

                                { variable=input;

                                pow=getch();

                                if(pow=='2')

                                    { putch(253);

                                }

                                else if(pow=='!')

                                    { break;}

                                    else

                                        { putch(pow);

                                         input=pow;

                                         goto begin;

                                    }

                                }

                            }

                            textcolor(12);

                            cout<<"\nThe equation:\n "<

                            showsign(B);

                            cout<

                            showsign(C);

                            if(C>=0)

                                {cout<

                                else if(C<0)

                                    {cout<<-C<<"=0";}

                                    if((A!=0)&&(B!=0)&&(C!=0))

                                        {

                                        float bsqr=(B*B);

                                        float fourac=(4*A*C);

                                        if (fourac<=bsqr)

                                            { float delta=((B*B)-(4*A*C));

                                            float sqrtdelta=sqrt(delta);

                                            float ans1=((sqrtdelta-B)/(2*A));

                                            float ans2=((sqrtdelta+B)/(2*A));

                                            cout<<"\n"<

                                            cout<<"\nor "<

                                        }

                                        else if(fourac>bsqr)

                                            { float delta=(fourac-bsqr);

                                            float sqrtdelta=sqrt(delta);

                                            cout<<"\n\n "<

                                        }

                                    }

                                    if(A==0)

                                        { if(B!=0)

                                        cout<<"\n "<

                                        if((B==0)||(C==0))

                                        cout<<"\n "<

                                        goto finish;

                                    }

                                    if(B==0)

                                        { if(C!=0)

                                            { if(-C>0)

                                                { cout<<"\n "<

                                            }

                                            else

                                                { cout<<"\n "<

                                            }

                                        }

                                        if(C==0)

                                            { cout<<"\n "<

                                        }

                                        goto finish;

                                    }

                                    if(C==0)

                                        { cout<<"\n "<

                                        cout<<"\n "<

                                        goto finish;

                                    }

                                    finish:

                                    getch();

                                }

                                float getno(int fn)

                                    { char inchar='o';

                                    int cnt=0,sign=1;

                                    float noval=0,acval=0;

                                    while(inchar!='*')

                                        { cnt++;

                                        if(cnt==1)

                                            { inchar=getch();

                                              if(isdigit(inchar))

                                             { sign=1;

                                              noval=(fn*10)+(inchar-48);

                                              putch(inchar);

                                             }

                                              if(isalpha(inchar))

                                                 {

                                                 }

                                                  if(inchar=='*')

                                                 { noval=fn;goto maran;}

                                                  else

                                                 { if(inchar=='+')

                                                 { sign=1;

                                                  putch(inchar);

                                                  inchar=getche();

                                                  noval=(fn*10)+(inchar-48);

                                                 }

                                                  if(inchar=='-')

                                                 { sign=0;

                                                  putch(inchar);

                                                  inchar=getche();

                                                  noval=(fn*10)+(inchar-48);

                                                 }

                                                 }

                                            }

                                            inchar=getch();

                                            if(inchar=='*')

                                                { goto maran;

                                            }

                                            if(inchar!='*')

                                                { putch(inchar);

                                                noval=(noval*10)+((inchar)-48);

                                            }

                                        }

                                        maran:

                                        cnt=0;

                                        if(sign==0)

                                            { acval=-noval;

                                        }

                                        else if(sign==1)

                                            { acval=noval;

                                        }

                                        return(acval);

                                    }

                                    void showsign(float no)

                                        { if(no>=0)

                                            { putch(43);

                                        }

                                        else if(no<0)

                                            { putch(45);

                                        }

                                    }