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

📄 ucalculator.pas

📁 EXE+代码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
          top:=top-1;
          case c of
          '+': v := x + y;
          '-': v := x - y;
          '*': v := x * y;
          '/': begin
               if y<>0 then begin
                  v := x / y; // 第一操作数 / 第二操作数 注意操作数顺序对除法的影响
                  end else begin
                  calculateexpress:='Dividend By Zero' ;exit;end;
               end;
          else v := 0;
          end; //end case
          top:=top+1;
          Operands[Top] := v;//push 中间结果再次入栈
          end;
       end; //end for
       v := Operands[Top]; //pop 最终结果
       top:=top-1;
       // showmessage('计算结果为:    '+floattostr(v));
 //////////计算后缀表达式:结束

       result:=floattostr(v);
   end;


procedure TForm1.Button2Click(Sender: TObject);
  var
    strexpress:string;
    iR,i:integer;
    a:char;
    tag:integer;
begin
   tag:=0;
    //输入:取得表达式

    iR:=sg.row;
    strexpress:=trim(sg.Rows[iR].Text);
    if pos('=',strexpress)<>0 then
       begin
       strexpress:=LeftStr(strexpress,pos('=',strexpress)-1);
       strexpress:=trim(strexpress);
       end;
    //====
    for i:=1 to length(strexpress) do
          begin
          a:=copy(trim(strexpress),i,1)[1];
          if ord(a)>127 then
          tag:=1
          end;
      if  trim(strexpress)='' then
          begin
          messagebox(handle,'对不起!表达式不能为空!',pchar(caption),mb_ok+mb_iconexclamation);
          exit;
          end
      else if  tag=1 then
          begin
          messagebox(handle,'对不起!表达式中包含全角字符!'+#10#13+'请改用半角字符!'+#10#13+'可能是 (  或  ) ',pchar(caption),mb_ok+mb_iconexclamation);
          exit;
          end
      else
          begin
          //输出:结果
               sg.rows[iR].text:=strexpress+'='+calculateexpress(strexpress);
               //将焦点移到下一行
               if sg.Row>=(sg.RowCount-1) then
                  sg.RowCount:=sg.RowCount+1;
               sg.Row:=sg.Row+1;
          end;

end;


procedure TForm1.FormResize(Sender: TObject);
begin
sg.ColWidths[0]:=form1.Width-20;
end;

procedure TForm1.sgKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if Key=VK_RETURN  then
   begin
  Button3.Click;
    if sg.Row>=(sg.RowCount-1) then
                  sg.RowCount:=sg.RowCount+1;
    sg.Row:=sg.Row+1;
   end;
IF Key=VK_DOWN then
    sg.RowCount:=sg.RowCount+1;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
 ts:string;
 s:Tstrings;
begin
   s:=TstringList.Create;
   s.Add(trim(sg.Rows[sg.row].Text)) ;
   if pos('=',s.Strings[0])<>0 then
       begin
       s.Strings[0]:=LeftStr(s.Strings[0],pos('=',s.Strings[0])-1);
       s.Strings[0]:=trim(s.Strings[0]);
       end;
   ExprParser1.Expression.Assign(s);
 try
   ts:=ExprParser1.F([3,2,4])
 except
   ts:='Error';
 end;    
   sg.rows[sg.row].text:=s.Strings[0]+'='+ts;
   sb.Panels[0].Text:=s.Strings[0]+'='+ts;
   s.Destroy
end;

function TForm1.ExprParser1Function(Sender: TObject;
  const FuncName: String; const FuncParams: array of Variant): Variant;
var I : Integer;
begin
   Result:=0;
   If CompareText(FuncName,'MyFunc')=0 then begin
     if High(FuncParams)<2 then
       raise ESyntaxError.Create(msgMismatchArgs+': '+FuncName+'('+')');
     Result:=Extended(FuncParams[0])*Extended(FuncParams[1])+Extended(FuncParams[2]);
   end else
   If CompareText(FuncName,'MyFunc1')=0 then begin
     if High(FuncParams)<1 then
       raise ESyntaxError.Create(msgMismatchArgs+': '+FuncName+'('+')');
     Result:=SQR(FuncParams[0]) + Extended(FuncParams[1]);
   end else
   If CompareText(FuncName,'MySumFunc')=0 then begin
     For I:=0 to Integer(High(FuncParams)) do Result:=Result+Extended(FuncParams[I]);
   end else raise Exception.Create('Unknown function name: '+FuncName);
end;

procedure TForm1.Button22Click(Sender: TObject);
var
  Pos: TPoint;
  Handle: HWND;
  Buf: array[0..16] of Char;
  s:string;
begin
  GetCursorPos(Pos); // 得到当前光标位置
  Handle := WindowFromPoint(Pos); // 返回当前位置的句柄
  GetClassName(Handle, Buf, 1024); // 得到类名
//  form1.Caption := IntToStr(Handle);
  getwindowtext(handle,buf,16);
  sg.SetFocus; s:=buf;
  s:=formatstr(s);
  SendKeys(Pchar(s),False);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
sendmessage(sg.handle,WM_KEYDOWN,VK_RETURN,0)
end;

procedure TForm1.Button4Click(Sender: TObject);
var i:integer;
begin
for i:=0 to sg.Row do sg.Rows[i].Text:='';
sg.RowCount:=0;
end;
procedure TForm1.Button28Click(Sender: TObject);
begin //模拟退格键
  postmessage(sg.Handle,WM_KEYDOWN,VK_BACK,0);
end;

procedure TForm1.Sin1Click(Sender: TObject);
begin
sg.SetFocus; sendkeys(PChar(formatstr((sender as Tmenuitem).caption)),false);
end;

procedure TForm1.SaveToFile1Click(Sender: TObject);
Var s:TStrings; i:integer;
begin
 s:=TstringList.Create;
 for i:=0 to sg.RowCount-1 do      s.Add(sg.Rows[i].Text);
 s.SaveToFile('History.txt');  s.Destroy;
 sb.Panels[0].Text:='File saved.';
end;



procedure tform1.ufOnClick(sender:TObject);
var
s:string;
begin
s:=stringreplace((sender as TmenuItem).Caption,'&','',[rfReplaceAll]) ;
sendkeys(pchar(formatstr(iniF.ReadString(s,'Expression','',))),false);
sb.Panels[0].Text:=iniF.ReadString(s,'Hint','Error');
end;
{
ini文件的读写:
1:在Interface 的 uses节增加 IniFiles;
2:定义 Myinifiel:Tinifile;
3:打开ini 文件
   myinifile:=Tinifile.create(`program.ini`);
4:默认的Ini文件在Windows目录下,
   当前INI文件Filename:=ExtractFilePath(Paramstr(0))+`program.ini`;
5: 读INI文件
   vs:=myinifile.Readstring(`小节名`,`关键字`,缺省值);
   vi:=myinifile.Readinteger(`小节名`,`关键字`,缺省值);
   vb:=myinifile.Readbool(`小节名`,`关键字`,缺省值);
6:写入INI文件
    myinifile.writestring(`小节名`,`关键字`,变量或字符串值);
    myinifile.writeinteger(`小节名`,`关键字`,变量或整型数值);
    myinifile.writebool(`小节名`,`关键字`,变量或True或False);
    当这个INI文件不存在时,上面的语句还会自动创建该INI文件。 
7:删除关键字
    myinifile.DeleteKey(`小节名`,`关键字`);
8:小节操作
    增加一个小节可用写入的方法来完成,删除一个小节可用下面的对象方法:
    myinifile.EraseSection(`小节名`);
    另外Tinifile类还提供了三种对象方法来对小节进行操作:
    myinifile.readsection(`小节名`,Tstrings变量);可将指定小节中的所有关键字名读取至一个字符串列表变量中;
    myinifile.readsections(Tstrings变量);可将INI文件中所有小节名读取至一个字符串列表变量中去。
    myinifile.readsectionvalues(`小节名`,Tstrings变量);可将INI文件中指定小节的所有行(包括关键字、=、值)读取至一个字符串列表变量中去。
9:释放
    在适当的位置用下面的语句释放myinifile:
    myinifile.distory;

}
procedure TForm1.FormCreate(Sender: TObject);
var
StrMs,StrSMs:Tstrings;
strm,strsm:string;
im,ism:integer;
begin
Strms:=TStringlist.Create;StrSMs:=TstringList.Create;
//得到所有字段名字
iniF:=TiniFile.Create(ExtractFilePath(Paramstr(0))+'Function.ini');
iniF.ReadSection('menus',Strms); //一级菜单 集合
iniF.ReadSections(StrSMs);      //子菜单   集合
//根据 段名字读特定子键的值
sb.Panels[0].text:=inif.ReadString('Test Function','text','null');
//添加菜单
for im:=0 to strms.Count-1 do
 begin
 strm:=trim(inif.ReadString('menus',strms[im],'null'));
 pm.Items.Insert(0,NewItem(strm,0,False,True,nil,0,'UserDefine'+IntTostr(im)));
 for ism:=0 to StrSMs.Count-1 do
 begin
  if trim(iniF.ReadString(Strsms[ism],'parent','Null'))=StrM then
     begin
     ipm:=ipm+1;
     pm.Items[0].Add(NewItem(StrSMs[ism],0,False,True,ufOnclick,0,'UserFunction'+inttostr(ipm)));
     end;
 end;
end;
 Strms.Free;StrSMs.Free;
end;


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

procedure TForm1.Help1Click(Sender: TObject);
begin
showmessage('目录下的Function.ini文件可以自己扩充快捷函数');
end;

end.
//--------

⌨️ 快捷键说明

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