[写代码]处理一组lrc歌词文件
2015-06-14 15:30
423 查看
看到博客http://wwj718.github.io/regex-demo2.html,又想用C++写文本处理了。
将如下文件:
转换成:
在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; }
相关文章推荐
- Java安全之SSL/TLS
- 如何开发一个java开源框架-----Jvn框架之实现用注解代替配置文件
- JAVA中断线程Thread.interrupt()
- 春天的故事-Spring Security3十五日研究
- 使用IntelliJ IDEA和gradle 构建spring 项目
- Java基础数据类型
- 点击 按钮条qq对话框的窗口实现方式。(复制代码直接运行的。、)
- 单例模式——探索之旅
- 【java】itoo项目实战之常被忽视的性能优化
- 图书馆管理系统面向对象编程
- [leetcode] Invert Binary Tree
- C++|C++学习笔记|二、判断与循环
- 深入了解java的一维数组
- 无公式无代码白话机器学习算法之决策树
- 编写高质量的代码
- C语言,求两个有序数组的公共元素
- Demo Java Socket
- 【PHP学习】PHP 变量
- java String类小结
- NYOJ 68 三点顺序