Delphi计算器控制台版(含括号)

来源:互联网 发布:华为大数据研发岗累吗 编辑:程序博客网 时间:2024/06/08 16:20
program Project1;
{$APPTYPE CONSOLE}{$R *.res}uses  System.SysUtils;procedure translate(str:Array of char;var exp:array of char);type   StrStruct =record   data:Array[0..99]of char;   top:integer;   end; var ch:char; i,t:integer;       op:StrStruct; begin  i:=0;  t:=0;  op.top:=-1;  ch:=str[i];  i:=i+1;  while(ch<> '') do  begin    Case ch of    '(':begin         op.top:=op.top+1;         op.data[op.top]:=ch;        end;    ')':begin         while(op.data[op.top] <> '(') do           begin            exp[t]:=op.data[op.top];            op.top:=op.top-1;            t:=t+1;           end;           op.top:=op.top-1;        end;    '+','-':begin            while(op.top <> -1)and(op.data[op.top] <> '(') do              begin                exp[t] := op.data[op.top];                op.top:=op.top-1;                t:=t+1;              end;            op.top:=op.top+1;            op.data[op.top] := ch;            end;    '*','/':begin            while(op.data[op.top] = '/') or (op.data[op.top] = '*') do             begin              exp[t] := op.data[op.top];              op.top:=op.top-1;              t:=t+1;             end;             op.top:=op.top+1;             op.data[op.top] := ch;            end;    ' ':;    else  begin           while(ch >= '0')and(ch <= '9') do             begin                  exp[t] := ch;                  t:=t+1;                  ch := str[i];                  i:=i+1;             end;           i:=i-1;           exp[t] := '#';           t:=t+1;          end;    end;  ch := str[i];  i:=i+1;  end;  while(op.top <> -1) do  begin        exp[t] := op.data[op.top];        t:=t+1;        op.top:=op.top-1;  end;  exp[t] := #0; end;function cal_value(exp:Array of char):real;type   FloatStruct =record   data:Array[0..99] of real;   top:integer;   end;var  st:FloatStruct;  d:real;  ch:char;  t:integer;begin  t:=0;  st.top := -1;  ch := exp[t];  t:=t+1;  while(ch <> '') do  begin  case ch of  '+':begin        st.data[st.top-1] := st.data[st.top-1]+st.data[st.top];        st.top:=st.top-1;      end;  '-':begin        st.data[st.top-1] := st.data[st.top-1]-st.data[st.top];        st.top:=st.top-1;      end;  '*':begin        st.data[st.top-1] := st.data[st.top-1]*st.data[st.top];        st.top:=st.top-1;      end;  '/': begin         if(st.data[st.top] <> 0) then            st.data[st.top-1]:=st.data[st.top-1]/st.data[st.top]         else begin                write('除数不能为0!');                exit;              end;         st.top:=st.top-1;      end;  else  begin          d:=0;          while(ch >= '0')and(ch <= '9') do            begin                 d := d*10+ord(ch)-48;                 ch := exp[t];                 t:=t+1;            end;          st.top:=st.top+1;          st.data[st.top] := d;        end;  end;   ch := exp[t];   t:=t+1;  end;  cal_value:=st.data[st.top];end;var  str,exp:array[0..99] of char;begin  write('表达式:');  readln(str);  translate(str,exp);  write('计算结果:',format('%g',[cal_value(exp)]));  readln;end.