现在的位置: 首页 > 综合 > 正文

【CS107】Assignment 2 总结

2017年12月13日 ⁄ 综合 ⁄ 共 2711字 ⁄ 字号 评论关闭

programming paradigms的assignment 2很有意思,满足了我对电影和编程学习的双重爱好,可惜的是,它需要unix环境,在windows中用minGWf编译都不成,很是郁闷,难道硬要我装个linux不成?所以我只能小小尝试,却不能享受最后程序运行的快感。

编程过程中碰到个难题,就是它有个void* actorFile指向一长串raw data空间,结构的话,我贴个作业中截出来的图:

第一个int,代表总共的演员数,然后是每个演员的int索引,int的大小代表它到基址的偏移字节。每个演员的索引之后,便是演员的信息,名字在最前面。

作业有一个地方需要查找一个演员是否在actorFile中,若在的话把演员的相关信息提取出来。这个actorFile空间是超大的,这个例子是简化的。

它推荐用bsearch,但这可难倒我的,把string作为key传入,但bearch查找的区域是int区啊,int值本身是偏移,要获取相应的演员信息还需要基址,可bsearch是这么个函数:

void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const void*))

cmp如何才能利用到base呢?后来苦思不得,看了assignment 2的FAQ,有一个问题和回答是这样的:

Question: If I define a top-level comparison function, I don't have access to actorFile or movieFile. How can I translate integer 
offsets into movie and actor locations if I don't have access to either of those variables.  
Answer: Excellent question. The programmer can pass the address of anything s/he wants as the first argument of bsearch. 
You're probably passing the address of a string or a film, but it would make more sense to pass the address of a struct as the key. 
You can include anything you think your comparison function will need to do a full comparison of actors and movies. This requires 
a genuine understanding of how function pointers and casting works, but it's the genuine understanding that we're shooting for.  

直接命中G点,基本上就是我要提的问题啊,key不能传string,而是个结构体,在我以为要豁然开朗的时候,却笨笨地还是想不到何解。其实关键是不知道bsearch是如何运作的,大部分网上对bsearch的描述都不够细,直到搜到微软自己的MSDN,它对fcmp这个比较函数的两个参数有具体描述,前者传入的是key,后者传入的是搜索区的相应指针,这才算是明白了,通过key来传入基址,然后在fcmp中进行解析。我靠,终于TM有解了。

以下是我写的测试代码,没办法,只有模拟actorFile了。对于这种bsearch用法,感觉非常巧妙,第一次如此自如地操作内存,发现所谓的数据结构完全可以不用struct或class来抽象,直接void*,自己在内存中操作就行了,不过调试起来不方便就是了,要通过void*指向的地址来看内存的分布。

还有几个需要注意的地方,比较函数中*(void**)代表的是对指向actorFile这个void*的指针的dereference,还有就是名字的排序需要按字母排序,因为bsearch的前提是有序,这里体现在名字字符串中,而不是所谓的int偏移区,假如名字不按字母排序,将找不到想要的名字,有兴趣的同学也可以试下调换名字位置。

int compFunc(const void* key, const void* b){  
  return strcmp((char*)((int*)key+1) , (char*)*(void**)key+*(int*)b);  
} 

int main(){  
  void* actorFile=malloc(4*sizeof(int)+4+4+6);  
  int num=3;  
  int first=16;  
  int second=20;  
  int third=24;  
  char* s1="Bob";  
  char* s2="Ted";  
  char* s3="Venus";  
  memcpy(actorFile,&num,sizeof(int));  
  memcpy((int*)actorFile+1,&first,sizeof(int));  
  memcpy((int*)actorFile+2,&second,sizeof(int));  
  memcpy((int*)actorFile+3,&third,sizeof(int));  
  memcpy((int*)actorFile+4,s1,4);  
  memcpy((int*)actorFile+5,s2,4);  
  memcpy((int*)actorFile+6,s3,6);  
  
  int numOfActor=*(int*)actorFile;  
  void* key=malloc(sizeof(int)+5);  
  memcpy(key,&actorFile,4);  
  memcpy((int*)key+1,s3,6);  
  void *item=bsearch(key, (int*)actorFile+1, numOfActor, sizeof(int), compFunc);  
  if(item==NULL)  
      cout<<"can not find string"<<endl;  
  else   {  
    char* findStr=(char*)actorFile+*(int*)item;  
    cout<<findStr<<endl;  
  }  
}
======================================================
转载请注明出处:http://blog.csdn.net/utimes/article/details/8743324
======================================================

抱歉!评论已关闭.