查看文章
 
微软亚洲研究院笔试题
2007-08-27 16:52
1.改错

#include <stdio.h>
#include <String.h>
class CBuffer
{
   char * m_pBuffer;
   int m_size;
   public:
     CBuffer()
     {
       m_pBuffer=NULL;
     }
     ~CBuffer()
     {
       Free();
     }
     void Allocte(int size)   (3)     {
       m_size=size;
       m_pBuffer= new char[size];
     }
   private:
     void Free()
     {
       if(m_pBuffer!=NULL)
       {
         delete m_pBuffer;
         m_pBuffer=NULL;
       }
     }
     public:
     void SaveString(const char* pText) const
     {
       strcpy(m_pBuffer, pText);
     }
     char* GetBuffer() const
     {
       return m_pBuffer;
     }
};

void main (int argc, char* argv[])
{
   CBuffer buffer1;
   buffer1.SaveString("Microsoft");
   printf(buffer1.GetBuffer());
}

答:改正后
主要改正SaveString函数

void SaveString(const char* pText) const
{
strcpy(m_pBuffer, pText);
}
改为
void SaveString(const char* pText) (1)
{
Allocte(strlen(pText)+1); (2)
strcpy(m_pBuffer, pText);
}
原因:
(1) const成员函数表示不会修改数据成员,而SaveString做不到,去掉const声明
(2) m_pBuffer指向NULL,必须用Allocte分配空间才能赋值。
(3) 另外需要将Allocte成员函数声明为私有成员函数更符合实际

2.下来程序想打印“Welcome MSR Asia”,改正错误

#include <stdio.h>
#include <string.h>
char * GetName (void)
{
   //To return “MSR Asia” String
   char name[]="MSR Asia";
   return name;
}
void main(int argc, char* argv[])
{
   char name[32];
   //Fill in zeros into name
   for(int i=0;i<=32;i++)
   {
     name='\0';
   }
   //copy “Welcome” to name
   name="Welcome";
   //Append a blank char
   name[8]=" ";
   //Append string to name
   strcat(name,GetName());
   //print out
   printf(name);
}

答:改正后为

#include <stdio.h>
#include <string.h>
char * GetName (void)
{
   //To return “MSR Asia” String
   //char name[]="MSR Asia";       (1)  
         char *name=(char *)malloc(strlen("MSR Asia")+1);    
   strcpy(name,"MSR Asia");
   return name;
}
void main(int argc, char* argv[])
{
   char name[32];
   //Fill in zeros into name
   for(int i=0;i<=32;i++)
   {
     name='\0';
   }
   //copy “Welcome” to name
   //name="Welcome";         (2)
   strcat(name,"Welcome ");
   //Append a blank char
//   name[8]=' ';             (3)
   //Append string to name
   char *p=GetName();         (4)
   strcat(name,p);
   free (p);
   //print out
   printf(name);
}
原因:(1)在函数内部定义的变量在函数结束时就清空了,必须动态分配内存
(2)字符串赋值语句错误,应该用strcat
(3)该语句无效,可去掉
(4)定义一个指针指向动态分配的内存,用完后需用free语句释放


3.写出下面程序的输出结果

#include <stdio.h>
class A
{
public:
   void FuncA()
   {
     printf("FuncA called\n");
   }
   virtual void FuncB()
   {
     printf("FuncB called\n");
   }
};

class B: public A
{
public:
   void FuncA()
   {
     A::FuncA();
     printf("FuncAB called\n");
   }
   virtual void FuncB()
   {
     printf("FuncBB called\n");
   }
};

void main(void)
{
   B b;
   A *pa;
   pa=&b;
   A *pa2=new A;
   b.FuncA();       (1)
   b.FuncB();       (2)  
         pa->FuncA();       (3)
   pa->FuncB();       (4)
   pa2->FuncA();       (5)
   pa2->FuncB();
   delete pa2;
}
答:
1.b.FuncA(); 输出
FuncA called
FuncAB called
2.b.FuncB();输出
FuncBB called
上两者好理解,直接调用类B的相应成员函数
3.pa->FuncA();输出
FuncA called 调用类A的FuncA()
4.pa->FuncB();输出
FuncBBcalled调用类B的FuncB(),原因是C++的动态决议机制,当基类函数声明为virtual时,指向派生类对象的基类指针来调用该函数会选择派生类的实现,除非派生类没有才调用基类的虚函数。还有一点注意的是:指向基类类型的指针可以指向基类对象也可以指向派生类对象,如pa=&b;
5. pa2->FuncA();
pa2->FuncB();输出
FuncA called
FuncB called
这也好理解,直接调用类A的相应成员函数

4.In the main() function, after ModifyString(text) is called, what’s the value of ‘text’?

#include <stdio.h>
#include <string.h>
int FindSubString(char* pch)
{
   int count=0;
   char* p1=pch;
   while(*p1!='\0')
   {
     if(*p1==p1[1]-1)
     {
       p1++;
       count++;
     }
     else
     {
       break;
     }
   }
   int count2=count;
   while(*p1!='\0')
   {
     if(*p1==p1[1]+1)
     {
       p1++;
       count2--;
     }
     else
     {
       break;
     }
   }
   if(count2==0)
     return count;
   return 0;
}

void ModifyString(char* pText)
{
   char* p1=pText;
   char* p2=p1;
   while(*p1!='\0')
   {
     int count=FindSubString(p1);
     if(count>0)
     {
       *p2++=*p1;
       sprintf(p2, "%I", count);
       while(*p2!= '\0')
       {
         p2++;
       }
       p1+=count+count+1;
     }
     else
     {
       *p2++=*p1++;
     }
   }
}
void main(void)
{
   char text[32]="XYBCDCBABABA";
   ModifyString(text);
   printf(text);
}
答:最后运行结果是XYBCDCBAIBAAP

类别:求职应聘||添加到搜藏 |分享到i贴吧|浏览(1514)|评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu