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

📄 unit2.pas

📁 delphi 环境下的一个游戏
💻 PAS
字号:
unit Unit2;

interface

uses
    Sysutils,Math;

    //定位最后一个算术符号的位置
    function AnyLastPos(Str:String):integer;
    //定位最先一个算术符号的位置
    function AnyFirstPos(Str:String):integer;
    //判断最先出现的符号是+号、-号、*号还是/号
    function AnyFirstF(Str:String):Char;
    //此计算用于计算不带()号的加、减、乘、除运算
    function SubCompute(Str:String):integer;
    //用于计算表达式的结果
    function TotalCompute(Str:String):integer;


implementation

function AnyLastPos(Str:String):integer;
var
SubPos:integer;
PluPos:integer;
MulPos:integer;
DivPos:integer;
Pos:Integer;
begin
   //定位字符串中最后一个运算符的位置
   SubPos:=LastDelimiter('-',Str);
   PluPos:=LastDelimiter('+',Str);
   MulPos:=LastDelimiter('*',Str);
   DivPos:=LastDelimiter('/',Str);
   Pos:=SubPos;
   if (Pos<PluPos) then
        Pos:=PluPos;
   if (Pos<MulPos) then
        Pos:=MulPos;
   if (Pos<DivPos) then
        Pos:=DivPos;
   AnyLastPos:=Pos; //结束函数,返回位置
end;

//定位最先一个算术符号的位置
function AnyFirstPos(Str:String):integer;
var
SubPos:integer;
PluPos:integer;
MulPos:integer;
DivPos:integer;
ForPos:integer;
FirstPos:integer;
begin
   //定位字符串中最先一个运算符的位置
   SubPos:=Pos('-',Str);
   PluPos:=Pos('+',Str);
   MulPos:=Pos('*',Str);
   DivPos:=Pos('/',Str);
   ForPos:=Pos('^',Str);
   FirstPos:=200;
   if (SubPos=0) then //如果没有-号
        SubPos:=200; //将SubPos设置成一个不可能的值
   if (PluPos=0) then //如果没有+号
        PluPos:=200; //将PluPos设置成一个不可能的值
   if (MulPos=0) then //如果没有*号
        MulPos:=200; //将MulPos设置成一个不可能的值
   if (DivPos=0) then //如果没有/号
        DivPos:=200; //将DivPos设置成一个不可能的值
   if (ForPos=0) then //如果没有^号
        ForPos:=200; //将ForPos设置成一个不可能的值

    if(FirstPos>SubPos) then
      FirstPos:=SubPos;
    if(FirstPos>PluPos) then
      FirstPos:=PluPos;
    if(FirstPos>MulPos) then
      FirstPos:=MulPos;
    if(FirstPos>DivPos) then
      FirstPos:=DivPos;
    if(FirstPos>ForPos) then
      FirstPos:=ForPos;

   AnyFirstPos:=FirstPos; //结束函数,返回位置
end;

//判断最先出现的符号是+号、-号、*号还是/号
function AnyFirstF(Str:String):Char;
var
SubPos:integer;
PluPos:integer;
MulPos:integer;
DivPos:integer;
Operator:char;
tempPos:integer;
begin
   SubPos:=Pos('-',Str);
   PluPos:=Pos('+',Str);
   MulPos:=Pos('*',Str);
   DivPos:=Pos('/',Str);

   if (SubPos=0) then //如果没有-号
        SubPos:=200; //将SubPos设置成一个不可能的值
   if (PluPos=0) then //如果没有+号
        PluPos:=200; //将PluPos设置成一个不可能的值
   if (MulPos=0) then //如果没有*号
        MulPos:=200; //将MulPos设置成一个不可能的值
   if (DivPos=0) then //如果没有/号
        DivPos:=200; //将DivPos设置成一个不可能的值

   Operator:='-';
   tempPos:=SubPos;

   if(tempPos>PluPos) then
   begin
      tempPos:=PluPos;
      Operator:='+';
   end;
   if(tempPos>MulPos) then
   begin
      tempPos:=MulPos;
      Operator:='*';
   end;
   if(tempPos>DivPos) then
   begin
      tempPos:=DivPos;
      Operator:='/';
   end;

   AnyFirstF:=Operator; //结束函数,返回位置
end;

//此计算用于计算不带()号的加、减、乘、除运算
function SubCompute(Str:String):integer;
var
Middle:String;
Mul2:String;
Right:String;
First:integer;
tempStr:String;
temp:integer;
Left:String;
Mul1:String;
MulPos:Integer;
DivPos:Integer;
Fuhao:Char;
begin
  Middle:='';
  Mul2:='';
  Right:='';

 //定位第一个^号位置 ,计算乘方
 First:=Pos('^',Str);
 if (First<>0) then//循环计算乘方
 begin
    tempStr:=Copy(Str,1,First-1);
    temp:=AnyLastPos(tempStr);
    Left:=Copy(Str,1,temp);
    Mul1:=Copy(str,temp+1,First-temp-1);
    tempStr:=Copy(str,First+1,Length(str)-First);
    temp:=AnyFirstPos(tempStr);
    if(temp=200) then
    begin
      Mul2:=tempStr;
      Right:='';
    end
    else
    begin
      Mul2 :=Copy(tempStr,1,temp-1);
      Right:=Copy(tempStr,temp,Length(tempStr)-temp+1);
    end;
    Middle:=FloatToStr(IntPower(StrToInt(Mul1),StrToInt(Mul2)));
    Str:=Left+Middle+Right;
    First:=Pos('^',Str);
 end;

 //定位第一个*号或/号的位置
 MulPos:=Pos('*',Str);
 DivPos:=Pos('/',Str);
 First:=MulPos;
 if (MulPos>DivPos) then
   First:=DivPos;
 if ((DivPos=0) and (MulPos<>0)) then
 begin
    First:=MulPos;
    DivPos:=2000; // 将除号所在位置设置成一个大于MulPos但又不可能的值
 end;
 if ((DivPos<>0) and (MulPos=0)) then
 begin
    First:=DivPos; // 将乘号所在位置设置成一个大于DivPos但不可能的值
    MulPos:=2000;
 end;
 while(First<>0) do//循环计算乘、除
 begin
    tempStr:=Copy(Str,1,First-1);
    temp:=AnyLastPos(tempStr);
    Left:=Copy(Str,1,temp);
    Mul1:=Copy(Str,temp+1,First-temp-1);
    tempStr:=Copy(Str,First+1,Length(Str)-First);
    temp:=AnyFirstPos(tempStr);
    if(temp=200) then
    begin
      Mul2:=tempStr;
      Right:='';
    end
    else
    begin
      Mul2 :=Copy(tempstr,1,temp-1);
      Right:=Copy(tempStr,temp,Length(tempStr)-temp+1);
    end;
    if(MulPos>DivPos) then
       Middle:=IntToStr(StrToInt(Mul1) div StrToInt(Mul2))
    else
       Middle:=IntToStr(StrToInt(Mul1)*StrToInt(Mul2));
    Str:=Left+Middle+Right;

    MulPos:=Pos('*',Str);
    DivPos:=Pos('/',Str);
    First:=MulPos;
    if (MulPos>DivPos) then
       First:=DivPos;

    if((DivPos=0) and (MulPos<>0)) then
    begin
       First:=MulPos;
       DivPos:=2000; // 将除号所在位置设置成一个大于MulPos但又不可能的值
    end;
    if((DivPos<>0) and (MulPos=0)) then
    begin
       First:=DivPos; // 将乘号所在位置设置成一个大于DivPos但不可能的值
       MulPos:=2000;
    end;
 end;
 //定位+、-号首先出现的位置
 First:=AnyFirstPos(Str);
 if (First=200) then//如果没有+、-号,则可以直接返回结果
 begin
   SubCompute:=StrToInt(Str);
   exit;
 end;
 Fuhao:=AnyFirstF(Str); //确定首先出现的符号是+号还是-号
 while (First<>0) do
 begin
     //如果找到+号或-号
     tempStr:=Copy(Str,1,First-1);
     temp:=AnyLastPos(tempStr);
     Left:=Copy(Str,1,temp);
     Mul1:=Copy(Str,temp+1,First-temp-1);
     tempStr:=Copy(Str,First+1,Length(Str)-First);
     temp:=AnyFirstPos(tempStr);
     if(temp=200) then
     begin
        Mul2:=tempStr;
        Right:='';
     end
     else
     begin
        Mul2 :=Copy(tempStr,1,temp-1);
        Right :=Copy(tempStr,temp,Length(tempStr)-temp+1);
     end;
     if (Fuhao='+') then
       Middle:=IntToStr(StrToInt(Mul1)+StrToInt(Mul2))
     else
       Middle:=IntToStr(StrToInt(Mul1)-StrToInt(Mul2));
     Str:=Left+Middle+Right;
     First:=AnyFirstPos(Str);
     if (First=200) then break;
     Fuhao:=AnyFirstF(Str);
   end;

   SubCompute:=StrToInt(Middle);
end;

//用于计算表达式的结果
function TotalCompute(Str:String):integer;
var
First:integer;
Last:integer;
SubStr:String;
LeftStr:String;
Middle:String;
Right:String;
temp:integer;
begin
First:=LastDelimiter ('(',Str); //定位最后一个(号位置
while(First<>0) do
begin
    SubStr:=Copy(Str,First+1,Length(Str)-First);
    Last:= Pos (')',SubStr);
    Last:=Last+First;  //定位最后一个(号以后的最开始的)号位置
    LeftStr:=Copy(Str,1,First-1); //(号左边的字符串
    Middle:=Copy(Str,First+1,Last-First-1); //()号中间的字符串
    Right:=Copy(Str,Last+1,Length(Str)-Last); //)号右边的字符串
    temp:=SubCompute(Middle);  //进入下面的计算
    Middle:=IntToStr(temp);

    Str:=LeftStr+Middle+Right;
    First:=LastDelimiter ('(',Str);
end;

    Result:=SubCompute(Str);
end;
end.

⌨️ 快捷键说明

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