var
a,b,c:integer;
p:int64;
x1,x2:string;
begin
  a:=strtoint(edit1.Text);
  b:=strtoint(edit2.Text);
  c:=strtoint(edit3.Text);
  p:=b*b-4*a*c;
  edit6.Text:=floattostr(p);
   if p>=0 then
    begin
      try
        x1:=floattostr(-b/2+sqr(p));
        x2:=floattostr((-b/2-sqr(p));
         edit4.Text:=x1;
         edit5.Text:=x2;
         except
           showmessage('溢出');
     end;
    if p<0 then
     begin
       try
         x1:=floattostr(-b/2)+'i'+floattostr(sqr(-p));
         x2:=floattostr(-b/2)+'-'+'i'+floattostr(sqr(-p));
         edit4.Text:=x1;
         edit5.Text:=x2;
         except
           showmessage('溢出');
     end;
  end; end;
 end;
计算出来的结果跟实际的不相符

解决方案 »

  1.   

    拜托,公式对吗?
    x1:=FloatToStr((-b+sqrt(p))/(2*a));
    x2:=FloatToStr((-b-sqrt(p))/(2*a));
    虚数那部分以此类推.
      

  2.   

    非常感谢jorge 的提醒 
    可是虚数不能计算
    而且
    好象还捕获不到异常
      

  3.   

    第二个if有错
    if p<0 then
        begin
          try
            x1:=floattostr(-b/2))+'+'+'i'+floattostr(sqr(-p));
             x2:=floattostr(-b/2)+'-'+'i'+floattostr(sqr(-p));
             edit4.Text:=x1;
             edit5.Text:=x2;
             except
               showmessage('溢出');
      

  4.   

    测试过 还是有问题
    比如a:=1 b:=3 c;=1 
    计算出x1=11 x2:=11
      

  5.   

    var
    a,b,c:integer;
    p:int64;
    x1,x2:string;
    begin
      a:=strtoint(edit1.Text);
      b:=strtoint(edit2.Text);
      c:=strtoint(edit3.Text);
      p:=b*b-4*a*c;
      edit6.Text:=floattostr(p);
       if p>=0 then
        begin
          try
            x1:=floattostr((-b+sqr(p))/2/a);
            x2:=floattostr((-b-sqr(p))/2/a);
             edit4.Text:=x1;
             edit5.Text:=x2;
             except
               showmessage('溢出');
         end;
        if p<0 then
         begin
           try
             x1:=floattostr(-b/2))+'+'+'i'+floattostr(sqr(-p));
             x2:=floattostr(-b/2)+'-'+'i'+floattostr(sqr(-p));
             edit4.Text:=x1;
             edit5.Text:=x2;
             except
               showmessage('溢出');
         end;
      end; end;
     end;
      

  6.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    a,b,c:integer;
    p:int64;
    x1,x2:string;
    begin
      a:=strtoint(edit1.Text);
      b:=strtoint(edit2.Text);
      c:=strtoint(edit3.Text);
      p:=b*b-4*a*c;
      edit6.Text:=floattostr(p);   if p>=0 then
        begin
          try
            x1:=floattostr((-b+sqr(p))/2/a);
            x2:=floattostr((-b-sqr(p))/2/a);
             edit4.Text:=x1;
             edit5.Text:=x2;
             except
               showmessage('溢出');
           end
         end
        else 
        if p<0 then
         begin
           try
             x1:=floattostr(-b/2)+'+'+'i'+floattostr(sqr(-p));
             x2:=floattostr(-b/2)+'-'+'i'+floattostr(sqr(-p));
             edit4.Text:=x1;
             edit5.Text:=x2;
             except
               showmessage('溢出');
             end;
          end;
    end;
      

  7.   

    呵呵sqr 和 sqrt 的区别,还不好发现呀!
      

  8.   

    改了以后的(其实什么都没改)var
    a,b,c:integer;
    p:real;
    x1,x2:string;
    begin
      a:=strtoint(edit1.Text);
      b:=strtoint(edit2.Text);
      c:=strtoint(edit3.Text);
      p:=b*b-4*a*c;
      edit6.Text:=floattostr(p);   if p>=0 then
        begin
          try
            showmessage(floattostr(sqrt(p)));
            x1:=floattostr((-b+sqrt(p))/2/a);
            x2:=floattostr((-b-sqrt(p))/2/a);
             edit4.Text:=x1;
             edit5.Text:=x2;
             except
               showmessage('溢出');
         end
         end
        else if p<0 then
         begin
           try
             x1:=floattostr(-b/2)+'+'+'i'+floattostr(sqrt(-p));
             x2:=floattostr(-b/2)+'-'+'i'+floattostr(sqrt(-p));
             edit4.Text:=x1;
             edit5.Text:=x2;
             except
               showmessage('溢出');
             end;
          end;
    end;