工友要求做A工作和B工作

1.    Please specify what does “func()” do with the list “pParam”, and
what are the errors.

1.常用宏或函数的达成
_T,_L宏:
#define unsigned short L
#define _T(x)       __T(x)
#define _TEXT(x)    __T(x)

struct LIST 

    int nValue; 
    struct LIST * pPrev; 
    struct LIST * pNext; 
}; 
struct LIST * func(struct LIST * pParam) 

    struct LIST* pCur = pParam; 
    struct LIST* pNext; 
    struct LIST* pPrev = NULL; 
    struct LIST* pTail; 
    struct LIST* pReturn = NULL;

#ifdef  _UNICODE
#define __T(x) L##x     
#else
#define __T(x) x          
#endif

    if (pCur == NULL) 
    { 
        return pReturn; 
    } 
    else 
    { 
        pPrev = pCur->pPrev; 
        if (pCur->pNext == NULL) 
        { 
             pReturn = pCur; 
        } 
        else 
        { 
             pReturn = pCur->pNext; 
        } 
    }

#define _L(x) L##x

    while (pCur != NULL) 
    { 
        pNext = pCur->pNext; 
        if (pNext == NULL) 
        { 
            return pReturn; 
        } 
        else 
        { 
            pTail = pNext->pNext;

assert宏实现:
#define assert(expr)
do{
    if (!(expr))
    {
            outputx(“Assertion failed! %s, %s, %s, line %d\n”,
                            #expr, __FILE__, __func__,
__LINE__);
        }
}while(0)
assert函数落成:
inline bool Assert(bool result, const char* function, const char*
file,  
                       int line, const char* expression)
{  
    if (!result)
    {  
            Log_Assert(function, file, line, expression);  
            Break();  
            return false;  
    }  
    return true;  
}

            pNext->pPrev = pPrev; 
            pNext->pNext = pCur; 
            pCur->pPrev = pNext; 
            if (pTail == NULL) 
            { 
                pCur->pNext = pTail; 
            } 
            else 
            { 
                if (pTail->pNext == NULL) 
                { 
                    pCur->pNext = pTail; 
                } 
                else 
                { 
                    pCur->pNext = pTail->pNext; 
                } 
            } 
        }

赢得钦定地址上的四个字节或字:
#define  MEM_B(x) (*((byte *)(x)))
#define  MEM_W(x) (*((word *)(x))  

        pPrev = pCur; 
        pCur = pTail; 
    }

取得贰个变量的地方(word宽度):
#define B_PTR(var)  ((byte *) (void *) &(var))
#define W_PTR(var)  ((word *) (void *) &(var))

    return pReturn; 

2.    Please complete the standard C function: memmove(), here is the
description (don’t use any C standard function): 
void * memmove (void *to, const void *from, unsigned int size) 
memmove copies the size bytes at from into the size bytes at to. The
value returned by memmove is the value of to. 
3.    please complete this function, get binary tree’s depth. For
example, the following binary tree’s depth is 4. The function returns
depth. 
   1 
/     \ 
2    3 
     /       \ 
   4          5 
/     \ 
6        7

得到1个字的高位和没有字节:
#define WORD_LO(xxx)  ((byte) ((word)(xxx) & 255))
#define WORD_HI(xxx)  ((byte) ((word)(xxx) >> 8))

struct NODE 

    struct NODE* pLeft;        // pLeft is NULL if it has no left sub
node 
    struct NODE* pRight;    // pRight is NULL if it has no right sub
node 
}; 
int GetDepth(const struct NODE* pRoot) 

}

将一个假名转换为大写:
#define UPCASE(c) (((c)>=’a’ && (c) <= ‘z’) ? ((c) – 0x20) :
(c))

4.    A worker needs to do A work and B work. B’s priority is higher
than A. For example, if he shall do A from 9:00 to 13:00, and shall
doing B from 10:00 to 11:00, his choice shall be doing A from 9:00 to
10:00, doing B from 10:00 to 11:00, and doing A from 11:00 to 13:00.

防备溢出的三个主意:
#define INC_SAT(val) (val=((val)+1>(val)) ? (val)+1 : (val))

Complete the following function (you can use pseudo-code or solution
steps explanation instead of actual code), ”pSchedule“ is a worker’s
work schedule (it’s an array), “nNum” is number of elements in array
“pSchedule”, “ppResult” is the result array which shall be returned and
its buffer shall be allocated by yourself, “nRNum” is number of elements
in “ppResult”. The time phases in “pSchedule” cover each other, and not
sorted, the output data in “ppResult” shall be a new schedule that are
not covered of any phase, and sorted by start time. Return 0 if success.

DEBUG宏:
#ifdef _DEBUG
#define DEBUGMSG(msg,date)
printf(msg);printf(“%d%d%d”,date,_LINE_,_FILE_)
#else
#define DEBUGMSG(msg,date)
#endif

enum WORK 

    A,        // A work 
    B        // B work 
}; 
struct SCHED 

    int nStartHour;        // at that hour work start 
    int nEndHour;            // at that hour work end 
    enum WORK work;        // work type 
}; 
int func(const struct SCHED* pSchedule, unsigned int nNum, struct
SCHED** ppResult, unsigned int& nRNum) 

}

2.字符串相关函数
int _strlen(const char* src)
{
    assert(src != NULL);
      const char *p = src;
      while(*p++!=NULL);
      return p – str – 1;
}

1.    请建议以下函数对参数”pParam”做了哪些动作,并建议在这之中的荒谬

char* _strstr(const char* str, const char* sub)
{
    for(int i=0; str[i]!=’\0′; i++)
    {
        int pos = i,j=0;
        while(str[i]==sub[j] && sub[j]!=’\0′)
        {
            ++i;
            ++j;
        }
        if(sub[j] == ‘\0’)
            return &str[pos];
    }    

    将链表中的成分奇数位与偶数位调换。

    return NULL;
}

int func(struct LIST * pParam) 

    // … … 
    while (pCur != NULL) 
    { 
        pNext = pCur->pNext; 
        if (pNext == NULL) 
        { 
            pCur->pPrev = pPrev; 
            return pReturn; 
        } 
        // … … 
    } 
    return pReturn; 
}

char* _strncpy(char* dest, const char* src, int count)
{
    assert(dest!=NULL && src!=NULL && count>0);
    char* tmp = dest;
    while(count–>0 && (*dest++=*src++)!=’\0′);
    return tmp;
}

2.    请完毕正式C函数:memmove()

char* _strcpy(char* dest, const char* src)
{
    assert(dest!=NULL && src!=NULL);
    char* tmp = dest;
    while((*dest++=*src++) != ‘\0’);
    return tmp;
}

void *memmove(void *dest, const void *src, size_t count) 

    char *tmp; 
    const char *s;

int _strcmp(const char* first, const char* second)
{
    assert(first!=NULL && src!=second);
    int ret = 0;

    if (dest == NULL || src == NULL) 
    { 
        return NULL; 
    }

    while(!(ret=(*(unsigned char*)first) – (*(unsigned
char*)second))
        && (*first != ‘\0’))
    {
        ++first;
        ++second;
    }

    if (dest <= src) { 
        tmp = dest; 
        s = src; 
        while (count–) 
            *tmp++ = *s++; 
    } else { 
        tmp = dest; 
        tmp += count; 
        s = src; 
        s += count; 
        while (count–) 
            *–tmp = *–s; 
    } 
    return dest; 
}

    if(ret < 0)
        ret = -1;
    else if(ret > 0)
        ret = 1;
    
    return ret;
}

3.    请达成以下函数,重返二叉树的深度。例如下边所示二叉树的深度为4。

char* _strcat(char* dest,char* src)
{
    assert(dest!=NULL && src!=NULL);
    char* tmp = dest;

#include <stdlib.h>

    while(*dest != ‘\0’)
        ++dest;
    while(((*dest++)=(*src++)) != ‘\0’);
    return tmp;
}

struct NODE 

    struct NODE* pLeft;    // pLeft is NULL if it has no left sub
node 
    struct NODE* pRight;    // pRight is NULL if it has no right sub
node 
};

void _loopmove(char* str, int steps)
{
    int n = strlen(str)-steps;
    char tmp[256];
    memcpy(tmp,str+n,steps);
    memcpy(str+steps,str,n);
    memcpy(str,tmp,steps);
}

int GetDepth(const struct NODE* pRoot) 

    if (pRoot == NULL) 
    { 
        return 0; 
    } 
    int nLeft = GetDepth(pRoot->pLeft); 
    int nRight = GetDepth(pRoot->pRight); 
    return nLeft > nRight ? nLeft + 1 : nRight + 1; 
}

char* _reversestring(char* src)
{
    assert(src!=NULL);
    int len=strlen(src);
    char tmp = ‘\0’;
    
    for(int i=0; i<len/2; i++)
    {
        tmp = src[i];
        src[i] = src[len-i-1];
        src[len-i-1] = tmp;
    }
    return src;
}

4.   
工人须要做A工作和B工作。B工作的优先级比A工作更高。例如,假若他要在9:00至13:00做A工作,而且在10:00至11:00做B工作,那么他
应该在9:00至10:00做A工作,在10:00至11:00做B工作,在11:00至13:00做A工作。 
成就上面函数,”pSchedule”是工人的劳作安插(它是贰个数组),”nNum”是数组”pSchedule”中的成分数量,”ppResult”
是亟需重临的结果数组,”nPAJERONum”是结果中的成分数量。”pSchedule”中的时间段相互覆盖,而且未排序,输出结果”ppResult”中的时
间段不允许有覆盖,并且按先河时间排序。函数执行成功重返0。

void _makepath(char* path, const char* drive, const char* dir, const
char*filename, const char* ext)
{
    assert(path!=NULL && drive!=NULL && filename!=NULL && ext!=NULL);

//以下是2012年的笔试题

    register const char* p;
    if(*drive)
    {
        *path++ = *drive;
        *path++ = ‘:’;
        *path++ = ‘\\’;
    }
    else
        return;

⑤ 、已知二零零六年的一月117日是周四,写3个函数,输入M年和N
月,计算出该月的第叁个星期四是几号?

    if((p=dir) && *p)
    {
        do
        {
            *path++ = *p++;
        }while(*p);
        if(*(p-1) != ‘\\’)
            *path++ = ‘\\’;
    }
    
    if(p = filename)
    {
        while(*p)
            *path++ = *p++;
    }

六 、写SQL 语句的题,在这之中有三个是何许创设索引?

    if(p = ext)
    {
        if(*p && *p != ‘.’)
            *path++ = ‘.’;
        while(*path++ = *p++);
    }

答案:(http://www.cnblogs.com/hanjin/archive/2008/09/09/1287505.html

    *path++ = ‘\0’;
}

 

3.内存相关函数
void* _memset(void* dest, int c,int count)
{
    assert(dest != NULL);
    char* tmp = (char*)dest;
    int i = 0;
    
    while(count–)
        dest[i++] = c;

转自:http://www.cnblogs.com/cswolf/archive/2011/10/13/2267124.html  

    return tmp;
}

void _memcpy(void* dest, const void* src, int count)
{
    assert(dest!=NULL && src!=NULL);
    unsigned char* pdst = (unsigned char*)dest;
    unsigned char* psrc = (unsigned char*)src;
    assert(!(psrc<=pdst && pdst<psrc+count));
    assert(!(pdst<=psrc && psrc<pdst+count));

    while(count–)
        *pdst++ = *psrc++;     
}

4.类型转换函数
void _itoa(long num,char str[],int radix)  
{  
    if( str == NULL)
        return;
    long sign = num,i = 0,j = 0;  
        char temp[100];  
 
        if(sign < 0)  
            num = -num;  
   
        do  
        {  
            temp[i] = num % radix+’0′;  
            num /= radix;  
            i++;  
        }while(num > 0);  
    
    if(sign < 0)  
            temp[i++] = ‘-‘;    
        temp[i] = ‘\0’;  
        i–;   
 
        while(i >= 0)  
        {  
            str[j] = temp[i];  
            j++;  
            i–;  
        }  
        str[j] = ‘\0’;  
}

void _ftoa(float number,int ndigit,char buf[])
{
    assert(buf!=NULL);
    
    long int_part;  
        double float_part;  
        char str_int[512];  
        char str_float[256];  
        memset(str_int,0,512);  
        memset(str_float,0,256);  
        int_part = (long)number;  
        float_part = number – int_part;  
        _itoa(int_part,str_int,10);  
 
       if(ndigit>0)  
        {  
            float_part =fabs(pow(10,ndigit)*float_part);  
            _itoa((long)float_part,str_float,10);  
        }  
        int i = strlen(str_int);  
        str_int[i] = ‘.’;  
        strcat(str_int,str_float);  
        strcpy(buf,str_int);  
}

int _atoi(const char str[])  
{  
        int ret_Integer = 0;  
        int integer_Sign = 1;  
      
        if(str == NULL)  
            return 0;   

        while(*str == ‘ ‘)  
            str++;  

        if(*str == ‘-‘)  
            integer_Sign = -1;  
    
        if(*str == ‘-‘ || *str == ‘+’)  
            str++;   

        while(*str >= ‘0’ && *str <= ‘9’)  
        {  
            ret_Integer = ret_Integer * 10 + *str – ‘0’;  
            str++;  
        }  
        ret_Integer = integer_Sign * ret_Integer;  
      
        return ret_Integer;  
}

float _atof(const char str[])  
{  
    if(str == NULL)  
            return 0.0;
 
        int len = strlen(str),i = 0;  
        if(str[0] == ‘-‘)  
        i++;  
      
        float s = 0.0;  
        while(i < len && (str[i]>=’0′ && str[i]<=’9′))  
        {  
            s = s * 10 + (float)(str[i]-‘0’);  
            i++;  
        }  
          
        if(str[i] == ‘.’)
        i++;  
        else
        return s;  
      
        float order = 1.0;  
        while(i < len && (str[i]>=’0′ && str[i]<=’9′))  
        {  
            order = order / 10;  
            s = s + (float)(str[i]-‘0’)*order;  
            i++;  
        }  
      
        if (str[i] == ‘-‘)
        s = -1*s;  
      
        return s;  
    }