2013年3月30日星期六

【转】Delphi中单链表操作

单链表顾名思义就是每一个元素只有一个一个直接前驱和一个直接后驱,链表中的每一个元素称为节点,一个节点包括数据区和指针区,数据区即存储数据的部分,指针区即下一个节点的存储地址,操作链表主要就是操作节点的指针区,链表在内存中存储不是连续的,也不是一次性分配和释放的,用链表可以方便的插入和删除元素。下面是一个用Delphi控制台程序写的单链表的增删改查程序。

用Delphi新建一个控制台应用程序,源码如下:

program linklit;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  MyP=^MyRecord;
  MyRecord=record
   data:Integer;
   next:MyP;
end;

function Createlist():MyP;  //创建带头指针的单链表
var
  head1,curnode,newnode:MyP;  //定义头结点、当前节点、新生成节点
  ch:Integer;
begin
  New(head1);
  head1.data:=0;
  curnode:=head1;  //当前指针指向头指针
  Writeln('input:');
  readln(ch);
  while ch<1000 do
  begin
    New(newnode); //生成新节点
    newnode.data:=ch;
    curnode.next:=newnode; //新节点插入表尾节点
    curnode:=newnode;  //指针移到指向新的节点
    Readln(ch);  //输入下一个节点值
  end;
 Writeln('list has been created!');
 curnode.next:=nil;
 Result:=head1;
end;

function Searchlist(head1:MyP;x:Integer):MyP;
var
  curnode:MyP;
  index:Integer;
begin
  index:=0;
  if Assigned(head1.next) then     //判断是否为nil
   begin
     curnode:=head1.next;     //curnode指向第一个节点
     while Assigned(curnode) do     //遍历查找指向值为x的节点
     begin
       Inc(index);
       if curnode.data=x then  //找到返回当前节点
        begin
          Result:=curnode;
          Break;
        end
       else
          curnode:=curnode.next;   //继续下次遍历
     end;
     if not Assigned(curnode) then
       Writeln('data is not in the list!')  //没找到节点
     else
      begin
        Writeln('data has been found!');
        Writeln('the location is:'+inttostr(index));
      end;
   end
  else
   Writeln('the list is empty');
end;

procedure Insertlist(head1:MyP;apos,x:integer);    //在apos节点之前插入元素x
var
  curnode,insertnode:MyP;
  j:Integer;
begin               //只考虑apos大于1的情况
  if Assigned(head1.next) then
   begin
     curnode:=head1.next;
     j:=1;
     while(Assigned(curnode) and (j<apos-1)) do  //查找apos-1个节点,并将curnode指向其直接前驱
      begin
         curnode:=curnode.next;
         Inc(j);
      end;
     if not Assigned(curnode) then
       Writeln('error')
     else
       begin
         New(insertnode);
         insertnode.data:=x;    //生成插入节点
         if Assigned(curnode.next) then       //判断apos-1个节点是否有后继节点
          begin
            insertnode.next:=curnode.next;
            curnode.next:=insertnode;
          end
         else
          begin
            curnode.next:=insertnode;
            insertnode.next:=nil;
          end;
       end;
   end
  else
   Writeln('the list is empty');
end;

procedure Deletelist(head1:MyP;index:integer);  //删除单链表中第index个节点
var
  curnode,delnode:MyP; //当前节点和要删除的节点
  curpos:Integer;
begin
 try
  if Assigned(head1.next) then
   begin
     curpos:=0;
     curnode:=head1;
     while (Assigned(curnode.next)) and (curpos<index-1) do   //查找要删除节点的前一个节点并将指针指向其直接前驱
     begin
       curnode:=curnode.next;
       Inc(curpos);
     end;
     if not Assigned(curnode.next) then  //不存在第index个节点
      Writeln('error')
     else
      begin
        delnode:=curnode.next;
        if not Assigned(delnode.next) then  //判断删除的节点是否有后继节点
          begin
            Dispose(delnode);
            curnode.next:=nil;
          end
        else
          begin
            curnode.next:=delnode.next;
            Dispose(delnode);
          end;
      end;
   end
  else
   Writeln('the list is empty');
 except
   on e:Exception do
     Writeln(e.message);
 end;
end;

procedure Viewlist(head1:MyP);  //遍历链表
var
  p:MyP;
begin
  Writeln('the list is:');
  if Assigned(head1.next) then
   begin
     p:=head1.next;
     while Assigned(p) do
     begin
       Write(Inttostr(p.data)+' ');
       p:=p.next;
     end;
     Writeln;
   end
  else
   Writeln('the list is empty');
end;

procedure Updatelist(head1:MyP;index,avalue:Integer);
var
  curnode:MyP;
  i:integer;
begin
  if not Assigned(head1.next) then
   writeln('the list is empty')
  else
   begin
     i:=0;
     curnode:=head1.next;
     while Assigned(curnode) do
      begin
        Inc(i);
        if i=index then Break;
        curnode:=curnode.next;
      end;
     if not Assigned(curnode) then
      Writeln(Format('input index %d is error',[index]))
     else
      curnode.data:=avalue;
   end;
end;

 

//

var
  head:MyP;
  searchdata,delindex,insertindex,invalue,upIndex,upValue:Integer;
begin
 Writeln('Create list!');
 head:=Createlist;
 Viewlist(head);
 Writeln('searching operation!');
 Writeln('input data:');
 Readln(searchdata);
 Searchlist(head,searchdata);
 Writeln('deleting operation!');
 writeln('input deleted position:');
 Readln(delindex);
 Deletelist(head,delindex);
 Writeln('after deleting operation:');
 Viewlist(head);
 Writeln('inserting operation!');
 writeln('input inserting position and value:');
 Readln(insertindex,invalue);
 Insertlist(head,insertindex,invalue);
 Writeln('after inserting operation:');
 Viewlist(head);
 Writeln('updating operation!');
 Writeln('input updating position and value:');
 Readln(upIndex,upValue);
 Updatelist(head,upIndex,upValue);
 Writeln('after updating operation:');
 Viewlist(head);
 Readln;
end.

 

程序运行截图如下:

2013年3月29日星期五

【转】ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'

提示:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'。前两天也出现过这个问题,网上找了一个比较流行的方法(见方法一),搞定了。今天又用这个试了试,却搞不定,在网上找了半天,终于发现是因为mysql数据库的user表里,存在用户名为空的账户即匿名账户,导致登录的时候是虽然用的是root,但实际是匿名登录的,通过错误提示里的''@'localhost'可以看出来,于是解决办法见方法二。

方法一:
1.关闭mysql
   # service mysqld stop
2.屏蔽权限
   # mysqld_safe --skip-grant-table
   屏幕出现: Starting demo from .....
3.新开起一个终端输入
   # mysql -u root mysql
   mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
   mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误
   mysql> \q

方法二:
1.关闭mysql
   # service mysqld stop
2.屏蔽权限
   # mysqld_safe --skip-grant-table
   屏幕出现: Starting demo from .....
3.新开起一个终端输入
   # mysql -u root mysql
   mysql> delete from user where USER='';
   mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误
   mysql> \q

2013年3月28日星期四

【转】使用PHP CURL的POST数据

摘自:http://www.nowamagic.net/librarys/veda/detail/124

curl 是使用URL语法的传送文件工具,支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。curl 支持SSL证书、HTTP POST、HTTP PUT 、FTP 上传,kerberos、基于HTT格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道和大量其他有用的技巧。

原来php默认并不进行此项功能的扩展,但还是有的,只是没有让它生效罢了。打开PHP安装目录,搜索以下三个文件 ssleay32.dll、libeay32.dll和 php_curl.dll,一一拷贝到系统目录下的system32文件夹下,修改php.ini文件,找到;extension= php_curl.dll行,去掉前面的;号,保存,重启服务器。

下面举几个例子。

短彩信发送

01 $xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
02 <TaskDataTransfer4EReq xmlns="http://www.aspirehld.com/iecp/TaskDataTransfer4EReq">
03 <eid> </eid>
04 <username> </username>
05 <password> </password>
06 <src> </src>
07 <destmsisdn>'.$pns.'</destmsisdn>
08 <content type="sms">
09     <title>'.$content.'</title>
10 </content>
11 </TaskDataTransfer4EReq>';
12  
13 $url 'http://www.nowamagic.net/service/taskSubmit';//接收XML地址
14  
15 $header "Content-type: text/xml";//定义content-type为xml
16 $ch = curl_init(); //初始化curl
17 curl_setopt($ch, CURLOPT_URL, $url);//设置链接
18 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
19 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头
20 curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
21 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);//POST数据
22 $response = curl_exec($ch);//接收返回信息
23 if(curl_errno($ch)){//出错则显示错误信息
24     print curl_error($ch);
25 }
26 curl_close($ch); //关闭curl链接
27 echo $response;//显示返回信息

POST数据飞信接口

01 $username = 13800138000;
02 $password = 123456;
03 $sendto = 13912345678;
04 $message "测试一个试试看!";
05  
06 $curlPost 'username='.urlencode($username).'&
07 password='.urlencode($password).'&
08 sendto='.urlencode($sendto).'&
09 message='.urlencode($message).'';
10  
11 $ch = curl_init();//初始化curl
12 curl_setopt($ch,CURLOPT_URL,'http://sms.api.bz/fetion.php');//抓取指定网页
13 curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
14 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
15 curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
16 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
17 $data = curl_exec($ch);//运行curl
18 curl_close($ch);
19 print_r($data);//输出结果

飞信接口模式:http://sms.api.bz/fetion.php?username=您的移动飞信登录手机号,&password=您的移动飞信登录密码,&sendto=接收短信的飞信好友手机号,&message=短信内容。

总结一下使用curl方法:

  1. 初始化curl
  2. 使用curl_setopt设置目标url,和其他选项
  3. curl_exec,执行curl
  4. 执行后,关闭curl
  5. 最后一步就是输出

CERL 多线程

curl一般用来抓取网页,第二种就是get或者post数据,第三种应用就是实现PHP的多线程任务。下面来实现多线程的:

01 <?php
02 /*
03 curl 多线程抓取
04 */
05  
06  /**
07      * curl 多线程
08      
09      * @param array $array 并行网址
10      * @param int $timeout 超时时间
11      * @return array
12      */
13  function Curl_http($array,$timeout){
14     $res array();
15     $mh = curl_multi_init();//创建多个curl语柄
16     $startime = getmicrotime();
17     foreach($array as $k=>$url){
18         $conn[$k]=curl_init($url);
19          
20         curl_setopt($conn[$k], CURLOPT_TIMEOUT, $timeout);//设置超时时间
21         curl_setopt($conn[$k], CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
22         curl_setopt($conn[$k], CURLOPT_MAXREDIRS, 7);//HTTp定向级别
23         curl_setopt($conn[$k], CURLOPT_HEADER, 0);//这里不要header,加块效率
24         curl_setopt($conn[$k], CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
25         curl_setopt($conn[$k],CURLOPT_RETURNTRANSFER,1);
26         curl_multi_add_handle ($mh,$conn[$k]);
27     }
28      //防止死循环耗死cpu 这段是根据网上的写法
29         do {
30             $mrc = curl_multi_exec($mh,$active);//当无数据,active=true
31         while ($mrc == CURLM_CALL_MULTI_PERFORM);//当正在接受数据时
32         while ($active and $mrc == CURLM_OK) {//当无数据时或请求暂停时,active=true
33             if (curl_multi_select($mh) != -1) {
34                 do {
35                     $mrc = curl_multi_exec($mh$active);
36                 while ($mrc == CURLM_CALL_MULTI_PERFORM);
37             }
38         }
39      
40     foreach ($array as $k => $url) {
41           curl_error($conn[$k]);
42           $res[$k]=curl_multi_getcontent($conn[$k]);//获得返回信息
43           $header[$k]=curl_getinfo($conn[$k]);//返回头信息
44           curl_close($conn[$k]);//关闭语柄
45           curl_multi_remove_handle($mh  $conn[$k]);   //释放资源 
46         }
47          
48         curl_multi_close($mh);
49         $endtime = getmicrotime();
50         $diff_time $endtime $startime;
51          
52         return array('diff_time'=>$diff_time,
53                      'return'=>$res,
54                     'header'=>$header       
55                     );
56      
57  }
58  //计算当前时间
59  function getmicrotime() {
60         list($usec$sec) = explode(" ",microtime());
61         return ((float)$usec + (float)$sec);
62     }
63      
64     //测试一下,curl 三个网址
65     $array array(
66                 "http://www.weibo.com/",
67                 "http://www.renren.com/",
68                 "http://www.qq.com/"
69                 );
70  
71     $data = Curl_http($array,'10');//调用
72     var_dump($data);//输出
73       
74 ?>

因为$active要等全部url数据接受完毕才变成false,所以这里用到了curl_multi_exec的返回值判断是否还有数据,当有数据的时候就不停调用curl_multi_exec,暂时没有数据就进入select阶段,新数据一来就可以被唤醒继续执行。这里的好处就是CPU的无谓消耗没有了。

这个多线程的写法步骤:

  1. 调用curl_multi_init
  2. 循环调用curl_multi_add_handle,这一步需要注意的是,curl_multi_add_handle的第二个参数是由curl_init而来的子handle。
  3. 持续调用curl_multi_exec
  4. 根据需要循环调用curl_multi_getcontent获取结果
  5. 调用curl_multi_remove_handle,并为每个字handle调用curl_close
  6. 调用curl_multi_close