运行异常 求解

jokerwqn 2012-07-20 05:33:42
#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 ★
...全文
133 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
庄鱼 2012-07-21
  • 打赏
  • 举报
回复
说出你的构思,要求达到的效果,现在的问题在哪?
没有这些是无法准确推断逻辑错误的。
jokerwqn 2012-07-21
  • 打赏
  • 举报
回复
#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 *head=new Lrc;
Lrc *p1=new Lrc;
p1=NULL;
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<<endl;
Lrc *p=new Lrc;
if(i!=0)
p1->next=p;
if(str.empty())//空串,没意义返回
continue;
int len=str.size()-1;
//cout<<len<<endl;
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)//只有一个[]
{
if(index2==(len-1))//[]后面没有文字,加上……
{
p->T.time=Gettime(str.substr(index1,index3-index1+1)); //获取播放时间
p->lrc_text=" ";
p->T.lasttime=0;
p->next=NULL;
p1=p;
i++;
}

else//[]后面有歌词
{
p->T.time=Gettime(str.substr(index1,index3-index1+1));
p->lrc_text=str.substr(index3+1); //获取歌词
p->T.lasttime=0;
p->next=NULL;
p1=p;
i++;
}
}

else//有多个[]
{
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=" ";
p->T.lasttime=0;
Lrc *p2=new Lrc;
p->next=p2;
p2->T.time=Gettime(s2);
p2->lrc_text=" ";
p2->T.lasttime=0;
p2->next=NULL;
p1=p2;
i++;
}

else
{
s3=str.substr(index2+1);
s1=str.substr(index1,index3-index1+1);
p->T.time=Gettime(s1);
p->lrc_text=s3;
p->T.lasttime=0;
s2=str.substr(index3+1,index2-index3);
Lrc *p2=new Lrc;
p->next=p2;
p2->T.time=Gettime(s2);
p2->lrc_text=s3;
p2->T.lasttime=0;
p2->next=NULL;
p1=p2;
i++;
}


}

if(i==1)
head=p1;
}

infile.close();
return head;
}

void print(struct Lrc *head)
{
Lrc *p=new Lrc;
p=head;
while(p!=NULL)
{
cout<<p->lrc_text<<endl;
cout<<p->T.lasttime<<endl;
p=p->next;
}
delete p;
}


void sort(struct Lrc *head)//排序,计算每段持续时间
{
string str;
float time;
Lrc *p=new Lrc;
Lrc *p1=new Lrc;
Lrc *p2=new Lrc;
//p1=head;
for(p=head;p!=NULL;p=p->next)
{
p2=p;//记录最小值节点
//cout<<p2->lrc_text<<endl;
// cout<<p->T.time;
for(p1=p->next;p1!=NULL;p1=p1->next)
if(p2->T.time>p1->T.time) p2=p1;
//交换数据
str=p->lrc_text;
time=p->T.time;
p->T.time=p2->T.time;
p->lrc_text=p2->lrc_text;
p2->T.time=time;
p2->lrc_text=str;
}

//回带

p=head;
while(1)
{
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;

}


void main()
{


Lrc *head=new Lrc;
head=Creatlrc();
sort(head);
print(head);

}



妹的 基础不够好啊 一个是节点没弄清楚 没有申请新的节点空间 二是指针还是理解的不够透彻 三就是动态分配的删除问题 回去看了下书 修改好了 其实很简单的 问题 啊 啊 啊 归根到底基础不扎实 谢谢 楼上两位 结贴 日
jixingzhong 2012-07-20
  • 打赏
  • 举报
回复
没格式化,看着太乱了。。。。。。

尝试着断点后调试一下吧。

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧