个人总结的代码

CLog.h

#ifndef LOG_CLOG_H_
#define LOG_CLOG_H_

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <stdarg.h>

#define __FILE_INFO__     __FILE__, __LINE__, __FUNCTION__
#define INFO(f, arg...)    event_info("%s:%d***%s"f"\n", __FILE_INFO__, ##arg)

void static event_info(const char *fmt, ...)
{
        char buff[1024];
        memset(&buff, 0x00, sizeof(buff));
        va_list ap;
        va_start(ap, fmt);
        vsnprintf(buff, sizeof(buff) - 1, fmt, ap);

        struct tm tt;
        struct timeval tv;
        memset(&tt, 0, sizeof(tt));
        memset(&tv, 0, sizeof(tv));
        gettimeofday(&tv, NULL);
        localtime_r(&tv.tv_sec, &tt);
        fprintf(stderr, "[%s]%04d-%02d-%02d %02d:%02d:%02d.%04ld %s", "DEBUG",
                        tt.tm_year + 1900, tt.tm_mon + 1, tt.tm_mday, tt.tm_hour, tt.tm_min,
                        tt.tm_sec, tv.tv_usec/1000, buff);

        va_end(ap);
}

#endif /* LOG_CLOG_H_ */


#include <stdio.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>

#include <iostream>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <vector>
#include <stdarg.h>

#include "CLog.h"

#include <deque>

using namespace std;

//===============构造函数执行顺序
//抽象类A  
class A  
{  
public:  
    A()  
    {  
        cout<<"抽象类A的构造函数"<<endl;  
    }  
    //纯虚函数fun  
    virtual void fun1() = 0;  
};  
 
//抽象类B  
class B  
{  
public:  
    B()  
    {  
        cout<<"抽象类B的构造函数"<<endl;  
    }  
    //纯虚函数fun  
    virtual void fun2() = 0;  
};  
 
//普通类C  
class C  
{  
public:  
    C()  
    {  
        cout<<"类C的构造函数"<<endl;  
    }  
};  
 
//普通类D  
class D  
{  
public:  
    D()  
    {  
        cout<<"类D的构造函数"<<endl;  
    }  
};  
 
//普通类C  
class E  
{  
public:  
    E()  
    {  
        cout<<"类E的构造函数"<<endl;  
    }  
};  
 
//普通类D  
class F  
{  
public:  
    F()  
    {  
        cout<<"类F的构造函数"<<endl;  
    }  
};  
 
//普通类D  
class G  
{  
public:  
    G()  
    {  
        cout<<"类G的构造函数"<<endl;  
    }  
};  
 
//普通类D  
class H  
{  
public:  
    H()  
    {  
        cout<<"类H的构造函数"<<endl;  
    }  
};  
//普通类D  
class M  
{  
public:  
    M()  
    {  
        cout<<"类M的构造函数"<<endl;  
    }  
};  
 
class Test: public A,public B,virtual public C,virtual public D,public E,public F  
{  
public:  
    Test():B(),A(),D(),C(),F(),E()
    {  
        cout<<"类Test的构造函数"<<endl;  
    }  
    void fun1()  
    {  
    }  
    void fun2()  
    {  
    }  
private:  
    G g;  
    static H h;  
    static M m;  
};  
 
H Test::h;

//===============构造函数执行顺序

typedef struct Node
{
    int data;
    struct Node *pLeft;
    struct Node *pRight;
} BiTreeNode, *BiTree;

struct LinkNode
{
    int data;
    struct LinkNode *next;
};

// 创建二叉树
BiTree CreateTree()
{
    BiTree T;
    int data;
    scanf("%d", &data);

    if (0 == data)
    {
        T = NULL;
    }
    else
    {
        T = (BiTree) malloc(sizeof(BiTree));
        T->data = data;
        T->pLeft = CreateTree();
        T->pRight = CreateTree();
    }

    return T;
}

void Visit(int value)
{
    printf("[%d]", value);
}

// 先序遍历
void PreOrderTree(BiTree pTree)
{
    if (pTree)
    {
        Visit(pTree->data);
        PreOrderTree(pTree->pLeft);
        PreOrderTree(pTree->pRight);
    }
}

// 先序非递归
void PreOrderTreeMy(BiTree pTree)
{
    stack<BiTree> s;
    BiTree p = pTree;

    while (NULL != p || !s.empty())
    {
        while (NULL != p)
        {
            Visit(p->data);
            s.push(p);
            p = p->pLeft;
        }

        if (!s.empty())
        {
            p = s.top();
            s.pop();
            p = p->pRight;
        }
    }
}

// 中序遍历
void InOrderTree(BiTree pTree)
{
    if (pTree)
    {
        InOrderTree(pTree->pLeft);
        Visit(pTree->data);
        InOrderTree(pTree->pRight);
    }
}

// 中序非递归
void InOrderTreeMy(BiTree pTree)
{
    stack<BiTree> s;
    BiTree p = pTree;

    while (NULL != p || !s.empty())
    {
        while (NULL != p)
        {
            //Visit(p->data);
            s.push(p);
            p = p->pLeft;
        }

        if (!s.empty())
        {
            p = s.top();
            Visit(p->data);
            s.pop();
            p = p->pRight;
        }
    }
}

// 后序遍历
void PostOrderTree(BiTree pTree)
{
    if (pTree)
    {
        PostOrderTree(pTree->pLeft);
        PostOrderTree(pTree->pRight);
        Visit(pTree->data);
    }
}

// 后序非递归
void PostOrderTreeMy(BiTree pTree, vector<int> &path)
{
    stack<pair<BiTree, bool> > s;
    BiTree root = pTree;
    s.push(make_pair(root, false));
    bool visit;

    while (!s.empty())
    {
        root = s.top().first;
        visit = s.top().second;
        s.pop();

        if (NULL == root)
        {
            continue;
        }

        if (visit)
        {
            path.push_back(root->data);
        }
        else
        {
            // 后续遍历,先左孩子右孩子 最后根节点
            s.push(make_pair(root, true));
            s.push(make_pair(root->pRight, false));
            s.push(make_pair(root->pLeft, false));
        }
    }
    return;
}

// 求一个二叉树的镜像
BiTree GetMirror(BiTree pTree)
{
    if (NULL == pTree)
    {
        return NULL;
    }

    // 左孩子节点
    BiTree pLeft = GetMirror(pTree->pLeft);
    // 右孩子节点
    BiTree pRight = GetMirror(pTree->pRight);

    // 交换节点
    pTree->pLeft = pRight;
    pTree->pRight = pLeft;

    return pTree;
}

// 求二叉树深度
int GetDepth(BiTree pTree)
{
    if (NULL == pTree)
    {
        return 0;
    }

    int depthLeft = GetDepth(pTree->pLeft);
    int depthRight = GetDepth(pTree->pRight);

    return (depthLeft > depthRight) ? (depthLeft + 1) : (depthRight + 1);
}

// 求二叉树叶子节点个数
int GetLeafNum(BiTree pTree)
{
    if (NULL == pTree)
    {
        return 0;
    }

    if ((NULL == pTree->pLeft) && (NULL == pTree->pRight))
    {
        Visit(pTree->data);
        return 1;
    }

    int leafLeft = GetLeafNum(pTree->pLeft);
    int leafRight = GetLeafNum(pTree->pRight);

    return leafLeft + leafRight;
}

int BinarySearch(int a[], int len, int value)
{
    int low = 0;
    int high = len - 1;
    int mid = (high - low)/2;

    while (low <= high)
    {
        if (a[mid] == value)
        {
            return mid;
        }
        else if (a[mid] > value)
        {
            high = mid - 1;
        }
        else
        {
            low = mid + 1;
        }
    }

    return -1;
}

int Partion(int a[], int low, int high)
{
    int i = low;
    int j = high;
    int key = a[low];

    while (i < j)
    {
        while (i < j && a[j] >= key)
        {
            j--;
        }
        // 从后面开始找到第一个小于key的元素,放到low位置
        a[i] = a[j];

        while (i < j && a[i] <= key)
        {
            i++;
        }
        // 从前面开始找到第一个大于key的元素,放到high位置
        a[j] = a[i];
    }

    // 枢轴归位
    a[i] = key;
    return i;
}

// 求一个数组前K个最小值
void GetMinK(int a[], int k, int len)
{
    printf("前%d个最小值", k);
    int low = 0;
    int high = len - 1;

    int index = Partion(a, low, high);
    while (index != k-1)
    {
        if (index > k - 1)
        {
            high = index - 1;
            index = Partion(a, low, high);
        }
        else
        {
            low = index + 1;
            index = Partion(a, low, high);
        }
    }

    for (int i = 0; i <k; i++)
    {
        Visit(a[i]);
    }

    return;
}

// 归并排序
void Merger(int v[], int first, int mid, int last)
{
   deque<int> tempV;
   int indexA, indexB;
   //设置indexA,并扫描subArray1 [first,mid]
   //设置indexB,并扫描subArray2 [mid,last]
   indexA = first;
   indexB = mid;
   //在没有比较完两个子标的情况下,比较 v[indexA]和v[indexB]
   //将其中小的放到临时变量tempV中
   while (indexA < mid && indexB < last)
   {
       if (v[indexA] < v[indexB])
       {
           tempV.push_back(v[indexA]);
           indexA++;
       }
       else
       {
           tempV.push_back(v[indexB]);
           indexB++;
       }
   }
   //复制没有比较完子表中的元素
   while (indexA < mid)
   {
       tempV.push_back(v[indexA]);
       indexA++;
   }
   while (indexB < last)
   {
       tempV.push_back(v[indexB]);
       indexB++;
   }
   int index = 0;
   while (tempV.size() > 0)
   {
       v[first+index] = tempV.front();
       index++;
       tempV.pop_front();
   }
}

// 归并排序
void MergerSort(int v[], int first, int last)
{
   if (first + 1 < last)
   {
       int mid = (first + last) / 2;
       MergerSort(v, first, mid);
       MergerSort(v, mid, last);
       Merger(v, first, mid, last);
   }
}

void QuickSort(int a[], int low, int high)
{
    // 此处防止死循环
    if (low >= high)
    {
        return;
    }

    int i = low;
    int j = high;
    int key = a[low];

    while (i < j)
    {
        while (i < j && a[j] > key)
        {
            j--;
        }
        // 交换
        swap(a[i], a[j]);

        while (i < j && a[i] < key)
        {
            i++;
        }
        // 交换
        swap(a[i], a[j]);
    }

    // 枢轴归位
    a[i] = key;
    QuickSort(a, low, i - 1);
    QuickSort(a, i + 1, high);
}

// 打印数组
void PrintArray(int a[], int len)
{
    for (int i = 0; i < len; i++)
    {
        Visit(a[i]);
    }
}

// 创建链表
LinkNode *CreateLink()
{
    LinkNode *head = (LinkNode *) malloc(sizeof(LinkNode));
    if (!head)
    {
        return NULL;
    }
    head->data = -1;
    head->next = NULL;
    LinkNode *pHead = head;

    int data;
    scanf("%d", &data);
    while (0 != data)
    {
        LinkNode *pNode = (LinkNode *) malloc(sizeof(LinkNode));
        pNode->data = data;
        pNode->next = NULL;
        head->next = pNode;
        head = pNode;
        scanf("%d", &data);
    }

    return pHead;
}

// 打印链表
void PrintLink(LinkNode *phead)
{
    LinkNode *pTemp = phead->next;
    while (pTemp)
    {
        Visit(pTemp->data);
        pTemp = pTemp->next;
    }
}

// o(n)获取链表倒数第K个节点的值
LinkNode *GetKth(LinkNode *phead, int k)
{
    if (NULL == phead)
    {
        return phead;
    }

    LinkNode *pAhead = phead;
    LinkNode *pBehind = phead;

    // pAhead先走k-1步
    while (k > 1 && (NULL != pAhead->next))
    {
        pAhead = pAhead->next;
        k--;
    }

    // K大于链表长度
    if (k > 1 || (NULL == pAhead->next))
    {
        return NULL;
    }

    // pBehind走
    while (pAhead->next)
    {
        pAhead = pAhead->next;
        pBehind = pBehind->next;
    }

    return pBehind;
}

// 获取链表的中间节点
// 前面指针每走两步,后面指针走一步
LinkNode *GetMiddle(LinkNode *phead)
{
    if (NULL == phead)
    {
        return phead;
    }

    LinkNode *pAhead = phead;
    LinkNode *pBehind = phead;

    while (pAhead->next)
    {
        pBehind = pBehind->next;
        pAhead = pAhead->next;
        if (NULL != pAhead->next)
        {
            pAhead = pAhead->next;
        }
    }

    return pBehind;
}

// 到序打印链表,借助stack
void PrintLinkRev(LinkNode *phead)
{
    if (NULL == phead)
    {
        return;
    }

    stack<LinkNode *> s;
    LinkNode *pNode = phead;
    while (pNode->next)
    {
        s.push(pNode->next);
        pNode = pNode->next;
    }

    while (!s.empty())
    {
        pNode = s.top();
        s.pop();
        Visit(pNode->data);
    }
}

// 链表反转
LinkNode *ReverseLink(LinkNode *phead)
{
    LinkNode *head = phead;
    LinkNode *cur = head->next;
    head->next = NULL;
    LinkNode *ne = NULL;
    LinkNode *temp = NULL;

    while (cur)
    {
        ne = cur->next;
        cur->next = temp;
        temp = cur;
        cur = ne;
    }

    head->next = temp;
    return head;
}

//0x1           低地址------>高地址
// 大端(大尾端)   0x0          0x1
// 小端(小尾端)   0x1          0x0
// 返回0表示是大端,返回1表示小端
int CheckSystem()
{
    union check
    {
        int i;
        char ch;
    } c;
    c.i = 1;

    return (c.ch == 1);
}

// 返回0表示是大端,返回1表示小端
int BigLittle()
{
    int a = 1;
    char b = *(char *) &a;
    return (b == 1);
}

// 枚举法计算小于100的素数(除了本身和1外没有别的因数)
void GetSuShu()
{
    int n;
    int i;
    for (n = 2; n <= 100; n++)
    {
        for (i = 2; i < n; i++)
        {
            if (n % i == 0)
            {
                break;
            }
        }

        if (i >= n)
        {
            Visit(n);
        }
    }
}

// 实现一个只能指针
template<typename T>
class smart_shart_ptr
{
public:
    smart_shart_ptr(T *ptr):m_ptr(ptr)
    {
        printf("init\n");
    }

    ~smart_shart_ptr()
    {
        printf("destory\n");
        delete m_ptr;
    }

    smart_shart_ptr(const T& other)
    {
        m_ptr = other;
    }

    T* operator->() const
    {
        return m_ptr;
    }

private:
        T* m_ptr;
};

struct namestc
{
    int len;
    char name[];
};
struct memberstc
{
    int mem;
    int len;
    char member[];
};

struct test
{
    struct namestc a;
    struct memberstc b;
};


//timer
void seconds_sleep(int32_t seconds)
{
    struct timeval tv;
    tv.tv_sec = seconds;
    tv.tv_usec = 0;
    int err;
    do
    {
        err = select(0, NULL, NULL, NULL, &tv);
        if (0 == err)
        {
            tv.tv_sec = seconds;
            tv.tv_usec = 0;
            debug("timer, err[%d]", err);
        }
        else
        {
            debug("err[%d]", err);
            break;
        }
    } while (true);
}
int main(int argc, char **argv)
{
    Test test;
    //debug("%s", "aaa");
    // seconds_sleep(2);
    int v[] = {10, 3, 5, 9, 1, 8, 6, 0, 4, 7, 2};
    int d[] = {10, 3, 5, 9, 1, 8, 6, 0, 4, 7, 2};
    int len = sizeof(d)/sizeof(int);
    GetMinK(d, 4, len);

     printf("\nMergerSort:\n");
    MergerSort(v, 0, 11);
    for (int lo = 0; lo <= 10; ++lo)
    {
        Visit(v[lo]);
    }


    // 素数
//    printf("\n素数\n");
//    GetSuShu();
//    // 大小端判断
//    printf("\n大小端判断,返回0表示是大端,返回1表示小端\n");
//    Visit(CheckSystem());
//    Visit(BigLittle());
//
//    test *ptest = new test();
//    printf("\nptest[%p], ptest->a.name[%p], ptest->b[%p]\n", ptest, &ptest->a.name, &ptest->b);
//    Visit(sizeof(memberstc));

//    int a[] = {10, 3, 5, 9, 1, 8, 6, 0, 7, 2};
//    int len = sizeof(a)/sizeof(int);
//
//    printf("\n快排序前:\n");
//    PrintArray(a, len);
//    QuickSort(a, 0, len-1);
//    printf("\n快排序后:\n");
//    PrintArray(a, len);
//
//    int loc = 0;
//    printf("\ninput search value:");
//    scanf("%d", &loc);
//    int ret = BinarySearch(a, len, loc);
//    Visit(ret);
//
//    printf("\n\n创建链表, 请输入数字,0表示结束, 例如1 2 3 4 5 6 7 0\n");
//    LinkNode *pLink = CreateLink();
//    printf("打印链表:\n");
//    PrintLink(pLink);
//
//    //获取链表倒数第K个节点的值
//    printf("\n请输入倒数第K个节点的值:");
//    int k;
//    scanf("%d", &k);
//    LinkNode *pkth = GetKth(pLink, k);
//    if (NULL == pkth)
//    {
//        printf("\n链表倒数第[%d]个节点不存在:\n", k);
//    }
//    else
//    {
//        printf("\n链表倒数第[%d]节点值:\n", k);
//        Visit(pkth->data);
//    }
//
//    // 获取链表的中间节点
//    printf("\n链表的中间节点:\n");
//    LinkNode *pMiddle = GetMiddle(pLink);
//    if (pMiddle)
//    {
//        Visit(pMiddle->data);
//    }
//
//    // 到序打印链表,借助stack
//    printf("\n到序打印链表:\n");
//    PrintLinkRev(pLink);
//
//    LinkNode *pLinkRev = ReverseLink(pLink);
//    printf("\n打印反转后的链表: \n");
//    PrintLink(pLinkRev);
//
//    printf("\n\n输入二叉树节点值,0表示结束,例如1 2 3 0 0 4 0 0 5 6 0 0 0\n");
//    BiTree pTree = CreateTree();
//    printf("先序遍历: \n");
//    PreOrderTree(pTree);
//    printf("\n先序遍历非递归: \n");
//    PreOrderTreeMy(pTree);
//
//    printf("\n中序遍历: \n");
//    InOrderTree(pTree);
//    printf("\n中序遍历非递归: \n");
//    InOrderTreeMy(pTree);
//
//    printf("\n后序遍历: \n");
//    PostOrderTree(pTree);
//    printf("\n后序遍历非递归: \n");
//    vector<int> path;
//    PostOrderTreeMy(pTree, path);
//    vector<int>::iterator iter = path.begin();
//    for (; iter != path.end(); ++iter)
//    {
//        Visit(*iter);
//    }
//
//    printf("\n二叉树深度:\n");
//    int depth = GetDepth(pTree);
//    Visit(depth);
//
//    printf("\n二叉树叶子节点:\n");
//    int leaf = GetLeafNum(pTree);
//    printf("\n二叉树叶子节点个数:\n");
//    Visit(leaf);
//
//    printf("\n二叉树镜像:\n");
//    BiTree pMirror = GetMirror(pTree);
//    PreOrderTree(pMirror);

    printf("\n\n");
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值