金山C++开发笔试

1.格式化日期

#include <assert.h>

#include <iostream>
#include <vector>
#include <string>
using namespace std;

bool IsLeapYear(int year)
{
    if((year%4==0 && year%100!=0) || year%400==0)
        return true;
    return false;
}

bool IsBigMonth(int month)
{
    if(month == 4 || month == 6 || month == 9 || month == 11)
        return false;
    return true;
}

wstring FormatDate(const wstring& format,int year,int month,int day)
{
    assert(year > 1000 && year <= 9999);
    assert(year >= 1 && month <= 12);
    assert(day >= 1);
    if(month == 2)
        IsLeapYear(year) ? assert(day <= 29) : assert(day <= 28);
    else if(IsBigMonth(month))
        assert(day <= 31);
    else
        assert(day <= 30);
    //上面所做的都是为了判断输入的数据是否合法
    wchar_t *resultstring = new wchar_t[12];
       vector<wstring> support_format;
    support_format.push_back(L"YYYYMMDD");
    support_format.push_back(L"YY/MM/DD");
    support_format.push_back(L"MM/DD/YYYY");

    if(format == support_format[0])
    {
        swprintf(resultstring,12,L"%.4d%.2d%.2d",year,month,day);
    }
    else if(format == support_format[1])
    {
        swprintf(resultstring,12,L"%.2d/%.2d/%.2d",year%100,month,day);
    }
    else if(format == support_format[2])
    {
        swprintf(resultstring,12,L"%.2d/%.2d/%.4d",month,day,year);
    }
    else
    {
        cerr<<"no this format"<<endl;
    }
    return wstring(resultstring);
}

int main()
{
    wstring format1(L"YYYYMMDD");
    wstring format2(L"YY/MM/DD");
    wstring format3(L"MM/DD/YYYY");
    wstring resultstring =       FormatDate(format1,2017,8,23);
    wstring resultstring1 = FormatDate(format2,2017,8,23);
    wstring resultstring2 = FormatDate(format3,2017,8,23);
    wcout<<resultstring<<endl;
    wcout<<resultstring1<<endl;
    wcout<<resultstring2<<endl;
    return 0;
}

2.设计一个内存池,支持1K以下内存的分配操作。参照STL源码剖析中的内存适配器就可以得到。
代码量有些长,点击github地址

3.

#include <string.h>
#include <assert.h>

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;


//读取文件
vector<string> readfile(string filename)
{
    vector<string> result;
    string charline;

    ifstream fin( filename.c_str() );
    assert( fin.is_open() );
    while( !fin.eof())
    {
        getline(fin,charline);
        cout<<charline<<endl;
        result.push_back(charline);
    }
    fin.close();
    return result;
}

int Count(const string& format,string& str)
{
    char temp_str[20];
    int j = 0,count = 0;
    for(int i = 0; i <= str.size(); ++i)
    {
            if(str[i] == ';' || str[i] == '\0')
        {
            temp_str[j] = '\0';
            string temp(temp_str);
            if(temp == format)
                return count;
            count++;
            j = 0;
            continue;
        }
        temp_str[j++] = str[i];
    }
    return -1;
}


void GetNumberFormateCount(const string& format,vector<int>& vecCounts)
{
    string filename("test.txt");
    vector<string> result = readfile(filename);
    for(int i = 0; i < 3; ++i)
        vecCounts.push_back(0);
    for(int i = 0; i < result.size(); ++i)
    {
        int j = Count(format,result[i]);
        if(j >= 0)
            vecCounts[j]++;
    }
}

int main()
{
    vector<int> vecCounts;
    string format("1.");
        GetNumberFormateCount(format,vecCounts);
    for(int i = 0; i < vecCounts.size(); ++i)
    {
        cout<<i+1<<" "<<vecCounts[i]<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值