當前位置:高考升學網 > 招聘筆試題 > 正文

創維集團2019筆試題和面試題答案(三)

更新:2023-09-20 05:09:42 高考升學網

五、編寫strcpy函數(10分)

  已知strcpy函數的原型是

  char strcpy(char strdest, const char strsrc);

  其中strdest是目的字符串,strsrc是源字符串。

  (1)不調用c++/c的字符串庫函數,請編寫函數 strcpy

  char strcpy(char strdest, const char strsrc);

  {

  assert((strdest!=null) & (strsrc !=null)); // 2分

  char address = strdest; // 2分

  while( (strdest++ = strsrc++) != ‘\0’ ) // 2分

  null ;

  return address ; // 2分

  }

  (2)strcpy能把strsrc的內容復制到strdest,為什么還要char 類型的返回值?

  答:為了實現鏈式表達式。 // 2分

  例如 int length = strlen( strcpy( strdest, “hello world”) );

六、編寫類string的構造函數、析構函數和賦值函數(25分)

  已知類string的原型為:

  class string

  {

  public:

  string(const char str = null); // 普通構造函數

  string(const string other); // 拷貝構造函數

  ~ string(void); // 析構函數

  string operate =(const string &other); // 賦值函數

  private:

  char m_data; // 用于保存字符串

  };

  請編寫string的上述4個函數。

  標準答案:

  // string的析構函數

  string::~string(void) // 3分

  {

  delete [] m_data;

  // 由于m_data是內部數據類型,也可以寫成 delete m_data;

  }

  // string的普通構造函數

  string::string(const char str) // 6分

  {

  if(strnull)

  {

  m_data = new char; // 若能加 null 判斷則更好

  m_data = ‘\0’;

  }

  else

  {

  int length = strlen(str);

  m_data = new char[length+1]; // 若能加 null 判斷則更好

  strcpy(m_data, str);

  }

  }

  // 拷貝構造函數

  string::string(const string other) // 3分

  {

  int length = strlen(other.m_data);

  m_data = new char[length+1]; // 若能加 null 判斷則更好

  strcpy(m_data, other.m_data);

  }

  // 賦值函數

  string string:perate =(const string other) // 13分

  {

  // (1) 檢查自賦值 // 4分

  if(this other)

  return this;

  // (2) 釋放原有的內存資源 // 3分

  delete [] m_data;

  // (3)分配新的內存資源,并復制內容 // 3分

  int length = strlen(other.m_data);

  m_data = new char[length+1]; // 若能加 null 判斷則更好

  strcpy(m_data, other.m_data);

  // (4)返回本對象的引用 // 3分

  return this;

  }

最新圖文

2020年河北新聞網兩學一做

時間:2023-09-18 07:0:24

2020年河北新聞網兩學一做

時間:2023-09-15 11:0:59

兩學一做學習教育知

時間:2023-09-21 06:0:30

2020年開展兩學一做學習教

時間:2023-09-19 21:0:30
9999久久久国产精品,日韩在线一区二区三区欧美,日韩精品综合在线人妻,免费AAAAAA毛片看
日本啪视频在线观看精品综合 | 中文字幕一区二区日韩欧美 | 日本欧美在线播放免费人成 | 亚洲精品AⅤ在线观看 | 亚洲中文字幕精品一区二区三区 | 日韩专码一区二区 |