早教吧 育儿知识 作业答案 考试题库 百科 知识分享

菜鸟程序改错#include#includeintmain(void){inta=1;intb=2;intc=1;doubledelta;doublex1;doublex2;intdelta=b*b-4*a*c;if(delta>0){x1=(-b+sqrt(delta)/(2*a);x2=(-b-sqrt(delta)/(2*a);printf("该一元二次方程有

题目详情
菜鸟程序改错
# include
# include
int main(void)
{ int a = 1;
int b = 2;
int c = 1;
double delta;
double x1 ;
double x2 ;
int delta = b*b-4*a*c;
if ( delta > 0 )
{ x1= (-b + sqrt(delta)/(2*a);
x2= (-b - sqrt(delta)/(2*a);
printf ("该一元二次方程有两个解,x1 = %f,x2 = %f\n",x1,x2);
}
else if ( delta == 0 )
\x05{ x1=(-b)/(2*a);
x2 = x1 ;
\x05\x05printf ("该一元二次方程有唯一解,x1 = x2 = %f\n",x1);
\x05\x05
\x05\x05}
else
\x05{ printf ("无解"\n",x1); }
\x05return 0;
}
-------------------Configuration:Cpp2 - Win32 Debug--------------------
Compiling...
Cpp2.cpp
C:\Documents and Settings\Administrator\桌面\新建文件夹\Cpp2.cpp(12) :error C2371:'delta' :redefinition; different basic types
C:\Documents and Settings\Administrator\桌面\新建文件夹\Cpp2.cpp(9) :see declaration of 'delta'
C:\Documents and Settings\Administrator\桌面\新建文件夹\Cpp2.cpp(15) :error C2143:syntax error :missing ')' before ';'
C:\Documents and Settings\Administrator\桌面\新建文件夹\Cpp2.cpp(16) :error C2143:syntax error :missing ')' before ';'
C:\Documents and Settings\Administrator\桌面\新建文件夹\Cpp2.cpp(26) :error C2017:illegal escape sequence
C:\Documents and Settings\Administrator\桌面\新建文件夹\Cpp2.cpp(26) :error C2001:newline in constant
C:\Documents and Settings\Administrator\桌面\新建文件夹\Cpp2.cpp(26) :error C2146:syntax error :missing ')' before identifier 'n'
C:\Documents and Settings\Administrator\桌面\新建文件夹\Cpp2.cpp(30) :fatal error C1004:unexpected end of file found
执行 cl.exe 时出错.
Cpp2.obj - 1 error(s),0 warning(s)
▼优质解答
答案和解析
编译的问题这样改就没了.


# include 
# include 
  
int main(void)
{  int a = 1;
   int b = 2;
   int c = 1;
   double delta = b*b-4*a*c;
   double x1 ;
   double x2 ;
//   int delta = b*b-4*a*c; // <== delta定义过了
    if ( delta > 0 )
    {  x1= (-b + sqrt(delta))/(2*a); // <== 原来括号不配对
       x2= (-b - sqrt(delta))/(2*a); // <== 原来括号不配对
       printf ("该一元二次方程有两个解,x1 = %f, x2 = %f\n",x1, x2);
      }
    else if ( delta == 0 )
{   x1=(-b)/(2*a);
          x2 = x1 ;
printf ("该一元二次方程有唯一解,x1 = x2 = %f\n",x1);    
}
    else
{    printf ("无解\n",x1); } // <== 原来多一个双引号
 
return 0;
}


运行结果:


该一元二次方程有唯一解,x1 = x2 = -1.000000