這個代碼因為配置信息的原因不能直接整個拿去用(o゜▽゜)o☆
這個代碼因為配置信息的原因不能直接整個拿去用(o゜▽゜)o☆
這個代碼因為配置信息的原因不能直接整個拿去用(o゜▽゜)o☆
只能提供一個思路(其實也可以把里面函數拿去用)ˋ( ° ▽、° )
(ง •_•)ง
這個小系統包含一些除任務要求外額外附加的功能
本來做出來差不多是這個效果基本上來說比任務要求多加了:
{ 1.導入音樂文件並播放;2簡單的圖形化界面;3.模糊查詢功能;}
所有信息的顯示與輸入都是在dos窗口完成,圖形界面實質只是提供了更加便利調用函數的方式(偽圖形化操作)ˋ( ° ▽、° )
(納個suprise其實就是隨機播放一首導入的音樂。)
當時使用codeblocks(現在早就轉VS) Win32 GUI project 項目加上制作圖形的插件。
因為時間較長,只記得當時這個軟件配置比較麻煩。。就在這只分享一些源碼。
首先文件總體分為三部分(因為用到了這個圖形化界面所以多出了main.cpp)
簡單說一下程序的邏輯:開啟程序 --> 讀取磁盤上的配置文件以檢測是否是第一次使用該系統以跳轉不同管理模式 --> 第一次使用要求設置密碼或者限制一些權限。
創建鏈表並從文件讀取信息(其實這部沒有必要,之間對文件進行操作即可,沒必要單獨創建鏈表把文件中的信息掛載上來)
接下來就是一些查刪改等基礎操作
先介紹核心部分 ——resource.cpp

1 #include<string.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #include<stdio.h> 5 #include"resource.h" 6 #include<stdbool.h> 7 #include <windows.h> 8 #include<io.h> 9 10 extern int N1; // 虛擬音樂信息數目 11 extern int N2; // 音樂文件數目//不超過10個 12 char add[10][50]={0};// 音樂文件地址 13 FILE *file1;// 內置默認文件 //moren.txt 14 FILE *file2;// 用戶更改的信息庫//data.txt 15 extern char code[11];// 記錄密碼 16 extern MUSIC *head; 17 extern bool FFLAG;//FFLAG 是否第一次使用該系統 18 extern bool FLAG;//FLAG 是否擁有管理員權限 19 extern FILE *fp; 20 MUSIC *nowlist;// 鏈表最后一個節點 21 MUSIC *now;// 在鏈表中移動的節點 22 void print_help(void)// 幫助按鈕 23 { 24 printf("請通過按鈕選擇你所需要的功能\n" 25 "ps: 1.第一次使用,請先初始化並刷新音樂庫\n" 26 " 2.若要播放音樂,請先刷新音樂庫\n" 27 " 4.通過CLEAR按鍵清除屏幕\n" 28 " 3.若要退出系統請先關閉圖形化窗口,不要直接關閉dos窗口\n"); 29 } 30 31 void Scanf(MUSIC *now)// 讀取MUSIC 32 { 33 scanf("%d%s%s%s",&canshu); 34 } 35 void Printf(MUSIC *now)// 打印MUSIC 36 { 37 printf("%7d%10s%10s%10s\n",canshu); 38 } 39 void Fscanf(MUSIC *now,FILE *nowf)// 從文件中獲取信息 40 { 41 fscanf(nowf,"%d%s%s%s",&canshu); 42 } 43 void Fprint(MUSIC *now,FILE *nowf)// 將輸入的信息保存到文件中 44 { 45 fprintf(nowf,"%d %s %s %s ",canshu); 46 } 47 void Fprint1(MUSIC *now,FILE *nowf)// 將輸入的信息保存到文件中 48 { 49 fprintf(nowf,"%d %s %s %s\n",canshu); 50 } 51 void chushihua1(void)// 預置信息 52 { 53 printf("第一次使用,請先設置密碼\n"); 54 setcode(); 55 CreatList(head,3,0); 56 } 57 void chushihua(void)// 初始化按鍵功能 58 { 59 if(FFLAG==true)printf("你還沒有管理員權限,請先登錄\n"); 60 else{chushihua1();return;} 61 if(FLAG)//如果有權限 62 { 63 fp=fopen("C:\\Users\\EA\\Desktop\\date.txt","w"); 64 chushihua1(); 65 fclose(fp); 66 } 67 else printf("你還沒有管理員權限,請先登錄\n"); 68 } 69 void setcode(void)// 設置密碼 70 { 71 scanf("%s",code); 72 printf("密碼設置成功!!\n"); 73 } 74 void onling(void)// 管理員登陸 75 { 76 char nowcode[11]={0}; 77 printf("請輸入密碼\n"); 78 scanf("%10s",nowcode); 79 while(!Strcmp(code,nowcode,strlen(code),strlen(nowcode))) 80 {////如果密碼正確 81 printf("密碼錯誤,請再次輸入" 82 "若取消登陸,請輸入Q\n"); 83 memset(nowcode,0,sizeof(nowcode)); 84 scanf("%s",nowcode); 85 if(*nowcode=='Q'&&strlen(nowcode)==1)break;//如果用戶選擇退出 86 } 87 if(strlen(nowcode)!=1){printf("登陸成功\n");FLAG=true;} 88 } 89 int Strcmp(const char *x,const char *y,int n,int m)// 密碼比較 90 { 91 int i=0; 92 if(n!=m)return 0; 93 while(*x==*y&&*x&&*y){i++;x++;y++;} 94 if(i==m)return 1; 95 return 0; 96 } 97 void play_sound(void)// 隨機播放音樂 98 { 99 srand(time(NULL)); 100 PlaySound(TEXT(add[rand()%N2]),NULL, SND_ASYNC | SND_NODEFAULT); 101 } 102 void CreatList(MUSIC *head,int n,int m)// 創建鏈表,並保存數據 103 { 104 N1+=n;// 虛擬信息數目 105 int i = 0;// 當m==0初始化讀取預置信息,否則創建新的節點 106 if(m==0){file1=fopen("C:\\Users\\EA\\Desktop\\moren.txt","r");nowlist = (MUSIC*)head;} 107 else{nowlist->next = (MUSIC *)malloc(sizeof(MUSIC));nowlist=nowlist->next;} 108 for(i=0; i<n; i++) 109 { 110 if(m==0)Fscanf(nowlist,file1);//從預置信息文件讀取信息 111 else Scanf(nowlist); 112 if(i<n-1)// 當節點數量小於所需數量 113 { 114 nowlist ->next =(MUSIC *)malloc(sizeof(MUSIC)); 115 nowlist = nowlist ->next; 116 } 117 } 118 nowlist->next=NULL; 119 if(m==0)fclose(file1); 120 //fclose(file2); 121 } 122 void load_music_info(void)// 在鏈表后加入新節點 123 { 124 int n=0; 125 printf("請輸入你想要導入音樂信息的數量:"); 126 scanf("%d",&n); 127 printf("請按以下格式輸入信息:\n" 128 "0001 musicname singer album\n"); 129 CreatList(nowlist,n,1); 130 } 131 132 void search_actual(int x)// 尋找真實具有的音樂 133 { 134 N2=0;// 將文件數目初始化為0 135 long hand; 136 struct _finddata_t fileinfo; 137 int i=0; 138 char fileadd[]="C:\\Users\\EA\\Music\\shili\\";//在該文件下搜索 139 char fileformat[]="*.wav";// 搜索該格式文件 140 char name[100]; 141 strcpy(name,fileadd); 142 strcat(name,fileformat); 143 hand=_findfirst(name,&fileinfo); 144 if(hand!=-1L) 145 { 146 if(x!=0)printf("音樂庫有以下音樂文件:\n"); 147 do 148 { 149 strcpy(name,fileadd); 150 strcat(name,fileinfo.name); 151 if(x!=0)printf("%s\n",fileinfo.name); 152 strcpy(add[i++],name);// 把音樂文件路徑保存下來 153 N2++;// 如果掃描到一個,則真實文件數目加1 154 }while(_findnext(hand,&fileinfo)==0); 155 } 156 // if(x!=0)else{printf("no sereach !!!\n");} 157 for(i=0; i<N2; i++)abss(add[i],add[i]); 158 } 159 void abss(char *x,char*z)// 轉化成絕對地址 160 { 161 int k=0; 162 char b[100] = {0}; 163 for(; (*x)!=0; x++,k++) 164 { 165 if((*x)!='\\')b[k]=*x; 166 else{b[k++]='\\';b[k]='\\';} 167 } 168 b[k]=0;strcpy(z,b); 169 } 170 171 void chudu(void)// 從系統文件加載信息 172 { 173 file2=fopen("C:\\Users\\EA\\Desktop\\date.txt","r"); 174 int i = 0; 175 MUSIC *nowlist = (MUSIC*)head;// 把磁盤信息導入到鏈表中 176 for(i=0; i<N1; i++) 177 { 178 Fscanf(nowlist,file2); 179 if(i<N1-1) 180 { 181 nowlist ->next =(MUSIC *)malloc(sizeof(MUSIC)); 182 nowlist = nowlist ->next; 183 } 184 } 185 nowlist->next=NULL; 186 fclose(file2); 187 } 188 void view_all(void)// 顯示所有鏈表信息 189 { 190 printf("共有%d條音樂信息!!\n",N1); 191 now = (MUSIC *)head; 192 while(now) 193 { 194 printf("%04d%12s%12s%12s\n",canshu); 195 now = now->next; 196 } 197 } 198 void revise(void)// 修改音樂信息 199 { 200 int n; 201 int m=0; 202 printf("請輸入你想要修改的音樂編號;"); 203 scanf("%d",&n); 204 now = (MUSIC *)head; 205 while(now) 206 { 207 if(now->next->number==n) 208 { 209 printf("請輸入更改后的信息;\n"); 210 Scanf(now->next);m=1;break; 211 } 212 now = now->next; 213 } 214 if(m==0)printf("no search!!\n"); 215 else printf("修改成功!!\n"); 216 } 217 int searchname(int x,int y)// 系列查找 218 { 219 int i=0; 220 int m=0; 221 char name1[20]={0}; 222 if(x==1)printf("請輸入你想要查詢的歌手:"); 223 else 224 { 225 if(y==1)printf("請輸入你想要播放的音樂名:"); 226 else printf("請輸入你想要查詢的音樂名:"); 227 } 228 scanf("%s",name1); 229 now = (MUSIC *)head; 230 while(now) 231 { 232 if(x==1) 233 {if(Strcmp(now->S_N,name1,strlen(now->S_N),strlen(name1))) 234 {Printf(now);m=1;break;}} 235 else 236 { 237 if(Strcmp(now->M_N,name1,strlen(now->M_N),strlen(name1))) 238 {Printf(now);m=1;if(y==1)return i;break;}//當x==0,y==1,返回查找所要播放音樂文件的地址下標 239 } 240 now = now->next; 241 i++; 242 } 243 if(m==0)printf("no search!!\n"); 244 return -1; 245 } 246 void play(void)// 輸入音樂名播放音樂 247 { 248 int i=-1; 249 i = searchname(0,1); 250 if(i!=-1){printf("請欣賞!!\n");PlaySound(TEXT(add[i]),NULL, SND_ASYNC | SND_NODEFAULT);} 251 } 252 void searchkey(void)// 關鍵字查找 253 { 254 int i=0; 255 int m=0; 256 char name1[20]={0}; 257 printf("請輸入你想查找的關鍵字:"); 258 scanf("%s",name1); 259 now = (MUSIC *)head; 260 while(now) 261 { 262 if(strstr(now->M_N,name1)||strstr(now->S_N,name1)) 263 {Printf(now);i++;m=1;} 264 now = now->next; 265 } 266 printf("共找到了%d條相關信息!!\n",i); 267 if(m==0)printf("未找到\n"); 268 } 269 270 void wenjian(void) 271 { 272 printf("請把音樂文件放入C:\\Users\\EA\\Music\\shili\n"); 273 printf("請按以下格式輸入你導入音樂信息:\n" 274 "0001 musicname singer album\n"); 275 charu(1);// 在真實音樂文件信息后插入 276 } 277 void charu(int x) 278 { 279 now = (MUSIC*)head; 280 MUSIC *nowlist2 = (MUSIC*)malloc(sizeof(MUSIC));// 要插入的節點信息 281 int i=0; 282 if(x==1)for(i=0; i<N2-1; i++)now=now->next;//在預置音樂后插入真實音樂文件信息 283 else 284 { 285 int n; 286 printf("請輸入你想要插入的位置:"); 287 scanf("%d",&n); 288 if(n<N2){printf("前%d個為系統預置,不能插入!!\n",N2);return;} 289 for(i=0; i<n-1; i++)now=now->next; 290 printf("請輸入音樂信息:\n"); 291 292 } 293 Scanf(nowlist2); 294 nowlist2 ->next = now->next; 295 now ->next = nowlist2; 296 if(x==1) 297 { 298 file1=fopen("C:\\Users\\EA\\Desktop\\moren.txt","a+"); 299 fprintf(file1,"%04d %s %s %s\n",nowlist2->number,nowlist2->M_N,nowlist2->S_N,nowlist2->album); 300 fclose(file1); 301 } 302 if(x!=1){N1++;printf("插入成功!!\n");} 303 if(x==1)printf("保存成功!!\n"); 304 if(x==1)search_actual(0); 305 } 306 void sc(void) 307 { 308 int m=0; 309 now = (MUSIC*)head; 310 MUSIC *nowlist2; 311 int num; 312 printf("請輸入你要刪除音樂的編號:"); 313 scanf("%d",&num); 314 if(num<=N2){printf("預置音樂無法刪除!!\n");return;} 315 while(now) 316 { 317 if(now->next->number==num) 318 { 319 nowlist2 = now->next; 320 now->next = nowlist2->next; 321 free(nowlist2);m=1; 322 break; 323 } 324 now = now->next; 325 } 326 if(m==1)printf("刪除成功!!\n"); 327 else printf("no search!!\n"); 328 } 329 void baocun(void) 330 { 331 file2=fopen("C:\\Users\\EA\\Desktop\\date.txt","w"); 332 now = (MUSIC*)head; 333 N1=0; 334 while(now) 335 { 336 N1++;//音樂信息文件++ 337 Fprint(now,file2); 338 now = now->next; 339 } 340 fclose(file2); 341 } 342 void px(void)// 排序 343 { 344 //先把所有信息放在數組上,再對數組進行排序,再把信息返回鏈表 345 int i=0; 346 int j=0; 347 MUSIC a[N1]; 348 now = (MUSIC*)head; 349 while(now) 350 {a[i].number = now->number; 351 strcpy(a[i].M_N,now->M_N); 352 strcpy(a[i].S_N,now->S_N); 353 strcpy(a[i].album,now->album); 354 now=now->next;i++;} 355 int minnum; 356 int mins; 357 now = (MUSIC*)head; 358 MUSIC temp; 359 for(i=0; i<N1; i++)// 選擇排序 360 { 361 minnum=a[i].number; 362 mins = i; 363 for(j=i; j<N1; j++) 364 {if(a[j].number<minnum) 365 {minnum = a[j].number;mins = j;}} 366 if(i!=mins)//MUSIC類型變量交換 367 { 368 sscp(&temp,&a[mins]); 369 sscp(&a[mins],&a[i]); 370 sscp(&a[i],&temp); 371 } 372 if(i>=N2)//對除預置信息外的音樂排序 373 { 374 now->number=a[i].number; 375 strcpy(now->M_N,a[i].M_N); 376 strcpy(now->S_N,a[i].S_N); 377 strcpy(now->album,a[i].album); 378 } 379 now=now->next; 380 } 381 } 382 void sscp(MUSIC *x,MUSIC *y)// MUSCI類型賦值 383 { 384 x->number = y->number; 385 strcpy(x->M_N,y->M_N); 386 strcpy(x->S_N,y->M_N); 387 strcpy(x->album,y->M_N); 388 }
因為用到了查找磁盤上音樂文件,所以可能要用到c++函數(時間長了不確定了)
接下來是頭文件——里面包含了一些按鈕的定義是圖形化插件主動創建的,這些按鈕將在main.cpp檢測是否點擊按鈕

1 #ifndef IDC_STATIC 2 #define IDC_STATIC (-1) 3 #define canshu now->number,now->M_N,now->S_N,now->album 4 5 typedef struct MUSIC 6 { 7 int number;// 編號 8 char M_N[20];// 歌曲名 9 char S_N[20];// 歌唱者 10 char album[20];// 專輯 11 struct MUSIC *next; 12 }MUSIC; 13 int Strcmp(const char *,const char *,int ,int );// 核對密碼 //前兩個字符串。后兩個字符串長度 14 void setcode(void);// 設置密碼 15 void chushihua(void);// 系統初始化 16 void chushihua1(void);// 導入預存歌曲信息 17 void Printf(MUSIC *);// 將信息打印 18 void Fprint(MUSIC *,FILE *);// 將信息保存到文件中 19 void Scanf(MUSIC *);// 從鍵盤獲取成員信息 20 void Fscanf(MUSIC *,FILE *);// 從文件獲取成員信息 21 void onling(void);// 登陸 22 void print_help(void);// 打印幫助信息 23 void CreatList(MUSIC *,int n,int m);// 創建鏈表 //n:創建的鏈表數,m==0,導入默認信息,m==1,加入新的節點 24 void play_sound(void);// 播放音樂 25 void abss(char *,char*);// 轉化絕對地址 26 void search_actual(int);// 查看真實存在的音樂 27 void load_music_info(void);// 錄入音樂信息 28 void view_all(void);// 顯示所有音樂信息 29 void revise(void);// 修改音樂信息 30 int searchname(int x,int y);// 系列查詢 //當x==1查詢歌手,否則{當y==1時尋找要播放的音樂名,否則查詢音樂名} 31 void searchkey(void);// 模糊查找 32 void play(void);// 播放音樂 33 void chudu(void);// 從文件中讀取數據 34 void baocun(void);// 保存數據到文件 35 void charu(int x);// 插入音樂信息 //當x==1時導入真實音樂信息,否則插入虛擬信息 36 void wenjian(void);// 導入音樂文件 37 void sscp(MUSIC *x,MUSIC *y);// MUSIC類型賦值 38 void px(void);// 排序 39 void sc(void);// 刪除音樂信息 40 41 #endif 42 43 #define DLG_MAIN 100 44 #define daoruxinxi 40000 45 #define search_singer 40001 46 #define Surprise 40002 47 #define On 40003 48 #define Off 40004 49 #define search_key 40005 50 #define INITIALISE 40006 51 #define daoruwenjian 40007 52 #define xiugaixinxi 40008 53 #define onlin 40009 54 #define CLEAR 40010 55 #define HELP 40011 56 #define settcode 40012 57 #define bofangyinyue 40013 58 #define shuaxin 40014 59 #define search_name 40015 60 #define xianshixinxi 40016 61 #define charuxinxi 40018 62 #define paixu 40019 63 #define shanchuxinxi 40020
最后就是一個Win32 GUI project的框架
主要是檢測按鈕是否被按下以調用對應函數

1 #include <windows.h> 2 #include <commctrl.h> 3 #include <stdio.h> 4 #include <windef.h> 5 #include "resource.h" 6 #include <mmsystem.h> 7 int N1=0;// 虛擬文件數目 8 int N2=0;// 真實文件數目 9 bool FFLAG = false;// 是否第一次使用系統(有沒有創建數據文件) 10 bool FLAG= false;// 是否具有管理員權限 11 FILE *fp;// 配置信息文件 12 char code[11];// 保存密碼 13 MUSIC *head = (MUSIC *)malloc(sizeof(MUSIC)); 14 HINSTANCE hInst; 15 BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) 16 { 17 18 switch(uMsg) 19 { 20 case WM_INITDIALOG: 21 { 22 printf("歡迎使用歌曲管理系統!!\n"); 23 search_actual(0); 24 if((fp=fopen("C:\\Users\\EA\\Desktop\\setting.txt","r"))==NULL) 25 {FFLAG = 0;printf("您是第一次使用本系統,請先初始化。\n");} 26 else 27 {fscanf(fp,"%s%d%d",code,&N1,&N2);chudu();fclose(fp);}//從配置文件讀取信息 28 } 29 return TRUE; 30 31 case WM_CLOSE: 32 { 33 EndDialog(hwndDlg, 0); 34 baocun();// 則保存鏈表信息到文件 35 fp=fopen("C:\\Users\\EA\\Desktop\\setting.txt","w"); 36 fprintf(fp,"%s %d %d",code,N1,N2);//把配置信息保存至文件 37 fclose(fp); 38 printf("謝謝你的使用!\n"); 39 } 40 return TRUE; 41 42 case WM_COMMAND: 43 { 44 switch(LOWORD(wParam)) 45 { 46 case HELP:// 打印help 47 {print_help();break;} 48 case INITIALISE:// 初始化 49 {chushihua();break;} 50 case onlin:// 登陸 51 {onling();break;} 52 case On:// 隨機播放音樂 53 {play_sound();break;} 54 case Off:// 關閉音樂 55 {PlaySound(NULL,NULL,SND_FILENAME);break;} 56 case Surprise:// 隨機播放音樂 57 {play_sound();break;} 58 case shuaxin:// 刷新音樂庫列表 59 {search_actual(1);break;} 60 case xianshixinxi:// 顯示所有音樂信息 61 {view_all();break;} 62 case search_name:// 按名稱查詢 63 {searchname(0,0);break;} 64 case search_singer:// 按歌手查詢 65 {searchname(1,0);break;} 66 case search_key:// 關鍵字查詢 67 {searchkey();break;} 68 case CLEAR:// 清理屏幕 69 {system("cls");printf("歡迎使用歌曲管理系統!!\n");break;} 70 case bofangyinyue:// 選擇播放音樂 71 {play();break;} 72 case paixu:// 對音樂信息進行排序 73 {px();break;} 74 case charuxinxi:// 插入音樂信息 75 { if(FLAG)charu(0); 76 else printf("你還沒有管理員權限,請先登陸\n"); 77 break; 78 } 79 case shanchuxinxi:// 刪除音樂信息 80 { 81 if(FLAG)sc(); 82 else printf("你還沒有管理員權限,請先登陸\n"); 83 break; 84 } 85 86 case daoruwenjian:// 導入音樂文件 87 { 88 if(FLAG)wenjian(); 89 else printf("你還沒有管理員權限,請先登陸\n"); 90 break; 91 } 92 case xiugaixinxi:// 修改音樂信息 93 { 94 if(FLAG)revise(); 95 else printf("你還沒有管理員權限,請先登陸\n"); 96 break; 97 } 98 case settcode:// 設置密碼 99 { 100 if(FLAG){printf("請輸入你修改后的密碼:");setcode();} 101 else printf("你還沒有管理員權限,請先登陸\n"); 102 break; 103 } 104 case daoruxinxi:// 加入音樂信息 105 { 106 if(FLAG)load_music_info(); 107 else printf("你還沒有管理員權限,請先登陸\n"); 108 break; 109 } 110 } 111 } 112 return TRUE; 113 } 114 return FALSE; 115 } 116 117 118 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 119 { 120 hInst=hInstance; 121 InitCommonControls(); 122 return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain); 123 }
源文件
鏈接:https://pan.baidu.com/s/1JHDw9HnoC4VtaUurJRrl3w
提取碼:lfmg
復制這段內容后打開百度網盤手機App,操作更方便哦