zoukankan      html  css  js  c++  java
  • C/C++ 笔试、面试题目汇总1——代码相关

    1.求下面函数的返回值( 微软)

    int func(int x) 
    { 
        int countx =0; 
    
        while(x) 
        { 
            countx ++; 
            x = x&(x-1); 
        } 
    
        return countx; 
    }
    View Code

    思路:将x转化为2进制,看含有的1的个数。

    2. 下面关于“联合”的题目的输出?

    a)

    #include <stdio.h>
    union
    {
      int i;
      char x[2];
    }a;
    
    void main()
    {
      a.x[0] =10; 
      a.x[1] =1;
      printf("%d",a.i);
    }
    View Code

     答案:266 (低位低地址,高位高地址,内存占用情况是Ox010A)

    b)

    int main() 
    { 
        union   /*定义一个联合*/ 
        { 
            int i;
    
            struct  /*在联合中定义一个结构*/ 
            { 
                char first; 
                char second; 
            }half; 
        }number;
    
        number.i=0x4241; /*联合成员赋值*/ 
        printf("%c%c
    ", number.half.first, mumber.half.second); 
        number.half.first='a'; /*联合中结构成员赋值*/ 
        number.half.second='b'; 
        printf("%x
    ",number.i); 
        getch(); 
    
        return 0;
    } 
    View Code

     答案: AB   (0x41对应'A',是低位;Ox42对应'B',是高位)

           6261 (number.i和number.half共用一块地址空间)

    3. 已知strcpy的函数原型:char *strcpy(char *strDest, const char *strSrc)其中strDest 是目的字符串,strSrc 是源字符串。不调用C++/C 的字符串库函数,请编写函数 strcpy。

    /*
    编写strcpy函数(10分)
    已知strcpy函数的原型是
    char *strcpy(char *strDest, const char *strSrc);
    其中strDest是目的字符串,strSrc是源字符串。
    (1)不调用C++/C的字符串库函数,请编写函数 strcpy
    (2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?
    答:为了 实现链式表达式。 // 2分
    例如 int length = strlen( strcpy( strDest, “hello world”) );
    */
    
    #include <assert.h>
    char* strcpy(char*strDest, const char*strSrc)
    {
        assert((strDest!=NULL) && (strSrc !=NULL)); // 2分
        char *address = strDest;    // 2分
    
        while ((*strDest++=*strSrc++) !='')
            ;                       // 2分
    
        return address;             // 2分
    }
    View Code

      另外strlen函数如下:

    #include<assert.h> 
    int strlen( const char*str ) // 输入参数const
    {
        assert( str != NULL ); // 断言字符串地址非0
        int len = 0;
    
        while( (*str++) !='' ) 
        { 
            len++; 
        } 
    
        return len;
    }
    View Code

    4. 已知String类定义如下

    class String
    {
    public:
      String(const char *str = NULL); // 通用构造函数
      String(const String &another); // 拷贝构造函数
      ~String(); // 析构函数
      String& operater =(const String &rhs); // 赋值函数
    private:
      char* m_data; // 用于保存字符串
    };
    View Code

    尝试写出类的成员实现。
    答案:

    #include <string.h>
    
    #pragma warning(disable:4996)       // warning C4996: “strcpy”被声明为否决的
    
    class String
    {
    public:
        String(const char *str = NULL); // 通用构造函数
        String(const String &another); // 拷贝构造函数
        ~String(); // 析构函数
        String& operator =(const String &rhs); // 赋值函数
    private:
        char* m_data; // 用于保存字符串
    };
    
    String::String(const char *str)
    {
        if (str == NULL)    // strlen在参数为NULL时会抛异常才会有这步判断
        {
            m_data = new char[1];
            m_data[0] = '';
        }
        else 
        {
            size_t size = strlen(str) + 1;
    
            m_data =new char[size];
            strcpy(m_data,str);
        }
    }
    
    String::String(const String &another)
    {
        m_data =new char[strlen(another.m_data) +1];
        strcpy(m_data,another.m_data);
    
    }
    
    String& String::operator=(const String &rhs)
    {
        if (this == &rhs)
            return *this;
    
        delete []m_data; //删除原来的数据,新开一块内存
        
        m_data =new char[strlen(rhs.m_data) +1];
        strcpy(m_data,rhs.m_data);
    
        return*this ;
    }
    
    String::~String()
    {
        delete []m_data;
    }
    View Code

    5. 引用相关知识

    参见《引用相关知识总结》

    参考资料: http://www.cnblogs.com/fangyukuan/archive/2010/09/18/1829871.html

  • 相关阅读:
    解决SharePoint 文档库itemadded eventhandler导致的上传完成后,编辑页面保持报错的问题,错误信息为“该文档已经被编辑过 the file has been modified by...”
    解决SharePoint 2013 designer workflow 在发布的报错“负载平衡没有设置”The workflow files were saved but cannot be run.
    随机实例,随机值
    Spring4笔记
    struts2笔记(3)
    struts2笔记(2)
    获取文本的编码类型(from logparse)
    FileUtil(from logparser)
    DateUtil(SimpleDateFormat)
    struts2笔记
  • 原文地址:https://www.cnblogs.com/wangzhijun/p/3154253.html
Copyright © 2011-2022 走看看