Chinaunix首页 | 论坛 | 博客
  • 博客访问: 363278
  • 博文数量: 112
  • 博客积分: 5245
  • 博客等级: 大校
  • 技术积分: 1120
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-07 09:20
个人简介

静下来,定好方向,好好干。

文章分类
文章存档

2017年(1)

2012年(1)

2011年(5)

2010年(6)

2009年(16)

2008年(59)

2007年(24)

我的朋友

分类: C/C++

2007-11-15 11:56:50

关于格式:
1. ,空   ;空   二元操作符前后留空: 空=空   关键词+空: if ()
2. 边写代码边写注释是好习惯;
3. if ( NULL == ptr) 写法比较好;

提供readme 使用方法;
文件头格式:
/***********************************************************************************
# Filename:  
# Description:
# Version: 1.12
# Compiler: GCC.2.95
# History:   
                     2007.1.1 Created by aaa
                     2007.2.5 Modified by bbb
*/

/****************************************Copyright (c)**************************************************
**                               XXXXX Development Co.,LTD.
**                                
**
**--------------File Info-----------------------------------------------------
** File name:   xxxx.c
** Last modified Date: 
** Last Version: 
** Descriptions: 
**
**--------------------------------------------------------------------------
** Created by:  
** Created date: 
** Version:    1.0
** Descriptions:  The original version
**
**--------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/

函数头格式:
/***********************************************************************************
# Function Name:
# Description:
# Parameters:
# Return Value:
# Note:
*/

/*********************************************************************************************************
** Function name:
** Descriptions: 
** Input: Parameter: Parameter string
**            PLen: Parameter string length
** Output: TRUE:  OK
**              FALSE: NOT OK
** Created by: 
** Created Date:
**--------------------------------------------------------------------------
** Modified by:
** Modified Date:
**--------------------------------------------------------------------------
********************************************************************************************************/


scanf的应用: int m;    scanf("%d", &m);   char a[10];    scanf(" %s", a);
scanf ( "%s%d", p->name, p->age);   输入时一般用空格分开2个数据项;

struct mystruct *p = NULL;   初始化时置NULL比较安全;

内存malloc, free函数在库;

void strcopy( char *s, char *t)
{
       while ( *s++ = *t++)
             ;
}

void strcomp( char *s, char *t)
{
          if ( ; *s == *t; s++, t++)
                if ( *s == '\0')
                      return 0;

          return *s - *t;
}

二叉数, 遍历顺序:
    前序: 中左右   中序: 左中右   后序: 左右中

闰年的条件:   ( year % 4 == 0 && year % 100 ! = 0) | | year % 400 == 0

转为整型数据:   atoi( argv[1])   转为浮点数据:   atof(argv[2])  

库编译时要加上gcc . . . -lm

周全的做法是检查输入参数的个数, 然后再使用参数;

下标循环问题的解决:   i = (i + 1) % N

char *str;   str = (char *) malloc (sizeof(char) * 50);   free(str);

一个变量的生命周期:   定义, 初始化, 读写值, 销毁;

任何数不作特别说明都是有符号的;
printf ( " -3  0x%x   %u   %d\n", -3, -3, -3);

main() {}别把子程序包进;

原码, 补码:   原码就是在绝对值的最高位由0改1得到的, 补码就是由原码除去最高位的部分取反加1得到的.
计算机里存储数据的补码, -1在32bit计算机里就是0xFFFFFFFF;

给某位置0, data & ~( 1 << p);

if ( 0 ==strcmp ( type, " %x" ))字符串比较    if( 'z' ==type)字符比较       strstr()查找字符串;

函数中return之后的语句不被执行;

递归的终止条件是不再调用自身;

void func(void)   定义时括号里的void应加上;

sqrt (double t)   参数需要double型;

sort链表应采取的办法: 从前面往后推进, tail从后往前推进, 相邻交换节点;

删除链表节点要free(ptr);

关于链表的操作:
在头节点后面插入:   item->next = head->next;   head->next = item;
双向链表的删除:  
delete_node ( struct node *b)
{
    if ( NULL ! = b->prev)
       b->prev->next = b->next;
    if (NULL ! = b->next)
       b->next->prev = b->prev;
}

交换a, b的值有种方法:   a = a + b;   b = a - b;   a = a - b;

MST最小生成树   Minimum Spanning Tree
树:   具有n个节点, n-1条边.

排序算法: 选择排序, 插入排序, 快速排序, 归并排序, 冒泡排序, shell希尔排序
查找算法: 折半查找(有序序列) O ( lg N), 逐个查找O( N)   其他>>>>????
sentry哨兵技术???

xgraph:   /usr/local/bin/xg
#!/bin/sh
xgraph -geometry 800x600+100+50   -bb   -tk   -p   -bg   white   $@
. /mst | xg

. /freuecy < makefile | ptree
/usr/local/bin/ptree

管道与重定向:   管道可以层层传递, 而重定向就导向结束;
程序的结构化:   分函数;   模块化:   分文件;
形参:   int func(int a);        无名形参:    void push(double);   实参:   func(15);


int  (*day) [13]   指向整型数组的指针;
int * day[13]   整型指针的数组;
void *comp( )   函数返回类型;
void (*comp) ( )   函数指针;    一般用于回调用户定制的函数;
函数名是函数的地址, 就类似数组名;   函数指针的引用: (* comp)(   );

while (--argc)   printf ( "%s\n", *++argv);
(*++argv) [0]   ==>argv[1][0];
*++argv[0] ==>argv[0][1];

优先级问题:
aa->zz . x   从左向右结合;
2 << 7 >> 2   从左到右处理;
top++   先用值, 再++;
从右到左的运算符有:
    !   ~   ++   --   +   -   *   &   (type)   sizeof
    +=   -= *=   /=   %=   &=
    ?  :

指针之间的加法是非法的, 所以求中值需要这样:   p1 + (p2 - p1)/2;

MST查找最短路径:
    st[]   存放已经上树的节点/双亲节点;   st[] = -1是未上树的部分;
    fr[]   未收入树的节点中, 与树最近的节点号;
    wt[]   未收入树的节点中, 与树最近的距离值;

. h里面一般是函数申明及类的定义, 也有部分宏定义.

快速排序qsort():
int partition ( int l, int r)
{
    int i= l +1, j = r;
    do{
       while ( i<=j && v[i]<=v[j] ) i++;
       while ( i<=j && v[l]<=v[j] ) j--;
       swap ( i, j);            //若i或j越界, 均会发生一次多余swap;
    }while ( i <= j);
    swap ( i, j);               //恢复i, j的位置
    swap ( l, j);               //j最终会 停留在<=v[l]的数据上, 交换符合左小右大的大原则;
    return j;
}

对有序表, 择半查找:
    定义全局V[N], 返回下标值;
int bisearch( int x, int l, int r)
{
    int m = ( l + r)/2;
    if ( l > r) return -1;
    if ( x > V[m] ) return bisearch( x, m+1, r);
    if ( x < V[m] ) return bisearch( x, l, m-1);
    return m;
}

对无序表排序, 希尔排序:
void shellsort(void)
{
    int gap, i, j, temp;
    for ( gap = N/2; gap > 0; gap /= 2)
       for ( i = gap; i < N; i++)
          for ( j=i-gap; j>=0 && V[j]>V[j+gap]; j-=gap)
             temp = V[j], v[j] = V[j+gap], V[j+gap] = temp;
}

递归调用:
putchar若在if后面, 则输出顺序正确; 若放在if前面, 则输出顺序反了;
void printd ( int n)
{
    if ( n / 10 ) printd ( n / 10 );
    putchar ( n % 10 + '0');
}
   

一些C++例程:
/*mystring.h
 * this is a string class
 */

#include
#include

using namespace std;

/*MyString class*/
class MyString
{
friend ostream& operator<<(ostream&, const MyString&);
friend MyString operator+(const MyString&, const MyString&);
public:
    MyString(const char*);
    MyString(const MyString&);
    ~MyString(void);
    MyString& operator=(const MyString&);
    int length(void) const;
    bool isEmpty(void) const;
private:
    char *data;
    int len;  //don't include '\0'
};

/*construct function*/
MyString::MyString(const char* str="")
{
     char* cp;
     const char* cs;
     len = strlen(str);
     data = new char [len + 1];
     cp = data;
     cs = str;
     while (*cs)
         *cp++ = *cs++;
     *cp = '\0';
}

MyString::MyString(const MyString& mstr)
{
    char* cp;
    char* cs;
    len = mstr.len;
    data = new char [len + 1];
    cp = data;
    cs = mstr.data;
    while (*cs)
        *cp++ = *cs++;
    *cp = '\0';
}
MyString::~MyString(void)
{
    delete [] data;
}

MyString& MyString::operator=(const MyString& mstr)
{
    char* cp;
    char* cs;
    if (this == &mstr) return *this;
    delete [] data;
    len = mstr.len;
    data = new char [len + 1];
    cp = data;
    cs = mstr.data;
    while (*cs)
        *cp++ = *cs++;
    *cp = '\0';
    return *this;
}    
int MyString::length(void) const
{
     return len;
}
bool MyString::isEmpty(void) const
{
     if (len == 0)
          return true;
     else
          return false;
}
ostream& operator<<(ostream& o, const MyString& mstr)
{
     return o << mstr.data;   
}
MyString operator+(const MyString& ma, const MyString& mb)
{
     char *cp, *cs;
     MyString addrs;
     addrs.len = ma.len + mb.len;
     delete [] addrs.data;
     addrs.data = new char [addrs.len+1];
     cp = addrs.data;
     cs = ma.data;
     while (*cs)
          *cp++ = *cs++;
     cs = mb.data;
     while (*cs)
          *cp++ = *cs++;
     *cp = '\0';
     return addrs;
}

//main.cpp

#include "mystring.h"

int main()
{
    MyString str("hello");
    MyString str1 = str;
    cout<<"Length of str1: "<    cout<<"Value of str1: "<
    MyString str2 = "";
    cout<<"Is str2 empty? "<<(str2.isEmpty()?"true":"false")<
    str2 = " world";
    cout<<"Now value of str2: "<
    cout<<"Now value of str: "<    cout<<"Value of another expression: "<    cout<<"Value of yet another expression: "<<"Hello," + str2<
    MyString str3 = str1 + str2;
    cout<<"Value of str3: "<
    return 0;
}


/*list.cc
*class inherit
*/

#include
#include
using namespace std;
class List
{
     protected:
     int *list_;
     int n_;

     public:
     List(int n=10) : n_(n)
     {
          list_ = new int [n_];
          srand(time(NULL));
          for (int i=0; i               list_[i] = rand() % n_;
     }
     ~List()
     {
          delete [] list_;
          cout << "List dtor" << endl;
     }
     virtual void show() const
     {
          for (int i=0; i               cout << list_[i] << ' ';
          cout << endl;
     }
};
class OList : public List
{
     private:
          void swap(int &a, int &b)
          {
               int c;
               c = a;
               a = b;
               b = c;
          }
          void Qsort(int l, int r)
          {
               if (l>=r) return;
               int k = partition(l, r);
               Qsort(l, k-1);
               Qsort(k+1, r);
          }
          int partition(int l, int r)
          {
               int i, j;
               for (i=l+1, j=l; i<=r; i++)
                    if (list_[i]                         swap(list_[i], list_[++j]);
               swap(list_[l], list_[j]);
               return j;
          }
     public:
     OList(int n=8) : List(n)
     {
     }   
     virtual void show()
     {
          Qsort(0, n_-1);
          for (int i=0; i               cout << list_[i] << ' ';
          cout << endl;
     }
    
};
const int N = 8;
int main()
{
     List l(N);
     l.show();
     sleep(1);
     OList o(N);
     o.show();
     return 0;
}

/*mymatrix.cc
*matrix deal
*/

#include
#include

using namespace std;

class Matrix
{
     double **matrix_;
     int n_;

     public:
          Matrix(int n=10) : n_(n)
          {
               matrix_ = new double* [n_];
               for (int i=0; i                    matrix_[i] = new double [n_];
               srand(time(NULL));
               for (int i=0; i                    for (int j=0; j                         matrix_[i][j] = rand()%10;
          }
          ~Matrix()
          {
               for (int i=0; i                    delete [] matrix_[i];
               delete [] matrix_;
         
          }
         
          Matrix(const Matrix &m)
          {
               cout << "copy" << endl;
               n_ = m.n_;
               matrix_ = new double*[n_];
               for (int i=0; i               {
                    matrix_[i] = new double [n_];
                    for (int j=0; j                    {
                         matrix_[i][j] = m.matrix_[i][j];
                    }
               }
          }
          Matrix& operator=(const Matrix &m)
          {
               int i, j;
               cout << "=" << endl;
               for (i=0; i                    delete [] matrix_[i];
               delete [] matrix_;

               matrix_ = new double*[m.n_];
               for (i=0; i               {
                    matrix_[i] = new double [m.n_];
                    for (j=0; j                    {
                         matrix_[i][j] = m.matrix_[i][j];
                    }
               }
               return *this;
          }

         
          void transpose()
          {
               Matrix temp(n_);
               for (int i=0; i               {
                    for (int j=0; j                    {
                         temp.matrix_[i][j] = matrix_[j][i];
                    }
               }
                for (int i=0; i               {
                    for (int j=0; j                    {
                         matrix_[i][j] = temp.matrix_[i][j];
                    }
               }

          }
          /*
Matrix& operator+\
            (const Matrix &m)
{
               cout << "+" << endl;
     for (int i=0; i          for (int j=0; j               matrix_[i][j] += m.matrix_[i][j];
     return *this;
}*/
          friend Matrix operator+\
               (const Matrix &m1, const Matrix &m2);
          friend Matrix operator*\
               (const Matrix &m1, const Matrix &m2);
          friend ostream &operator<<\
               (ostream &o, const Matrix &m);
};

Matrix operator*\
            (const Matrix &m1, const Matrix &m2)
{
               cout << "*" << endl;
     Matrix temp(m1.n_);
     for (int i=0; i     {
          for (int j=0; j          {
               temp.matrix_[i][j] = 0;
               for (int k=0; k               {
                    temp.matrix_[i][j] +=\
                          m1.matrix_[i][k] * m2.matrix_[k][j];
               }
          }
     }
     return temp;
}
Matrix operator+\
            (const Matrix &m1, const Matrix &m2)
{
               cout << "+" << endl;
     Matrix temp(m1.n_);
     for (int i=0; i     {
          for (int j=0; j          {
               temp.matrix_[i][j] = 0;
               temp.matrix_[i][j] =\
                     m1.matrix_[i][j] + m2.matrix_[i][j];
          }
     }
     return temp;
}
ostream &operator<<\
     (ostream &o, const Matrix &m)
{
     for (int i=0; i     {
          for (int j=0; j          {
               o << m.matrix_[i][j] << "  ";
          }
          o << endl;
     }
     return o;
}

const int N = 5;
int main()
{
     Matrix m(N);
     sleep(1);
     Matrix n(N);
     cout << "use = or copy?" << endl;
     Matrix k=n+m;
     cout << m << endl;
     cout << n << endl;
     cout << k << endl;
     k = m + n;
     cout << m + n << endl;
     cout << m * n << endl;
     m.transpose();
     cout << m << endl;
     return 0;
}


/*stack.cc*/

#include
using namespace std;

class Stack
{
     int *stack_;
     int n_;
     int top_;
     public:
          Stack(int n=1024) : n_(n)
          {
               //stack = malloc(n*sizeof(int));
               stack_ = new int [n_];
               top_ = 0;
          }
          ~Stack() { delete [] stack_;}
          Stack(const Stack &S)
          {
               n_ = S.n_;
               top_ = S.top_;
               stack_ = new int [n_];
               for (int i=0; i                    stack_[i] = S.stack_[i];
          }
          Stack operator=(const Stack &S)
          {
               n_ = S.n_;
               top_ = S.top_;
               stack_ = new int [n_];
               for (int i=0; i                    stack_[i] = S.stack_[i];
               return *this;
          }
          void push(int item);
          int pop(void);
          bool empty() const;
};
void Stack::push(int item)
{
     stack_[top_++] = item;
}
int Stack::pop(void)
{
     return stack_[--top_];
}
bool Stack::empty() const
{
     return top_ == 0;
}

const int N = 10;
int main()
{
     Stack S1;
     for (int i=0; i     {
          S1.push(i);
     }
    /*
     for (int i=0; i     {
          cout << S1.pop() << endl;
     }*/
     Stack S2(S1);
     while(!S2.empty())
     {
          cout << S2.pop() << endl;
     }
     return 0;
}


信息点滴:
! ! ! 结构体, 共用体, 枚举
! ! ! BSTree   Hash   Binary-search
debian浏览器位置不对, 注销重新登录即可;    菜单隐藏ctrl + M;
gcc --version查版本号;
EOF = -1;   在stdio.h中定义;
wc test.c   统计数据;
float, double用%d打印出来是乱数;
, 运算符是从左往右结合. 右边的值是结果.   如: i = 1, i =2;
ctrl+D 是退出shell;
sudo apt-get install gnome;
x =3, y=2;    x += y+= 1;   从右往左运算;
palindrome:   Madam i'm Adam==>madamimadam    deed   level
ps -aux,   netstat -apn;
ctrl+ 退格   删除当前行;
*p++ == *(p++)   *++p == *(++p)
堆栈段只能读写不能执行;
拼音输入法不稳定, 也许输入法重装可以;
RTFM--->read the fine(f**k) mannual
CGDB   ESC由下到上, i由上到下, 但scanf不能输入;
哲学家用餐问题:   5个哲学家, 每人一根筷子, 每人有三种状态: 思考   吃   饥饿;



阅读(782) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~