运行异常 求解
#include<iostream>
#include<fstream>
#include<string>
#include<cmath>
#include<cstdlib>
using namespace std;
struct Time
{
float time;//读取时间
float lasttime;//持续时间
};
struct Lrc
{
Time T;
string lrc_text;
struct Lrc *next;
};
float Gettime(string s)//获取播放时间
{
//char *str1,*str2;
string str3,str4;
int flag1=s.find_first_of('[');
int flag2=s.find_first_of(']');
int flag3=s.find_first_of(':');
str3=s.substr(flag1+1,flag3-flag1-1);
str4=s.substr(flag3+1,flag2-flag3-1);
//strcpy(str1,str3.c_str());
//strcpy(str2,str4.c_str());
float t1=atof(str3.c_str())*60000;
float t2=atof(str4.c_str())*1000;
return t1+t2;
}
struct Lrc *Creatlrc()//创建歌词链表
{
Lrc *p=new Lrc;
Lrc *head=new Lrc;
int flag=0;
string str;//获得歌词字符串
ifstream infile("Linkin Park - in the end.lrc",ios_base::binary);
int i=0;
while(getline(infile,str,'\n'))
{
//cout<<str;
if(str.empty())//空串,没意义返回
continue;
int len=str.size()-1;
int index1=str.find_first_of('[');
if(index1==-1)//过滤
continue;
int index2=str.find_last_of(']');
int index3=str.find_first_of(']');
if(index2==index3)//只有一个[]
{ string s1,s2;
s1=str.substr(index1,index3-index1+1);
// s2=str.substr(index3+1);
if(index2==(len-1))//[]后面没有文字,加上……
{
t=p->T.time=Gettime(s1); //获取播放时间
p->lrc_text=" ";
}
else//[]后面有歌词
{
p->T.time=Gettime(str.substr(index1,index3-index1+1));
p->lrc_text=str.substr(index3+1); //获取歌词
}
// cout<<"ssss";
}
else//有多个[]
{ //cout<<"[ti:In the End]";
string s1,s2,s3;
if(index2==(len-1)) //后面无歌词
{
s1=str.substr(index1,index3-index1+1);
s2=str.substr(index3+1,index2-index3);
p->T.time=Gettime(s1);
p->lrc_text="";
//cout<<"[ti:In the End]";
p=p->next;
p->T.time=Gettime(s2);
cout<<"[ti:In the End]";
p->lrc_text="";
}
else
{
s3=str.substr(index2+1);
s1=str.substr(index1,index3-index1+1);
p->T.time=Gettime(s1);
p->lrc_text=s3;
s2=str.substr(index3+1,index2-index3);
p->next->T.time=Gettime(s2);
p->next->lrc_text=s3;
}
}
if(flag=0)
{
head=p;flag=1;
}
p=p->next;
if(!infile.eof())
{
p=NULL;
break;
}
}
infile.close();
delete p;
return head;
}
void sort(Lrc *head)//排序,计算每段持续时间
{
Lrc *p=new Lrc;
Lrc *p1=new Lrc;
Lrc *p2=new Lrc;
Lrc *p3=new Lrc;
//p1=head;
for(p=head;p!=NULL;p=p->next)
{
p2=p;//记录最小值节点
for(p1=p;p1!=NULL;p1=p1->next)
if(p2->T.time>p1->T.time) p2=p1;
//交换数据
p3=p;
p->T.time=p2->T.time;
p->lrc_text=p2->lrc_text;
p2->T.time=p3->T.time;
p2->lrc_text=p3->lrc_text;
}
//回带
p=head;
while(p!=NULL)
{
if(p->next==NULL){p->T.lasttime=2000;break;}
p->T.lasttime=p->next->T.time-p->T.time;
p=p->next;
}
delete p;
delete p1;
delete p2;
delete p3;
}
void print(struct Lrc *head)
{
Lrc *p=new Lrc;
p=head;
while(p!=NULL)
{
cout<<p->lrc_text<<endl;
p=p->next;
}
delete p;
}
void main()
{
Lrc *head=new Lrc;
head=Creatlrc();
//sort(head);
print(head);
}
主要是将歌词提取出来 用到了链表 写得时候就预感会出现n多错误 经过调试编译 链接没问题 运行老是出现问题 搞了一下午 还是不得要领 求大神 指点一二。
歌词如下(把前边歌词标签之类省了)
[00:01.04]Linkin Park - In the End
[00:06.04]Album:Hybrid Theory
[00:11.04]Lrc by 賴潤誠 oicq:85860288 ★
[00:16.04]
[00:20.04]It starts with one thing
[00:22.58]I don't know why
[00:23.80]It doesn't even matter how hard you try keep that in mind
[00:27.02]I designed this rhyme
[00:28.10]To explain in due time
[00:29.38]All I know
[00:31.01]Time is a valuable thing
[00:32.88]Watch it fly by as the pendulum swings
[00:35.15]Watch it count down to the end of the day
[00:37.22]The clock ticks life away
[00:38.78]It's so unreal
[00:40.31]Didn't look out below
[00:41.95]Watch the time go right out the window
[00:44.42]Trying to hold on, but didn't even know
[00:46.59]Wasted it all just to watch you go
[00:49.39]I kept everything inside and even though I tried, it all fell apart
[00:53.43]What it meant to me will eventually be a memory of a time when
[00:56.35]
[00:56.89]I tried so hard
[00:58.94]And got so far
[01:01.79]But in the end
[01:03.56]It doesn't even matter
[01:05.82]I had to fall
[01:08.06]To lose it all
[01:10.92]But in the end
[01:12.74]It doesn't even matter
[01:14.94]
[01:16.32]One thing, I don't know why
[01:18.58]It doesn?t even matter how hard you try, keep that in mind
[01:21.80]I designed this rhyme, to explain in due time
[01:24.22]I tried so hard
[01:26.01]In spite of the way you were mocking me
[01:28.08]Acting like I was part of your property
[01:30.16]Remembering all the times you fought with me
[01:32.35]I'm surprised it got so (far)
[01:34.85]Things aren't the way they were before
[01:36.91]You wouldn't even recognize me anymore
[01:39.19]Not that you knew me back then
[01:41.57]But it all comes back to me (in the end)
[01:43.74]You kept everything inside and even though I tried, it all fell apart
[01:48.21]What it meant to me will eventually be a memory of a time when I
[01:51.26]
[02:48.48][01:51.75]I tried so hard
[02:50.74][01:53.72]And got so far
[02:53.57][01:56.56]But in the end
[02:55.32][01:58.37]It doesn't even matter
[02:57.55][02:00.56]I had to fall
[02:59.83][02:02.91]To lose it all
[03:02.66][02:05.68]But in the end
[03:04.43][02:07.37]It doesn't even matter
[02:10.86]
[02:30.23][02:12.07]I put my trust in you
[02:34.96][02:16.59]Pushed as far as I can go
[02:39.55][02:21.35]And for all this
[02:41.82][02:23.54]There's only one thing you should know
[03:09.82][02:47.63][02:29.39]
[03:12.82]Lrc by 賴潤誠 oicq:85860288 ★