Created
June 5, 2014 08:34
-
-
Save kimitoboku/48e880ca8516e8ebcee7 to your computer and use it in GitHub Desktop.
music data list print shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<string.h> | |
#include<stdlib.h> | |
#include<ctype.h> | |
#include<sys/types.h> | |
#include<sys/stat.h> | |
typedef struct { | |
char name[200]; | |
char artist[200]; | |
int rate; | |
}songdata; | |
void init(songdata array[],int n); //ソング構造体の初期化 | |
void sort_artist(songdata array[],int f,int l); //アーティスト順でのソート | |
void sort_name(songdata array[],int max); //曲名順でのソート | |
void sort_rate(songdata array[],int max); //レートでのソート | |
int set_rate(songdata array[],char *name,int r,int n); //曲にレートをつける | |
int remove_song(songdata array[],int last); //曲を削除する | |
void print_data(songdata array[],int n); //songdataの内容を出力する | |
void add_data(songdata array[],int n); //listにsongdataを追加する | |
int main(void){ | |
songdata list[100]; | |
init(list,100); | |
FILE *fp; | |
char buf[200]; | |
int n = 0; | |
struct stat st; | |
int ret; | |
ret = stat("list.txt",&st); | |
if(0==ret){ | |
if((fp = fopen("list.txt","r")) == NULL){ | |
printf("Can't open file"); | |
exit(1); | |
} | |
char ne[200]; | |
char ar[200]; | |
int r; | |
while(fgets(buf,200,fp) != NULL){ | |
sscanf(buf,"%s %s %d\n",ne,ar,&r); | |
strcpy(list[n].name,ne); | |
strcpy(list[n].artist,ar); | |
list[n].rate = r; | |
n++; | |
} | |
fclose(fp); | |
}else{ | |
printf("Create new artist list\n"); | |
} | |
char cmd[20]; | |
int error; | |
while(1){ | |
printf("command? > "); | |
scanf("%s",cmd); | |
getchar(); | |
if(!strcmp(cmd,"artist_sort") || !strcmp(cmd,"as")){ | |
sort_artist(list,0,n); | |
print_data(list,n); | |
}else if(!strcmp(cmd,"exit") || !strcmp(cmd,"q")){ | |
if(ret == 0){ | |
error = remove("list.txt"); | |
if(error == -1){ | |
printf("can't remove list.txt"); | |
exit(1); | |
} | |
} | |
if((fp = fopen("list.txt","a++")) == NULL){ | |
printf("Can't open file"); | |
exit(1); | |
} | |
int i; | |
for(i=0;i<n;i++){ | |
fprintf(fp,"%s %s %d\n",list[i].name,list[i].artist,list[i].rate); | |
} | |
printf("exit.\n"); | |
return 0; | |
}else if(!strcmp(cmd,"name_sort") || !strcmp(cmd,"ns")){ | |
sort_name(list,n); | |
print_data(list,n); | |
}else if(!strcmp(cmd,"add")){ | |
add_data(list,n); | |
n++; | |
}else if(!strcmp(cmd,"rate_sort") || !strcmp(cmd,"rs")){ | |
sort_rate(list,n); | |
print_data(list,n); | |
}else if(!strcmp(cmd,"norate")){ | |
int i; | |
for(i=0;i<n;i++){ | |
if(list[i].rate == 99){ | |
printf("%s %s\n",list[i].name,list[i].artist); | |
} | |
} | |
}else if(!strcmp(cmd,"print")){ | |
print_data(list,n); | |
}else if(!strcmp(cmd,"remove")||!strcmp(cmd,"rm")){ | |
int err = remove_song(list,n); | |
if(err == 0){ | |
n--; | |
}else{ | |
printf("Can't remove this song\n"); | |
} | |
}else if(!strcmp(cmd,"help")||!strcmp(cmd,"h")){ | |
printf("Commands:\n\thelp or h\t\t:this command\n\tprint \t\t\t:print music list\n\tnorate\t\t:print no rateed music\n\tadd\t\t\t:add new music\n\tname_sort or ns\t\t:sort of name and print\n\treta_sort or rs\t\t:sort of rate and print\n\tartist_sort or as\t:sor of artist and print\n\tset_rate or sr\t\t:set rate as music\n\tremove or rm\t\t:remove music\n\texit or q\t\t: exit\n"); | |
}else if(!strcmp(cmd,"set_rate")||!strcmp(cmd,"sr")){ | |
char name[200]; | |
int rate; | |
printf("Please Input Set Rate Music Name: "); | |
scanf("%s",name); | |
printf("Plaese Input Set Rate: "); | |
scanf("%d",&rate); | |
puts("-"); | |
if(0 == set_rate(list,name,rate,n)){ | |
printf("Seted\n"); | |
}else{ | |
printf("Can't set rate"); | |
} | |
}else{ | |
printf("Command Not Found!\n"); | |
} | |
} | |
return 0; | |
} | |
void add_data(songdata array[],int n){ | |
char ne[200]; | |
char ar[200]; | |
printf("Please Input Music Name:"); | |
scanf("%s",ne); | |
getchar(); | |
printf("Please Input Music Artist:"); | |
scanf("%s",ar); | |
getchar(); | |
strcpy(array[n].name,ne); | |
strcpy(array[n].artist,ar); | |
} | |
void print_data(songdata array[],int n){ | |
int i; | |
for(i=0;i<n;i++){ | |
printf("%s\t%s\t%d\n",array[i].name,array[i].artist,array[i].rate); | |
} | |
} | |
void init(songdata array[],int n){ | |
int i; | |
for(i=0;i<n;i++){ | |
strcpy(array[i].name,""); | |
strcpy(array[i].artist,""); | |
array[i].rate = 99; | |
} | |
} | |
void sort_artist(songdata array[],int f,int l){ | |
char aartist[200]; | |
char bartist[200]; | |
int i,j,k; | |
songdata buf; | |
int cmp; | |
for(i=f;i<l-1;i++){ | |
for(j=i+1;j<l;j++){ | |
strcpy(aartist,array[i].artist); | |
strcpy(bartist,array[j].artist); | |
for(k=0;aartist[k];k++){ | |
aartist[k] = toupper(aartist[k]); | |
} | |
for(k=0;bartist[k];k++){ | |
bartist[k] = toupper(bartist[k]); | |
} | |
cmp = strcmp(aartist,bartist); | |
if(cmp>0){ | |
buf = array[i]; | |
array[i] = array[j]; | |
array[j] = buf; | |
} | |
} | |
} | |
} | |
void sort_name(songdata array[],int max){ | |
char aname[200]; | |
char bname[200]; | |
int i,j,k; | |
songdata buf; | |
int cmp; | |
for(i=0;i<max-1;i++){ | |
for(j=i+1;j<max;j++){ | |
strcpy(aname,array[i].name); | |
strcpy(bname,array[j].name); | |
for(k=0;aname[k];k++){ | |
aname[k] = toupper(aname[k]); | |
} | |
for(k=0;bname[k];k++){ | |
bname[k] = toupper(bname[k]); | |
} | |
cmp = strcmp(aname,bname); | |
if(cmp>0){ | |
buf = array[i]; | |
array[i] = array[j]; | |
array[j] = buf; | |
} | |
} | |
} | |
} | |
void sort_rate(songdata array[],int max){ | |
int i,j; | |
songdata buf; | |
for(i=0;i<max;i++){ | |
for(j=i+1;j<max;j++){ | |
if(array[i].rate>array[j].rate){ | |
buf = array[i]; | |
array[i] = array[j]; | |
array[j] = buf; | |
} | |
} | |
} | |
int let; | |
int fast=0; | |
for(i=0;i<max+1;i++){ | |
if(fast!=array[i].rate){ | |
sort_artist(array,fast,i-1); | |
fast = i; | |
} | |
} | |
} | |
int set_rate(songdata array[],char *name,int r,int n){ | |
int i; | |
if(r<0 || 5<r){ | |
r = 99; | |
} | |
for(i=0;i<n;i++){ | |
if(!strcmp(array[i].name,name)){ | |
array[i].rate = r; | |
return 0; | |
} | |
} | |
return 1; | |
} | |
int remove_song(songdata array[],int last){ | |
int i; | |
char name[200]; | |
printf("Pleas Input Del Song Name: "); | |
scanf("%s",name); | |
getchar(); | |
for(i=0;i<last+1;i++){ | |
if(!strcmp(array[i].name,name)){ | |
array[i] = array[last]; | |
strcpy(array[last].name,""); | |
strcpy(array[last].artist, ""); | |
array[last].rate = 99; | |
return 0; | |
} | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment