您的位置:首页 > 编程语言

[写代码]处理一组lrc歌词文件

2015-06-14 15:30 423 查看
看到博客http://wwj718.github.io/regex-demo2.html,又想用C++写文本处理了。

将如下文件:

[00:00.12]section 2.
[00:02.68]you will part of a radio programme about the opening of a new local sports shop.
[00:09.32]first you have some time to look at questions 11 to 16.
[00:39.64]now listen carefully and answer questions 11 to 16.
[00:48.24]now we go to Jane who is going to tell us about what's happening in town this weekend.
[00:52.24]right,thanks Andrew,
[00:53.92]and now on to what's new,
[00:56.48]and do we really need yet another sports shop in Bradcaster?
[01:01.24]well,most of you probably know Sports World-
[01:04.44]the branch of a Danish sports goods company that opened a few years ago-
[01:09.04]it's attracted a lot of custom,
[01:11.36]and so the company has now decided to open another branch in the area.
[01:16.60]it's going to be in the shopping centre to the west of Bradcaster,
[01:20.44]so that will be good news for all of you who've found the original shop in the north of the town hard to get to.
[01:27.12]i was invited to a special preview
[01:29.60]and i can promise you,this is the ultimate in sports retailing.
[211:29.60]dnasdasdjasiodjaoid


转换成:

0.0     0.12  section 2.
0.12     2.68  you will part of a radio programme about the opening of a new local sports shop.
2.68     9.32  first you have some time to look at questions 11 to 16.
9.32    39.64  now listen carefully and answer questions 11 to 16.
39.64    48.24  now we go to Jane who is going to tell us about what's happening in town this weekend.
48.24    52.24  right,thanks Andrew,
52.24    53.92  and now on to what's new,
53.92    56.48  and do we really need yet another sports shop in Bradcaster?
56.48    61.24  well,most of you probably know Sports World-
61.24    64.44  the branch of a Danish sports goods company that opened a few years ago-
64.44    69.04  it's attracted a lot of custom,
69.04    71.36  and so the company has now decided to open another branch in the area.
71.36    76.60  it's going to be in the shopping centre to the west of Bradcaster,
76.60    80.44  so that will be good news for all of you who've found the original shop in the north of the town hard to get to.
80.44    87.12  i was invited to a special preview
87.12    89.60  and i can promise you,this is the ultimate in sports retailing.
89.60 12689.60  dnasdasdjasiodjaoid


在C++用正则的库进行匹配要扫好几遍,所以放弃使用正则了。

提供解决方案的代码如下:

#include <iostream>
#include <deque>
#include <iomanip>
#include <string>
#include <cstring>
#include <sstream>
#include <fstream>
#include <vector>

using namespace std;

typedef struct LINE {
string time;
string sentence;
}LINE;

typedef struct CLINE {
string begin;
string end;
string sentence;
}CLINE;

string htom(string time) {
string temp[3];    //h:m:s
deque<int> sign;
for (auto i = 0; i != time.length(); i++) {
if (time[i] == '.' || time[i] == ':') {
sign.push_back(i);    //example: 01:27.12
}
}
sign.push_back(time.size());    //end.
int i = 0, t = 0, num[3];
while (!sign.empty()) {
for (; i != sign.front(); i++) {
temp[t] += time[i];
}
t = t + 1;
i = sign.front() + 1;
sign.pop_front();
}
t = 0;    //set 0
for (auto i = 0; i < 3; i++) {
num[t++] = atoi(temp[i].data());    //convert
}
num[1] = num[0] * 60 + num[1];    //minute
stringstream stoi;
string last;
if (num[2] > 10) {
stoi << num[1] << "." << num[2];
}
else {
stoi << num[1] << ".0" << num[2];
}
stoi >> last;
return last;
}

void input(vector<LINE> &lrc) {
ifstream fileRead("lrc.txt");
while (!fileRead.eof()) {
char stmp[1024];
LINE ltmp;
fileRead.getline(stmp, sizeof(stmp));
int LEN = strlen(stmp);
bool sflag = false;

for (auto i = 1; i < LEN; i++) {
if (stmp[i] == ']') {
sflag = true;
}
if (!sflag) {
ltmp.time += stmp[i];
continue;
}
if (stmp[i] != ']') {
ltmp.sentence += stmp[i];
}
}
lrc.push_back(ltmp);
}
fileRead.close();
}

void convert(vector<LINE> &lrc, vector<CLINE> &cvt) {
int LEN = lrc.size() - 1;
for (auto i = 0; i != LEN; i++) {
CLINE temp;
if (i == 0) {
temp.begin = "0.0";
}
else {
temp.begin = htom(lrc[i - 1].time);
}
temp.end = htom(lrc[i].time);
temp.sentence = lrc[i].sentence;
cvt.push_back(temp);
}
}

void output(vector<CLINE> &cvt) {
ofstream fileWrite("cvtlrc.txt");
for (auto clrc : cvt) {
fileWrite << setw(6) << clrc.begin.data() << " "
<< setw(8) << clrc.end.data()
<< "  " << clrc.sentence.data() << endl;
}
fileWrite.close();
}

int main() {
vector<LINE> lrc;
vector<CLINE> cvt;
input(lrc);
convert(lrc, cvt);
//for (auto clrc : cvt) {
//    cout << setw(6) << clrc.begin.data() << " "
//        << setw(8) << clrc.end.data()
//        << "  " << clrc.sentence.data() << endl;
//}
output(cvt);
return EXIT_SUCCESS;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: