這個代碼因為配置資訊的原因不能直接整個拿去用(o゜▽゜)o☆
隻能提供一個思路(其實也可以把裡面函數拿去用)ˋ( ° ▽、° )
(ง •_•)ง
這個小系統包含一些除任務要求外額外附加的功能
本來做出來差不多是這個效果基本上來說比任務要求多加了:
{ 1.導入音樂檔案并播放;2簡單的圖形化界面;3.模糊查詢功能;}
所有資訊的顯示與輸入都是在dos視窗完成,圖形界面實質隻是提供了更加便利調用函數的方式(僞圖形化操作)ˋ( ° ▽、° )
c語言課程設計--圖書/音樂管理系統

(納個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 }
resource.cpp
因為用到了查找磁盤上音樂檔案,是以可能要用到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
recourse.h
最後就是一個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 }
main.cpp
源檔案
連結:https://pan.baidu.com/s/1JHDw9HnoC4VtaUurJRrl3w
提取碼:lfmg
複制這段内容後打開百度網盤手機App,操作更友善哦