嵌入式培训机构四个月实训课程笔记(完整版)-C语言综合练习-歌词解析器(物联技术666)

  更多配套资料CSDN地址:点赞+关注,功德无量。更多交流+:wulianjishu666

物联技术666_嵌入式C语言开发,嵌入式硬件,嵌入式培训笔记-CSDN博客物联技术666擅长嵌入式C语言开发,嵌入式硬件,嵌入式培训笔记,等方面的知识,物联技术666关注机器学习,arm开发,物联网,嵌入式硬件,单片机领域.https://blog.csdn.net/weixin_39804904?type=blog

歌词解析器:做一个如下图的歌词解析器

===================== 代码实现 ================

//--------------------------------main.c

#include

#include

#include "link.h"

int main()

{

       LRC_INFO *pf;

       FILE *fd;

       int size,rsize,t,num1=0;

       char *pbuffer=NULL,*p;

       char a[4][100]={{""},{""},{""},{""}};

      

       int hour=0,minute=0,second=0;

       unsigned int num=0;

      

       LRC_INFO *head=NULL;

       if( (fd = fopen("简单爱.lrc","rb")) == NULL )//当前路径

       {

             printf("No Lrc File!\n");                         //打开歌词文件

       }

       else

       {

             fseek(fd,0,2);

             size = ftell(fd);

             rewind(fd);

            

             pbuffer = (char *)malloc(size+1);

             rsize = fread(pbuffer,1,size,fd);        //将全部歌词读到缓冲区

             fclose(fd);

             *(pbuffer+rsize)='\0';

       }

       p=Message_Song(pbuffer);

       head=Message_Song_Play(head,p);

       ShellExecute( NULL, NULL, "TTPlayer.exe","简单爱.mp3",NULL,1);   //执行千千静         

/*

第一个值为背景色,第二个值为前景色:

0 = 黑   8 = 灰

1 = 蓝   9 = 淡蓝

2 = 绿   A = 淡绿

3 = 湖蓝 B = 淡浅绿

4 = 红   C = 淡红

5 = 紫   D = 淡紫

6 = 黄   E = 淡黄

7 = 白   F = 亮白

*/

//设置接下来终端显示文本的背景色和文本颜色      

       while(1)

       {

             system("cls");

             SetText_Color(0x0f);

             p=Message_Song(pbuffer);

             GoToXY(25,5);

             printf("时间:%02d:%02d:%02d\n\n\n",hour,minute,second);

             GoToXY(20,8);

             SetText_Color(0x0f);

             printf("%s\n",a[3]);

             GoToXY(20,9);

             printf("%s\n",a[2]);

             GoToXY(20,10);

             printf("%s\n",a[1]);

             GoToXY(20,11);

             SetText_Color(0x0c);

             printf("%s\n",a[0]);

             Sleep(1000);

             if(++second >= 60)

             {

                    second = 0;

                    if(++minute >= 60)

                    {

                          minute = 0;

                          if(++hour >= 24)

                          {

                                 hour = 0;

                          }

                    }

             }    

              t=hour*60*60+minute*60+second;

             if (((head->Time)/100)==t)

             {

                    pf=head;

                    num1++;

                    if (num1<4)

                    {

                          if (num1==1)

                                 strcpy(a[0],head->LrcText);

                          if(num1==2)

                          {

                                 strcpy(a[1],a[0]);

                                 strcpy(a[0],head->LrcText);

                          }

                          if (num1==3)

                          {

                                 strcpy(a[2],a[1]);

                                 strcpy(a[1],a[0]);

                                 strcpy(a[0],head->LrcText);

                          }

                    }

                    else

                    {

                          strcpy(a[3],a[2]);

                          strcpy(a[2],a[1]);

                          strcpy(a[1],a[0]);

                           strcpy(a[0],head->LrcText);

                    }

                    head=pf;

                    head=head->next;

                    free(pf);

             }

             if (head==NULL)

             {

                    free(pbuffer);

                    break;

             }

       }                              

       free(pbuffer);

       return 0;

}

//-------------------------link.c

#include

#include

#include

#include "link.h"

/*

终端打印控制子程序,获取光标位置,将光标移到到指定位置

*/

//获取当前光标位置,并存放到变量 x,y中

void whereXY(int *x,int *y)

{

       HANDLE h1;

       CONSOLE_SCREEN_BUFFER_INFO scrInfo;

h1=GetStdHandle(STD_OUTPUT_HANDLE);       

GetConsoleScreenBufferInfo(h1,&scrInfo);       

       *x=scrInfo.dwCursorPosition.X;

       *y=scrInfo.dwCursorPosition.Y;

}

//跳转到光标指定位置

void GoToXY(int x,int y)

{

       HANDLE h1;

       COORD pos;

       pos.X=x;

       pos.Y=y;

h1=GetStdHandle(STD_OUTPUT_HANDLE);

       SetConsoleCursorPosition(h1,pos);

}

/*

第一个值为背景色,第二个值为前景色:

0 = 黑   8 = 灰

1 = 蓝   9 = 淡蓝

2 = 绿   A = 淡绿

3 = 湖蓝 B = 淡浅绿

4 = 红   C = 淡红

5 = 紫   D = 淡紫

6 = 黄   E = 淡黄

7 = 白   F = 亮白

*/

//设置接下来终端显示文本的背景色和文本颜色

void SetText_Color(int color)

{

       HANDLE hStdout;

       hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hStdout,color);

}

//----------------------------------------

//char * Message_Song(char* pbuffer)

//参数:pbuffer:歌曲内存的首地址

//作用:显示歌曲的信息

//返回:返回歌曲开始的首地址

//----------------------------------------

char * Message_Song(char* pbuffer)

{

       char *ph,*plf,*plb;

       char num[100];

       ph=pbuffer;

       if(strstr(pbuffer,"ti:")!=0)

       {

             memset(num,0,100);

             plf=strstr(pbuffer,"ti:");

             plb=strstr(plf,"]");

             strncpy(num,plf,plb-plf);

             printf("%s\n",num);

       }

       if(strstr(pbuffer,"ar:")!=0)

       {

             memset(num,0,100);

             plf=strstr(pbuffer,"ar:");

             plb=strstr(plf,"]");

             strncpy(num,plf,plb-plf);

             printf("%s\n",num);

       }

       if(strstr(pbuffer,"al:")!=0)

       {

             memset(num,0,100);

             plf=strstr(pbuffer,"al:");

             plb=strstr(plf,"]");

             strncpy(num,plf,plb-plf);

             printf("%s\n",num);

       }

       if (strstr(pbuffer,"by:")!=0)

       {

             memset(num,0,100);

             plf=strstr(pbuffer,"by:");

             plb=strstr(plf,"]");

             strncpy(num,plf,plb-plf);

             printf("%s\n",num);

             ph=plb+2;

       }

       return ph;

}

//----------------------------------------

//LRC_INFO * insert(LRC_INFO *head,char *txt,int n)

//参数:head:链表首地址,txt新结构体的文本输入,n新结构体的数字输入

//作用:建立有序链表

//返回:返回链表的首地址

//----------------------------------------

LRC_INFO *insert(LRC_INFO *head,char *txt,int n)

{

       LRC_INFO *pc,*pf,*pb;

       char c[200]="0";

       pf=pb=head;

       pc=(LRC_INFO *)malloc(sizeof(LRC_INFO));

       strcpy(pc->LrcText,txt);

       pc->Time=n;

       if(head==NULL)

       {

             head=pc;

             head->next=NULL;

       }

       else

       {

             while((pc->Time > pb->Time)&&pb->next!=NULL)

             {

                    pf=pb;

                    pb=pf->next;

             }

             if (pb->next==NULL&&head->next!=NULL)

             {

                    if (pc->Time > pb->Time)

                    {

                          pb->next=pc;

                          pc->next=NULL;

                    }

                    else

                    {

                          pf->next=pc;

                          pc->next=pb;

                    }

             }

             else if (pb->next==NULL&&head->next==NULL)

             {

                    if (pc->Time >= head->Time)

                    {

                          head->next=pc;

                          pc->next=NULL;

                    }

                    else

                    {                        

                          pc->next=head;

                          head=pc;

                    }                 

             }

             else

             {

                    if (pb==head)

                    {

                          head=pc;

                          pc->next=pb;

                    }

                    else

                    {

                          pf->next=pc;

                          pc->next=pb;

                    }

             }

       }

       return head;

}

//----------------------------------------

//LRC_INFO * Message_Song_Play(LRC_INFO *head,char *p)

//参数:head:链表首地址,p:歌词的开始地址

//作用:把歌词有序的压入链表,以时间顺序

//返回:返回链表的首地址

//----------------------------------------

LRC_INFO *Message_Song_Play(LRC_INFO *head,char *p)

{

       char *pb,*end;

       int n;

       p=strtok(p,"\n"); 

       while (p!=NULL)

       {

             pb=p;

             end=strrchr(pb,']');

             while(strchr(pb,'[')!=NULL)

             {

                    pb=strchr(pb,'[');

                    n=(*(pb+1)-'0')*10*60*100+(*(pb+2)-'0')*60*100+(*(pb+4)-'0')*10*100+(*(pb+5)-'0')*100+(*(pb+7)-'0')*10+(*(pb+8)-'0');

                    pb++;

                    head=insert(head,end+1,n);

             }

             p=strtok(NULL,"\n");

       }

       return head;

}

//---------------------------------------link.h

#ifndef __LINK_H__

#define __LINK_H__

typedef int s32_t;

typedef unsigned int u32_t;

typedef unsigned short u16_t;

typedef unsigned char u8_t;

typedef struct _LRC_INFO

{

       u32_t Time;

       char LrcText[100];

       struct _LRC_INFO *next;

} LRC_INFO;

//extern LRC_INFO *InsertList( LRC_INFO * head, LRC_INFO *pi);//存入链表,head链表的头地址,pi插入的头地址;

//extern LRC_INFO *GetLrcInfo(char *LrcFile, u32_t FileSize);

//extern void print( LRC_INFO * head);

extern char* Message_Song(char *Lrc_Buffer);//解析歌曲的作者、歌曲名、专辑、歌词制作信息取出

extern LRC_INFO *insert(LRC_INFO *head,char *txt,int n);

extern LRC_INFO *Message_Song_Play(LRC_INFO *head,LRC_INFO *p);

//extern LRC_INFO * seach(LRC_INFO *head,int t);

extern void whereXY(int *x,int *y) ;

extern void GoToXY(int x,int y);

extern void SetText_Color(int color) ;

#endif

/*

       #if 1     

       system("color 0 0");//非法操作,提示正确操作说明

       system("color 47");//背景红,前景白

       SetText_Color(0x47);//文字背景红,前景白

       system("pause");

       system("cls");      //清屏

       #endif  

       //if( (fd = fopen("D:\\study_test\\clanguage\\lrc_display_demo\\LRC\\简单爱.lrc","rb")) == NULL )//全路径

             //if( (fd = fopen("./LRC/简单爱.lrc","rb")) == NULL )//相对路径*/

//--------------------------------------------------

  更多配套资料CSDN地址:点赞+关注,功德无量。更多交流+:wulianjishu666

物联技术666_嵌入式C语言开发,嵌入式硬件,嵌入式培训笔记-CSDN博客物联技术666擅长嵌入式C语言开发,嵌入式硬件,嵌入式培训笔记,等方面的知识,物联技术666关注机器学习,arm开发,物联网,嵌入式硬件,单片机领域.https://blog.csdn.net/weixin_39804904?type=blog

你可能感兴趣的:(嵌入式培训课程笔记,笔记,c语言,开发语言)