unit Unit1;interfaceuses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,Math;type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button4Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  temp1:string;               //第一个操作数
  temp2:string;             //第二个操作数
  sky:boolean;
  x2:char;             //运算操作符
  de:integer=0;         //等号是否被按下implementation{$R *.dfm}
procedure Calc(x,y:double;op:char);
var
   temresult:double;
   Edit1: TEdit;
   begin
     case  op  of
     '+':temresult:=x+y;
     '-':temresult:=x-y;
     '*':temresult:=x*y;
     '/':temresult:=x/y;
     '%':temresult:=Round(x) mod Round(y);
     's':temresult:=sqrt(x);
     'x':temresult:=1/x;
     '^':temresult:=power(1,2);
     end;
   temp1:=floattostr(temresult);
   temp2:='';
   x2:=' ';
   edit1.Text:=temp1;
   end;procedure TForm1.Button1Click(Sender: TObject);
begin
 if sky =false then
    begin
      Edit1.Text := '';
      sky := false;
      temp1:=temp1+'1';
      edit1.Text:=temp1;
    end;
   if (sky=true)and(temp1<>'')then
     begin
     temp2:=temp2+'1';
     edit1.Text :=temp2;
   end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
sky:=true;
if (temp1<>'') and (temp2<>'') then //先对上一次的运算求结果
begin
  Calc(strtofloat(temp1),strtofloat(temp2),x2); //调用过程
   end;
x2:='+';
end;procedure TForm1.Button3Click(Sender: TObject);
begin
if (temp1<>'') and (temp2<>'') and (x2<>'') then
begin
  Calc(strtofloat(temp1),strtofloat(temp2),x2); //调用过程
   end;
end;procedure TForm1.Button4Click(Sender: TObject);
begin
  if sky=false  then
   begin
    temp1:=temp1+'0';
    edit1.Text:=temp1;
    end;
   if (sky=true) and (temp1<>'') and (temp2<>'0') then
   begin
   temp2:=temp2+'0';
   edit1.Text :=edit1.Text+'0';
   end;
   if  (sky=true)  and (temp1<>'') and (temp2='0') and (de=0) then
   begin
   temp2:='0';
   end;
   if  (sky=true)  and  (temp1<>'')  and (temp2='') and (de=0) then
   begin
    temp2:='0';
    edit1.Text :=edit1.Text +'0';
   end;
   if  (sky=true)  and (de=1) then
   begin
   temp1:=temp1+'0';
   edit1.Text :=edit1.Text +'0';
   end;
end;end.求助此段代码问题何在,按等号时出现access violation at address错误,已添加数据执行保护,错误依然存在,编译和运行时没有问题 请大神指教  先谢了。

解决方案 »

  1.   

    procedure Calc(x,y:double;op:char);
     var
        temresult:double;
        Edit1: TEdit;//这是怎么回事啊,新增一个edit1?后面也没见你创建啊?
        begin
       .....应该是这么做的
    .....
    procedure Button3Click(Sender: TObject);
    procedure Calc(x,y:double;op:char);//这新增这个过程
       private
    .....procedure TForm1.Calc(x,y:double;op:char);
     var
        temresult:double;
        //Edit1: TEdit;//这个你本来就有
        begin
    ..........
      

  2.   


    procedure Calc(x,y:double;op:char);
    var
       temresult:double;
       //Edit1: TEdit; { 去掉 }
       begin
         case  op  of
         '+':temresult:=x+y;
         '-':temresult:=x-y;
         '*':temresult:=x*y;
         '/':temresult:=x/y;
         '%':temresult:=Round(x) mod Round(y);
         's':temresult:=sqrt(x);
         'x':temresult:=1/x;
         '^':temresult:=power(1,2);
         end;
       temp1:=floattostr(temresult);
       temp2:='';
       x2:=' ';
       //edit1.Text:=temp1; 
       Form1.edit1.Text:= temp1; {如果你是想访问窗体1上的控件}
       end;
      

  3.   

    procedure Calc(x,y:double;op:char);
     var
        temresult:double;
        //Edit1: TEdit; //Edit1定义的是局部变量,没有创建,你应该是想访问主窗口的Edit1吧,去掉这行
       //后面的改为Form1.edit1.Text:=temp1;
        begin
          case  op  of
          '+':temresult:=x+y;
          '-':temresult:=x-y;
          '*':temresult:=x*y;
          '/':temresult:=x/y;
          '%':temresult:=Round(x) mod Round(y);
          's':temresult:=sqrt(x);
          'x':temresult:=1/x;
          '^':temresult:=power(1,2);
          end;
        temp1:=floattostr(temresult);
        temp2:='';
        x2:=' ';
        Form1.edit1.Text:=temp1;
        end;