delphi2007 教程

delphi2007 教程

首页 新随笔 联系 聚合 管理
  1013 Posts :: 0 Stories :: 28 Comments :: 0 Trackbacks
我要实现十进制数转换为2进制数保存到个数组里 请看我写的对不对 谢谢 Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiBase/html/delphi_20061206215537250.html
unit     Unit1;      
     
  interface      
     
  uses      
        Windows,     Messages,     SysUtils,     Variants,     Classes,     Graphics,     Controls,     Forms,      
        Dialogs,     StdCtrls,     Mask;      
     
  type      
        TForm1     =     class(TForm)      
                MaskEdit1:     TMaskEdit;//从这里输入数字      
                Label1:     TLabel;      
                Label2:     TLabel;      
                Button1:     TButton;      
                procedure     Button1Click(Sender:     TObject);      
        private      
                {     Private     declarations     }      
        public      
        x,i:Integer;//在这里声明变量可以吗?还是应该在button的事件里声明?      
        Array16:Array[1..16]     of     Integer;//保存在这个数组里      
                {     Public     declarations     }      
        end;      
     
  var      
        Form1:     TForm1;      
  implementation      
     
  {$R     *.dfm}      
     
  procedure     TForm1.Button1Click(Sender:     TObject);      
  begin      
  x:=strtoint(Form1.MaksEdit1.Text);//赋值给中间变量x      
  i:=16;      
  repeat      
  Array16[i]:=x     mod     2;//偶这里是用最原始的方法了,连续取余呵呵      
  x:=x     div     2;      
  i:=i-1;      
  until     i=0;      
  end;      
  end.      
     
  ------------------------------------分割线--------------------------------      
  程序可以运行      
  1.我不会查看结果到底对不对,我只会用把数组某一位赋值给另外的edit.text来看某1位是不是对的,请问有什么方法可以查看下整个数组是否正确      
  2.MaskEdit1的属性为9999;0;          
  当输入是4位的时候,点击按钮可以成功,但是如果少于4位数,就会出错,说不是有效的值      
  3.有没有这类函数inttobinary()     如果有,如何实现我的要求,我看过些例子都是连续shr转到个string里的,转到数组里如何实现?      
  4.如果我这个程序其他位置还要使用该过程,请问如何定义?定义写在哪,主体写在哪?调用的时候格式是如何?      
  ------------------------------------分割线---------------------------------      
  偶是新手,希望朋友能耐心读完,帮忙解决下问题,解答时候尽量详细些     谢谢  
 

补充一下  
  2.MaskEdit1的属性为!9999;0;           第3个属性用空格和0   都试过   问题依旧

unit     Unit1;      
     
  interface      
     
  uses      
        Windows,     Messages,     SysUtils,     Variants,     Classes,     Graphics,     Controls,     Forms,      
        Dialogs,     StdCtrls,     Mask;      
     
  type      
        TForm1     =     class(TForm)      
                MaskEdit1:     TMaskEdit;//从这里输入数字      
                Label1:     TLabel;      
                Label2:     TLabel;      
                Button1:     TButton;      
                procedure     Button1Click(Sender:     TObject);      
        private      
                {     Private     declarations     }      
        public      
        x,i:Integer;//在这里声明变量可以吗?还是应该在button的事件里声明?      
        在这里和在button里都可以声明变量,但生存周期不一样  
        Array16:Array[1..16]     of     Integer;//保存在这个数组里      
        这里似乎没有必要用integer,有点浪费了。  
                {     Public     declarations     }      
        end;      
     
  var      
        Form1:     TForm1;      
  implementation      
     
  {$R     *.dfm}      
     
  procedure     TForm1.Button1Click(Sender:     TObject);      
  begin      
  x:=strtoint(Form1.MaksEdit1.Text);//赋值给中间变量x      
  i:=16;      
  repeat      
  Array16[i]:=x     mod     2;//偶这里是用最原始的方法了,连续取余呵呵      
  x:=x     div     2;      
  i:=i-1;      
  until     i=0;      
  end;      
  end.      
     
  后面你那许多问题,我建议你找本OBJECT   PASCAL或者DELPHI的书看看,  
   
   
  给你写了一个例子:把一个整形数转换成2进制字符串输出  
   
  unit   Unit1;  
   
  interface  
   
  uses  
      Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,  
      Dialogs,   StdCtrls;  
   
  type  
      TForm1   =   class(TForm)  
          Edit1:   TEdit;  
          Button2:   TButton;  
          Edit2:   TEdit;  
          procedure   Button2Click(Sender:   TObject);  
      private  
          {   Private   declarations   }  
      public  
          {   Public   declarations   }  
      end;  
   
  var  
      Form1:   TForm1;  
   
  implementation  
   
  {$R   *.dfm}  
   
  function   IntToBinaryStr(TheVal:   LongInt):   string;  
  var  
      counter:   LongInt;  
  begin  
      if   TheVal   =   0   then   begin  
          result   :=   '0';  
          exit;  
      end;  
      result   :=   '';  
      counter   :=   $80000000;  
      {Suppress   leading   zeros}  
      while     ((counter   and   TheVal)   =   0)   do   begin  
          counter   :=   counter   shr   1;  
          if   (counter   =   0)   then   break;   {We   found   our   first   "1".}  
      end;  
      while   counter   >   0   do   begin  
          if   (counter   and   TheVal)   =   0   then   result   :=   result   +   '0'  
          else     result   :=   result   +   '1';  
          counter   :=   counter   shr   1;  
      end;  
  end;  
   
   
  procedure   TForm1.Button2Click(Sender:   TObject);  
  begin  
        Edit2.Text   :=   IntToBinaryStr(StrToInt(Edit1.Text))  
  end;  
   
  end.  
   
   
 

恩恩   还没学得太透彻   借了些书对Pascal讲解篇幅不太多  
   
  我这里后面是要做个串口传输控制的程序  
   
  要一位一位传  
   
  把数组每一位陆续传到串口  
   
  不使用控件   不使用Api   是内嵌汇编   在98系统下做  
   
  如果是string型   如何实现一位一位   请给出方法  
   
  继续等  
  谢谢

不知楼主说的一"位"是不是指string里的一个字符..如果是,那么:  
   
  如有:  
  string:='abcdefg';  
  则:  
  string[1]     ==     'a';  
  string[2]     ==     'b';  
  ...

强烈鄙视问题解决后不结贴的人!  
  强烈鄙视技术问题解决后把贴子转移到非技术区的人!  
  鄙视你们!  
   
  http://community.csdn.net/Expert/topic/5216/5216675.xml?temp=.9262659

posted on 2009-02-11 16:49 delphi2007 阅读(175) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。