小白学习,歌词解析(全)

main.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"console.h"
#include"fun.h"
int main(int  argc,char argv[])
{
   char *file_date=NULL;//存歌词数据
   char *buf[128]={NULL}; //存分割后的歌词
    int i=0;            //记录歌词行数
    int n=0;
    char name[]="";         //放歌曲名字
    LRC *head=NULL;  //链表头
    /*printf("请输入以下歌曲的名字\n");
    prienf("玫瑰花的葬礼,简单爱");
    scanf("%s",name);*/
    file_date=read_file("玫瑰花的葬礼.lrc");//读取歌词数据
   /* printf("%s",file_date);   */          //测试是否读到
    //切割歌词
    buf[i] = strtok(file_date,"\r\n");

    while(buf[i] !=NULL)
    {
        i++;
        buf[i]=strtok(NULL,"\r\n");
    }
    
    n=i;
     /*  //遍历分割后的歌词
    i=0;
	while(buf[i] != NULL)
	{
		printf("%s\n",buf[i]);
		i++;
	}*/
    
        //分析前四行
        clear_screen();
        char *arr[4]={"歌曲","歌手","专辑","制作"};
   for(i=0;i<3;i++)
   {
       char tmp[128]="";
       sscanf(buf[i],"%*[^:]:%[^]]",tmp);
        cusor_moveto(20,i+1);
       printf("%s:%s\n",arr[i],tmp);//打印前四行
   }
    
    
    for(i=4;i<n;i++)
    {
        //歌词的位置
       char *str_lrc = buf[i];
        while(*str_lrc =='[')
        {
            str_lrc+=10;
        }

        char *str_time = buf[i];
        while(*str_time == '[')
        {
            int m = 0;
            int s = 0;
            int time = 0;

            sscanf(str_time,"[%d:%d.41]", &m,&s);
        
            time = m*60+s;
        
            LRC tmp;
            tmp.time = time;
            strcpy(tmp.lrc,str_lrc);
            head = insert_link(head,tmp);//插入链表
            str_time  += 10;
        }
    }
   /* print_link(head);*/     //遍历链表

    mplayer_play("玫瑰花的葬礼.mp3");
  
    int j = 0;
    char gun1[128]="";
    char gun2[128]="";
    char gun3[128]="";
    char gun4[128]="";
    
    while(1)
    {

        cusor_moveto(20,7);
        printf("%02d:%02d",j/60,j%60);
        fflush(stdout);

        LRC *ret = search_link(head,j);
        if(ret != NULL)
        {
            strcpy(gun1,gun2);
            strcpy(gun2,gun3);
            strcpy(gun3,gun4);
            strcpy(gun4,ret->lrc);

            cusor_moveto(15,9);
            printf("%s                ",gun1);

            cusor_moveto(15,10);
            printf("%s                ",gun2);

            cusor_moveto(15,11);
            printf("%s                ",gun3);

            cusor_moveto(15,12);
            set_fg_color(COLOR_YELLOW);
            printf("%s                 ",ret->lrc);
            set_fg_color(COLOR_BLACK);
            fflush(stdout);

        }
        

        sleep(1);
        j++;
    }


   
    return 0;
}

fun.c

#include<stdio.h>
#include <unistd.h>
#include<stdlib.h>
#include"fun.h"
char* read_file(char *name)  //读取歌词
{
    char *data=NULL;
    long file_length = 0;
    FILE *fp=NULL;
    fp = fopen(name ,"r");
    if(fp==NULL)
    {
        perror("fopen");
        return NULL;
    }
    fseek(fp,0,2);
    file_length=ftell(fp);
    rewind(fp);
    data=(char*)calloc(1,file_length);
    if (data==NULL)
    {
        perror("calloc");
        return NULL;

    }

    fread(data,file_length,1,fp);
    fclose(fp);
    return data;
}


 LRC* insert_link(LRC *head,LRC tmp)//链表插入
{
      LRC *pi =(LRC *)calloc(1,sizeof(LRC));
      if(pi==NULL)
      {
          perror("calloc");
          return head;
      }

      *pi = tmp;
      pi->next=NULL;

      if(head==NULL)
      {
          head=pi;
          return head;
      }
      else
       {
            LRC *pb=head,*pf=head;
            while(pb->time<pi->time &&pb->next!=NULL)
            {
              pf = pb;
              pb = pb->next;
            }
            if(pb->time>=pi->time)
            {
                if(pb==head)
                {
                  pi->next=head;
                  head=pi;
                  return head;
                }
                else
               {
                  pf->next=pi;
                  pi->next=pb;
                  return head;
               }
            }
            else
            {
                pb->next=pi;
                return head;
            }
        }
      return head;

}

void  print_link(LRC *head)//打印链表
{
      if(head==NULL)
      {
          printf("link not find");
          return;
      }
      else
      {
          LRC *pb=head;
          do
            {
              printf("time =%d,lrc=%s\n",pb->time,pb->lrc);
              pb=pb->next;
            }while(pb!=NULL);
       }
      return ;
}

LRC* search_link(LRC *head,int j)//查询链表
{
    if(head ==NULL)
    {
        printf("link not find");
        return NULL;
    }
    else
    {
        LRC *pb=head;
        while(pb->time!= j &&pb->next!=NULL)
        {
            pb=pb->next;
        }
        if(pb->time==j)
        {
            return pb;
        }
        else
        {
            return NULL;
        }
        
    }
    return NULL;
    
}

void mplayer_play(char * song_path)//播放歌曲函数
{
	pid_t pid;
	pid=fork();
	if(pid<0)
	{
		perror("fork");
	}
	else if(pid==0)
	{
		close(1);
		close(2);
		execlp("mplayer","mplayer","-slave","-quiet",song_path,NULL);
		exit(0);
	}
	else
		;
}

fun.h

#ifndef __FUN_H__
#define __FUN_H__
typedef struct lrc
{
    int time;
    char lrc[128];


    struct lrc *next; 

}LRC;
extern char* read_file(char *name);
extern LRC* insert_link(LRC *head,LRC tmp);
extern void  print_link(LRC *head);
extern LRC* search_link(LRC *head,int j);
extern void mplayer_play(char * song_path);
#endif

console.c

#include <stdio.h>
#include <stdlib.h>
#include "console.h"

void cusor_moveto(int x, int y)
{// ESC[y;xH
    printf("\033[%d;%dH",y,x);
    fflush(stdout);
} 

//保存光标位置
void cusor_get_pos(void)
{// ESC[s
    printf("\033[s");
    fflush(stdout);
} 

//恢复光标位置
void cusor_set_pos(void)
{// ESC[u
    printf("\033[u");
    fflush(stdout);
} 
void cusor_hide(void)
{
	printf("\033[?25l");
}
//清屏
void clear_screen(void)
{// ESC[2J
    printf("\033[2J");
    fflush(stdout);
}

/*
COLOR_RED              红
COLOR_BLACK            黑
COLOR_GREEN            绿
COLOR_BLUE             蓝
COLOR_YELLOW           黄
COLOR_WHITE            白
COLOR_CYAN             青
COLOR_MAGENTA          洋红
*/
//设置前景颜色
void set_fg_color(int color)
{// ESC[#m
    printf("\033[%dm",color);
    fflush(stdout);
}

//设置背景颜色
void set_bg_color(int color)
{// ESC[#m
    printf("\033[%dm",(color+10));
    fflush(stdout);
}

console.h

#ifndef  _CONSOLE_H_
#define  _CONSOLE_H_

#define     COLOR_RED              31
#define     COLOR_BLACK            30
#define     COLOR_GREEN            32
#define     COLOR_BLUE             34
#define     COLOR_YELLOW           33
#define     COLOR_WHITE            37
#define     COLOR_CYAN             36
#define     COLOR_MAGENTA          35
/*
COLOR_RED              红
COLOR_BLACK            黑
COLOR_GREEN            绿
COLOR_BLUE             蓝
COLOR_YELLOW           黄
COLOR_WHITE            白
COLOR_CYAN             青
COLOR_MAGENTA          洋红
*/

extern void cusor_moveto(int x, int y);//光标跳转到 y行 x列
extern void cusor_get_pos(void);//保存光标位置
extern void cusor_hide(void);
extern void cusor_set_pos(void);//恢复光标位置
extern void clear_screen(void);//清屏
extern void set_fg_color(int color);//设置字体前景色
extern void set_bg_color(int color);//设置字体背景色

#endif	//_CONSOLE_H_



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学习 Python 对于初学者来说是一个很好的选择,因为它有着简洁明了的语法和丰富的资源。以下是一个适合 Python 小白学习计划: 1. **基础知识**: - 学习基础语法:变量、数据类型(整型、浮点型、字符串等)、运算符、控制流(条件语句、循环)。 - Python 基本结构:函数定义和调用,模块和包的理解。 2. **文本处理与文件操作**: - 使用 `open()` 函数读写文件,掌握基本的文件操作模式 (`r`, `w`, `a` 等)。 - 正则表达式(re模块)的学习,用于文本处理和搜索替换。 3. **数据分析入门**: - 安装并熟悉 NumPy 和 Pandas 库,用于数据处理和分析。 - 初步了解列表推导式、Pandas DataFrame 结构以及数据清洗。 4. **函数编程**: - 学习高阶函数、闭包和装饰器等概念,理解如何利用它们提高代码复用性和可读性。 5. **面向对象编程**: - 掌握类和对象的概念,学会封装、继承和多态的基本使用。 - 学会使用 Python 内置的 OOP 工具如 `super()` 和魔术方法 (`__init__`, `__str__`, `__del__` 等)。 6. **Python Web框架** (选修): - 如果对 Web 开发感兴趣,可以选择 Flask 或 Django 中的一个作为入门,了解 HTTP 请求响应、路由和视图函数。 7. **实战项目**: - 通过实际项目应用所学知识,如爬虫、数据分析小工具或简单的网页应用。 8. **持续学习与巩固**: - 阅读 Python 书籍和官方文档,关注 Python 新版本更新。 - 参加在线论坛或社区交流,解决遇到的问题。 - 定期做练习题和小项目,保持动手实践的习惯。 记得边学边做,理论结合实践是最好的学习方式。在每个阶段结束后,都不要忘记总结回顾和做一些自我测试。祝你在 Python 的学习旅程中顺利!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值