最近在網易公開課上看斯坦福大學的《編程範式》,外國人講課思路就是清晰,上了幾節課,感受難度確實比咱們普通大學大不少,可是卻頗有趣,讓人能邊學邊想。 編程
範式編程,交換兩個數,利用 void * 數組
void Swap (void * lhs,void * rhs,int size)return
{範式
void * temp = malloc(sizeof(size));void
memmove(temp,lhs,sizeof(size));公開課
memmove(lhs,rhs,sizeof(size));arc
memmove(rhs,temp,sizeof(size));
free(temp);
temp = NULL;
}
查找數組中的元素
void * Search(void * key,void *arr,int arrCount,int size,int (*Compare)(void *lhs,void *rhs))
{
void * temp = (char *) arr+i*size;
for(int i=0;i<arrCount;i++)
{
if(Compare(key,temp)==0)
return temp;
}
return NULL;
}
int IntCompare(void *lhs,void *rhs)
{
int * plhs = (int *) lhs;
int * prhs = (int *) rhs;
return *plhs - *prhs;
}
……