64,646
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#define SONG_LENGTH 20
#define ARTIST_LENGTH 20
#define CD_LENGTH 20
class Song{
public:
char* song;
char* artist;
char* cd;
int rating;
Song* next;
};
Song* new_song(char* song, char* artist, char* cd, int rating){
Song *head=new Song;
head->song = new char(SONG_LENGTH+10);
head->artist = new char(ARTIST_LENGTH+10);
head->cd = new char(CD_LENGTH+10);
strcpy(head->song,song);
strcpy(head->artist,artist);
strcpy(head->cd,cd);
head->rating=rating;
head->next=NULL;
return head;
}
Song *insert_by_song(Song *head,Song *node)//插入
{
if(head==NULL)
{
node->next=NULL;
head=node;
return head;
}
else
{
node->next=head;
head=node;
return head;
}
}
Song* delete_odd(Song* head){
Song *p=head;
Song *before = NULL;
while(p!=NULL){
cout<<strlen(p->artist)<<endl;
system("pause");
if (p->rating%2==0){
if(before==NULL){
head=p->next;
delete [] (p->artist); //在运行时会出现heap corruption detected
delete [] (p->cd);
delete [] (p->song);
delete p;
p=head;
}
else{
delete [](p->artist);
delete [](p->cd);
delete [](p->song);
p->artist=NULL;
p->cd=NULL;
p->song=NULL;
before->next=p->next;
delete p;
p=before->next;
}
}
else{
before=p;
p=p->next;
}
}
return head;
}
int main(int argc, char** argv){
cout<<"The size of the struct type Song is "
<<sizeof(Song)
<<" bytes."
<<endl;
if(argc<2)
{
cout<<"Error: Insufficient CommandLine arguments.\n";
cout<<"Usage: linkedlist <filename><CR>\n";
return 0;
}
Song *head = NULL;
print_list(head);
Song *node = NULL;
int rating; /* cout<< hex <<uppercase;*/
ifstream source(*(argv+1));
while(!source.eof())
{
char *song = new char(SONG_LENGTH);
char *artist = new char(ARTIST_LENGTH);
char *cd = new char(CD_LENGTH);
source >> song; //从文件输入
source >> artist;
source >> cd;
source >> rating;
new_song(song,artist,cd,rating); //创建新的结构
head = insert_by_song(head,node); //插入链表
cout << endl;
}
source.close();
delete_odd(head);
system("pause");
return 0;
}
//荷塘月色 凤凰传奇 未知/Unknown 4
//光辉岁月 Beyond 未知/Unknown 3
//天机 张学友&白鹭 影视歌曲 4
//Yesterday Beatles Help! 5
//Paradise Cody_Simpson <Paradise> 3
//Misery Maroon_5 Hands_All_Over 4
//Smooth Santana Supernatural 5
//Something Beatles Abbey_Road 5
//GoodtimeGirl Grant_Grieves 45S 4
//Sunshine Minnutes The_Big_C 2
//Time E.J.Bucher SongInGoodTime 3
//去伊犁的路上 刀郎 未知/Unknown 5
//葫芦娃 未知/Unknown 儿童歌曲 4
//恰似你的温柔 邓丽君 经典歌曲 5
//You_And_Me Shane_Filan 奥运 5
//外婆的澎湖湾 蔡琴 爱像一首歌 5