const的使用!

WEB2.0 jack 2008-12-4 15:40
C中CONST的使用:
const是一个C语言的关键字,它限定一个变量不允许被改变。使用const在一定程度上可以提
高程序的健壮性,另外,在观看别人代码的时候,清晰理解const所起的作用,对理解对方的
程序也有一些帮助。
  虽然这听起来很简单,但实际上,const的使用也是c语言中一个比较微妙的地方,微妙在
何处呢?请看下面几个问题。阅读全文

static的使用!

WEB2.0 jack 2008-12-4 15:39
一、 static 变量

static变量大致分为三种用法
1. 用于局部变量中,成为静态局部变量. 静态局部变量有两个用法,记忆功能和全局生存期.
2. 用于全局变量,主要作用是限制此全局变量被其他的文件调用.
3. 用于类中的成员.表示这个成员是属于这个类但是不属于类中任意特定对象阅读全文
a) 一个整型数(An integer)
b)一个指向整型数的指针( A pointer to an integer)
c)一个指向指针的的指针,它指向的指针是指向一个整型数( A pointer to a pointer to an intege)r
d)一个有10个整型数的数组( An array of 10 integers)
e) 一个有10个指针的数组,该指针是指向一个整型数的。(An array of 10 pointers to integers)
f) 一个指向有10个整型数数组的指针( A pointer to an array of 10 integers)
g) 一个指向函数的指针,该函数有一个整型参数并返回一个整型数(A pointer to a function that takes an integer as an argument and returns an integer)
h) 一个有10个指针的数组,该指针指向一个函数,该函数有一个整型参数并返回一个整型数( An array of ten pointers to functions that take an integer argument and return an integer )

答案是:
a) int a; // An integer
b) int *a; // A pointer to an integer
c) int **a; // A pointer to a pointer to an integer
d) int a[10]; // An array of 10 integers
e) int *a[10]; // An array of 10 pointers to integers
f) int (*a)[10]; // A pointer to an array of 10 integers
g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer
h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer


如何区分:

在没括号的定义中我们以*为分界如(int */a[10])首先看右边我们确定它是一个数组,再看左边确定它的数组内容是一个指针并且指针指向整型数;

在有括号的定义中我们就把有括号中有指针的都删掉如
(int (*a)[10]=`=int () [10])由删掉的内容我们首先确定它是一个指针,再通过删掉后剩下的内容我们确定了这个指针指向了一个有10整型数的数组;

对于函数指针也一样;

C语言模块化

WEB2.0 jack 2008-12-4 15:38
在C语言的应用领域,如通讯领域和嵌入式系统领域,一个的软件项目通常包含很多复杂的功能,实现这个项目不是一个程序员单枪匹马可以胜任的,往往需要一个团队的有效合作,另外,在一个以C代码为主的完整的项目中,经常也需要加入一些其他语言的代码,例如,C代码和汇编代码的混合使用,C文件和C++的同时使用。这些都增加了一个软件项目的复杂程度,为了提高软件质量,合理组织的各种代码和文件是非常重要的。        阅读全文
最近需要在pxa270的嵌入式平台上应用数据库进行通信信息管理,客户要求使用mysql数据库,接下任务后,在网上一查才知道mysql 根本很少用于嵌入式环境,而且支持的少数几种芯片中并没有arm的分发包!继续搜索有没有mysql在arm平台移植的相关文章,几乎找不到相关资料.而自己在网上发了一些帖却没有收到一个回复,没办法,只能硬着头皮自己动手了.幸好经过一番”苦战”,今天终于克服了所有障碍,让mysql在270板子上跑了起来!而且简单的测试也都收到了预期的效果,不过还要看客户的经一步测试了.

现在将mysql 的arm+linux移植经验贴出来,希望对以后做相关方面工作的人有所帮助.

首先是要从网上获取mysql的源码包,官方网址如下:

http://ftp.plusline.de/mysql/Downloads/

我用的是mysql-5.0.22.tar.gz

采用的交叉编译器是适合于270板子的codesourcery这要根据具体的情况来进行修改.阅读全文
1.5 写个C语言程序调用SQLite
    现在我们来写个C/C++程序,调用 sqlite    的 API 接口函数。
    下面是一个C程序的例子,显示怎么使用 sqlite 的 C/C++ 接口. 数据库的名字由第一个参数取得且第二个参数或更多的参数是 SQL 执行语句. 这个函数调用sqlite3_open() 在 22 行打开数据库, sqlite3_exec() 在 27 行执行 SQL 命令, 并且sqlite3_close() 在 31 行关闭数据库连接。
代码:

//     name: opendbsqlite.c
//    This file is used to test C/C++ API for sqlite
//     Author : zieckey
//     2006/04/11
#include
#include

int main( void )
{
    sqlite3 *db=NULL;
    char *zErrMsg = 0;
    int rc;
    rc = sqlite3_open("zieckey.db", &db);   //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
    if( rc ){
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(1);
    }
    else printf("open zieckey.db successfully!\n");
  
    sqlite3_close(db);                //关闭数据库
    return 0;
}

编译:# gcc opendbsqlite.c -o db.out
也许会碰到类似这样的问题:
/tmp/ccTkItnN.o(.text+0x2b): In function `main':
: undefined reference to `sqlite3_open'
/tmp/ccTkItnN.o(.text+0x45): In function `main':
: undefined reference to `sqlite3_errmsg'
/tmp/ccTkItnN.o(.text+0x67): In function `main':
: undefined reference to `sqlite3_close'
/tmp/ccTkItnN.o(.text+0x8f): In function `main':
: undefined reference to `sqlite3_close'
collect2: ld returned 1 exit status

这是个没有找到库文件的问题。
由于用到了用户自己的库文件,所用应该指明所用到的库,我们可以这样编译:

# gcc opendbsqlite.c -o db.out -lsqlite3

我用用 -lsqlite3 选项就可以了(前面我们生成的库文件是 libsqlite3.so.0.8.6 等,
去掉前面的lib和后面的版本标志,就剩下 sqlite3 了所以是 -lsqlite3 )。
如果我们在编译安装的时候,选择了安装路径,例如这样的话:
.......
# ../sqlite/configure --prefix=/usr/local/arm-linux/sqlite-ix86-linux
.......
这样编译安装时,sqlite的库文件将会生成在  /usr/local/arm-linux/sqlite-ix86-linux/lib 目录下
这时编译还要指定库文件路径,因为系统默认的路径没有包含 /usr/local/arm-linux/sqlite-ix86-linux/lib

# gcc opendbsqlite.c -lsqlite3 -L/usr/local/arm-linux/sqlite-ix86-linux/lib

如果还不行的话,可能还需要指定头文件 sqlite3.h 的路径,如下:

# gcc opendbsqlite.c -lsqlite3 -L/usr/local/arm-linux/sqlite-ix86-linux/lib -I/usr/local/arm-linux/sqlite-ix86-linux/include

这样编译应该就可以了 ,运行:
# ./db.out
open zieckey.db successfully!
是不是很有成就感阿 ,呵呵,这个上手还是很快的。
1)linux下查找指定文件名:
find PATH -type f -name "aaa.txt"


2)find+grep配合查找包含某字符串的文件并显示行号:
#> find . -type f  -exec grep 'mingtian'  -l {} \;
grep -l :是显示匹配的内容的文件名字!
或者:
#> find . -type f -name "*.*" |xargs grep 'xiaoshou' -l
(这个方法 不好,挺乱的,还慢,推荐前一个方法!)

such as :
find . -type f -name "*.php"|xargs grep -in "date" -R
linux下文件的类型是不依赖于其后缀名的,但一般来讲:
.o,是目标文件,相当于windows中的.obj文件
.so 为共享库,是shared object,用于动态连接的,和dll差不多
.a为静态库,是好多个.o合在一起,用于静态连接
.la为libtool自动生成的一些共享库,vi编辑查看,主要记录了一些配置信息。可以用如下命令查看*.la文件的格式   $file *.la
      *.la: ASCII English text
所以可以用vi来查看其内容。
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@阅读全文

面试题解答!

WEB2.0 jack 2008-12-4 15:20
1.数组a[N],存放了1至N-1范围内N个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型:

int do_dup(int a[],int N)

算法:
a1+a2+ ......+aN=1+2+3+..+x+..+(N-1)+(N-x);
s1=a1+a2+....+aN;
s2=1+2+3+...+N;
x=s1-s2+N;
#####################################################阅读全文
sizeof()---函数用法
[int占4字节,short占2字节]
1.0 回答下列问题:[答案在文章末尾]
1. sizeof(char) =                           
2. sizeof 'a'   =                           
3. sizeof "a"   =                        
4. strlen("a")) =
  如果你答对了全部四道题,那么你可以不用细看下面关于sizeof的论述。如果你答错了部分题目,
    那么就跟着我来一起探讨关于sizeof的用法了。  
  对于前面的题目,我想一般有一定C基础的同志应该不会答错1和4题。至于第2题,我想应该要清楚sizeof是求字符串所占的内存。
    "a"在内存中的表现为a\0,别忘了末尾的\0也占一个字节呢。至于第2题,可能有些人会惊讶了。C 语言中,字符常数是int 型,
    因此 sizeof('a') 是 sizeof(int), 这是另一个与 C++ 不同的地方。既然字符常数是int 型,那么int就可以存放4个字符,
    我们可以得到sizeof 'abcd'为 4。  
===========================================================================================================阅读全文
首先打开两个shell终端;

一个shell下输入如下命令:
[root@MagicLinux ~]# echo -e "at \r" >/dev/ttyS0      //{echo -e可以发送回车等特殊字符;}

这个是发送字符"at "到/dev/ttyS0即串口0;

另一个shell下输入如下命令:
[root@MagicLinux ~]# cat /dev/ttyS0

这个是接收来自串口0的字符;


一个shell下输入如下命令:
[root@MagicLinux ~]# echo -e "at \r" >/dev/ttyS0      //{echo -e可以发送回车等特殊字符;}

这个是发送字符"at <CR>"到/dev/ttyS0即串口0;

另一个shell下输入如下命令:
[root@MagicLinux ~]# cat /dev/ttyS0

这个是接收来自串口0的字符;

求助shell  echo “HEX字符串” 到串口ttyUSB0的方法
发送字符到串口
echo -n “abc” > /dev/ttyUSB0 #这个正常
串口接受到 61 62 63  即abc
但是我想echo 到串口直接发送FE 01 02 的HEX值
echo -n “FE 01 02” /dev/ttyUSB0 #这里该怎么写呢?
不知道描述的清楚否  请教大神~解惑

echo -n -e "\xFE\x01\x02"

来自:http://www.right.com.cn/forum/forum.php?mod=viewthread&tid=147778&highlight=ttyUSB0
http://bbs.chinaunix.net/thread-4057939-1-1.html
http://blog.csdn.net/sidely/article/details/40181463
http://blog.chinaunix.net/uid-7491192-id-2051154.html
dmidecode -t 1   #Manufacturer,Product Name判断是否是虚拟机还是实体机。
根据这个NF5280M2,能查出更多信息机房和机架信息罢?

dmidecode在 Linux 系统下获取有关硬件方面的信息。dmidecode 遵循 SMBIOS/DMI 标准,以一种可读的方式dump出机器的DMI(Desktop Management Interface)信息, 其输出的信息包括 BIOS、系统、主板、处理器、内存、缓存等等, 既可以得到当前的配置,也可以得到系统支持的最大配置,比如说支持的最大内存数等。
1、查看内存槽数、那个槽位插了内存,大小是多少
dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range

2、查看最大支持内存数
dmidecode|grep -P 'Maximum\s+Capacity'

3、查看槽位上内存的速率,没插就是unknown。


dmidecode|grep -A16 "Memory Device"|grep 'Speed'
dmidecode|grep -A16 "Memory Device"|grep 'Speed'

来自:http://www.ttlsa.com/linux/the-linux-dmidecode-command-to-get-the-hardware-information/

Linux---查看内存型号:
sudo dmidecode -t memory



简单点
1. 查看物理CPU的个数
#cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
2. 查看逻辑CPU的个数
#cat /proc/cpuinfo |grep "processor"|wc -l
3. 查看CPU是几核
#cat /proc/cpuinfo |grep "cores"|uniq
4. 查看CPU的主频
#cat /proc/cpuinfo |grep MHz|uniq
来自:http://ldbjakyo.iteye.com/blog/785745
http://jingyan.baidu.com/article/63acb44a81001361fcc17e21.html
       RSS -- 进程本身的内存占用
  VSZ -- 算上共享库的总占用
  ps -eo pid,user,comm,args,%cpu,%mem,rss,vsz,sz
  查看内存、CPU
  ps -fewL|grep face_svr
  ps -fp $(pgrep -d, face_svr)
  top c
  VIRT 使用的虚拟内存总量, VIRT=SWAP+RES
  RES 使用的、未被换出的物理内存大小, RES=CODE+DATA
  SWAP 使用的虚拟内存中被换出的大小
  SHR 共享内存大小
  CODE 可执行代码占用的物理内存大小
  DATA 可执行代码以外的部分(数据段+栈)占用的物理内存大小
  %MEM 使用的物理内存百分比, =RES/总共内存大小
示例:
  ps -eo pid,user,comm,args,%cpu,%mem,rss,vsz,sz
  PID USER     COMMAND         COMMAND      %CPU %MEM   RSS    VSZ    SZ
  1    root               init            /sbin/init        0.0     0.3      1236   2900   725
注释:
  RSS -- 进程本身的内存占用
  VSZ -- 算上共享库的总占用

linux基本操作

passwd修改密码

如果忘记密码,进single模式直接设置新密码(e-à+single-àb)。

查看系统版本:

[root@localhost ~]# lsb_release -a阅读全文
根本办法:  
  
  Options-〉“Document   Options”-〉在“File   filter”   里边加上*.cc  
  
  或者  
  Options-〉Preferences-〉Languages-〉点选C++   Language,然后点击右边“Doc   Types”按钮弹出-〉“Document   Options”-〉在“File   filter”   里边加上*.cc
post.php

<?php
$cfg = array (
  // 用户名
  name=>"xdy108@126.com",
  // 密码
  pass=>"001002",
    cookie_file=>"cookie.txt"
);
include("httpd.php");
$c = new http("login.kaixin.com", "/Login.do", "POST");
$c->set_val("email", $cfg["name"]);
$c->set_val("password", $cfg["pass"]);
$c->set_val("login_type", "1000");
$c->set_val("autoLogin", "true");
$c->set_val("origURL", "http://www.kaixin.com/SysHome.do");
$c->send();
$source = $c->get_cnt(true);
$s_regex = '@(societyguester=\w+;)@s';
$a_matches = array ();
preg_match_all($s_regex, $source, $a_matches);
$cookie_login_key = $a_matches[1][1];//cookie放入这个变量
$c2 = new http("gf.kaixin.com", "/bank.do", "POST", 80, $cookie_login_key);
$c2->send();
$source = $c2->get_cnt();
echo $source;  
exit;
?>


httpd.php阅读全文
    用一片DS18B20构成测温系统,测量的温度精度达到0.1度,测量的温度的范围在-20度到+100度之间,用8位数码管显示出来。
阅读全文

Polymorphism in PHP

WEB2.0 jack 2008-12-3 11:19
        While thinking about a programming language deficiency, I rediscovered polymorphism. Overloading a function allows a function call to behave differently when passed variables of different type. I was trying to devise a method of simulating function overloading, because PHP does not support it. I considered implementing a function with an if-else statement ladder that tests the type of the actual argument and executes statements that correspond to the argument’s type. This technique may ultimately result in a monolithic function or a function implementation that is too knowledgeable of multiple class hierarchies. Rethinking a problem that I was hoping to solve with function overloading allowed me to accept the lack of this language feature and think of other techniques.
There are two problems that arise from the if-else statement ladder approach. The implementation of such a function would require the function to have knowledge of every data type to be used with it. This problem can be extended to knowing class hierarchies, if subclassing is involved. This means that an introduction of a new data type to be processed by the function would require the function to be modified, which creates the possibility that the modification will break other existing code that relies on the function. The second problem is having the function’s maintainer think about how the function should operate on the different data types that can be passed to it. This responsibility is better placed on the people who maintain the different data types.
One solution that deals with the problems in the if-else statement ladder is polymorphism. Polymorphism allows a set of heterogeneous elements to be treated identically. It is achieved through inheritance. In PHP, interface inheritance and implementation inheritance can be written explicitly through the use of interfaces and class extensions, respectively. Interfaces specify a class interface without providing an implementation. Classes from different class hierarchies can implement an interface, and in this way, it can be seen as different from abstract classes. When a class is defined to implement an interface, the language enforces a rule that the class implements all features of the interface. A method that operates on an interface will accept an object of any class that implements the interface, and it will function correctly.
Here is a toy example of interface inheritance, polymorphism, and PHP type hinting:

<?
        interface HasArea
        {
                public function area();
                public function areaUnit();
        }

        abstract class Shape
        {
                private $color;

                public function __construct( $color )
                {
                        $this->color = $color;
                }

                public function getColor()
                {
                        return $this->color;
                }
        }

        class Rectangle extends Shape implements HasArea
        {
                private $w;
                private $h;

                public function __construct( $color, $w, $h )
                {
                        parent::__construct($color);
                        $this->w = $w;
                        $this->h = $h;
                }

                public function area()
                {
                        return ($this->w * $this->h);
                }

                public function areaUnit()
                {

                        if( $this->area() > 1 )
                                return "square meters";
                        else
                                return "square meter";
                }
        }

        class Territory implements HasArea
        {
                private $r;

                public function __construct()
                {
                        $this->r = $r;
                }

                public function area()
                {
                        return 5;
                }

                public function areaUnit()
                {
                        return "cities";
                }
        }

        function outputArea( HasArea $ha )
        {
                echo "The area is:  "
                 . " {$ha->area()} {$ha->areaUnit()}\n";
        }

        $HAs = array(
                new Rectangle("red",2,3),
                new Territory()
        );

        foreach( $HAs as $HA )
        {
                outputArea( $HA );
        }
?>

Type hints help the PHP interpreter enforce the restriction that outputArea() operates only on objects of data types that implement the HasArea interface. Rectangle and Territory are from unrelated class hierarchies. outputArea() can operate on these classes, since these classes implement the HasArea interface.
The explored method accomplishes only some of the features offered by function overloading. In the above example, outputArea() was restricted to one argument. In some programming languages, a function can be overloaded on the number of arguments along with the types of those arguments and the order that those types appear in the argument list. This method, however, was useful in a problem I considered solving with function overloading.

http://www.phpchina.com/html/10/10-3496.html
原理

    一般,有2种方法可以导出doc文档,一种是使用com,并且作为php的一个扩展库安装到服务器上,然后创建一个com,调用它的方法。安装过 office的服务器可以调用一个叫word.application的com,可以生成word文档,不过这种方式我不推荐,因为执行效率比较低(我测试了一下,在执行代码的时候,服务器会真的去打开一个word客户端)。理想的com应该是没有界面的,在后台进行数据转换,这样效果会比较好,但是这些扩展一般需要收费。
    第2种方法,就是用PHP将我们的doc文档内容直接写入一个后缀为doc的文件中即可。使用这种方法不需要依赖第三方扩展,而且执行效率较高。
    word本身的功能还是很强大的,它可以打开html格式的文件,并且能够保留格式,即使后缀为doc,它也能识别正常打开。这就为我们提供了方便。但是有一个问题,html格式的文件中的图片只有一个地址,真正的图片是保存在其他地方的,也就是说,如果将HTML格式写入doc中,那么doc中将不能包含图片。那我们如何创建包含图片的doc文档呢?我们可以使用和html很接近的mht格式。
    mht格式和html很类似,只不过在mht格式中,外部链接进来的文件,比如图片、Javascript、CSS会被base64进行编码存储。因此,单个mht文件就可以保存一个网页中的所有资源,当然,相比html,它的尺寸也会比较大。
    mht格式能被word识别吗?我将一个网页保存成mht,然后修改后缀名为doc,再用word打开,OK,word也可以识别mht文件,并且可以显示图片。
    好了,既然doc可以识别mht,下面就是考虑如何将图片放入mht了。由于html代码中的图片的地址都是写在img标签的src属性中,因此,只要提取html代码中的src属性值,就可以获得图片地址。当然,有可能您获取到的是相对路径,没关系,加上URL的前缀,改成绝对路径就可以了。有了图片地址,我们就可以通过file_get_content函数获取到图片文件的具体内容,然后调用base64_encode函数将文件内容编码成 base64编码,最后插入到mht文件的合适位置即可。
    最后,我们有两种方法将文件发送给客户端,一种是先在服务器端生成一个doc文档,然后将这个doc文档的地址记录下来,最后,通过 header("location:xx.doc");就可以让客户端下载这个doc。还有一种是直接发送html请求,修改HTML协议的header 部分,将它的content-type设置为application/doc,将content-disposition设置为attachment,后面跟上文件名,发送完html协议以后,直接将文件内容发送给客户端,也可以让客户端下载到这个doc文档。
阅读全文
分页: 262/338 第一页 上页 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 下页 最后页 [ 显示模式: 摘要 | 列表 ]