手里有份编程题 自己写出来了 但是有错纠不了~ 求个C语言高手帮解决下~ 新手初来乍到 谢谢帮忙~
#include <stdio.h>#include <stdlib.h>
#include <string.h>
typedef struct //定义一个结构体
{
char song[100];
char singer[10];
int number;
char special[10];
char genre[10];
}KTVSong;
//定义一个静态的常量,记录songs数组中元素的个数,歌曲所在的位置可以用KTVsong里面的number控制。
int menu(); //声明函数
void choose(); //声明函数
void add(); //声明函数
void edt(); //声明函数
void del(); //声明函数
void req(); //声明函数
void bro(KTVSong); //声明函数
int main()//主函数
{
KTVSong songs[100];
choose(menu());
//用到bro的时候传songs进去
}
int menu()//菜单功能函数
{
int n;
printf("\npress any key to enter the menu");
getch();
system("cls");
printf("*********************************************************************\n");
printf("\t\t\tWELCOME TO KTV SYSTEM\n");
printf("**********************************MENU*******************************\n");
printf("\t\t\tINPUT 1 TO ADD SONGS\n");
printf("\t\t\tINPUT 2 TO EDIT SONGS\n");
printf("\t\t\tINPUT 3 TO DELETE SONGS\n");
printf("\t\t\tINPUT 4 TO REQUEST SONGS\n");
printf("\t\t\tINPUT 5 TO BROWSE SONGS\n");
printf("\t\t\tINPUT 6 TO EXIT");
printf("*********************************************************************\n");
do
{
printf("\n\n\t\t\tENTER YOUR CHOICE(1-6):");
scanf("%d",&n);
}while(n < 1 || n > 6);
return(n);
}
void choose(int i)//选择功能函数
{
if(i == 1)
add();
else if(i == 2)
edt();
else if(i==3)
del();
else if(i==4)
req();
else if(i==5)
bro();
else
exit(0);
}
void add()//添加功能函数
{
int i = 0;
char src_address[50] , tar_address[50] , ch;
KTVSong songs[100];
FILE *infile , *outfile;
do
{
flushall();
printf("\nPlease input the singer of the song:");
gets(songs[i].singer);
flushall();
printf("\nPlease input the name of the song:");
gets(songs[i].song);
songs[i].number = i;
flushall();
printf("\nPlease input the special of the song:");
gets(songs[i].special);
flushall();
printf("\nPlease input the genre of the song:");
gets(songs[i].genre);
flushall();
printf("\nPlease input the address(the lyric of the song,for example D:\\songs\\hello.txt):");
gets(src_address);
flushall();
printf("Please choose the address that your want to save the lyric(for example E:\\songs):");
gets(tar_address);
infile = fopen(src_address,"r");
outfile = fopen(strcat(strcat(strcat(tar_address , "\\"), songs[i].song),".txt"),"w");
if(!infile || !outfile)
{
printf("Save the lyric failed!");
exit(1);
}
while(!feof(infile))
{
ch = fgetc(infile);
if(ch == EOF)
break;
fputc(ch , outfile);
}
fclose(infile);
fclose(outfile);
printf("Lyric added successfully!\n");
i++;
}while(menu() == 1);
}
void edt()//编辑功能函数
{
bro();
//属性修改就行。
//歌词,删掉重新添加一个。(可以把删除、添加歌词做成一个单独的函数);
}
void del()//删除功能函数
{
char file[100];
bro();
printf("Please choose the song'number that your want to delete:");
//删除,并重新调整songs数组,同时删掉对应的歌词。歌词路径通req()函数中的处理。
if(remove(file) == 0)
printf("Removed %s\n",file);
else
perror("remove");
}
void req()//点歌功能函数
{
FILE *infile;
bro(songs);//传songs进来
printf("Please choose the song'number that your want to request:");//根据编号点歌,同理可以多些几个函数在这调用。
//根据选择的条件,找到歌词文件的路径,解决办法:可以在上面的结构体中加一个属性,lyric_address[30]记录歌词存放的路径,方便查询和删除。
infile = fopen(src_address,"r");
if(!infile)
{
printf("Read the lyric failed!");
exit(1);
}
while(!feof(infile))
{
ch = fgetc(infile);
if(ch == EOF)
break;
printf("%s",ch);;
}
fclose(infile);
}
void bro(KTVSong) //浏览功能函数
{
//编号 专辑 歌手 名称(按照编号。。。可以多些几个函数,方便分类查询)
int i;
printf("Please choose the condition:\n");
printf("Query by singer to input 1\n");
printf("Query by special to input 2\n");
printf("Query all to input Enter\n");
while(getch() != 1 && getch() != 2)
{
printf("Bad choosen!\n");
printf("Input the choosen that you want:");
}
if(getch() == '\n')//可以单独写成函数,这里调用
{
for(i = 0 ; i < KTVSong.lenth ; i++) //KTVSong.lenth用上面定义那个静态常量代替
{
printf("*\t%s\t",songs[i].song);
printf("%s\t",songs[i].singer);
printf("%s\t",songs[i].number);
printf("%s\t",songs[i].special);
printf("%s\n",songs[i].genre);
}
}
else if(getch() == 1) //可以单独写成函数,这里调用
{
char singer[30];
printf("Please input the singer:");
for(i = 0 ; (singer[i] = getch()) != '\n' ; i++);
for(i = 0 ; i < KTVSong.lenth ; i++) //KTVSong.lenth用上面定义那个静态常量代替
{
if(songs[i].singer.equals(singer))
{
printf("*\t%s\t",songs[i].song);
printf("%s\t",songs[i].number);
printf("%s\t",songs[i].special);
printf("%s\n",songs[i].genre);
}
}
}
else if(getch()==2) //可以单独写成函数,这里调用
{
char special[30];
printf("Please input the special:");
for(i = 0 ; (special[i] = getch()) != '\n' ; i++);
for(i = 0 ; i < KTVSong.lenth ; i++) //KTVSong.lenth用上面定义那个静态常量代替
{
if(songs[i].special.equals(special))
{
printf("*\t%s\t",songs[i].song);
printf("%s\t",songs[i].singer);
printf("%s\t",songs[i].number);
printf("%s\n",songs[i].genre);
}
}
}
}