zoukankan      html  css  js  c++  java
  • [写代码]处理一组lrc歌词文件

    看到博客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++用正则的库进行匹配要扫好几遍,所以放弃使用正则了。

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

      1 #include <iostream>
      2 #include <deque>
      3 #include <iomanip>
      4 #include <string>
      5 #include <cstring>
      6 #include <sstream>
      7 #include <fstream>
      8 #include <vector>
      9 
     10 using namespace std;
     11 
     12 typedef struct LINE {
     13     string time;
     14     string sentence;
     15 }LINE;
     16 
     17 typedef struct CLINE {
     18     string begin;
     19     string end;
     20     string sentence;
     21 }CLINE;
     22 
     23 string htom(string time) {
     24     string temp[3];    //h:m:s
     25     deque<int> sign;
     26     for (auto i = 0; i != time.length(); i++) {
     27         if (time[i] == '.' || time[i] == ':') {
     28             sign.push_back(i);    //example: 01:27.12
     29         }
     30     }
     31     sign.push_back(time.size());    //end.
     32     int i = 0, t = 0, num[3];
     33     while (!sign.empty()) {
     34         for (; i != sign.front(); i++) {
     35             temp[t] += time[i];
     36         }
     37         t = t + 1;
     38         i = sign.front() + 1;
     39         sign.pop_front();
     40     }
     41     t = 0;    //set 0
     42     for (auto i = 0; i < 3; i++) {
     43         num[t++] = atoi(temp[i].data());    //convert
     44     }
     45     num[1] = num[0] * 60 + num[1];    //minute
     46     stringstream stoi;
     47     string last;
     48     if (num[2] > 10) {
     49         stoi << num[1] << "." << num[2];
     50     }
     51     else {
     52         stoi << num[1] << ".0" << num[2];
     53     }
     54     stoi >> last;
     55     return last;
     56 }
     57 
     58 void input(vector<LINE> &lrc) {
     59     ifstream fileRead("lrc.txt");
     60     while (!fileRead.eof()) {
     61         char stmp[1024];
     62         LINE ltmp;
     63         fileRead.getline(stmp, sizeof(stmp));
     64         int LEN = strlen(stmp);
     65         bool sflag = false;
     66 
     67         for (auto i = 1; i < LEN; i++) {
     68             if (stmp[i] == ']') {
     69                 sflag = true;
     70             }
     71             if (!sflag) {
     72                 ltmp.time += stmp[i];
     73                 continue;
     74             }
     75             if (stmp[i] != ']') {
     76                 ltmp.sentence += stmp[i];
     77             }
     78         }
     79         lrc.push_back(ltmp);
     80     }
     81     fileRead.close();
     82 }
     83 
     84 void convert(vector<LINE> &lrc, vector<CLINE> &cvt) {
     85     int LEN = lrc.size() - 1;
     86     for (auto i = 0; i != LEN; i++) {
     87         CLINE temp;
     88         if (i == 0) {
     89             temp.begin = "0.0";
     90         }
     91         else {
     92             temp.begin = htom(lrc[i - 1].time);
     93         }
     94         temp.end = htom(lrc[i].time);
     95         temp.sentence = lrc[i].sentence;
     96         cvt.push_back(temp);
     97     }
     98 }
     99 
    100 void output(vector<CLINE> &cvt) {
    101     ofstream fileWrite("cvtlrc.txt");
    102     for (auto clrc : cvt) {
    103         fileWrite << setw(6) << clrc.begin.data() << " "
    104                   << setw(8) << clrc.end.data()
    105                   << "  " << clrc.sentence.data() << endl;
    106     }
    107     fileWrite.close();
    108 }
    109 
    110 int main() {
    111     vector<LINE> lrc;
    112     vector<CLINE> cvt;
    113     input(lrc);
    114     convert(lrc, cvt);
    115     //for (auto clrc : cvt) {
    116     //    cout << setw(6) << clrc.begin.data() << " "
    117     //        << setw(8) << clrc.end.data()
    118     //        << "  " << clrc.sentence.data() << endl;
    119     //}
    120     output(cvt);
    121     return EXIT_SUCCESS;
    122 }
  • 相关阅读:
    C语言提供的位运算符
    JAVA反射改动常量,以及其局限
    直击中关村创业大街,新街头霸王来了
    bind() to 0.0.0.0:80 failed (98: Address already in use)
    Eclipse 快捷方式 指定 固定 workspace
    C++对象模型——Inline Functions(第四章)
    eclipse中安装freemarker插件及ftl使用freemarker编辑器
    迷茫了好一阵决定做WEB前端
    ios代理的使用,正向传值,逆向传值
    easyUI Tab href,content差别
  • 原文地址:https://www.cnblogs.com/kirai/p/4575135.html
Copyright © 2011-2022 走看看