【发布时间】:2013-04-01 22:50:16
【问题描述】:
我正在开发一个求解方程组的程序。当它给我结果时,它就像:“x1 = 1,36842”。我想得到那个“1,36842”的分数,所以我写了这段代码。
procedure TForm1.Button1Click(Sender: TObject);
var numero,s:string;
a,intpart,fracpart,frazfatta:double;
y,i,mcd,x,nume,denomin,R:integer;
begin
a:=StrToFloat(Edit1.Text); //get the value of a
IntPart := Trunc(a); // here I get the numerator and the denominator
FracPart := a-Trunc(a);
Edit2.Text:=FloatToStr(FracPart);
numero:='1';
for i:= 1 to (length(Edit2.Text)-2) do
begin
numero:=numero+'0';
end; //in this loop it creates a string that has many 0 as the length of the denominator
Edit3.text:=FloatToStr(IntPart);
y:=StrToInt(numero);
x:=StrToInt(Edit3.Text);
while y <> 0 do
begin
R:= x mod y;
x:=y;
y:=R;
end;
mcd:=x; //at the end of this loop I have the greatest common divisor
nume:= StrToInt(Edit3.Text) div mcd;
denomin:= StrToInt(numero) div mcd;
Memo1.Lines.Add('fraction: '+IntToStr(nume)+'/'+IntToStr(denomin));
end;
它不能正常工作,因为它给我的分数是错误的。谁能帮帮我?
【问题讨论】:
-
当 x1= 123,56(例如)时,正确的分数应该是 3089/25。我的程序作为输出显示 123/100 是错误的
-
看来你自己不知道怎么做。一旦你能做到这一点,你就可以轻松地用任何语言创建一个程序来完成你已经知道的事情。我的建议是在纸上写一些数字,然后开始编写你的算法(也许从头开始)