[转载]C,C++及数据结构笔试题1(转)

it2022-05-08  10

原文地址:C,C++及数据结构笔试题1(转) 作者:chord

一:

已知类String的原型为:

class String

{

 public:

     String(const char *str = NULL);            

     String(const String ©);                    

     ~String(void);                

     String & operator = (const String ©);  

 private:

     char * m_data;  

};

请编写String的上述4个函数。

答案:

版本1

// String 的析构函数

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

{

 delete [] m_data;

// 由于m_data 是内部数据类型,也可以写成delete m_data;

}

String::String(const char *str)

{

 if(str==NULL)

{

 m_data = new char[1]; // 若能加NULL 判断则更好

 *m_data = ‘{post.content}’;

 }

else

 {

 int length = strlen(str);

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

 strcpy(m_data, str);

 }

}

// 拷贝构造函数

String::String(const String &other)

{

 int length = strlen(other.m_data);

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

 strcpy(m_data, other.m_data);

}

// 赋值函数

String & String:operate =(const String &other)

{

// (1) 检查自赋值

if(this == &other)

return *this;

// (2) 释放原有的内存资源

delete [] m_data;

// (3)分配新的内存资源,并复制内容

 int length = strlen(other.m_data);

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

 strcpy(m_data, other.m_data);

// (4)返回本对象的引用

 return *this;

}

版本2

String::String (const char *str)

{

     if(str){

      memset(m_data,0,strlen(m_data));

      strcpy(m_data,str);

     }

     else *m_data=0;

}

  String::String (const String ©)

 {

     strcpy(m_data,copy.m_data);

  }

  String& String:operator =(const String ©)

 {

    if(this==©) retrun *this;

     strcpy(m_data,copy.m_data);

      return *this;

}

版本3

String::String (const char *str)

{

     if ( m_data )

         delete[] m_data;

     if(str){

      m_data = new char[strlen(str)];

      memset(m_data,0,strlen(m_data));

      strcpy(m_data,str);

     }

     else *m_data=0;

}

 

  String::String (const String ©)

 {

     if ( m_data )

     delete[] m_data;

     m_data = new char[strlen(copy.m_data+1)]

     strcpy(m_data,copy.m_data);

  }

 

  String& String:operator =(const String ©)

 {

    if(this==©) retrun *this;

     if ( m_data )

         delete[] m_data;

     m_data = new char[strlen(copy.m_data+1)]

     strcpy(m_data,copy.m_data);

      return *this;

  }

~String::String(void)

{

  if ( m_data )

      delete[] m_data;

}

二:改错题,只能在原来的基础上增加代码,不能删除代码

#include

#include

void foo(int age,char *b)

{

   b = (char *)malloc(64);

   sprintf(b,"Your Age is %d",age);

}

int main()

{

  char *f;

  foo(23,f);

  printf("%sn",f);

}

答案

版本1

#include

#include

void foo(int age,char **b)

{

   *b = (char *)malloc(64);

   sprintf(*b,"Your Age is %d",age);

}

int main()

{

  char **f;

  foo(23,f);

  printf("%sn",**f);

  return 0;

}

版本2

#include

#include

void foo(int age,char *&b)

{

   b = (char *)malloc(64);

   sprintf(b,"Your Age is %d",age);

}

int main()

{

  char *f;

  foo(23,f);

  printf("%sn",f);

  free(f);//不要忘了free;

}

三:有程序片断如下

int main()

{

   int I = 20;

   pid_t pid = 5;

   if((pid = fork()) > 0)

   {

      I = 50;

      printf("%dn",I);   (1)

   }

   else if(pid == 0)

   {

      printf("%dn",I);   (2)

   }

}

请问该程序用的是进程方式还是线程方式,并说明进程与线程的区别:

请问该程序输出什么结果?

四、constant pointer points for String

   pointer points for constant string

五、下面等价的是:

A int i=0

    if(i)

   {

    printf("hello,world");

   }

   B  int i=1;

      int j=2;

      if(i==1 || j==2)

      {

       printf("hello,world");

       }

   C  Boolean b1=true;

      Boolean b2=true;

      if(b1==b2)

      {

       printf("hello,world");

     }

    D  int i=1;

      int j=2;

      if(i==1 &| j==2)

      {

       printf("hello,world");

     }

六、排序二叉树插入一个节点或双向链表的实现

四~六为IBM面试题。

七、指针++的含义和用法

八、stack 和heap的分配,rt-os的特点、同步的方式

九、怎样避免内存泄漏的问题

十、编程实现十进制数转化为十六进制输出,不准用任何已经定义的库函数,比方说String,Math。int toHex(int ) 

十一、编程实现大于100的两个数值相乘的结果输出,同样不准使用任何已定义函数,Math,string,convert等。比方说12345*32534677                                                                      

输入为两个string      int toPlus('12345','32434677')                                     

输出为一个长型的

十二、int delete(node * head)

{                                                                                             free(head);                          

head=head->link;                                  

return 0;

}                                                                           

指出程序的错误,并且写出正确的程序

十三、写一个程序可以算出字节在计算机中的存储是由大到小还是有小到大。

十四、一段程序,写出输出结果     

大概是                                                                                   class A                                                                                 {                                                                                  

转载于:https://www.cnblogs.com/liuzhuqing/archive/2012/06/29/7480810.html


最新回复(0)