#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ITEM "Song management system"
struct song *shuru(struct song **head);
void charu(struct song **head);
void shanchu(struct song **head);
void liulan(struct song **head);
void xiugai(struct song **head);
void creat(struct song **head);
struct date
{
int year;
int month;
}tis;
struct song //声明结构体类型struct song
{
char music[20];
char company[20];
char author[20];
char singer[20];
struct date tis;
struct song *next;
};
int main() //对所需进行操作的选择
{
struct song *shuru(struct song **head);
void charu(struct song **head);
void shanchu(struct song **head);
void liulan(struct song **head);
void xiugai(struct song **head);
void creat(struct song **head);
void fenzu(struct song **head);
struct song *head; //定义一个结构体类型的指针作为实参
int k,w1;
char ch;
z:
while(k!=7)
{
do{
printf("**********歌曲信息管理系统*********\n\n");
printf("1.输入\n");
printf("2.插入\n");
printf("3.删除\n");
printf("4.浏览\n");
printf("5.修改\n");
printf("6.分组\n");
printf("7.退出\n");
printf("请选择你所需的功能:");
scanf("%d",&k);
if(k<1&&k>7)
w1=1;
else w1=0;
}
while(w1==1);
switch(k)
{
case 1 :
//system("cls");
shuru(&head);
printf("按z返回主菜单:");
scanf("%c",&ch);
if(ch=='z')
{
goto z;
}
break;
case 2 :
system("cls");
charu(&head);
printf("按z返回主菜单:");
scanf("%c",&ch);
if(ch=='z')
{
goto z;
}
break;
case 3 :
system("cls");
shanchu(&head);
printf("按z返回主菜单:");
scanf("%c",&ch);
if(ch=='z')
{
goto z;
}
break;
case 4 :
system("cls");
liulan(&head);
printf("按z返回主菜单:");
scanf("%c",&ch);
if(ch=='z')
{
goto z;
}
break;
case 5 :
system("cls");
xiugai(&head);
printf("按z返回主菜单:");
scanf("%c",&ch);
if(ch=='z')
{
goto z;
}
break;
case 6 :
system("cls");
fenzu(&head);
printf("按z返回主菜单:");
scanf("%c",&ch);
if(ch=='z')
{
goto z;
}
break;
case 7 :exit(0);
}
}
}
void creat(struct song **head)
{
struct song *p=(*head)->next;
int j=1;
if(p == NULL)
{
printf("保存失败");
}
FILE *fp=fopen(ITEM,"w");
if(fp == NULL)
{
printf("打开文件失败\n");
return;
}
while(p!=NULL)
{
fprintf(fp,"第%d首歌的信息如下:\n",j);
fprintf(fp,"名字 发行公司 作者 歌手 发行年 发行月\n");
fprintf(fp,"%s %s %s %s %d %d \n",p->music,p->company,p->author,p->singer,p->tis.year,p->tis.month);
j++;
p=p->next;
}
fclose(fp);
}
struct song *shuru(struct song **head) //对歌曲的输入
{
printf("***********输入**********\n");
int i,n;
*head = (struct song *)malloc(sizeof(struct song));
struct song *p=*head, *shang;
shang = (struct song *)malloc(sizeof(struct song));
(*head)->next=NULL;
printf("准备输入歌曲的数目:");
scanf("%d",&n);
printf("输入歌曲信息:\n");
for(i = 1;i<=n;i++)
{
p=(struct song *)malloc(sizeof(struct song));
shang->next = p;
printf("输入第%d歌曲的名字:",i);
scanf("%s",p->music);
printf("输入第%d歌曲的发行公司:",i);
scanf("%s",p->company);
printf("输入第%d歌曲的作者:",i);
scanf("%s",p->author);
printf("输入第%d歌曲的歌手:",i);
scanf("%s",p->singer);
printf("输入第%d歌曲的发行年:",i);
scanf("%d",(&p->tis.year));
printf("输入第%d歌曲的发行月:",i);
scanf("%d",(&p->tis.month));
(*head)->next=p;
shang = p;
}
printf("输入结束\n");
creat(head);
printf("保存成功!\n");
return *head;
}
void charu(struct song **head) //插入歌曲信息
{
void liulan(struct song **head);
printf("***********插入**********\n");
int i,j=1;
struct song *q,*p=(*head)->next;
printf("输入要插入歌曲的位置:\n");
scanf("%d",&i);
while(p->next&&j<i)
{
p=p->next;
j++;
}
if(p->next == NULL||j>i)
printf("未找到该结点\n");
else
{
q=(struct song *)malloc(sizeof(struct song));
printf("输入插入歌曲的名字:");
scanf("%s",q->music);
printf("输入插入歌曲的发行公司:");
scanf("%s",q->company);
printf("输入插入歌曲的作者:");
scanf("%s",q->author);
printf("输入插入歌曲的歌手:");
scanf("%s",q->singer);
printf("输入插入歌曲的发行年:");
scanf("%d",&(q->tis.year));
printf("输入插入歌曲的发行月:");
scanf("%d",&(q->tis.month));
q->next=p->next;
p->next=q;
}
printf("插入后的目录为\n");
liulan(head);
}
void shanchu(struct song **head) //删除某个歌曲信息
{
void liulan(struct song **head);
printf("***********删除**********\n");
int i,j=1;
struct song *q,*p=(*head)->next;
printf("输入要删除歌曲的位置:\n");
scanf("%d",&i);
while(p&&j<i)
{
p=p->next;
j++;
}
if(p->next==NULL||j>i)
printf("没有找到要删除歌曲的位置\n");
else
{
q=(struct song *)malloc(sizeof(struct song));
q=p->next;
p->next=q->next;
free(q);
}
printf("删除后的目录为\n");
liulan(head);
}
void liulan(struct song **head) //浏览目录中的歌曲
{
printf("***********歌曲**********\n");
int j=1;
struct song *p=(*head)->next;
while(p->next!=NULL)
{
printf("第%d首歌的信息如下:\n",j);
printf("名字 发行公司 作者 歌手 发行年 发行月\n");
printf("%s %s %s %s %d %d \n",p->music,p->company,p->author,p->singer,p->tis.year,p->tis.month);
j++;
p=p->next;
}
}
void xiugai(struct song **head) //修改歌曲信息
{
void liulan(struct song **head);
printf("***********修改**********\n");
int j=1,t;
struct song *p=(*head)->next;
printf("输入要修改歌曲的位置:\n");
scanf("%d",&t);
while(p&&j<t)
{
p=p->next;
j++;
}
if(p==NULL||j>t)
printf("未找到要修改的歌曲\n");
else
{
printf("输入修改后歌曲的名字:\n");
scanf("%s",p->music);
printf("输入修改后歌曲的发行公司:\n");
scanf("%s",p->company);
printf("输入修改后歌曲的作者:\n");
scanf("%s",p->author);
printf("输入修改后歌曲的歌手:\n");
scanf("%s",p->singer);
printf("输入修改后歌曲的发行年:\n");
scanf("%d",p->tis.year);
printf("输入修改后歌曲的发行月:\n");
scanf("%d",p->tis.month);
}
printf("修改后的目录为\n");
liulan(head);
}
void fenzu(struct song **head)
{
printf("***********分组**********\n");
int i,j,n,m,t=1;
char a[20]="\0";
struct song *p=(*head)->next;
printf("1.按歌手分组\n");
printf("2.按发行公司分组\n");
printf("请输入要分组的类型:");
scanf("%d",&j);
printf("请输入分组前几首歌:");
scanf("%d",&n);
printf("请输入分组信息:");
scanf("%s",a);
switch(j)
{
case 1:
for(i=0;i<n;i++)
{
if(strcmp(p->singer,a)==0)
{
printf("第%d首歌的信息为:\n",t);
printf("%s %s %s %s %d %d\n",p->music,p->singer,p->company,p->author,p->tis.year,p->tis.month);
t++;
p=p->next;
}break;
}
case 2:
for(i=0;i<n;i++)
{
if(strcmp(p->company,a)==0)
{
printf("第%d首歌的信息为:\n",t);
printf("%s %s %s %s %d %d\n",p->music,p->singer,p->company,p->author,p->tis.year,p->tis.month);
t++;
p=p->next;
}
}
}
}
为什么不能保存到文件
求指教