## Title: Find the roots of a quadratic equation (25 points)

#include <stdio.h>
#include <math.h>
int  main (void)
{   	double a,b,c,d,e,x1,x2;   
 	scanf("%lf %lf %lf",&a,&b,&c);  
  	 d=b*b-4*a*c;  
  	  if (d>0)  
  	  {    	if(a==0)     
  	    	 x1=-c/b;printf("%.2lf\n",x1);    
  	         if (a!=0)    
  	             {   x1=(-b+sqrt(d))/(2*a);       
  	                  x2=(-b-sqrt(d))/(2*a);            
  	                  printf("%.2lf\n%.2lf\n",x1,x2);    
  	             }   
  	  }   
  	  else if (d==0)  
  	  { 	  if (a==0&&b==0&&c==0)     
  	        	 printf("Zero Equation\n");   
  	          else if(a==0&&b==0&&c!=0)   
  	          	  printf("Not An Equation\n");     
  	          else     
  	                 {  
                             x1=-b/(2*a);     
  	                    printf("%.2lf\n",x1);
  	                 }   
  	    }   
  	    else  
  	       {   if (a!=0)        
  	       		    if(b==0)       
  	       		       {
                                x1=sqrt(-d)/(2*a);         
  	       		        x2=sqrt(-d)/(2*a);            
  	       		        printf("0.00+%.2lfi\n0.00-%.2lfi\n",x1,x2); 
  	         		}	 		 			  			                                                            
  	       		      else        
  	       		        {
                                  x1=sqrt(-d)/(2*a);       
  	       		          e=-b/(2*a);        
  	       		          x2=sqrt(-d)/(2*a);             		
  	       		          printf("%.2lf+%.2lfi\n%.2lf-%.2lfi\n",e,x1,e,x2);   
  	       		                   			          
  	       		         }  
  	      	}
  	return 0;
}


 

 

 This question requires the roots of a quadratic equation in one unknown, and the result is kept to 2 decimal places.
Input format:
Input three floating-point coefficients a, b, c in one line, separated by spaces.
Output format:
according to the coefficients, different results are output:
  1) If the equation has two unequal real roots, then one root will be output per line, first large and then small;
  2) If the equation has two unequal complex roots, each The line outputs a root according to the format "real part + imaginary part i", first output the imaginary part as positive, and then output the imaginary part as negative;
 3) If the equation has only one root, output this root directly;
 4) If the coefficients are all If it is 0, then output "Zero Equation";
 5) If a and b are 0 and c is not 0, output "Not An Equation".
 
Input example 1:
2.1 8.9 3.5

Output example 1:
-0.44
-3.80

Input example 2:
1 2 3
Output example 2:
-1.00+1.41i
-1.00-1.41i
Input example 3:
0 2 4
Output example 3:
-2.00
Input example 4:
0 0 0
Output example 4 :
Zero Equation
Input example 5:
0 0 1
Output example 5:
Not An Equation
 

 

Guess you like

Origin blog.csdn.net/L_Z_jay/article/details/103751929