东南大学计算机专业复试,东南大学 计算机复试2017年

今年也只有一道编程题呢

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

此处再次提出

atoi只需要提供一个参数

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

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

偷懒做法

#include

#include

#include

#include

#include

#include

#include

#include

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 <

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

#include

#include

#include

#include

#include

#include

#include

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值