×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

这样写是C++语法的C程序,点评见内

1、数据封装到哪里去了?Mylist中应当加private的listsize,Alist × firstnode, Alist * lastnode等,自己处理链表长度,首指针、尾指针等信息

2、Mylist的构造和析构函数呢?IoCreateAList不应该是成员函数,而应该对应成传入参数为const Alist & 的构造函数

3、C++中的new不应该检查是否为空,对应应该进行例外处理try-catch-throw,因为new根本不会返回null,分配失败,系统抛出例外,你要处理

4、IoAppendItem改为Append(const Alist & node); 要封装!

5、IoInsertItem改为Insert(const Alist &node, Iterator itr); 如果对Iterator是什么不知道的话,在学习STL前,至少也应该写成nsert(const Alist &node, int pos);

6、add, remove, delete见上

7、IoGetItem改为Iterator setto(int pos);

8、IoListSize改为const int size(){ return listsize;}

9、IoClearList改写为析构函数

10、IoFreeList改写为Alist的析构函数

...................不多说了,说句不客气的话,你是一个只知道C++语法的C程序员
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 通过一家公司的电话面试, 结果让编程
    本文发表在 rolia.net 枫下论坛Please rewrite AList defined in list.h and list.c into a C++ class. All the functions defined in list.c should be implemented as the class member functions.

    /*list.h */
    #ifndef _LIST_INCLUDED
    #define _LIST_INCLUDED


    #define ALIST_BLOCK_SIZE 100
    typedef struct {
    void **items;
    int numOfItems;
    } ALIST;

    /* --- The API functions for manipulating a list --- */
    #ifdef WIN32

    #ifdef __cplusplus
    extern "C" {
    #endif

    extern ALIST *IoCreateAList();
    extern int IoAppendItem(ALIST *pList,void *pItem);
    extern int IoInsertItem(ALIST *pList,void *pItem,int index);
    extern int IoAddItem(ALIST *pList,void *pItem,int index);
    extern int IoAddSortedItem(ALIST *pList,void *pItem,
    int (__cdecl *compareTwoElements)(const void **elem1,const void **elem2));
    extern void *IoFindSortedItem(ALIST *pList,void *item, int *pos, int *cmpRslt,
    int (__cdecl *compareTwoItems)(const void *item1,const void *item2));
    extern int IoRemoveItem(ALIST *pList,int index);
    extern int IoDeleteItem(ALIST *pList,int index);
    extern void *IoGetItem(ALIST *pList,int index);
    extern int IoListSize(ALIST *pList);
    extern int IoClearList(ALIST *pList);
    extern int IoFreeList(ALIST *pList);
    extern int IoMarkRemoveItem(ALIST *pList,int index);
    extern int IoMarkDeleteItem(ALIST *pList,int index);
    extern int IoSyncList(ALIST *pList);
    extern int IoRemoveItemByPointer(ALIST *pList,void *pItem);

    #ifdef __cplusplus
    }
    #endif
    #else
    extern ALIST *IoCreateAList();
    extern int IoInsertItem();
    extern int IoAddItem();
    extern int IoInsertSortedItem();
    extern void *IoFindSortedItem();
    extern int IoAppendItem();
    extern int IoRemoveItem();
    extern int IoDeleteItem();
    extern int IoListSize();
    extern void *IoGetItem();
    extern int IoClearList();
    extern int IoFreeList();
    extern int IoMarkRemoveItem();
    extern int IoMarkDeleteItem();
    extern int IoSyncList();
    extern int IoRemoveItemByPointer();
    #endif

    #endif


    ===========c file ========================
    /*list.c */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "args.h"
    #include "str.h"
    #include "list.h"

    #define ALLOC_OFFSET 10
    #define MAXLINESIZE 256


    ALIST *IoCreateAList()
    {
    ALIST *aList;
    aList=(ALIST *)malloc(sizeof(ALIST));
    if(aList==NULL)
    return(NULL);
    aList->items=malloc(ALIST_BLOCK_SIZE*sizeof(void*));
    if (aList->items==NULL)
    {
    free(aList);
    return(NULL);
    }
    aList->numOfItems=0;
    return(aList);
    }

    int IoAppendItem(pList,pItem)
    ALIST *pList;
    void *pItem;
    {
    void **pItems;
    if(pList->numOfItems>0 && pList->numOfItems%ALIST_BLOCK_SIZE==0)
    {
    pItems=realloc(pList->items,
    (pList->numOfItems+ALIST_BLOCK_SIZE)*sizeof(void*)
    );
    if(pItems==NULL)
    return(-1);
    pList->items = pItems;
    }
    pList->items[pList->numOfItems]=pItem;
    pList->numOfItems++;
    return(0);
    }

    int IoInsertItem(pList,pItem,index)
    ALIST *pList;
    void *pItem;
    int index;
    {
    int i;
    void **pItems;
    if (index<0 || index>pList->numOfItems)
    return(-1);
    else if(index==pList->numOfItems||pList->numOfItems==0)
    return(IoAppendItem(pList,pItem));

    if(pList->numOfItems>0 && pList->numOfItems%ALIST_BLOCK_SIZE==0)
    {
    pItems=realloc(pList->items,
    (pList->numOfItems+ALIST_BLOCK_SIZE)*sizeof(void*)
    );
    if(pItems==NULL)
    return(-1);
    pList->items = pItems;
    }
    for(i=pList->numOfItems;i>index;i--)
    pList->items[i] = pList->items[i-1];
    pList->items[index]=pItem;
    pList->numOfItems++;
    return(0);
    }

    int IoAddItem(pList,pItem,index)
    ALIST *pList;
    void *pItem;
    int index;
    {
    if(pList->numOfItems==0)
    return(IoAppendItem(pList,pItem));
    else
    return(IoInsertItem(pList,pItem,index+1));
    }


    int IoRemoveItem(pList,index)
    ALIST *pList;
    int index;
    {
    int i;
    void **pItems;
    if (index<0 || index>=pList->numOfItems)
    return(-1);
    for(i=index;i<pList->numOfItems-1;i++)
    pList->items[i]=pList->items[i+1];
    if(pList->numOfItems>ALIST_BLOCK_SIZE && pList->numOfItems%ALIST_BLOCK_SIZE==1)
    {
    pItems=realloc(pList->items,
    (pList->numOfItems-1)*sizeof(void*)
    );
    if(pItems==NULL)
    return(-1);
    pList->items=pItems;
    }
    pList->numOfItems--;
    return(0);
    }

    int IoDeleteItem(pList,index)
    ALIST *pList;
    int index;
    {
    void *pItem;
    if (index<0 || index>=pList->numOfItems)
    return(-1);
    pItem = IoGetItem(pList,index);
    if(IoRemoveItem(pList,index))
    return(-1);
    if(pItem)
    free(pItem);
    return(0);
    }

    void* IoGetItem(pList,index)
    ALIST *pList;
    int index;
    {
    if(index>=pList->numOfItems || index<0)
    return(NULL);
    return(pList->items[index]);
    }

    int IoListSize(pList)
    ALIST *pList;
    {
    return(pList->numOfItems);
    }

    int IoClearList(pList)
    ALIST *pList;
    {
    int i, size = IoListSize(pList);
    for(i=0;i<size;i++)
    {
    if(IoDeleteItem(pList,0))
    return(-1);
    }
    return(0);
    }

    int IoFreeList(pList)
    ALIST *pList;
    {
    free(pList->items);
    free(pList);
    return(0);
    }

    int IoMarkRemoveItem(pList,index)
    ALIST *pList;
    int index;
    {
    if(index<0||index>=pList->numOfItems)
    return(-1);
    pList->items[index]=NULL;
    return(0);
    }

    int IoMarkDeleteItem(pList,index)
    ALIST *pList;
    int index;
    {
    void *pItem;
    if (index<0 || index>=pList->numOfItems)
    return(-1);
    pItem = IoGetItem(pList,index);
    if(IoMarkRemoveItem(pList,index))
    return(-1);
    if(pItem)
    free(pItem);
    return(0);
    }

    int IoSyncList(pList)
    ALIST *pList;
    {
    int i,j;

    for(i=0,j=0;i<pList->numOfItems;i++)
    if(pList->items[i]!=NULL)
    {
    if(i!=j)
    {
    pList->items[j]=pList->items[i];
    pList->items[i]=NULL;
    }
    j++;
    }
    pList->numOfItems=j;
    return(0);
    }

    int IoRemoveItemByPointer(pList,pItem)
    ALIST *pList;
    void *pItem;
    {
    int i;

    for(i=0;i<pList->numOfItems;i++)
    if(pList->items[i]==pItem)
    break;
    if(i<pList->numOfItems)
    return(IoRemoveItem(pList,i));
    else
    return(0);
    }

    int IoAddSortedItem(pList,pItem,compareTwoElements)
    ALIST *pList;
    void *pItem;
    #ifdef WIN32
    int (__cdecl *compareTwoElements)(const void **elem1,const void **elem2);
    #else
    int (*compareTwoElements)();
    #endif
    {
    if(IoAppendItem(pList,pItem)<0)
    return(-1);
    qsort(pList->items,pList->numOfItems,sizeof(void*),compareTwoElements);
    return(0);
    }

    void *IoFindSortedItem(pList,item,pos,cmpRslt,compareTwoItems)
    ALIST *pList;
    void *item;
    int *pos,*cmpRslt;
    #ifdef WIN32
    int (__cdecl *compareTwoItems )(const void *item1,const void *item2);
    #else
    int (*compareTwoItems)();
    #endif
    {
    int i,j,k;
    void *itemInList;

    if(pList->numOfItems==0)
    {
    *pos=0;
    *cmpRslt = -1;
    return(NULL);
    }
    i=0;
    j=pList->numOfItems-1;
    *pos=0;
    *cmpRslt = -1;
    while(1)
    {
    if(j<i)
    {
    return(NULL); /* Can not find the item */
    }
    k=(i+j)/2;
    *pos=k;
    itemInList=IoGetItem(pList,k);
    if((*cmpRslt=compareTwoItems(item,itemInList))==0)
    {
    return(itemInList); /* The item is found */
    }
    if((*cmpRslt)<0)
    j=k-1;
    else
    i=k+1;
    }
    }更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • 自己写了一个,总感觉里面有陷阱, 不会这么简单吧
      本文发表在 rolia.net 枫下论坛#ifndef _MYLIST_INCLUDED
      #define _MYLIST_INCLUDED


      const int ALIST_BLOCK_SIZE = 100
      class ALIST {
      public:
      void **items;
      int numOfItems;
      } ;


      class MyList
      {
      public:

      ALIST *IoCreateAList();
      int IoAppendItem(ALIST *pList,void *pItem);
      int IoInsertItem(ALIST *pList,void *pItem,int index);
      int IoAddItem(ALIST *pList,void *pItem,int index);
      int IoAddSortedItem(ALIST *pList,void *pItem,
      int (__cdecl *compareTwoElements)(const void **elem1,const void **elem2));
      void *IoFindSortedItem(ALIST *pList,void *item, int *pos, int *cmpRslt,
      int (__cdecl *compareTwoItems)(const void *item1,const void *item2));
      int IoRemoveItem(ALIST *pList,int index);
      int IoDeleteItem(ALIST *pList,int index);
      void *IoGetItem(ALIST *pList,int index);
      int IoListSize(ALIST *pList);
      int IoClearList(ALIST *pList);
      int IoFreeList(ALIST *pList);
      int IoMarkRemoveItem(ALIST *pList,int index);
      int IoMarkDeleteItem(ALIST *pList,int index);
      int IoSyncList(ALIST *pList);
      int IoRemoveItemByPointer(ALIST *pList,void *pItem);

      };


      #endif

      =========cpp file====================
      #include "MyList.h"
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>

      const int ALLOC_OFFSET =10;
      const int MAXLINESIZE =256;

      Class ALIST;
      ALIST * MyList::IoCreateAList()
      {
      ALIST *aList;
      aList=(ALIST *)new ALIST();
      if(aList==NULL)
      return(NULL);
      aList->items=malloc(ALIST_BLOCK_SIZE*sizeof(void*));
      if (aList->items==NULL)
      {
      delete aList;
      return(NULL);
      }
      aList->numOfItems=0;
      return(aList);
      }

      int MyList::IoAppendItem(ALIST *pList,void* pItem)

      {
      void **pItems;
      if(pList->numOfItems>0 && pList->numOfItems%ALIST_BLOCK_SIZE==0)
      {
      pItems=realloc(pList->items,
      (pList->numOfItems+ALIST_BLOCK_SIZE)*sizeof(void*)
      );
      if(pItems==NULL)
      return(-1);
      pList->items = pItems;
      }
      pList->items[pList->numOfItems]=pItem;
      pList->numOfItems++;
      return(0);
      }

      int MyList::IoInsertItem(ALIST *pList,void* pItem,int index)


      {
      int i;
      void **pItems;
      if (index<0 || index>pList->numOfItems)
      return(-1);
      else if(index==pList->numOfItems||pList->numOfItems==0)
      return(IoAppendItem(pList,pItem));

      if(pList->numOfItems>0 && pList->numOfItems%ALIST_BLOCK_SIZE==0)
      {
      pItems=realloc(pList->items,
      (pList->numOfItems+ALIST_BLOCK_SIZE)*sizeof(void*)
      );
      if(pItems==NULL)
      return(-1);
      pList->items = pItems;
      }
      for(i=pList->numOfItems;i>index;i--)
      pList->items[i] = pList->items[i-1];
      pList->items[index]=pItem;
      pList->numOfItems++;
      return(0);
      }

      int MyList::IoAddItem(ALIST *pList,void* pItem,int index)

      {
      if(pList->numOfItems==0)
      return(IoAppendItem(pList,pItem));
      else
      return(IoInsertItem(pList,pItem,index+1));
      }


      int MyList::IoRemoveItem(ALIST* pList,int index)

      {
      int i;
      void **pItems;
      if (index<0 || index>=pList->numOfItems)
      return(-1);
      for(i=index;i<pList->numOfItems-1;i++)
      pList->items[i]=pList->items[i+1];
      if(pList->numOfItems>ALIST_BLOCK_SIZE && pList->numOfItems%ALIST_BLOCK_SIZE==1)
      {
      pItems=realloc(pList->items,
      (pList->numOfItems-1)*sizeof(void*)
      );
      if(pItems==NULL)
      return(-1);
      pList->items=pItems;
      }
      pList->numOfItems--;
      return(0);
      }

      int MyList::IoDeleteItem(ALIST* pList,index)

      {
      void *pItem;
      if (index<0 || index>=pList->numOfItems)
      return(-1);
      pItem = IoGetItem(pList,index);
      if(IoRemoveItem(pList,index))
      return(-1);
      if(pItem)
      free(pItem);
      return(0);
      }

      void* MyList::IoGetItem(ALIST* pList,int index)

      {
      if(index>=pList->numOfItems || index<0)
      return(NULL);
      return(pList->items[index]);
      }

      int MyList::IoListSize(ALIST* pList)

      {
      return(pList->numOfItems);
      }

      int MyList::IoClearList(ALIST* pList)

      {
      int i, size = IoListSize(pList);
      for(i=0;i<size;i++)
      {
      if(IoDeleteItem(pList,0))
      return(-1);
      }
      return(0);
      }

      int MyList::IoFreeList(ALIST* pList)

      {
      free(pList->items);
      free(pList);
      return(0);
      }

      int MyList::IoMarkRemoveItem(ALIST* pList,int index)

      {
      if(index<0||index>=pList->numOfItems)
      return(-1);
      pList->items[index]=NULL;
      return(0);
      }

      int MyList::IoMarkDeleteItem(ALIST* pList,int index)

      {
      void *pItem;
      if (index<0 || index>=pList->numOfItems)
      return(-1);
      pItem = IoGetItem(pList,index);
      if(IoMarkRemoveItem(pList,index))
      return(-1);
      if(pItem)
      free(pItem);
      return(0);
      }

      int MyList::IoSyncList(ALIST* pList)

      {
      int i,j;

      for(i=0,j=0;i<pList->numOfItems;i++)
      if(pList->items[i]!=NULL)
      {
      if(i!=j)
      {
      pList->items[j]=pList->items[i];
      pList->items[i]=NULL;
      }
      j++;
      }
      pList->numOfItems=j;
      return(0);
      }

      int MyList::IoRemoveItemByPointer(ALIST* pList,void* pItem)

      {
      int i;

      for(i=0;i<pList->numOfItems;i++)
      if(pList->items[i]==pItem)
      break;
      if(i<pList->numOfItems)
      return(IoRemoveItem(pList,i));
      else
      return(0);
      }

      int MyList::IoAddSortedItem(ALIST *pList,void* pItem,int __cdecl* compareTwoElements(const void **elem1,const void **elem2))


      {
      if(IoAppendItem(pList,pItem)<0)
      return(-1);
      qsort(pList->items,pList->numOfItems,sizeof(void*),compareTwoElements(elem1, elem2));
      return(0);
      }

      void *MyList::IoFindSortedItem(ALIST* pList,void* item,int* pos,int* cmpRslt,int (__cdecl *compareTwoItems )(const void *item1,const void *item2))

      {
      int i,j,k;
      void *itemInList;

      if(pList->numOfItems==0)
      {
      *pos=0;
      *cmpRslt = -1;
      return(NULL);
      }
      i=0;
      j=pList->numOfItems-1;
      *pos=0;
      *cmpRslt = -1;
      while(1)
      {
      if(j<i)
      {
      return(NULL); /* Can not find the item */
      }
      k=(i+j)/2;
      *pos=k;
      itemInList=IoGetItem(pList,k);
      if((*cmpRslt=compareTwoItems(item,itemInList))==0)
      {
      return(itemInList); /* The item is found */
      }
      if((*cmpRslt)<0)
      j=k-1;
      else
      i=k+1;
      }
      }更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • 这样写是C++语法的C程序,点评见内
        1、数据封装到哪里去了?Mylist中应当加private的listsize,Alist × firstnode, Alist * lastnode等,自己处理链表长度,首指针、尾指针等信息

        2、Mylist的构造和析构函数呢?IoCreateAList不应该是成员函数,而应该对应成传入参数为const Alist & 的构造函数

        3、C++中的new不应该检查是否为空,对应应该进行例外处理try-catch-throw,因为new根本不会返回null,分配失败,系统抛出例外,你要处理

        4、IoAppendItem改为Append(const Alist & node); 要封装!

        5、IoInsertItem改为Insert(const Alist &node, Iterator itr); 如果对Iterator是什么不知道的话,在学习STL前,至少也应该写成nsert(const Alist &node, int pos);

        6、add, remove, delete见上

        7、IoGetItem改为Iterator setto(int pos);

        8、IoListSize改为const int size(){ return listsize;}

        9、IoClearList改写为析构函数

        10、IoFreeList改写为Alist的析构函数

        ...................不多说了,说句不客气的话,你是一个只知道C++语法的C程序员
        • 功底深!在EA高就吧。我用过MFC, 基本没自己写过class
          • 我认识的朋友中,一个刚拒了公司offer, 还有一个连电话面试也拒绝了。
            • 据EA?
              • 不能拒啊?
                • 能, EA这么糟糕啊?
                  • 如果别人有合适的位子而且做的挺好,什么公司都能拒。
        • You should get a C++ book and re-learn the usage of "const", you are abusing it.
          本文发表在 rolia.net 枫下论坛- In general, why need MyList?

          1、数据封装到哪里去了?Mylist中应当加private的listsize,Alist × firstnode, Alist * lastnode等,自己处理链表长度,首指针、尾指针等信息
          - ?? You didn't understand the code. There are a list of items, not list of Alists

          2、Mylist的构造和析构函数呢?IoCreateAList不应该是成员函数,而应该对应成传入参数为const Alist & 的构造函数
          - "const Alist &" makes no sense here, a copy constructor? a const reference?? why?
          - To have "IoCreateAList" in Alist (we don't need MyList) is not an bad idea, just need to make it static, otherwise where do you want to put it? remember, "要封装!"

          3、C++中的new不应该检查是否为空,对应应该进行例外处理try-catch-throw,因为new根本不会返回null,分配失败,系统抛出例外,你要处理
          - Unfortuntely, not all C++ compilers are 100% ISO compliant, for example, previous version of VC++, so having that check may not be as bad as you think.

          4、IoAppendItem改为Append(const Alist & node); 要封装!
          - Read code first, he wants to append an "Item" to the list not another "Alist"

          5、IoInsertItem改为Insert(const Alist &node, Iterator itr); 如果对Iterator是什么不知道的话,在学习STL前,至少也应该写成nsert(const Alist &node, int pos);
          - You use const on AList (should be Item), why not using it on iterator?, if you 对const and const_iterator是什么不知道的话,在学习C++ and STL前, hehe ... ;-).
          - And see #7

          6、add, remove, delete见上
          - And see #7

          7、IoGetItem改为Iterator setto(int pos);
          - So, your logic is: "pos" ==> "iterator" ===> "item". Why just simply "pos" ===> "item". Iterator makes no sense to users of the class, all they want is "Item" and all they know is "pos". So why did you suggest giving them back iterator? Remember "要封装!". ( Note: People might want iterators if they need to use stl algoritms/functors/predictors...).

          8、IoListSize改为const int size(){ return listsize;}
          - FUNNY! Does it make any sense to return a CONST INT?? Though it's not wrong, but just very funny, don't you know return code is put in EAX and in 286 when int is 16bits the EAX happened to have 16bits, so now since 386 we've have 4bytes there and a int heppens to only need 4 bytes in a 32bit system?? People might want "int size() CONST { return listsize;}", people might what "const int AConstant = xxx', but I can tell you, you little junior programmer ;-), no one needs "const int" return code, well, no one cares, cos when it gets compiled, trust me, your code 100% sure will be ignored by compiler as a line of junk code.

          9、IoClearList改写为析构函数
          10、IoFreeList改写为Alist的析构函数
          - Please read code, again.

          Here is my comments for you : "...................不多说了,说句不客气的话,你是一个知道 a little bit C++语法的 junior 程序员" haha. Please don't take this seriously, just for fun.更多精彩文章及讨论,请光临枫下论坛 rolia.net
          • dicom.你的名字也太好玩了.
            • Y?
              • Digital Imaging And Communication in Medicine
                • Hehe, that's what I'm doing.
                  • i know radio and communication..