D
Delphi刘
Unregistered / Unconfirmed
GUEST, unregistred user!
序号 时间 优惠价额 汇入金额 期限(天) 利息
1 2003-1-1 8000 5000 30
2 2003-1-5 3000 2000 30
这时应该还欠1000元吧,如果2003-2-6他又买了3000的东东,不过没有给钱!!
序号 时间 优惠价额 汇入金额 期限(天) 利息
1 2003-1-1 8000 5000 30
2 2003-1-5 3000 2000 30
3 2003-2-6 3000 0 30
一个月之后还了3000元
序号 时间 优惠价额 汇入金额 期限(天) 利息
1 2003-1-1 8000 5000 30
2 2003-1-5 3000 2000 30
3 2003-2-6 3000 0 30
4 2003-3-6 0 3000
明白了吗??
我把我的源码贴上来,你看看
procedure TForm_Main.SB_Calculate_InterestClick(Sender: TObject);
var
I:integer;
G_qixian,Sum_G_qixian:integer;
Interest:real;
FirstDate:TDate;
Rec:Integer;
Cal_Interest,Cal_Int2:array of TCalculate;
payoff,payoff1:Real;
place,hr_Place,yh_Place:Integer;
hr_No:integer;
begin
with DM.Table_Basicdo
begin
First;
while not Eofdo
begin
with DM.Table_Interestdo
begin
Active:=True;
Rec:=RecordCount;
SetLength(Cal_Interest,Rec);
For I:=0 to Rec-1do
begin
Cal_Interest.DateTime:=0;
Cal_Interest.yh_Price:=0;
Cal_Interest.hr_Money:=0;
end;
For I:=0 to Rec-1do
begin
Cal_Interest.DateTime:=FieldByName('Date_Time').AsDateTime;
Cal_Interest.yh_Price:=FieldByName('Optimal_Price').AsCurrency;
Cal_Interest.hr_Money:=FieldByName('Remittance').AsCurrency;
Next;
end;
//--------------------------------------------------------
//Calculate Interest
//--------------------------------------------------------
case Rec of
//---------------------------------------------------------
//if only have one record
//---------------------------------------------------------
1:
begin
Interest:=0;
payoff:=Cal_Interest[0].yh_Price-Cal_Interest[0].hr_Money;
G_qixian:=Trunc(Date-Cal_Interest[0].DateTime)-30;
if G_qixian>0 then
begin
Interest:=Interest+payoff*G_qixian*0.01/30;
end
else
Interest:=Interest+0;
if Interest<0 then
Interest:=0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
end;
//-----------------------------------------------
//if there are two records
//-----------------------------------------------
2:
begin
First;
Interest:=0;
payoff:=Cal_Interest[0].yh_Price-Cal_Interest[0].hr_Money;
if Cal_Interest[1].hr_Money>0 then
//if have remittance
begin
G_qixian:=Trunc(Cal_Interest[1].DateTime-Cal_Interest[0].DateTime)-30;
if G_qixian>0 then
//if the second remittance excess 30 days
begin
Interest:=Interest+payoff*G_qixian*0.01/30;
payoff:=payoff-Cal_Interest[1].hr_Money;
if payoff>0 then
//if not pay off
begin
G_qixian:=Trunc(Date-Cal_Interest[1].DateTime);
Interest:=Interest+payoff*G_qixian*0.01/30;
payoff1:=-1;
end;
if payoff=0 then
begin
G_qixian:=Trunc(Cal_Interest[1].DateTime-Cal_Interest[0].DateTime);
Interest:=Interest+payoff*G_qixian*0.01/30;
payoff1:=-1;
end;
if payoff<0 then
begin
payoff1:=payoff;
end;
end
else
//if the second remittance havn't excess 30 days
begin
payoff:=payoff-Cal_Interest[1].hr_Money;
if payoff>0 then
//if not pay off
begin
G_qixian:=Trunc(Date-Cal_Interest[0].DateTime)-30;
if G_qixian>0 then
Interest:=Interest+payoff*G_qixian*0.01/30
else
Interest:=0;
payoff1:=0;
end;
if payoff=0 then
begin
G_qixian:=Trunc(Date-Cal_Interest[0].DateTime)-30;
if G_qixian>0 then
Interest:=Interest+payoff*G_qixian*0.01/30
else
Interest:=0;
payoff1:=0;
end;
if payoff<0 then
begin
payoff1:=payoff;
end;
end;
//end else
G_qixian<30
end
else
//if have no hr_Money
begin
G_qixian:=Trunc(Date-Cal_Interest[0].DateTime)-30;
if G_qixian>0 then
Interest:=payoff*G_qixian*0.01/30
else
Interest:=0;
end;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
begin
if (Cal_Interest[1].yh_Price>0) then
begin
G_qixian:=Trunc(Date-Cal_Interest[1].DateTime)-30;
if G_qixian>0 then
begin
if (payoff1<0) then
payoff:=Cal_Interest[1].yh_Price+payoff1
else
payoff:=Cal_Interest[1].yh_Price;
Interest:=payoff*G_qixian*0.01/30;
end
else
Interest:=0;
end
else
Interest:=0;
end;
if Interest<0 then
Interest:=0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
end;
//end case 2
end;
//end case
//----------------------------------------------------
//if there are three records
//----------------------------------------------------
if rec>2 then
begin
First;
FirstDate:=Cal_Interest[0].DateTime;
place:=0;
hr_No:=0;
For I:=1 to Rec-1do
begin
if Cal_Interest.hr_Money>0 then
hr_No:=hr_No+1;
end;
SetLength(Cal_Int2,hr_No);
For I:=1 to Rec-1do
begin
if Cal_Interest.hr_Money>0 then
begin
Cal_Int2[place].DateTime:=Cal_Interest.DateTime;
Cal_Int2[place].yh_Price:=Cal_Interest.yh_Price;
Cal_Int2[place].hr_Money:=Cal_Interest.hr_Money;
place:=Place+1;
end;
end;
//--------------------------------------------------------------------
//Calculate the first record's Interest
//Cyc to find the pay off place then
save the place and payoff value
//to Calculate the second record:
// 1: payoff > 0 not pay off
// 2: payoff = 0 just pay off
// 3: payoff < 0 pay more
//---------------------------------------------------------------------
hr_Place:=0;
yh_Place:=0;
G_qixian:=0;
Sum_G_qixian:=0;
Interest:=0;
payoff:=Cal_Interest[yh_Place].yh_Price-Cal_Interest[hr_Place].hr_Money;
if payoff<=0 then
Interest:=0;
while not eofdo
begin
while (payoff>0) and (hr_Place<place)do
begin
if Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)<=0 then
G_qixian:=Trunc(Cal_Interest[yh_Place+1].DateTime-FirstDate)-30-Sum_G_qixian
else
G_qixian:=Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)-30-Sum_G_qixian;
if G_qixian>0 then
begin
Sum_G_qixian:=Sum_G_qixian+G_qixian;
Interest:=Interest+payoff*G_qixian*0.01/30;
end
else
begin
Interest:=0;
G_qixian:=0;
Sum_G_qixian:=0;
end;
if Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)<=0 then
payoff:=payoff
else
payoff:=payoff-Cal_Int2[hr_Place].hr_Money;
hr_Place:=hr_Place+1;
if Interest<0 then
Interest:=0;
end;
//end if while payoff>0
//1: haven't pay off
if payoff>0 then
begin
if Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)<=0 then
G_qixian:=Trunc(Date-Cal_Interest[yh_Place+1].DateTime)
else
G_qixian:=Trunc(Date-Cal_Int2[hr_Place-1].DateTime);
if G_qixian>0 then
begin
Interest:=Interest+payoff*G_qixian*0.01/30;
end
else
begin
Interest:=Interest+0;
end;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
while yh_Place<Rec-1do
begin
Next;
yh_Place:=yh_Place+1;
payoff:=Cal_Interest[yh_Place].yh_Price;
G_qixian:=Trunc(Date-Cal_Interest[yh_Place].DateTime)-30;
if G_qixian>0 then
Interest:=Payoff*G_qixian*0.01/30
else
Interest:=0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
end;
end;
//end if payoff>0
//-----------------------------------------------------
//2ayoff=0 first write the Interest to the Datebase
//There are two condition:
// (1): hr_place>place (2)hr_Place<place
//------------------------------------------------------
if payoff=0 then
begin
Interest:=Interest+0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
//----------------------------------------------------
//(1):hr_Place>=place
//The Last Remittance just pay off the First yh_Price
//----------------------------------------------------
if hr_Place>=Place then
begin
while yh_Place<Rec-1do
begin
yh_Place:=yh_Place+1;
payoff:=Cal_Interest[yh_Place].yh_Price;
G_qixian:=Trunc(Date-Cal_Interest[yh_Place].DateTime)-30;
if G_qixian>0 then
Interest:=Payoff*G_qixian*0.01/30
else
Interest:=0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
end;
//end while yh_Place<Rec-1
end;
//end if hr_Place>=Place
//-----------------------------------------------------------------------
//(1):hr_Place<place
//The hr_Place Remittance just pay off the First yh_Price
//must save the hr_place and payoff information then
handle the next record
//-----------------------------------------------------------------------
if hr_Place<Place then
begin
yh_Place:=yh_Place+1;
FirstDate:=Cal_Interest[yh_Place].DateTime;
G_qixian:=0;
Sum_G_qixian:=0;
Interest:=0;
payoff:=Cal_Interest[yh_Place].yh_Price;
//if the next hr_Money excess 30 days
G_qixian:=Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)-30-Sum_G_qixian;
if G_qixian>0 then
begin
Sum_G_qixian:=Sum_G_qixian+G_qixian;
Interest:=Interest+payoff*G_qixian*0.01/30;
end
else
begin
Interest:=0;
G_qixian:=0;
Sum_G_qixian:=0;
end;
payoff:=payoff-Cal_Int2[hr_Place].hr_Money;
if payoff<=0 then
begin
Interest:=Interest+0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
end;
if payoff>0 then
Next;
end;
//end if hr_Place<Place
end;
//end if payoff=0
//-------------------------------
//if pay more and hr_place<Place
//-------------------------------
if payoff<0 then
begin
Interest:=Interest+0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
yh_Place:=yh_Place+1;
FirstDate:=Cal_Interest[yh_Place].DateTime;
G_qixian:=0;
Sum_G_qixian:=0;
Interest:=0;
payoff:=payoff+Cal_Interest[yh_Place].yh_Price;
if Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)<=0 then
G_qixian:=Trunc(Cal_Interest[yh_Place+1].DateTime-FirstDate)-30-Sum_G_qixian
else
G_qixian:=Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)-30-Sum_G_qixian;
if G_qixian>0 then
begin
Sum_G_qixian:=Sum_G_qixian+G_qixian;
Interest:=Interest+payoff*G_qixian*0.01/30;
end
else
begin
Interest:=0;
G_qixian:=0;
Sum_G_qixian:=0;
end;
payoff:=payoff-Cal_Int2[hr_Place].hr_Money;
if payoff<=0 then
begin
Interest:=Interest+0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
end;
if payoff>0 then
Next;
end;
//end payoff<0
end;
//end while not eof
end;
//end if rec>2
end;
//end with Table_Interest
Next;
end;
// end while Table_Basic
end;
//end with Table_Basic
end;
1 2003-1-1 8000 5000 30
2 2003-1-5 3000 2000 30
这时应该还欠1000元吧,如果2003-2-6他又买了3000的东东,不过没有给钱!!
序号 时间 优惠价额 汇入金额 期限(天) 利息
1 2003-1-1 8000 5000 30
2 2003-1-5 3000 2000 30
3 2003-2-6 3000 0 30
一个月之后还了3000元
序号 时间 优惠价额 汇入金额 期限(天) 利息
1 2003-1-1 8000 5000 30
2 2003-1-5 3000 2000 30
3 2003-2-6 3000 0 30
4 2003-3-6 0 3000
明白了吗??
我把我的源码贴上来,你看看
procedure TForm_Main.SB_Calculate_InterestClick(Sender: TObject);
var
I:integer;
G_qixian,Sum_G_qixian:integer;
Interest:real;
FirstDate:TDate;
Rec:Integer;
Cal_Interest,Cal_Int2:array of TCalculate;
payoff,payoff1:Real;
place,hr_Place,yh_Place:Integer;
hr_No:integer;
begin
with DM.Table_Basicdo
begin
First;
while not Eofdo
begin
with DM.Table_Interestdo
begin
Active:=True;
Rec:=RecordCount;
SetLength(Cal_Interest,Rec);
For I:=0 to Rec-1do
begin
Cal_Interest.DateTime:=0;
Cal_Interest.yh_Price:=0;
Cal_Interest.hr_Money:=0;
end;
For I:=0 to Rec-1do
begin
Cal_Interest.DateTime:=FieldByName('Date_Time').AsDateTime;
Cal_Interest.yh_Price:=FieldByName('Optimal_Price').AsCurrency;
Cal_Interest.hr_Money:=FieldByName('Remittance').AsCurrency;
Next;
end;
//--------------------------------------------------------
//Calculate Interest
//--------------------------------------------------------
case Rec of
//---------------------------------------------------------
//if only have one record
//---------------------------------------------------------
1:
begin
Interest:=0;
payoff:=Cal_Interest[0].yh_Price-Cal_Interest[0].hr_Money;
G_qixian:=Trunc(Date-Cal_Interest[0].DateTime)-30;
if G_qixian>0 then
begin
Interest:=Interest+payoff*G_qixian*0.01/30;
end
else
Interest:=Interest+0;
if Interest<0 then
Interest:=0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
end;
//-----------------------------------------------
//if there are two records
//-----------------------------------------------
2:
begin
First;
Interest:=0;
payoff:=Cal_Interest[0].yh_Price-Cal_Interest[0].hr_Money;
if Cal_Interest[1].hr_Money>0 then
//if have remittance
begin
G_qixian:=Trunc(Cal_Interest[1].DateTime-Cal_Interest[0].DateTime)-30;
if G_qixian>0 then
//if the second remittance excess 30 days
begin
Interest:=Interest+payoff*G_qixian*0.01/30;
payoff:=payoff-Cal_Interest[1].hr_Money;
if payoff>0 then
//if not pay off
begin
G_qixian:=Trunc(Date-Cal_Interest[1].DateTime);
Interest:=Interest+payoff*G_qixian*0.01/30;
payoff1:=-1;
end;
if payoff=0 then
begin
G_qixian:=Trunc(Cal_Interest[1].DateTime-Cal_Interest[0].DateTime);
Interest:=Interest+payoff*G_qixian*0.01/30;
payoff1:=-1;
end;
if payoff<0 then
begin
payoff1:=payoff;
end;
end
else
//if the second remittance havn't excess 30 days
begin
payoff:=payoff-Cal_Interest[1].hr_Money;
if payoff>0 then
//if not pay off
begin
G_qixian:=Trunc(Date-Cal_Interest[0].DateTime)-30;
if G_qixian>0 then
Interest:=Interest+payoff*G_qixian*0.01/30
else
Interest:=0;
payoff1:=0;
end;
if payoff=0 then
begin
G_qixian:=Trunc(Date-Cal_Interest[0].DateTime)-30;
if G_qixian>0 then
Interest:=Interest+payoff*G_qixian*0.01/30
else
Interest:=0;
payoff1:=0;
end;
if payoff<0 then
begin
payoff1:=payoff;
end;
end;
//end else
G_qixian<30
end
else
//if have no hr_Money
begin
G_qixian:=Trunc(Date-Cal_Interest[0].DateTime)-30;
if G_qixian>0 then
Interest:=payoff*G_qixian*0.01/30
else
Interest:=0;
end;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
begin
if (Cal_Interest[1].yh_Price>0) then
begin
G_qixian:=Trunc(Date-Cal_Interest[1].DateTime)-30;
if G_qixian>0 then
begin
if (payoff1<0) then
payoff:=Cal_Interest[1].yh_Price+payoff1
else
payoff:=Cal_Interest[1].yh_Price;
Interest:=payoff*G_qixian*0.01/30;
end
else
Interest:=0;
end
else
Interest:=0;
end;
if Interest<0 then
Interest:=0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
end;
//end case 2
end;
//end case
//----------------------------------------------------
//if there are three records
//----------------------------------------------------
if rec>2 then
begin
First;
FirstDate:=Cal_Interest[0].DateTime;
place:=0;
hr_No:=0;
For I:=1 to Rec-1do
begin
if Cal_Interest.hr_Money>0 then
hr_No:=hr_No+1;
end;
SetLength(Cal_Int2,hr_No);
For I:=1 to Rec-1do
begin
if Cal_Interest.hr_Money>0 then
begin
Cal_Int2[place].DateTime:=Cal_Interest.DateTime;
Cal_Int2[place].yh_Price:=Cal_Interest.yh_Price;
Cal_Int2[place].hr_Money:=Cal_Interest.hr_Money;
place:=Place+1;
end;
end;
//--------------------------------------------------------------------
//Calculate the first record's Interest
//Cyc to find the pay off place then
save the place and payoff value
//to Calculate the second record:
// 1: payoff > 0 not pay off
// 2: payoff = 0 just pay off
// 3: payoff < 0 pay more
//---------------------------------------------------------------------
hr_Place:=0;
yh_Place:=0;
G_qixian:=0;
Sum_G_qixian:=0;
Interest:=0;
payoff:=Cal_Interest[yh_Place].yh_Price-Cal_Interest[hr_Place].hr_Money;
if payoff<=0 then
Interest:=0;
while not eofdo
begin
while (payoff>0) and (hr_Place<place)do
begin
if Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)<=0 then
G_qixian:=Trunc(Cal_Interest[yh_Place+1].DateTime-FirstDate)-30-Sum_G_qixian
else
G_qixian:=Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)-30-Sum_G_qixian;
if G_qixian>0 then
begin
Sum_G_qixian:=Sum_G_qixian+G_qixian;
Interest:=Interest+payoff*G_qixian*0.01/30;
end
else
begin
Interest:=0;
G_qixian:=0;
Sum_G_qixian:=0;
end;
if Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)<=0 then
payoff:=payoff
else
payoff:=payoff-Cal_Int2[hr_Place].hr_Money;
hr_Place:=hr_Place+1;
if Interest<0 then
Interest:=0;
end;
//end if while payoff>0
//1: haven't pay off
if payoff>0 then
begin
if Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)<=0 then
G_qixian:=Trunc(Date-Cal_Interest[yh_Place+1].DateTime)
else
G_qixian:=Trunc(Date-Cal_Int2[hr_Place-1].DateTime);
if G_qixian>0 then
begin
Interest:=Interest+payoff*G_qixian*0.01/30;
end
else
begin
Interest:=Interest+0;
end;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
while yh_Place<Rec-1do
begin
Next;
yh_Place:=yh_Place+1;
payoff:=Cal_Interest[yh_Place].yh_Price;
G_qixian:=Trunc(Date-Cal_Interest[yh_Place].DateTime)-30;
if G_qixian>0 then
Interest:=Payoff*G_qixian*0.01/30
else
Interest:=0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
end;
end;
//end if payoff>0
//-----------------------------------------------------
//2ayoff=0 first write the Interest to the Datebase
//There are two condition:
// (1): hr_place>place (2)hr_Place<place
//------------------------------------------------------
if payoff=0 then
begin
Interest:=Interest+0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
//----------------------------------------------------
//(1):hr_Place>=place
//The Last Remittance just pay off the First yh_Price
//----------------------------------------------------
if hr_Place>=Place then
begin
while yh_Place<Rec-1do
begin
yh_Place:=yh_Place+1;
payoff:=Cal_Interest[yh_Place].yh_Price;
G_qixian:=Trunc(Date-Cal_Interest[yh_Place].DateTime)-30;
if G_qixian>0 then
Interest:=Payoff*G_qixian*0.01/30
else
Interest:=0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
end;
//end while yh_Place<Rec-1
end;
//end if hr_Place>=Place
//-----------------------------------------------------------------------
//(1):hr_Place<place
//The hr_Place Remittance just pay off the First yh_Price
//must save the hr_place and payoff information then
handle the next record
//-----------------------------------------------------------------------
if hr_Place<Place then
begin
yh_Place:=yh_Place+1;
FirstDate:=Cal_Interest[yh_Place].DateTime;
G_qixian:=0;
Sum_G_qixian:=0;
Interest:=0;
payoff:=Cal_Interest[yh_Place].yh_Price;
//if the next hr_Money excess 30 days
G_qixian:=Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)-30-Sum_G_qixian;
if G_qixian>0 then
begin
Sum_G_qixian:=Sum_G_qixian+G_qixian;
Interest:=Interest+payoff*G_qixian*0.01/30;
end
else
begin
Interest:=0;
G_qixian:=0;
Sum_G_qixian:=0;
end;
payoff:=payoff-Cal_Int2[hr_Place].hr_Money;
if payoff<=0 then
begin
Interest:=Interest+0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
end;
if payoff>0 then
Next;
end;
//end if hr_Place<Place
end;
//end if payoff=0
//-------------------------------
//if pay more and hr_place<Place
//-------------------------------
if payoff<0 then
begin
Interest:=Interest+0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
yh_Place:=yh_Place+1;
FirstDate:=Cal_Interest[yh_Place].DateTime;
G_qixian:=0;
Sum_G_qixian:=0;
Interest:=0;
payoff:=payoff+Cal_Interest[yh_Place].yh_Price;
if Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)<=0 then
G_qixian:=Trunc(Cal_Interest[yh_Place+1].DateTime-FirstDate)-30-Sum_G_qixian
else
G_qixian:=Trunc(Cal_Int2[hr_Place].DateTime-FirstDate)-30-Sum_G_qixian;
if G_qixian>0 then
begin
Sum_G_qixian:=Sum_G_qixian+G_qixian;
Interest:=Interest+payoff*G_qixian*0.01/30;
end
else
begin
Interest:=0;
G_qixian:=0;
Sum_G_qixian:=0;
end;
payoff:=payoff-Cal_Int2[hr_Place].hr_Money;
if payoff<=0 then
begin
Interest:=Interest+0;
Edit;
FieldByName('Interest').AsCurrency:=Interest;
Post;
Next;
end;
if payoff>0 then
Next;
end;
//end payoff<0
end;
//end while not eof
end;
//end if rec>2
end;
//end with Table_Interest
Next;
end;
// end while Table_Basic
end;
//end with Table_Basic
end;