Mar 25, 2011

c tips from wang

wlh走前给大家分享的一些c tips, 我还没搞懂,先记下
#include <stdio.h>

#define get_parent(x) (struct A*)((unsigned int)x - (unsigned int)&((((struct A*)0)->j)))

struct A
{
    char name[37];
    int i;
    int j;
    int k;
    char city[37];
};

void func(int* p_int)
{
    printf("in function %x\n", (unsigned int)get_parent(p_int));
}

int main()
{
    struct A a_var;
    printf("address of a_var is %x\n", (unsigned int)&a_var);
    func(&(a_var.j));
    return 0;
}

变态的面试题

从下面两个地方看到的 via Google Reader ...
http://www.kernelchina.org/?q=node/961
http://coolshell.cn/articles/3961.html
int n=20;
for(int i=0; i<n; i--){
    printf("-");
}
将上面的代码,修改或添加一个字符,使程序可以输出20个减号.(据说c有三种;java有二种修改方法)
C代码:
#include <stdio.h>

int main(int argc, char *argv[])
{
    /* first way */
    int n=20;
    for(int i=0; i<n; n--){
        printf("-");
    }
    printf("\n");

    /* second way */
    n=20;
    for(int i=0; -i<n; i--){
        printf("-");
    }
    printf("\n");

    /* third way */
    n=20;
    for(int i=0; i+n; i--){
        printf("-");
    }
    printf("\n");

    return 0;
}

Java代码:
class Test
{
    public static void main(String[] args)
    {
        /*
        int n=20;
        for(int i=0; i<n; i--){
            printf("-");
        }
        */

        /* first way */
        int n=20;
        for(int i=0; i<n; n--){
            System.out.print("-");
        }
        System.out.print("\n");

        /* second way */
        n=20;
        for(int i=0; -i<n; i--){
            System.out.print("-");
        }
        System.out.print("\n");
    }
}
C中的第三种:
/* third way */
n=20;
for(int i=0; i+n; i--){
    System.out.print("-");
}
System.out.print("\n");
$ javac Test.java
Test.java:28: incompatible types
found   : int
required: boolean
        for(int i=0; i+n; i--){
                      ^
1 error

Mar 23, 2011

enable core dump on Debian

globally enable core dump on Debian
1. edit /etc/security/limits.conf, add the bellowing line
*               soft    core            unlimited
2. logout and login
$ ulimit -c
$ ulimit -a

blogspot code syntax highlight

以前写的blogspot贴代码:http://ox0spy.blogspot.com/2009/03/test.html
参考: http://mlawire.blogspot.com/2009/07/blogger-syntax-highlighting.html
下载模板,在 </head> 前添加
    <!-- highlighting code -->
    <link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
    <link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
    <!-- add brushes here -->
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js' type='text/javascript'/>
    <script type='text/javascript'>
      SyntaxHighlighter.config.bloggerMode = true;
      SyntaxHighlighter.all();
    </script>
然后上传模板,保存 ...
测试c程序
#include <stdio.h>

int main(int argc, char* argv[])
{
    printf("test only ...\n");
    return 0;
}
测试bash程序
#/bin/bash

echo "test only ..."

不希望显示行号,使用: class="brush:bash; gutter:false"

Mar 22, 2011

一道笔试题

#include <stdio.h>
#include <string.h>

char *shift_r(const char *src, const int n, char *dst)
{
    int i, len;

    if (src == NULL || (len = strlen(src)) == 0)
        return (dst = NULL);

    for (i = 0; i < len; i++)
        dst[(i+n)%len] = src[i];
    dst[i] = '\0';

    return dst;
}

int main(int argc, char *argv[])
{
    const char *src = "abcdef";
    char dst[80];
    const int n = 2;

    printf("%s\n%s\n", src, shift_r(src, n, dst));

    return 0;
}
好吧, 这个也算 ... 口头描述这个真没劲 ...
#!/bin/bash

echo "testing for ..."
for ((i=0; i < 10; i++))
do
    echo $i
done

echo "testing while ..."
i=0
while [ $i -lt 10 ]
do
    echo $i
    i=$((i+1))
done

Mar 18, 2011

学习 qt c++

下次需要学什么还是碰到就开始好好学, 过去这么多时间,悲剧阿 ...

log ...

Mar 15, 2011

Installing Debian from USB

download files

Make bootable USB

  • Note: in this example, the USB stick is SCSI device /dev/sdb
  • $ sudo apt-get install mtools syslinux dosfstools
  • $ sudo modprobe usb-storage
  • $ sudo fdisk -l /dev/sdb
  • $ sudo mkdosfs -I /dev/sdb
  • $ sudo syslinux /dev/sdb
  • $ sudo mount /dev/sdb /mnt
  • $ sudo cp -f vmlinuz initrd.gz syslinux.cfg debian-6.0.0-i386-netinst.iso /mnt
  • $ sudo umount /mnt

boot from USB stick

  • set the first boot device in the BIOS to USB-ZIP.
  • insert your USB drive, and boot your computer.

Mar 10, 2011

同事的你 [zz]

/*
 * author: unknown, 来自网络
 * description: 这个时间看到这个歌词, 感慨万千 ~ 
 */


明天你是否会想起
昨天你写的程序
明天你是否还惦记
曾经爱编程的你
领导们都已想不起
曾经加班的你
我也是偶然看程序
才想起同事的你
谁聘了牛B烘烘的你
谁安慰天天加班的你
谁把你的设计做起
谁给你做的升级

你从前总是很小心
从不把离职的事提起
你也曾无意中说起
薪水实在太低
那时候屏幕总是在闪
日子总过得太慢
你总说离职遥遥无期
转眼就各奔东西
谁聘了牛B烘烘的你
谁安慰天天加班的你
谁看了我给你写的信
谁把它删在垃圾文件夹里

从前的日子都远去
我也将有我的新上司
我也会给他编程序
给他讲同事的你
谁聘了牛B烘烘的你
谁安慰天天加班的你
谁把你的设计做起
谁给你做的升级
. . .

Mar 3, 2011

Python Challenge

http://www.pythonchallenge.com

太有意思了, 已经使我无法认真看 Android App 开发了 ...
我把自己做得答案都放到 github 上了,
https://github.com/ox0spy/Python-Challenge