⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 汽车油耗计算工具 计算个人汽车的油耗记录
💻 PAS
📖 第 1 页 / 共 2 页
字号:
  //计算汇总信息
  TKM:=0.00;
  TOil:=0.00;
  TMoney:=0.00;
  tmpd1:=0.00;
  tmpd2:=0.00;
  begno:=0;
  isF:=True;
  listcnt:=ListView1.Items.Count;

  if listcnt>1 then
  begin
    for i:=1 To listcnt-1 do
    begin
      TOil:=TOil+myStrToFloat(ListView1.Items[i].SubItems.Strings[3],3);
      TMoney:=TMoney+myStrToFloat(ListView1.Items[i].SubItems.Strings[5],3);

      //更新未加满油的油耗计算处理
      if ListView1.Items[i].SubItems.Strings[6]='否' then
      begin
        if isF=True then
          begno:=i;
        isF:=False;
        tmpd1:=tmpd1+myStrToFloat(ListView1.Items[i].SubItems.Strings[2],5);
        tmpd2:=tmpd2+myStrToFloat(ListView1.Items[i].SubItems.Strings[3],3);
      end
      else
      begin
        if isF=False then
        begin
          tmpd1:=tmpd1+myStrToFloat(ListView1.Items[i].SubItems.Strings[2],5);
          tmpd2:=tmpd2+myStrToFloat(ListView1.Items[i].SubItems.Strings[3],3);

          if tmpd1<>0 then
            for j:=begno To i do
              ListView1.Items[j].SubItems.Strings[7]:=format('%.2f',[tmpd2*100/tmpd1])+' 升/百公里';
        end
        else
        begin
          tmpd1:=myStrToFloat(ListView1.Items[i].SubItems.Strings[2],5);
          tmpd2:=myStrToFloat(ListView1.Items[i].SubItems.Strings[3],3);

          if tmpd1<>0 then
            ListView1.Items[i].SubItems.Strings[7]:=format('%.2f',[tmpd2*100/tmpd1])+' 升/百公里';
        end;

        isF:=True;
        tmpd1:=0.00;
        tmpd2:=0.00;
        begno:=0;
      end;
    end;
    
    //至结尾全部是未加满油的情况
    if isF=False then
    begin
      for j:=begno To listcnt-1 do
            ListView1.Items[j].SubItems.Strings[7]:='不可计算';
    end;

    tmpd1:=myStrToFloat(ListView1.Items[0].SubItems.Strings[1],5);
    tmpd2:=myStrToFloat(ListView1.Items[Listcnt-1].SubItems.Strings[1],5);
    TKM:=tmpd2-tmpd1;

    TDays:=StrToDate(ListView1.Items[listcnt-1].SubItems.Strings[0])-
         StrToDate(ListView1.Items[0].SubItems.Strings[0]);

    if Toil<>0 then
      v_AvgPrice:=Trunc(Tmoney/TOil*100)/100;
    if TDays<>0 then
      v_AvgKM:=Trunc(TKM/TDays*100)/100;
    if TKM<>0 then
    begin
      v_AvgOil:=Trunc(TOil/TKM*10000)/100;
      v_AvgMoney:=Trunc(TMoney/TKM*100)/100;
    end;
  end;

  //显示汇总信息
  TotalKM.Caption:=FloatToStr(TKM)+' 公里 ';
  TotalOil.Caption:=format('%.2f',[TOil])+' 升 ';
  TotalMoney.Caption:=format('%.2f',[TMoney])+' 元 ';

  TotalDays.Caption:=FloatToStr(TDays)+' 天 ';
  AvgOilMoney.Caption:=format('%.2f',[v_AvgPrice]) +' 元/升  ';
  AvgKM.Caption:=format('%.2f',[v_AvgKM])+' 公里/天  ';
  AvgOil.Caption:=format('%.2f',[v_AvgOil])+' 升/百公里  ';
  AvgMoney.Caption:=format('%.2f',[v_AvgMoney])+'  元/公里  ';

end;

procedure TForm1.BtnDeleteClick(Sender: TObject);
var
  i,j: integer;
  tmpd1,tmpd2: double;
  delflag: integer;
begin
    delflag:=0;  //未选择

    ListView1.Items.BeginUpdate();

    for i:=0 to ListView1.Items.Count-1 do
    begin
      if ListView1.Items[i].Selected then //i=ListView1.Selected.index
      begin
        if Application.MessageBox('是否确认删除选择的记录? ',SYS_NAME,MB_YESNO+MB_ICONQUESTION)=IDYes then
        begin
          //删除当前选中行
          delflag:=1;  //已选中
          ListView1.Items.Delete(i);

          if (ListView1.Items.Count<>0) and (i<>ListView1.Items.Count) then
          begin
            //更新删除列之后,对应下一条记录的行驶公里数
            tmpd1:=myStrToFloat(ListView1.Items[i].SubItems.Strings[1],5);
            tmpd2:=myStrToFloat(ListView1.Items[i-1].SubItems.Strings[1],5);
            ListView1.Items[i].SubItems.Strings[2]:=FloatToStr(tmpd1-tmpd2)+' 公里';

            //更新删除列之后的记录编号
            for j:=i to ListView1.Items.Count-1 do
              ListView1.Items[j].Caption:=IntToStr(j+1);
          end;
        end
        else
          delflag:=2;  //选中但选择了否

        //计算并显示汇总信息
        showSumInfo();

        break;
      end;
     end;

     ListView1.Items.EndUpdate();

     if delflag=0 then
       Application.MessageBox('请先选择要删除的加油记录! ',SYS_NAME,MB_OK+MB_ICONINFORMATION);
end;

//判断是否新增了加油站列表,是则增加进入OilStationlist
procedure TForm1.Add_OilStationList(oilstname:string);
var
  i : integer;               //循环变量
begin
  oilstname:=trim(oilstname);

  if (oilstname<>' ') and (length(oilstname)>0) then
  begin
  if( OilSNum=0 ) then
  begin
    OilStationlist[1].name:=oilstname;
    OilStationlist[1].itemindex:=0;
    OilSNum:=1;
  end
  else
  begin
    //记录加油站的列表
    for i:=1 to OilSNum do
    begin
      if( OilStationlist[i].name=oilstname ) then
        break
      else if( i=OilSNum ) then
      begin
        OilSNum:=OilSnum+1;
        OilStationlist[OilSNum].itemindex:=OilSNum;
        OilStationlist[OilSNum].name:=oilstname;
      end;
    end;
    end;
  end;
end;

//设置Form2的显示信息,flag: =0,设初始值;=1,设置回显值
procedure TForm1.setShowValue(AddOilInfo: TAddOilInfo; flag: integer);
var
  i: integer;
begin
  if flag=0 then
  begin
    Form2.AddOilDate.Date:=Date;
    Form2.CurKM.Text:='0';
    Form2.OilType.ItemIndex:=2;
    Form2.OilPrice.Text:='0.00';
    Form2.Oil.Text:='0';
    Form2.Money.Text:='0.00';
    Form2.isFull.ItemIndex:=0;

    //缺省上次加油的加油站、油价、油类型
    if ListView1.Items.Count>=1 then
    begin
      for i:=1 to OilSNum do
      begin
        if( OilStationlist[i].name=ListView1.Items[ListView1.Items.Count-1].SubItems.Strings[8] ) then
        begin
          OilStnindex:=i-1;
          break;
        end;
      end;
      
      if ListView1.Items[ListView1.Items.Count-1].SubItems.Strings[4]='柴油' then
        Form2.OilType.ItemIndex:=0
      else if ListView1.Items[ListView1.Items.Count-1].SubItems.Strings[4]='90#汽油' then
        Form2.OilType.ItemIndex:=1
      else if ListView1.Items[ListView1.Items.Count-1].SubItems.Strings[4]='93#汽油' then
        Form2.OilType.ItemIndex:=2
      else if ListView1.Items[ListView1.Items.Count-1].SubItems.Strings[4]='97#汽油' then
        Form2.OilType.ItemIndex:=3;

      Form2.OilPrice.Text:=FloatToStr(myStrToFloat(ListView1.Items[ListView1.Items.Count-1].SubItems.Strings[9],3));
    end
    else
      Form2.OilStation.ItemIndex:=0;

    Form2.AddOilMemo.Text:=' ';
  end
  else
  begin
    Form2.AddOilDate.Date:=StrToDate(AddOilInfo.AddOilDate);
    Form2.CurKM.Text:=AddOilInfo.CurKM;

    if AddOilInfo.OilType='柴油' then
      Form2.OilType.ItemIndex:=0
    else if AddOilInfo.OilType='90#汽油' then
      Form2.OilType.ItemIndex:=1
    else if AddOilInfo.OilType='93#汽油' then
      Form2.OilType.ItemIndex:=2
    else if AddOilInfo.OilType='97#汽油' then
      Form2.OilType.ItemIndex:=3;

    Form2.OilPrice.Text:=AddOilInfo.OilPrice;
    Form2.Oil.Text:=AddOilInfo.Oil;
    Form2.Money.Text:=AddOilInfo.Money;
    if AddOilInfo.isFull='是' then
      Form2.isFull.ItemIndex:=0
    else
      Form2.isFull.ItemIndex:=1;

    //定位OilStation的选项itemindex
    for i:=1 to OilSNum do
    begin
      if( OilStationlist[i].name=AddOilInfo.OilStation ) then
      begin
        OilStnindex:=i-1;
        break;
      end
    end;
    
    Form2.AddOilMemo.Text:=AddOilInfo.AddOilMemo;
  end;
end;

procedure TForm1.BtnModifyClick(Sender: TObject);
var
  i: integer;
begin
  for i:=0 to ListView1.Items.Count-1 do
  begin
    if ListView1.Items[i].Selected then
    begin
      ModifyID:=i;
      AddOilInfo.serial:=IntToStr(i);
      AddOilInfo.AddOilDate:=ListView1.Items[i].SubItems.Strings[0];
      AddOilInfo.CurKM:=FloatToStr(myStrToFloat(ListView1.Items[i].SubItems.Strings[1],5));
      AddOilInfo.OilType:=ListView1.Items[i].SubItems.Strings[4];
      AddOilInfo.Oil:=FloatToStr(myStrToFloat(ListView1.Items[i].SubItems.Strings[3],3));
      AddOilInfo.Money:=FloatToStr(myStrToFloat(ListView1.Items[i].SubItems.Strings[5],3));
      AddOilInfo.isFull:=ListView1.Items[i].SubItems.Strings[6];
      AddOilInfo.OilStation:=ListView1.Items[i].SubItems.Strings[8];
      AddOilInfo.OilPrice:=FloatToStr(myStrToFloat(ListView1.Items[i].SubItems.Strings[9],3));
      AddOilInfo.AddOilMemo:=ListView1.Items[i].SubItems.Strings[10];
      setShowValue( AddOilInfo, 1 );
      Form2.show();
      break;
    end;
  end;
  if i=ListView1.Items.Count then
    Application.MessageBox('请先选择要修改的加油记录! ',SYS_NAME,MB_OK+MB_ICONINFORMATION);
end;

//将str串中的cutstr字符串截取掉,返回新的str
function TForm1.MyStrTOFloat(str:string; end_cutlen:integer):double;
var
  tmps1: string;
begin
  tmps1:=copy( str, 0, length(str)-end_cutlen );
  MyStrToFloat:=StrToFloat(tmps1);
end;

procedure TForm1.BtnGenReportClick(Sender: TObject);
var
  rptfp : TextFile;
  rptname : string;
  SysTmpDir : pchar;
  i : integer;
  tmps1: string;
begin
  GetMem(SysTmpDir,100);
  GetTempPath( 100, SysTmpDir );
  rptname:=SysTmpDir+'OilRecReport.htm';
  FreeMem(SysTmpDir);

  AssignFile(rptfp, rptname ); //打开文件
  rewrite( rptfp );

  writeln(rptfp,'<html><head><title>油耗全记录</title>');
  writeln(rptfp,'<meta http-equiv="Content-Type" content="text/html; charset=gb2312">');
  writeln(rptfp,'<STYLE>');
  writeln(rptfp,'BODY {FONT-SIZE: 14px; SCROLLBAR-ARROW-COLOR: #0099cc; SCROLLBAR-BASE-COLOR:#e8f4ff; BACKGROUND-COLOR: #ffffff}');
  writeln(rptfp,'TABLE {FONT-SIZE: 14px; COLOR: #000000; FONT-FAMILY: "Tahoma", "Verdana"}');
  writeln(rptfp,'</STYLE></head><body bgcolor="#FFFFFF" text="#000000">');
  writeln(rptfp,'<div align="center">  <h1>油耗全记录</h1></div>');
  writeln(rptfp,'<table width="100%" border="1" cellpadding="3" cellspacing="0">');
  writeln(rptfp,'<tr>');
  writeln(rptfp,'<td bgcolor="#99FFCC" height="11"><div align="center"><b>序号</b></div></td>');
  writeln(rptfp,'<td bgcolor="#99FFCC" height="11"><div align="center"><b>日期</b></div></td>');
  writeln(rptfp,'<td bgcolor="#99FFCC" height="11"><div align="center"><b>里程表读数</b></div></td>');
  writeln(rptfp,'<td bgcolor="#99FFCC" height="11"><div align="center"><b>行驶里程</b></div></td>');
  writeln(rptfp,'<td bgcolor="#99FFCC" height="11"><div align="center"><b>加油站</b></div></td>');
  writeln(rptfp,'<td bgcolor="#99FFCC" height="11"><div align="center"><b>加油量</b></div></td>');
  writeln(rptfp,'<td bgcolor="#99FFCC" height="11"><div align="center"><b>燃油类型</b></div></td>');
  writeln(rptfp,'<td bgcolor="#99FFCC" height="11"><div align="center"><b>单价</b></div></td>');
  writeln(rptfp,'<td bgcolor="#99FFCC" height="11"><div align="center"><b>金额</b></div></td>');
  writeln(rptfp,'<td bgcolor="#99FFCC" height="11"><div align="center"><b>加满</b></div></td>');
  writeln(rptfp,'<td bgcolor="#99FFCC" height="11"><div align="center"><b>油耗估算</b></div></td>');
  writeln(rptfp,'</tr>');

  for i:=0 to ListView1.Items.Count-1 do
  begin
    writeln(rptfp,'<tr bgcolor="#FFFFFF">');
    tmps1:='<td height="11"><div align="center">'+ListView1.Items[i].Caption+'</div></td>';
    writeln(rptfp, tmps1 );
    tmps1:='<td height="11"><div align="center">'+ListView1.Items[i].SubItems.Strings[0]+'</div></td>';
    writeln(rptfp, tmps1 );
    tmps1:='<td height="11"><div align="center">'+ListView1.Items[i].SubItems.Strings[1]+'</div></td>';
    writeln(rptfp, tmps1 );
    tmps1:='<td height="11"><div align="center">'+ListView1.Items[i].SubItems.Strings[2]+'</div></td>';
    writeln(rptfp, tmps1 );
    tmps1:='<td height="11"><div align="center">'+ListView1.Items[i].SubItems.Strings[8]+'</div></td>';
    writeln(rptfp, tmps1 );
    tmps1:='<td height="11"><div align="center">'+ListView1.Items[i].SubItems.Strings[3]+'</div></td>';
    writeln(rptfp, tmps1 );
    tmps1:='<td height="11"><div align="center">'+ListView1.Items[i].SubItems.Strings[4]+'</div></td>';
    writeln(rptfp, tmps1 );
    tmps1:='<td height="11"><div align="center">'+ListView1.Items[i].SubItems.Strings[9]+'</div></td>';
    writeln(rptfp, tmps1 );
    tmps1:='<td height="11"><div align="center">'+ListView1.Items[i].SubItems.Strings[5]+'</div></td>';
    writeln(rptfp, tmps1 );
    tmps1:='<td height="11"><div align="center">'+ListView1.Items[i].SubItems.Strings[6]+'</div></td>';
    writeln(rptfp, tmps1 );
    tmps1:='<td height="11"><div align="center">'+ListView1.Items[i].SubItems.Strings[7]+'</div></td>';
    writeln(rptfp, tmps1 );
    writeln(rptfp,'</tr>');
  end;

  writeln(rptfp,'</table><br>');
  writeln(rptfp,'<table border="0" align="center">');
  tmps1:='<tr><td>总里程:'+TotalKM.Caption+'</td>';
  writeln(rptfp,tmps1);
  tmps1:='<td width="50">&nbsp;</td><td>天数:'+TotalDays.Caption+'</td>';
  writeln(rptfp,tmps1);
  tmps1:='<td width="50">&nbsp;</td><td>平均里程:'+AvgKM.Caption+'</td></tr>';
  writeln(rptfp,tmps1);
  tmps1:='<tr><td>总耗油量:'+TotalOil.Caption+'</td><td width="50">&nbsp;</td><td>&nbsp;</td>';
  writeln(rptfp,tmps1);
  tmps1:='<td width="50">&nbsp;</td><td>平均油耗:'+AvgOil.Caption+'</td></tr>';
  writeln(rptfp,tmps1);
  tmps1:='<tr> <td>总油费:'+TotalMoney.Caption+'</td><td width="50">&nbsp;</td>';
  writeln(rptfp,tmps1);
  tmps1:='<td>平均油价:'+AvgOilMoney.Caption+'</td><td width="50">&nbsp;</td>';
  writeln(rptfp,tmps1);
  tmps1:='<td>平均油费:'+AvgMoney.Caption+'</td></tr></table>';
  writeln(rptfp,tmps1);
  writeln(rptfp,'<table width="790" border="0" align="center">');
  writeln(rptfp,'<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>');
  writeln(rptfp,'<tr><td colspan="4" height="5" bgcolor="#FFFF99"></td></tr>');
  writeln(rptfp,'<tr><td colspan="4"><div align="center">本页面由油耗记录工具自动生成<br>');
  writeln(rptfp,'版权所有 &copy; 2006 Daniel</div></td></tr></table><p>&nbsp;</p></body></html>');

  CloseFile(rptfp);//关闭文件

  ShellExecute(handle, 'open', pchar(rptname), ' ', '',SW_SHOWNORMAL)

end;

procedure TForm1.BtnAboutClick(Sender: TObject);
begin
  Form3.Show();
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  savDat2Xml();
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  if CurKM_Err=true then
  begin
    Application.MessageBox('请检查里程表读数是否正确! ',SYS_NAME,MB_OK+MB_ICONINFORMATION);
    ListView1.SetFocus;
    ListView1.Items[ErrListID].Selected := True;
  end;
end;

procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  DefaultDraw:=True;
  if Item.Index mod 2 =0 then
    Sender.Canvas.Font.Color:=clBlue; //clAppWorkSpace //clGradientActiveCaption
//  else  Sender.Canvas.Font.Color:=clGradientActiveCaption; //clSkyBlue;
  end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -