菜鸟呼喊----已经很努力了,求助!!
请高手指点!!!错误处:
查询音乐信息
求助:
缺少一个按姓名排序的程序
谢谢!!!
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define N 10
struct music /*声明 struct music 的类型*/
{ char mname[10]; char name[10];
char year[10]; char style[10];
};
void myprint(); /*函数原型说明*/
mycreat(struct music *p,int n);
void mydisplay(struct music *p,int n);
void mysearch(struct music *p,int n);
void mymodify(struct music *p,int n);
void myadd(struct music *p,int n);
void mydelete(struct music *p,int n);
void main() /*主函数*/
{ char choose,yes_no;
struct music record[N]; /*将存放N个音乐信息*/
do
{myprint(); /*显示菜单*/
printf("\n");
printf("请输入要选择的选项:"); choose=getche();
switch(choose)
{ case '1': mycreat(record,N); break; /*创建*/
case '2': mydisplay(record,N); break; /*显示*/
case '3': mysearch(record,N); break; /*查询*/
case '4': mymodify(record,N); break; /*修改*/
case '5': myadd(record,N); break; /*添加*/
case '6': mydelete(record,N); break; /*删除*/
case '0': break;
default : printf("\n %c为非法选项!\n",choose);
}
if(choose=='0') {printf("\n"); break;}
printf("\n 要继续选择吗(Y/N)?\n");
do
{ yes_no=getch();
} while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no=='Y'||yes_no=='y');
}
void myprint() /*显示菜单*/
{printf("\n\n\n\n\n\n\n");
printf(" ★★★★★★音乐信息管理系统★★★★★★\n");
printf(" § 1.创建音乐信息 §\n");
printf(" § 2.显示音乐信息 §\n");
printf(" § 3.查询音乐信息 §\n");
printf(" § 4.修改音乐信息 §\n");
printf(" § 5.添加音乐信息 §\n");
printf(" § 6.删除音乐信息 §\n");
printf(" § 0.退出 §\n");
printf(" ★★★★★★★★★★★★★★★★★★★★\n");
}
mycreat(struct music *p,int n) /*创建*/
{ int i=1; FILE *fp;
char sign;
static int k;
fp=fopen("d:\\information.txt","w");
while(sign!='n'&&sign!='N')
{printf("\n");
printf("请输入第%d个记录:\n",i);
printf("歌曲名:"); scanf("%s",p->mname);
printf("演唱者:"); scanf("%s",p->name);
printf("发行年:"); scanf("%s",p->year);
printf("音乐类型:"); scanf("%s",p->style);
fprintf(fp,"%10s%10s%10s%10s\n",p->mname,p->name,p->year,p->style);
p++; i++;
printf("\n是否继续输入?(Y/N)");
scanf("%s",&sign);
}
n=i;
fclose(fp);
return n;
}
void mydisplay(struct music *p,int n) /*显示*/
{ FILE *fp;
fp=fopen("d:\\information.txt","r");
if(fp==NULL) {printf("\n不能找到此文件\n"); return;}
printf("\n");
printf(" 歌曲名 演唱者 发行年 音乐类型\n");
while(n>0)
{fscanf(fp,"%10s%10s%10s%10s",p->mname,p->name,p->year,p->style);
printf("%10s%10s%10s%10s\n",p->mname,p->name,p->year,p->style);
p++; n--;
}
fclose(fp);
}
void mysearch(struct music *p,int n) /*查询*/
{ int i; FILE *fp;
char mnam[10];
fp=fopen("d:\\information.txt","r");
if(fp==NULL) {printf("\n不能找到此文件\n"); return;}
printf("\n请输入要查询的歌曲名:");
scanf("%s",&mnam);
for(i=0;i<n;i++,p++)
{if(strcmp(mnam,p->mname)==0)
break;
}
printf("\n");
printf(" 歌曲名 演唱者 发行年 音乐类型\n");
printf("%10s%10s%10s%10s\n",p->mname,p->name,p->year,p->style);
fclose(fp);
if(i==n)
printf("无此歌曲!\n");
}
void mymodify(struct music *p,int n) /*修改*/
{ int i; FILE *fp;
char Mnam[10];
fp=fopen("d:\\information.txt","w");
if(fp==NULL) {printf("\n不能找到此文件\n"); return;}
printf("\n请输入要修改的歌曲名:");
do
{gets(Mnam);
}while(strcmp(Mnam,"")==0);
for(i=0;i<n;i++,p++)
if(strcmp(Mnam,p->mname)==0) break;
if(i==n) printf("无此歌曲!\n");
else
{printf("请输入正确的演唱者:");
do
{gets(p->name);
}while(strcmp(p->name,"")==0);
printf("请输入正确的发行年:"); gets(p->year);
printf("请输入正确的音乐类型:"); gets(p->style);
}
fclose(fp);
}
void myadd(struct music *p,int n) /*添加*/
{FILE *fp; int i=0;
printf("\n");
fp=fopen("d:\\information.txt","w");
if(fp==NULL) {printf("\n不能找到此文件\n"); return;}
printf("\n");
printf("请输入增加的歌曲名:"); gets(p->mname);
printf("演唱者:"); gets(p->name);
printf("发行年:"); gets(p->year);
printf("音乐类型:");gets(p->style);
n++;
for(i=0;i<n;i++)
fprintf(fp,"%10s%10s%10s%10s\n",p->mname,p->name,p->year,p->style);
fclose(fp);
}
void mydelete(struct music *p,int n) /*删除*/
{FILE *fp;
char MName[10]; int i,j;
printf("\n");
printf("请输入要删除的歌曲名称:"); scanf("%s",&MName);
fp=fopen("d:\\information.txt","w");
if(fp==NULL) {printf("\n不能找到此文件\n"); return;}
for(i=0;i<n;i++)
{if(strcmp(MName,p->mname)==0) break;
p++;
}
if(i==n)
printf("没有找到你要删除的歌曲!\n");
else
{strcpy(p->mname,"");
strcpy(p->name,"");
strcpy(p->year,"");
strcpy(p->style,"");
n--;
for(i=0;i<n;i++)
fprintf(fp,"%10s%10s%10s%10s\n",p->mname,p->name,p->year,p->style);
printf("此歌曲已删除!\n");
}
fclose(fp);
}