C 代码 解析歌词,然后与时间轴一直显示

// hero.lrc

[ti:真心英雄]
[ar:成龙]
[al:1]
[by:w]
[00:00.00][00:08.22]周华健、成龙、黄耀明、李宗盛演唱
[00:09.08]李宗盛词曲
[01:53.46][00:10.07][01:55.54][00:11.60]周:在我心中曾经有一个梦
[02:00.11][00:17.34]要用歌声让你忘了所有的痛
[02:05.68][00:21.29]成:灿烂星空谁是真的英雄
[02:10.49][00:27.87]平凡的人们给我最多感动
[02:15.50][00:32.20]黄:再没有恨也没有了痛
[02:20.91][00:37.88]但愿人间处处都有爱的影踪
[02:26.07][00:41.89]李:用我们的歌换你真心笑容
[02:31.23][00:48.23]合:祝福你的人生从此与众不同
[02:35.69][00:52.74][03:18.02][02:36.61][00:53.96]把握生命里的每一分钟
[03:22.81][02:41.27][00:58.68]全力以赴我们心中的梦
[03:27.96][02:46.10][01:03.30]不经历风雨怎么见彩虹
[03:33.09][02:51.98][01:09.05]没有人能随随便便成功
[03:37.84][02:56.25][01:12.94][03:38.44][02:57.03][01:14.54]把握生命里每一次感动
[03:43.17][03:01.84][01:18.87]和心爱的朋友热情相拥
[03:48.41][03:07.48][01:23.90]让真心的话和开心的泪
[03:53.73][03:12.05][01:29.70]在你我的心里流动
[03:58.31][01:33.65][03:59.46]LaLaLaLa.........
[04:18.84]把握生命里每一次感动
[04:23.97]和心爱的朋友热情相拥
[04:29.66]让真心的话和开心的泪
[04:34.64]在你我的心里流动
[04:39.98]让真心的话和开心的泪
[04:44.99]在你我的心里流动.
[04:48.86]
[04:49.70]
[04:50.38]
[01:36.2]

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

typedef struct LyricEntry {
    char time[20];
    char text[200];
} LE_c;

LE_c lyrics[100];
int cnt;
void fun(char str[]) {
    char* p = str;
    char* times[10];
    int i = 0, n;
    while (*p) {
        if (*p == '[') {
            if (!isdigit(*(p + 1))) {
                n = strlen(p);
                *(p + n - 2) = '\0';
                printf("%s\n", p + 4);
                return;
            }
            times[i++] = p + 1;
        } else if (*p == ']') {
            *p = '\0';
            times[i] = p + 1;
        }
        p++;
    }

    n = i;

    for (i = 0; i < n; i++) {
        strcpy(lyrics[cnt].time, times[i]);
        strcpy(lyrics[cnt].text, times[n]);
        cnt++;
    }
}

int cmp(LE_c a, LE_c b) {
    int aMinutes, bMinutes;
    double aSeconds, bSeconds;
    char* p;
    p = strchr(a.time, ':');
    *p = '\0';
    aMinutes = atoi(a.time);
    aSeconds = atof(p + 1);

    p = strchr(b.time, ':');
    *p = '\0';
    bMinutes = atoi(b.time);
    bSeconds = atof(p + 1);

    if (aMinutes > bMinutes)
        return 1;
    else if (aMinutes < bMinutes)
        return 0;
    else {
        return aSeconds > bSeconds;
    }
}
int totalSeconds(LE_c a) {
    int aMinutes;
    double aSeconds;
    char* p;
    p = strchr(a.time, ':');
    *p = '\0';
    aMinutes = atoi(a.time);
    aSeconds = atof(p + 1);
    return aMinutes * 60 + aSeconds;
}

void swap(LE_c* a, LE_c* b) {
    LE_c c;
    c = *a;
    *a = *b;
    *b = c;
}
void sort() {
    int i, j;
    for (i = 0; i < cnt - 1; i++) {
        for (j = 0; j < cnt - i - 1; j++) {
            if (cmp(lyrics[j], lyrics[j + 1])) {
                swap(&lyrics[j], &lyrics[j + 1]);
            }
        }
    }
}
void print() {
    int i;
    for (i = 0; i < cnt; i++) {
        printf("%s:%s\n", lyrics[i].time, lyrics[i].text);
    }
}

int main() {
    char line[200];

    FILE* file = fopen("hero.lrc", "r");
    if (file == NULL) {
        perror("Failed to open file");
        return 1;
    }

    while (fgets(line, sizeof(line), file) != NULL) {
        // puts(line);
        fun(line);
    }
    fclose(file);
    sort(); //排序

    time_t startTime = time(NULL);
    int i = 0;
    while (1) {
        time_t currentTime = time(NULL);
        int elapsedSeconds = difftime(currentTime, startTime);
        if (totalSeconds(lyrics[i]) < elapsedSeconds) {
            printf("%s   ", lyrics[i].time);
            puts(lyrics[i++].text);
            if (i == cnt)
                break;
        }
    }
    return 0;
}

package net.Java.$0817;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

class LyricEntry {
    String time;
    String text;

    public LyricEntry(String time, String text) {
        this.time = time;
        this.text = text;
    }
}

public class LyricSorter {
    static LyricEntry[] lyrics = new LyricEntry[100];
    static int cnt = 0;

    public static void fun(String str) {
        int[][] times = new int[10][2];
        int i = 0, n = 0;
        char[] p = str.toCharArray();

        int j = 0;
        while (i < p.length) {
            if (p[i] == '[') {
                if (!Character.isDigit(p[i + 1])) {
                    System.out.println(new String(Arrays.copyOfRange(p,  4, p.length-1)));
                    return;
                }
                times[j][0] = i+1;                
            } else if (p[i] == ']') {
                times[j++][1]=i;
                n=i+1;
            }
                i++;

        }

        for (i = 0; i < j; i++) {
             String str1 = new String(Arrays.copyOfRange(p, times[i][0], times[i][1]));
             String text =new String(Arrays.copyOfRange(p, n,p.length));
            lyrics[cnt++] = new LyricEntry(str1,text);
        }
    }

    public static int cmp(LyricEntry a, LyricEntry b) {
        int aMinutes, bMinutes;
        double aSeconds, bSeconds;
        String[] p;

        p = a.time.split(":");
        aMinutes = Integer.parseInt(p[0]);
        aSeconds = Double.parseDouble(p[1]);

        p = b.time.split(":");
        bMinutes = Integer.parseInt(p[0]);
        bSeconds = Double.parseDouble(p[1]);

        if (aMinutes > bMinutes)
            return 1;
        else if (aMinutes < bMinutes)
            return -1;
        else {
            return Double.compare(aSeconds, bSeconds);
        }
    }

    public static int totalSeconds(LyricEntry a) {
        String[] p = a.time.split(":");
        int aMinutes = Integer.parseInt(p[0]);
        double aSeconds = Double.parseDouble(p[1]);
        return aMinutes * 60 + (int) aSeconds;
    }

    public static void swap(LyricEntry[] arr, int i, int j) {
        LyricEntry temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void sort() {
        for (int i = 0; i < cnt - 1; i++) {
            for (int j = 0; j < cnt - i - 1; j++) {
                if (cmp(lyrics[j], lyrics[j + 1]) > 0) {
                    swap(lyrics, j, j + 1);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        String line;

        BufferedReader reader = new BufferedReader(new FileReader("hero.lrc"));

        while ((line = reader.readLine()) != null) {
            fun(line);
        }
        reader.close();
        sort();


        long startTime = System.currentTimeMillis();
        int i = 0;
        while (true) {
            long currentTime = System.currentTimeMillis();
            int elapsedSeconds = (int) ((currentTime - startTime) / 1000);
            if (totalSeconds(lyrics[i]) < elapsedSeconds) {
                System.out.print(lyrics[i].time + "   ");
                System.out.println(lyrics[i++].text);
                if (i == cnt)
                    break;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值