Archive for 四月, 2007

apache加载php模块

Posted by 机器人 on 14th 四月 2007 in php/javascript

LoadModule php5_module "D:/php5/php5apache2.dll"

AddType application/x-httpd-php .php

apache2.0以后的版本,将加载:

LoadModule php5_module "D:/php5/php5apache2_2.dll"

一个链表类的实现(C++ PRIMER学习笔记)

Posted by 机器人 on 14th 四月 2007 in c/c++

链表是一个数据项序列每个数据项都包含一个某种类型的值和链表下一项的地址地址可以为空null 链表也可以为空即链表中可以没有数据项链表不可能为满但是当程序的空闲存储区被耗尽时试图创建一个新链表项会失败链表类必须支持的操作是什么用户必须能够插入insert 或删除remove 以及查找find 一个项用户必须能够查询链表的长度size 显示display 链表以及比较两个链表是否相等equality 另外还要支持翻转reverse 链表以及连接concatenate
两个链表:
接口部分
list.h

///list.h
using namespace std;
class ilist_item {
    public:
        ilist_item( int value, ilist_item *item_to_link_to = 0 );
        int value() { return _value; }
        ilist_item* next() { return _next; }
        void next( ilist_item *link ) { _next = link; }
        void value( int new_value ) { _value = new_value; }
    private:
        int _value;
        ilist_item *_next;
};
 
class ilist {
    public:
        ilist() : _at_front( 0 ),_at_end( 0 ), _size( 0 ) {}
        int size();
        void insert( ilist_item *ptr, int value );
        void bump_up_size();
        void bump_down_size();
        void insert_front( int value );
        void insert_end( int value );
        ilist_item* find( int value );
        void display( ostream &os = cout );
        void remove_front();
        void remove_all();
        int remove( int value );
        //连接两个链表
        void concat( const ilist &il );
        //翻转一个链表
        void reverse();
        void insert_all( const ilist &rhs );
        ilist( const ilist& );
        ilist& operator=( const ilist& );
    private:
        //禁止用一个链表对象初始化或赋值给另一个
        // ilist( const ilist& );
        //ilist& operator=( const ilist& );
        ilist_item *_at_front;
        ilist_item *_at_end;
        int _size;
        int _value;
        ilist_item *_next;
};

实现部分
list.cpp

//list.cpp
#include < iostream >
#include "list.h"
using namespace std;
 
inline ilist_item::ilist_item( int value, ilist_item *item ) : _value( value ) {
    if ( !item ) {
        _next = 0;
    } else {
        //这两步要多加注意
        _next = item->_next;
        item->_next = this;
    }
}
 
inline  void ilist::insert( ilist_item *ptr, int value ) {
    if ( !ptr ) {
        insert_front( value );
    } else {
        bump_up_size();
        new ilist_item(value, ptr );
    }
}
 
void ilist::insert_front( int value) {
    ilist_item *ptr = new ilist_item( value );
    if ( !_at_front ) {
        _at_front = _at_end = ptr;
    } else {
        ptr->next( _at_front );
        _at_front = ptr;
    }
    bump_up_size();     
}
 
inline void ilist::insert_end( int value ) {
    if ( !_at_end ) {
        _at_end = _at_front = new ilist_item( value );
    } else {
        _at_end = new ilist_item( value, _at_end );
    }
    bump_up_size();
}
 
 
ilist_item* ilist::find( int value ) {
    ilist_item *ptr = _at_front;
    while ( ptr ) {
        if ( ptr->value() == value ) break;
        ptr = ptr->next();
    }
    return ptr;
}
 
 
//拷贝构造函数
ilist::ilist( const ilist &rhs ): _at_front( 0 ), _at_end( 0 ) {
    insert_all( rhs );
}
 
void ilist::insert_all( const ilist &rhs ) {
    ilist_item *pt = rhs._at_front;
    while ( pt ) {
        insert_end( pt->value() );
        pt = pt->next();
    }
}
 
void ilist::display( ostream &os ) {
    os < < "\n( " << _size << ")(";
    ilist_item *ptr = _at_front;
    while ( ptr ) {
        os << ptr->value() < < " ";                   
        ptr = ptr->next();
    }
    os < < ")\n";
}
 
 
 
inline void ilist::remove_front(){
    if ( _at_front ) {
        ilist_item *ptr = _at_front;
        _at_front = _at_front->next();
        bump_down_size();
        delete ptr;
    }   
}
 
void ilist::remove_all() {
    while ( _at_front ) {
        remove_front();
    }
    _size = 0;
    _at_front = _at_end = 0;
}
 
int ilist::remove( int value ) {
    ilist_item *plist = _at_front;
    int elem_cnt = 0;
    while (plist && plist->value() == value ) {
        plist = plist->next();
        remove_front();
        ++elem_cnt;
    }
 
    if ( !plist ) return elem_cnt;
    ilist_item *prev = plist;
    plist = plist->next();
    while ( plist ) {
        if ( plist->value() == value ) {
            prev->next( plist->next() );
            delete plist;
            ++elem_cnt;
            bump_down_size();
            plist = prev->next();
            if ( !plist ) {
                _at_end = prev;
                return elem_cnt;
            }      
        } else {
            prev = plist;
            plist = plist->next();
        }   
    }   
    return elem_cnt;
}
 
void ilist::concat( const ilist &il ) {
    ilist_item *ptr = il._at_front;
    while ( ptr ) {
        insert_end( ptr->value() );
        ptr = ptr->next();
    }
}
 
void ilist::reverse() {
    ilist_item *ptr = _at_front;
    ilist_item *prev = 0;
    _at_front = _at_end;
    _at_end = ptr;
    while ( ptr != _at_front ) {
        ilist_item *tmp = ptr->next();
        ptr->next( prev );
        prev = ptr;
        ptr = tmp;
    }
    _at_front->next( prev );
}
 
inline void ilist::bump_up_size() { ++_size; }
inline void ilist::bump_down_size() { --_size; }

类测试代码

//下面类的测试代码
#include < iostream >
#include "list.h"
using namespace std;
 
int main()
{
    ilist mylist;
    /* cout < <"#####################测试一####################"<<endl;
       for ( int ix = 0; ix< 10; ++ix ) {
       mylist.insert_front( ix );
       mylist.insert_end( ix );
       }
       cout << "Ok: after insert_front() and insert_end()\n";
       mylist.display();
       ilist_item *it = mylist.find( 8);
       cout << "\n"
       << "Searching for the value 8: found it<<"
       << ( it ? " yes!\n" : " no!\n" );
       mylist.insert( it, 1024 );
       cout << "\n"
       << "Inserting element 1024 following the value 8\n";
       mylist.display();
       int elem_cnt = mylist.remove( 8 );
       cout << "\n"
       << "Removed " << elem_cnt << " of the value 8\n";
       mylist.display();
       cout << "\n" << "Removed front element\n";
       mylist.remove_front();
       mylist.display();
       cout << "\n" << "Removed all elements\n";
       mylist.remove_all();
       mylist.display();
       cout <<"#####################测试一结束################"<<endl;
       */
    /*cout <<"#####################测试二####################"<<endl;
      cout << "\n———————————————–\n"
      << "test #1: items at end\n"
      << "————————————————\n";
      mylist.insert_front( 1 );
      mylist.insert_front( 1 );
      mylist.insert_front( 1 );
      mylist.insert_front( 2 );
      mylist.insert_front( 3 );
      mylist.insert_front( 4 );
      mylist.display();
      int elem_cnt = mylist.remove( 1 );
      cout << "\n" << "Removed " << elem_cnt << " of the value 1\n";
      mylist.display();
      mylist.remove_all();
      cout << "\n———————————————–\n"
      << "test #2: items at front \n"
      << "————————————————-\n";
      mylist.insert_front( 1 ); mylist.insert_front( 1 );
      mylist.insert_front( 1 );
      mylist.display();
      elem_cnt = mylist.remove( 1 );
      cout << "\n" << "Removed " << elem_cnt << " of the value 1\n";
      mylist.display();
      mylist.remove_all();
      cout << "\n———————————————–\n"
      mylist.insert_front( 0 );
      mylist.insert_front( 2 );
      mylist.insert_front( 4 );
      mylist.display();
      elem_cnt = mylist.remove( 1 );
      cout << "\n" << "Removed " << elem_cnt << " of the value 1\n";
      mylist.display();
      mylist.remove_all();
      cout << "\n———————————————-\n"
      << "test #4: items at front and end\n"
      << "———————————————–\n";
      mylist.insert_front( 1 );
      mylist.insert_front( 1 );
      mylist.insert_front( 1 );
      mylist.insert_front( 0 );
      mylist.insert_front( 2 );
      mylist.insert_front( 4 );
      mylist.insert_front( 1 );
      mylist.insert_front( 1 );
      mylist.insert_front( 1 );
      mylist.display();
      elem_cnt = mylist.remove( 1 );
      cout << "\n" << "Removed " << elem_cnt << " of the value 1\n";
      mylist.display();
      cout << "#################测试二结束###################" << endl;*/
    cout <<"#####################测试三##################"<<endl;
    for ( int ix = 0; ix< 10; ++ix ) {
//        mylist.insert_front( ix );
        mylist.insert_front(ix);
    }
    mylist.display();
    cout << "\n" << "reverse the list\n";
    mylist.reverse();
    mylist.display();
    ilist mylist_too;
    mylist_too.insert_end( 0 );
    mylist_too.insert_end( 1 );
    mylist_too.insert_end( 1 );
    mylist_too.insert_end( 2 );
    mylist_too.insert_end( 3 );
    mylist_too.insert_end( 5 );
    cout << "\n" << "mylist_too:\n";
    mylist_too.display();
    mylist.concat( mylist_too );
    cout << "\n" << "mylist after concat with mylist_too:\n";
    mylist.display();
    cout <<"#####################测试三结束###########"<<endl;
    return 0;
}

机器人 2007年04月14日 17:44 于 北京

一个命令行程序的实现(C++ 学习笔记)

Posted by 机器人 on 14th 四月 2007 in c/c++

#include <iostream>

#include <string>

#include <vector>

#include <ctype.h>

 

using namespace std;

const char *const program_name = "comline";

const char *const program_version = "version 0.01 (08/07/97)";

inline void usage( int exit_value = 0 )

{

        // 输出一个格式化的用法信息

        // 并用 exit_value 退出

        cerr << "usage:\n"

        << program_name << " "

        << "[-d] [-h] [-v] \n\t"

        << "[-o output_file] [-l limit] \n\t"

        << "file_name\n\t[file_name [file_name [ ... ]]]\n\n"

        << "where [] indicates optional option: \n\n\t"

        << "-h: help.\n\t\t"

        << "generates this message and exits\n\n\t"

        << "-v: version.\n\t\t"

        << "prints version information and exits\n\n\t"

        << "-d: debug.\n\t\tturns debugging on\n\n\t"

        << "-l limit\n\t\t"

        << "limit must be a non-negative integer\n\n\t"

        << "-o ofile\n\t\t"

        << "file within which to write out results\n\t\t"

        << "by default, results written to standard output \n\n"

        << "file_name\n\t\t"

        << "the name of the actual file to process\n\t\t"

        << "at least one file_name is required –\n\t\t"

        << "any number may be specified\n\n"

        << "examples:\n\t\t"

        << "$command chapter7.doc\n\t\t"

        << "$command -d -l 1024 -o test_7_8 "

        << "chapter7.doc chapter8.doc\n\n";

        exit( exit_value );

}

 

int main( int argc, char* argv[] )

{

        bool debug_on = false;

        bool ofile_on = false;

        bool limit_on = false;

        int limit = -1;

        string ofile;

        vector< string > file_names;

        cout << "illustration of handling command line arguments:\n"

        << "argc: " << argc << endl;

        for ( int ix = 1; ix < argc; ++ix )

        {

                cout << "argv[ " << ix << " ]: "

                << argv[ ix ] << endl;

                char *pchar = argv[ ix ];

                switch ( pchar[ 0 ] )

                {

                        case ‘-’:

                        {

                                cout << "case \’-\’ found\n";

                                switch( pchar[ 1 ] )

                                {

                                case ‘d’:

                                        cout << "-d found: "

                                        << "debugging turned on\n";

                                        debug_on = true;

                                        break;

                                case ‘v’:

                                        cout << "-v found: "

                                        << "version info displayed\n";

                                        cout << program_name

                                        << " :: "

                                        << program_version

                                        << endl;

                                        return 0;

                                case ‘h’:

                                        cout << "-h found: "

                                        << "help information\n";

                                        // 这里没必要用 break , usage() 可以退出

                                        usage();

                                case ‘o’:

                                        cout << "-o found: output file\n";

                                        ofile_on = true;

                                        break;

                                case ‘l’:

                                        cout << "-l found: "

                                        << "resource limit\n";

                                        limit_on = true;

                                        break;

                                default:

                                        cerr << program_name

                                        << " : error : "

                                        << "unrecognized option: – "

                                        << pchar << "\n\n";

                                        // 这里没必要用 break , usage() 可以退出

                                        usage( -1 );

                                }

                                break;

                        }

                        default: // 或文件名

                        cout << "default nonhyphen argument: "

                        << pchar << endl;

                        if ( ofile_on ) {

                                ofile_on = false;

                                ofile = pchar;

                        }

                        else

                        if ( limit_on ) {

                                limit_on = false;

                                // 将字符型轮换为整形

                                limit = atoi( pchar );

                                if ( limit < 0 ) {

                                        cerr << program_name

                                        << " : error : "

                                        << "negative value for limit.\n\n";

                                        usage( -2 );

                                }

                        }

                        else file_names.push_back( string( pchar ));

                        break;

                }

        }

        if ( file_names.empty() ) {

                cerr << program_name

                << " : error : "

                << "no file specified for processing.\n\n";

                usage( -3 );

        }

        if ( limit != -1 )

        cout << "User-specifed limit: "

        << limit << endl;

        if ( ! ofile.empty() )

        cout << "User-specified output file: "

        << ofile << endl;

        cout << (file_names.size() == 1 ? "File " : "Files ")

        << "to be processed are the following:\n";

        for ( int inx = 0; inx < file_names.size(); ++inx )

        cout << "\t" << file_names[ inx ] << endl;

}

 

 

下面是程序的执行练习

a.out -d -l 1024 -o test_7_8 chapter7.doc chapter8.doc

下面是命令行选项处理的跟踪结果

illustration of handling command line arguments:

argc: 8

argv[ 1 ]: -d

case ‘-’ found

-d found: debugging turned on

argv[ 2 ]: -l

case ‘-’ found

-l found: resource limit

argv[ 3 ]: 1024

default nonhyphen argument: 1024

argv[ 4 ]: -o

case ‘-’ found

-o found: output file

argv[ 5 ]: test_7_8

default nonhyphen argument: test_7_8

argv[ 6 ]: chapter7.doc

default nonhyphen argument: chapter7.doc

argv[ 7 ]: chapter8.doc

default nonhyphen argument: chapter8.doc

User-specifed limit: 1024

User-specified output file: test_7_8

Files to be processed are the following:

chapter7.doc

chapter8.doc

C++之函数对象篇(C++学习笔记)

Posted by 机器人 on 14th 四月 2007 in c/c++

预定义的算术函数对象支持加减乘除求余和取反调用的操作符是与Type 相

关联的实例对一个class 类型如果它提供了该操作符的重载实例则调用该实例

加法plus<Types>

plus<string> stringAdd;

// 调用 string::operator+()

sres = stringAdd( sval1, sval2 );

dres = BinaryFunc( plus<double>(), dval1, dval2 );

减法minus<Type>

minus<int> intSub;

ires = intSub( ival1, ival2 );

dres = BinaryFunc( minus<double>(), dval1, dval2 );

乘法multiplies<Type>

multiplies<complex> complexMultiplies;

cres = complexMultiplies( cval1, cval2 );

dres = BinaryFunc( multiplies<double>(), dval1, dval2 );

除法divides<Type>

divides<int> intDivides;

ires = intDivides( ival1, ival2 );

dres = BinaryFunc( divides<double>(), dval1, dval2 );

求余modulus<Type>

modulus<int> IntModulus;

Ires = IntModulus( Ival1, Ival2 );

ires = BinaryFunc( modulus<int>(), ival2, ival1 );

取反negate<Type>

negate<int> intNegate;

ires = intNegate( ires );

Ires = UnaryFunc( negate<int>(), Ival1 );

关系函数对象预定义的关系函数对象支持等于不等于大于大于等于小于和小于等于

等于equal_to<Type>

equal_to<string> stringEqual;

sres = stringEqual( sval1, sval2 );

ires = count_if( svec.begin(), svec.end(),

equal_to<string>(), sval1 );

不等于not_equal_to<Type>

not_equal_to<complex> complexNotEqual;

cres = complexNotEqual( cval1, cval2 );

ires = count_if( svec.begin(), svec.end(),

not_equal_to<string>(), sval1 );

大于greater<Type>

greater<int> intGreater;

ires = intGreater( ival1, ival2 );

ires = count_if( svec.begin(), svec.end(),

greater<string>(), sval1 );

大于等于greater_equal<Type>

greater_equal<double> doubleGreaterEqual;

dres = doubleGreaterEqual( dval1, dval2 );

ires = count_if( svec.begin(), svec.end(),

greater_equal<string>(), sval1 );

小于less<Type>

less<int> IntLess;

Ires = IntLess( Ival1, Ival2 );

ires = count_if( svec.begin(), svec.end(),

less<string>(), sval1 );

小于等于less_equal<Type>

less_equal<int> intLessEqual;

ires = intLessEqual( ival1, ival2 );

ires = count_if( svec.begin(), svec.end(),

less_equal<string>(), sval1 );

逻辑函数对象预定义的逻辑函数对象支持逻辑与两个操作数都为true 时结果值为true——应用与

Type 相关联的&& 逻辑或两个操作数中有一个为true 返回true——应用与Type 相关联

的|| 和逻辑非操作数为false 则返回true——应用与Type 相关联的!操作符

逻辑与logical_and<Type>

logical_and<int> intAnd;

ires = intAnd( ival1, ival2 );

dres = BinaryFunc( logical_and<double>(), dval1, dval2 );

逻辑或logical_or<Type>

logical_or<int> intSub;

ires = intSub( ival1, ival2 );

dres = BinaryFunc( logical_or<double>(), dval1, dval2 );

逻辑非 logical_not<Type>

logical_not<int> IntNot;

Ires = IntNot( Ival1, Ival2 );

dres = UnaryFunc( logical_not<double>(), dval1 );

函数对象的函数适配器标准库还提供了一组函数适配器用来特殊化或者扩展一元和二元函数对象适配器是

一种特殊的类它被分成下面两类:

1 绑定器binder ( binder ) 通过把二元函数对象的一个实参绑定到一个特殊的值上

将其转换成一元函数对象例如为了计数一个容器中小于或等于10 的元素的个数我们可能会向count_if()传递一个less_equal 函数对象以及一个被绑定为10 的实参。

2 取反器negator ( negator ) 是一个将函数对象的值翻转的函数适配器例如为了

计数一个容器中所有大于10 的元素的个数我们可以向count_if()传递less_equal 函数对象的

negator 该函数对象有一个实参被绑定为10 当然在这种情况下直接传递greater 对象

的binder 并把一个实参绑定为10 更为简洁明了。

C++标准库提供了两种预定义的binder 适配器bind1st 和bind2nd 正如你所预料的

bind1st 把值绑定到二元函数对象的第一个实参上bind2nd 把值绑定在第二个实参上例如

为了计数容器中所有小于或等于10 的元素的个数我们可以这样向count_if()传递

count_if( vec.begin(), vec.end(),

bind2nd( less_equal<int>(), 10 ));

标准库提供了两个预定义的negator 适配器not1 和not2 同样正如你所料想的not1

翻转一元预定义函数对象的真值而not2 翻转二元谓词函数的真值为了取反less_equal 函

数对象的绑定我们可以这样写

count_if( vec.begin(), vec.end(),

not1( bind2nd( less_equal<int>(), 10 )));

实现函数对象函数对象类定义的最简单形式包含一个被重载的函数调用操作符例如下面是一个二

元函数对象它判定一个值是否小于等于10

// 函数对象类的简单形式

class less_equal_ten {

public:

bool operator() ( int val )

{ return val <= 10; }

};

使用这个对象的方式与使用预定义函数对象的方式相同例如下面是修改后的count_if()

调用它使用了我们的函数对象

count_if( vec.begin(), vec.end(), less_equal_ten() );

毫无疑问这个类是相当局限的我们可以应用一个negator 来计数容器中大于10 的元

素的个数

count_if( vec.begin(), vec.end(),

not1(less_equal_ten()) );

我们也可以通过允许用户提供一个与每个元素比较的值来扩展我们的实现一种做法是

引入一个数据成员来存储被比较的值以及一个构造函数把这个成员初始化为用户指定的值

class less_equal_value {

public:

less_equal_value( int val ) : _val( val ) {}

bool operator() ( int val ) { return val <= _val; }

private:

int _val;

};

我们现在用这个对象指定一个任意的整数值例如下面的调用计数小于等于25 的元素

的个数

count_if( vec.begin(), vec.end(), less_equal_value( 25 ));

另外一种类的实现方式不使用构造函数它根据被比较的值对类参数化例如

template < int _val >

class less_equal_value {

public:

bool operator() ( int val ) { return val <= _val; }

};

下面给出了怎样调用这个类来计数小于等于25 的元素的个数

count_if( vec.begin(), vec.end(), less_equal_value<25>() );