日韩亚洲专区中文字幕|五月天国产精品免费视频|中文字幕乱码亚州无线码二区|亚洲中文免费AV

<ul id="eu2wk"><blockquote id="eu2wk"></blockquote></ul>
  • <td id="eu2wk"><code id="eu2wk"></code></td>

    當前位置:高考升學網(wǎng) > 招聘筆試題 > 正文

    上海貝爾c語言筆試題和面試題答案(二)

    更新:2023-09-21 07:34:51 高考升學網(wǎng)

    五、編寫strcpy函數(shù)(10分)

      已知strcpy函數(shù)的原型是

      char strcpy(char strDest, const char strSrc);

      其中strDest是目的字符串,strSrc是源字符串。

      (1)不調(diào)用C++/C的字符串庫函數(shù),請編寫函數(shù) 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的內(nèi)容復制到strDest,為什么還要char 類型的返回值?

      答:為了實現(xiàn)鏈式表達式。 // 2分

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

      六、編寫類String的構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù)(25分)

      已知類String的原型為:

      class String

      {

      public:

      String(const char str = NULL); // 普通構(gòu)造函數(shù)

      String(const String &other); // 拷貝構(gòu)造函數(shù)

      ~ String(void); // 析構(gòu)函數(shù)

      String & operate =(const String &other); // 賦值函數(shù)

      private:

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

      };

      請編寫String的上述4個函數(shù)。

      標準答案:

      // String的析構(gòu)函數(shù)

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

      {

      delete [] m_data;

      // 由于m_data是內(nèi)部數(shù)據(jù)類型,也可以寫成 delete m_data;

      }

      // String的普通構(gòu)造函數(shù)

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

      {

      if(str==NULL)

      {

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

      m_data = ‘\0’;

      }

      else

      {

      int length = strlen(str);

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

      strcpy(m_data, str);

      }

      }

      // 拷貝構(gòu)造函數(shù)

      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);

      }

      // 賦值函數(shù)

      String & String::operate =(const String &other) // 13分

      {

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

      if(this == &other)

      return this;

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

      delete [] m_data;

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

      int length = strlen(other.m_data);

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

      strcpy(m_data, other.m_data);

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

    最新圖文

    2020年河北新聞網(wǎng)兩學一做

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

    2020年河北新聞網(wǎng)兩學一做

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

    兩學一做學習教育知

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

    2020年開展兩學一做學習教

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