Quadratic formula solver in C

Discussion in 'Programming General' started by Govind, Oct 29, 2008.

Quadratic formula solver in C
  1. Unread #1 - Oct 29, 2008 at 12:08 AM
  2. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Quadratic formula solver in C

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char *argv[])
    {
    	double a, b, c, x1, x2;
    	a = 0; b = 0; c = 0;
    	if(!(argv[1])||(argv[1][0]=='/'&&argv[1][1]=='?'))
    	{
    		printf("QUAD A B C\nTo solve quadratics in the form 
    
    Ax^2+Bx+C=0");
    		return 0;
    	}
    	if(argc!=4)
    	{
    		printf("Usage error: Must be QUAD a b c for any quadratic 
    
    ax^2+bx+c=0");
    		return 0;
    	}
    	a = strtod(argv[1],0);
    	b = strtod(argv[2],0);
    	c = strtod(argv[3],0);
    	if(a==0)
    	{
    		printf("A cannot be 0");
    		return 0;
    	}
    	//printf("%f %f %f\n",a,b,c);
    	if(((b*b)-(4*a*c))<0)
    	{
    		printf("Nonreal roots\n");
    		return 0;
    	}
    	x1 = (-b+sqrt((b*b)-(4*a*c)))/(2*a);
    	x2 = (-b-sqrt((b*b)-(4*a*c)))/(2*a);
    	printf("x[1] = %f\n",x1);
    	printf("x[2] = %f\n",x2);
    	return 0;
    }
    I know that the checking for whether argv[1] = "/?" was a little crude but other than that it's awesome, I think.
     
< [TUT][C] Handling User Input | visual basic: auto spammer >

Users viewing this thread
1 guest


 
 
Adblock breaks this site