东南大学 计算机复试2017年

今年也只有一道编程题呢

格式转换,从一个文件中读取日期07/21/2016,转换为以下格式July 21,2016并输出到屏幕上

此处再次提出
atoi只需要提供一个参数
但是itoa在vs2019中不可直接使用 需要使用_itoa_s 并提供四个参数 分别是原int,char*,字符串长度,基数(进制)

string与int的转换就很ez了 stoi / to_string

偷懒做法

#include<iostream>
#include<ctime>
#include<fstream>
#include<string>
#include<stack>
#include<vector>
#include<map>
#include<iomanip>
using namespace std;
void process1(char *d)
{
    char* s;
    char *a;
    a=strtok_s(d, "/", &s);
    if (a)
    {
        //cout << a << endl;
        string mon[13] = {"","Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec"}; 
        int month = atoi(a);
        cout << left << setw(5) << mon[month];
    }
    a= strtok_s(NULL, "/", &s);
    cout << left << setfill('0') << setw(2) << a;
    a = strtok_s(NULL, "/", &s);
    cout << left <<','<< setfill(' ') << setw(5) << a << endl;
    cout << setfill(' ') << endl;
    //
}

int main() {
    ifstream is;
    is.open("insert.txt");
    string a;
    while (getline(is, a))
    {
        char d[30];
        strcpy_s(d, 30, a.c_str());
    //  cout << d << endl;
        process1(d);
    }

}

不偷懒做法:
这里为了测试到底怎么识别\符号 我将文本文件中的’/‘修改为’\‘了 这个符号由于是转义符 需要额外的’\‘来表明这是个啥玩意

#include<iostream>
#include<ctime>
#include<fstream>
#include<string>
#include<stack>
#include<vector>
#include<map>
#include<iomanip>
using namespace std;
void process1(string d)
{
    int num=0;
    string mon[13] = { "","Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec" };
    if ((num=d.find('\\',num))!=string::npos) //我很怀疑这个地方到底是表示的是什么
    {
        string month=d.substr(0,num);
        d.erase(0, num+1);
        int i = stoi(month);
        cout << setw(5) << left << mon[i];
    }
    num = d.find('\\', num);
    if (num != string::npos)
    {
        string day = d.substr(0, num);
        d.erase(0, num+1);
        cout << setfill('0') << setw(2) << day << ',';
    }
    cout << setfill(' ') << left << setw(4) << d << endl;
}

int main() {
    ifstream is;
    is.open("insert.txt");
    string a;
    while (getline(is, a))
    {
        process1(a);
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容