codecamp

解方程

 #include<stdio.h>
#include<math.h>
float x1,x2;
void sit1(float a,float b)
{
  x1=x2=-b/(2.*a);
}
void sit2(float a,float b,float disc)
{
  x1=(-b+sqrt(disc))/(2.*a);
  x2=(-b-sqrt(disc))/(2.*a);
}
void sit3(float a,float b,float disc)
{
  float realpart,imagepart;
  realpart=-b/(2.*a);
  imagepart=sqrt(-disc)/(2.*a);
  x1=realpart+imagepart;
  x2=realpart-imagepart;
}
void main()
{
  float a,b,c,disc;
  scanf("%f,%f,%f",&a,&b,&c);
  printf("The equation ");
  if(fabs(a)<=1e-6)
    printf("is not a quadratic/n");
  else
  {
    disc=b*b-4.*a*c;
    if(fabs(disc)<=1e-6)
      {
 sit1(a,b);
 printf("has two equal roots:%8.4f/n",x1);
      }
    else
      if(disc>1e-6)
      {
 sit2(a,b,disc);
 printf("has distinct real roots:%8.4f and %8.4f/n",x1,x2);
      }
      else
      {
 sit3(a,b,disc);
 printf("has complex roots: /n");
 printf("%8.4f/n",x1);
 printf("%8.4f/n",x2);
      }
  }
}
魔方
链表
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }