今年也只有一道编程题呢
格式转换,从一个文件中读取日期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);
}
}