WPS 5月19号 C++实习 笔试题回顾

来源:互联网 发布:win10打不开任何软件 编辑:程序博客网 时间:2024/05/20 01:13

5月19号,和同学一块去西电老校区参加了wps的c++实习生的笔试,总体感觉wps的笔记题还是比较实在,没有那些坑爹的智力题,java题。

废话不多说,先上笔试题。
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

选择题

我几乎把每个题都运行了一遍,得到了下面一组答案,大家可以参考一下,如果有不放心的可以自己再运行测试一下。

1 2 3 4 5 6 7 8 9 10 B B C B D A C A D B

我对选择题考查的知识点进行了总结、拓展,大家可以看看:
1.C++关键字mutable,explicit,拓展:C++一些不常用但是很重要的关键字,参考:那些陌生的C++关键字
2.指针和引用的区别
3.vector,STL的应用
4.斐波那契数列的变形,递归
5.c库函数
6.sizeof计算相关大小,数组,指针,结构体,字节对齐,虚继承,虚函数,继承,组合等相关大小。
7.const关键词修饰引用,拓展const修饰指针的几种情况
8.函数重载
9.虚函数
10.map,vector,STL的应用

当时在做选择题3、7、10时有点困惑,我下来查了一下,查的结果如下:
3.关于vector的erase函数,我从《STL源码剖析》上找到了具体实现细节,
这里写图片描述
注意第三行,erase删除一个元素时,会将后续元素往前移动。
7.关于const修饰引用,修饰指针。我测试并做了个总结。

    const int &b = a;       int const &d = a;      //和上面一样,不能通过b改变其所指地址的值    //int & const b = a;   //不能这样用    const int *c = &a;     //不能通过c改变其所指地址的值,但可以改变c的值    int const *e = &a;     //同上    int *const f = &a;     //可以通过c改变其所指地址的值,但不可以改变c的值

10.第10题有两个地方要注意:
(1).map底层是红黑树实现,红黑树的排序能力强,因此map在插入一个元素后就保证其根据键值有序,map中的元素始终是有序的,这点和vector不同。
(2).map的迭代器和vector的迭代器底层的实现原理不同,map的迭代器底层是指针实现,vector的迭代器底层是数组下标,所以两者在for循环中迭代遍历容器元素时,for循环截止的条件不同。

简答题

这个简答题主要就是考查c++里面字符串的几种表达形式,鉴于wps就是专门做字符串处理的,出这么一个题也不奇怪。这个题唯一的吐槽点就是要写的字好多。

关于c++里面的string类,我找了一篇介绍的比较好的文章:C++ string介绍
另外,对于这套题里面出现了很多次的wstring,我也找了一篇文章:C++ wstring介绍

程序题

talk is cheap, show me the code.

1.设计日期格式化函数。

#include <iostream>#include <cassert>#include <string>#include <vector>using namespace std;wstring FormatDate( const wstring* format, int year, int month, int day );bool IsLeap( int year );bool IsLongMonth( int month );int main( void ){    //wstring format = L"YYYYMMDD";    //wstring format = L"YY/MM/DD";    //wstring format = L"MM/DD/YYYY";    wstring format = L"MMDDYYYY";    wstring result = FormatDate( &format, 2000, 2, 28 );    wcout << result << endl;    return 0;}wstring FormatDate( const wstring* format, int year, int month, int day ){    //保证输入数据的有效    assert( year > 1000 && year <= 9999 );    assert( month >= 1 && month <= 12 );    assert( day >= 1 );    if ( month == 2 )        IsLeap( year ) ? ( assert(day <= 28) ) : ( assert(day <= 29) );    else if ( IsLongMonth( month ) )        assert( ( day <= 31 ) );    else        assert( ( day <= 30 ) );    vector<wstring> support_format;    wchar_t result_wchar[12];    wchar_t support_for[3][12] = { L"YYYYMMDD", L"YY/MM/DD", L"MM/DD/YYYY" };    for ( int i = 0; i < 3; i++ )        support_format.push_back( wstring(support_for[i]) );    if ( *format == support_format[0] )    {        swprintf( result_wchar, 100, L"%.4d%.2d%.2d", year, month, day );    }    else if ( *format == support_format[1] )    {        swprintf( result_wchar, 100, L"%.2d/%.2d/%.2d", year%100, month, day );    }    else if ( *format == support_format[2] )    {        swprintf( result_wchar, 100, L"%.2d/%.2d/%.4d", month, day, year );    }    else    {        result_wchar[0] = L'\0';    }    return wstring( result_wchar );}bool IsLeap( int year ){    if ( (( year%4 == 0 ) && ( year%100 != 0 )) || ( year%400 == 0 ) )        return true;    return false;}bool IsLongMonth( int month ){    if ( month == 4 || month == 6 || month == 9 || month == 11 )        return false;    return true;}

2.实现一个内存池,支持1k以下内存的分配操作。

参考文章:内存池设计与实现

3.金山项目符号和编号功能

#include <iostream>#include <fstream>#include <vector>#include <string>#include <cassert>  //assert#include <cstdlib>  //mbstowcs wcstombsusing namespace std;const wstring fileName = L"wpstest";vector<wstring> ReadFile();int Find( const wstring &format, wstring& message );    //return value 0 1 2 -1void GetNumberFormateCount( const wstring& format, vector<int>& vecCounts );int main( void ){    wstring format = L"1.";    vector<int> vecCounts;    GetNumberFormateCount( format, vecCounts );    wcout <<  format << endl;    for ( int i = 0; i < vecCounts.size(); ++i )    {        wcout << i << " : " << vecCounts[i] << endl;    }    return 0;}void GetNumberFormateCount( const wstring& format, vector<int>& vecCounts ){    vector<wstring> fileContent;    for ( int i = 0;  i < 3; ++i )        vecCounts.push_back( 0 );   fileContent = ReadFile();   for ( int i = 0; i < fileContent.size(); ++i )       wcout << fileContent[i] << endl;   cout << endl;   int k;   for ( int i = 0; i < fileContent.size(); ++i )   {       k = Find( format, fileContent[i] );       if ( k >= 0 )       {           vecCounts[k]++;       }   }}//将fileName中的内容按行存入vector<wstring>中vector<wstring> ReadFile(){    char strFileName[50];    char s[50];    wchar_t ws[50];    vector<wstring> result;    //wcstombs 将宽字符串转换为普通字符串    assert (-1 != wcstombs( strFileName, fileName.c_str(), fileName.size()) );    //打开要操作的文件    ifstream fin( strFileName );    assert( fin.is_open() );    //按行获取文件内容    while ( fin.getline( s, 50 ) )    {        if ( -1 == mbstowcs( ws, s, 50 ) )            exit( 1 );        result.push_back( wstring( ws ) );    }    return result;}//从message中找到format的所在的级数int Find( const wstring &format, wstring& message ){    wstring temp;    wchar_t temp_wc[50];    int j = 0, count = 0;;    for ( int i = 0; i <= message.size(); ++i )    {        //分离出一个单词        if ( message[i] == ';' || message[i] == '\0' )        {            temp_wc[j] = '\0';            wstring temp = temp_wc;            if ( format == temp )                return count;            count ++;            j = 0;            continue;        }        temp_wc[j++] = message[i];    }    return -1;}
原创粉丝点击