生活随笔
收集整理的這篇文章主要介紹了
c语言垃圾分类系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#C語言簡單的垃圾分類查詢系統
目的:(1)通過實踐提高C語言的開發能力。(2)通過充分編程實踐和實際應用,進一步體會數據結構中的常用算法的實質,建立算法優劣的概念以及算法評估分析和比較的方法。(3)培養學生科技論文寫作技能,如文獻查找和引用,論文格式規范等。
課程設計內容:
基于C語言的簡單垃圾分類查詢系統,對垃圾進行分類管理,包括新增、刪除、修改、查詢、瀏覽等功能。
(1)新增:采用交互式方式錄入垃圾信息,保存在文件中,之后可導入;
(2)刪除:從垃圾信息管理文件中刪除一個垃圾對象;
(3)修改:檢索某個垃圾對象,對其某些屬性進行修改;
(4)查找:從垃圾信息管理文件中查詢符合某些條件的垃圾信息;
(5)瀏覽:從垃圾信息管理文件中按垃圾類別排列好,再瀏覽。
#以下是函數關系流程圖和運行截圖:(運行環境:Visual Studio 2019)
1.各函數層次關系圖
2.add
3.delete
4.update
5.select
6.browse
#以下貼上我的代碼:
(在該項目相同目錄下新建一個文本文件:garbage.txt 即可運行)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct _Garbage
{char garbageName
[20];char garbageType
[20];char garbageDescribe
[20];
}Garbage
;
typedef struct _GarbageNode
{Garbage garbage
;struct _GarbageNode
* prior
;struct _GarbageNode
* next
;
}GarbageNode
;
void readLink(GarbageNode
* g_head
, GarbageNode
* tail
)
{GarbageNode
* p
;p
= g_head
->next
;int i
= 1;printf("\n\t垃圾表:\n");printf("\t-----------------------------------------------------------------\n");printf("\t|編號 \t|名字\t\t|類型\t\t\t|描述\t\t\n");printf("\t-----------------------------------------------------------------\n");while (p
!= g_head
){printf("\t| %d\t|%s\t\t|%s\t\t|%s\t\t\n",i
,p
->garbage
.garbageName
,p
->garbage
.garbageType
,p
->garbage
.garbageDescribe
);p
= p
->next
;i
++;}printf("\t-----------------------------------------------------------------");printf("\n");
}
int add(GarbageNode
* g_head
, GarbageNode
* tail
)
{int reback
;int num
;FILE
* fp
;char a
[20];char b
[20];char c
[20];char d
[20];char getName
[20];
reAdd
:printf("\n");printf("\t| 請輸入想插入的垃圾名稱:");scanf("%s",getName
);GarbageNode
* p
;p
= g_head
->next
;while (0 != strcmp(getName
, p
->garbage
.garbageName
) && p
!= g_head
){p
= p
->next
;}if (0 == strcmp(getName
, p
->garbage
.garbageName
)){printf("\t| 該垃圾已添加過了,請重新輸入");goto reAdd
;}GarbageNode
* gNewNode
= (GarbageNode
*)malloc(sizeof(GarbageNode
));strcpy(gNewNode
->garbage
.garbageName
, getName
);printf("\n");printf("\t|-------------------------------------------------\n");printf("\t|1 廚余垃圾\t\t2 可回收垃圾\t\t|\n\n");printf("\t|3 有毒有害垃圾\t\t4 不可回收垃圾\t\t|\n");printf("\t|-------------------------------------------------\n");printf("\t| 請選擇該垃圾類型: ");scanf("%d", &num
);switch (num
){case 1:strcpy(a
, "廚余垃圾");strcpy(gNewNode
->garbage
.garbageType
, a
);break;case 2:strcpy(b
, "可回收垃圾");strcpy(gNewNode
->garbage
.garbageType
, b
);break;case 3:strcpy(c
, "有毒有害垃圾");strcpy(gNewNode
->garbage
.garbageType
, c
);break;case 4:strcpy(d
, "不可回收垃圾");strcpy(gNewNode
->garbage
.garbageType
, d
);break;}printf("\n");printf("\t| 請輸入該垃圾描述:");scanf("%s", gNewNode
->garbage
.garbageDescribe
);tail
->next
= gNewNode
;gNewNode
->prior
= tail
;tail
= gNewNode
;g_head
->prior
= tail
;tail
->next
= g_head
;fp
= fopen("garbage.txt", "at");if (fp
== NULL){printf("找不到文件");exit(0);}fprintf(fp
, "%s\t\t%s\t\t%s\n", gNewNode
->garbage
.garbageName
, gNewNode
->garbage
.garbageType
, gNewNode
->garbage
.garbageDescribe
);printf("\n");printf("\t|添加成功,您添加的垃圾是:\n\n");printf("\t|名字: %s\n", gNewNode
->garbage
.garbageName
);printf("\t|類型: %s\n", gNewNode
->garbage
.garbageType
);printf("\t|描述: %s \n",gNewNode
->garbage
.garbageDescribe
);printf("\n");fclose(fp
);system("pause");printf("\t|按 1 返回主菜單\n\t|按其他鍵退出系統\n");printf("\t|請選擇: ");scanf("%d", &reback
);if (reback
== 1){system("cls");return reback
;}else{exit(0);}
}
int delet(GarbageNode
* g_head
, GarbageNode
* tail
)
{int reback
;FILE
* fp
;readLink(g_head
, tail
);
reDelete
:if (g_head
->next
== g_head
&& g_head
->prior
== g_head
){printf("\n\t|目前垃圾表為空\n");}else{ printf("\t|請選擇要刪除的垃圾名稱:");char getName
[20];int ch
;scanf("%s", getName
);GarbageNode
* del
= g_head
->next
;GarbageNode
* reWrite
;while (del
!= g_head
){if (0 != strcmp(getName
, del
->garbage
.garbageName
)){del
= del
->next
; }else{printf("\n");printf("\t|要刪除的是:\n\n");printf("\t|名字: %s\n", del
->garbage
.garbageName
);printf("\t|類型: %s\n", del
->garbage
.garbageType
);printf("\t|描述: %s \n", del
->garbage
.garbageDescribe
);printf("\n");printf("\t| 1 確定刪除\n\t| 2 取消并重新選擇刪除的垃圾\n");printf("\t|請選擇:");scanf("%d", &ch
);if (ch
== 1){del
->prior
->next
= del
->next
;del
->next
->prior
= del
->prior
;free(del
);fp
= fopen("garbage.txt", "w");if (fp
== NULL){printf("\t|找不到文件");exit(0);}else{reWrite
= g_head
->next
;while (reWrite
!= g_head
){fprintf(fp
, "%s\t\t%s\t\t%s\n",reWrite
->garbage
.garbageName
,reWrite
->garbage
.garbageType
,reWrite
->garbage
.garbageDescribe
);reWrite
= reWrite
->next
;}fclose(fp
);printf("\n\t|刪除完畢后的結果:");readLink(g_head
, tail
);break;}}else if (ch
== 2){goto reDelete
;}else{printf("\t|輸入錯誤,請重新選擇刪除");goto reDelete
;}}}if (del
== g_head
){int num
;printf("\t|無此垃圾信息\n");printf("\t|重新刪除請按 1 , 其他鍵返回主菜單: ");scanf("%d", &num
);if (num
== 1){goto reDelete
;}}}system("pause");printf("\t|按 1 返回主菜單\n\t|按其他鍵退出系統\n");printf("\t|請選擇: ");scanf("%d", &reback
);if (reback
== 1){system("cls");return reback
;}else{exit(0);}
}
int update(GarbageNode
* g_head
, GarbageNode
* tail
)
{int reback
;FILE
* fp
;int num
;char a
[20];char b
[20];char c
[20];char d
[20];readLink(g_head
, tail
);
reUpdate
:if (g_head
->next
== g_head
&& g_head
->prior
== g_head
){printf("\n\t|目前垃圾表為空\n");}else{printf("\t|請選擇要修改的垃圾名稱:");char getName
[20];int ch
;scanf("%s", getName
);GarbageNode
* upd
= g_head
->next
;GarbageNode
* reWrite
= g_head
->next
;while (upd
!= g_head
){if (0 != strcmp(getName
, upd
->garbage
.garbageName
)){upd
= upd
->next
; }else{printf("\n");printf("\t|要修改的是:\n");printf("\t|名字: %s\n", upd
->garbage
.garbageName
);printf("\t|類型: %s\n", upd
->garbage
.garbageType
);printf("\t|描述: %s \n", upd
->garbage
.garbageDescribe
);printf("\n");printf("\t| 1 確定要修改垃圾信息\n\t| 2 取消并重新選擇修改的垃圾\n");printf("\t| 請選擇:");scanf("%d", &ch
);if (ch
== 1){printf("\t|請輸入修改成的垃圾名字:");scanf("%s", upd
->garbage
.garbageName
);printf("\n");printf("\t|請選擇要修改成的垃圾類型:");printf("\n");printf("\t|-------------------------------------------------\n");printf("\t|1 廚余垃圾\t\t2 可回收垃圾\t\t|\n");printf("\t|3 有毒有害垃圾\t\t4 不可回收垃圾\t\t|\n");printf("\t|-------------------------------------------------\n");printf("\t| 請選擇該垃圾類型: ");scanf("%d", &num
);switch (num
){case 1:strcpy(a
, "廚余垃圾");strcpy(upd
->garbage
.garbageType
, a
);break;case 2:strcpy(b
, "可回收垃圾");strcpy(upd
->garbage
.garbageType
, b
);break;case 3:strcpy(c
, "有毒有害垃圾");strcpy(upd
->garbage
.garbageType
, c
);break;case 4:strcpy(d
, "不可回收垃圾");strcpy(upd
->garbage
.garbageType
, d
);break;}printf("\n");printf("\t|請輸入修改成的垃圾描述:");scanf("%s", upd
->garbage
.garbageDescribe
);fp
= fopen("garbage.txt", "w");if (fp
== NULL){printf("找不到文件");exit(0);}else{while (reWrite
!= g_head
){fprintf(fp
, "%s\t\t%s\t\t%s\n",reWrite
->garbage
.garbageName
,reWrite
->garbage
.garbageType
,reWrite
->garbage
.garbageDescribe
);reWrite
= reWrite
->next
;}fclose(fp
);printf("\t|修改完畢后的結果:\n");readLink(g_head
, tail
);break;}}else if (ch
== 2){goto reUpdate
;}else{printf("\t|輸入錯誤,請重新選擇修改: ");goto reUpdate
;}}}if (upd
== g_head
){int reupdate
;printf("\t|無此垃圾信息\n");printf("\t|重新修改請按 1 , 其他鍵返回主菜單: ");scanf("%d", &reupdate
);if (reupdate
== 1){goto reUpdate
;}}}system("pause");printf("\t|按 1 返回主菜單\n\t|按其他鍵退出系統\n");printf("\t|請選擇: ");scanf("%d", &reback
);if (reback
== 1){system("cls");return reback
;}else{exit(0);}
}
int select(GarbageNode
* g_head
, GarbageNode
* tail
)
{int reback
;int ch
;int i
= 0;char getMassage
[20];GarbageNode
* p
;readLink(g_head
, tail
);reSelect
:if (g_head
->next
== g_head
&& g_head
->prior
== g_head
){printf("\n\t|目前垃圾表為空\n");}else{printf("\t| 請選擇您要查詢的方式\n\n");printf("\t| 1.按名稱查 ******** \n");printf("\t| 2.按類型查 ******** \n");printf("\t| 3.按描述查 ******** \n");printf("\t|請選擇:");scanf("%d", &ch
);p
= g_head
->next
;switch (ch
){case 1:printf("\t|請輸入想查詢的垃圾名字:");scanf("%s", getMassage
);i
= 0;while (p
!= g_head
){if (0 != strcmp(getMassage
, p
->garbage
.garbageName
)){p
= p
->next
; }else{printf("\n");printf("\t|查找成功,您查找的垃圾信息如下:\n\n");printf("\t|名字: %s\n", p
->garbage
.garbageName
);printf("\t|類型: %s\n", p
->garbage
.garbageType
);printf("\t|描述: %s \n", p
->garbage
.garbageDescribe
);printf("\n");i
++;break;}}break;case 2:printf("\t|請輸入想查詢的垃圾類型:");scanf("%s", getMassage
);i
= 0;while (p
!= g_head
){ if (0 == strcmp(getMassage
, p
->garbage
.garbageType
)){i
++;printf("\t|----------------------------------------------------------------\n");printf("\t|%d 名字: %s\t 類型: %s\t描述: %s\n",i
, p
->garbage
.garbageName
, p
->garbage
.garbageType
,p
->garbage
.garbageDescribe
);}p
= p
->next
; }printf("\t|----------------------------------------------------------------\n");break;case 3:printf("\t|請輸入想查詢的垃圾描述:");scanf("%s", getMassage
);i
= 0;while (p
!= g_head
){if (0 != strcmp(getMassage
, p
->garbage
.garbageDescribe
)){p
= p
->next
; }else{printf("\n");printf("\t|查找成功,您查找的垃圾信息如下:\n\n");printf("\t|名字: %s\n", p
->garbage
.garbageName
);printf("\t|類型: %s\n", p
->garbage
.garbageType
);printf("\t|描述: %s \n", p
->garbage
.garbageDescribe
);printf("\n");i
++;break;}}break;}if (p
== g_head
&& i
==0){printf("\t|無此垃圾信息\n"); }int reselect
;printf("\t|重新查找請按 1 , 其他鍵返回主菜單: ");scanf("%d", &reselect
);if (reselect
== 1){goto reSelect
;}}system("pause");printf("\t|按 1 返回主菜單\n\t|按其他鍵退出系統\n");printf("\t|請選擇: ");scanf("%d", &reback
);if (reback
== 1){system("cls");return reback
;}else{exit(0);}}
int browse(GarbageNode
* g_head
, GarbageNode
* tail
)
{int reback
;int num
;int rebrowse
;GarbageNode
* p
;int i
;char getTypeOne
[20] = "廚余垃圾";char getTypeTow
[20] = "可回收垃圾";char getTypeThree
[20] = "有毒有害垃圾";char getTypeFour
[20] = "不可回收垃圾";printf("\n");printf("\t|------------------------------------------------\n");printf("\t|1 廚余垃圾表\t\t2 可回收垃圾表\t\t|\n\n");printf("\t|3 有毒有害垃圾表\t4 不可回收垃圾表\t|\n\n");printf("\t|5 按以上四種垃圾排列好的總垃圾表\t\t|\n");printf("\t|------------------------------------------------\n");
reBrowse
:i
= 1;printf("\t|請選擇想瀏覽的垃圾表編號: ");scanf("%d", &num
);switch (num
){case 1:p
= g_head
->next
;printf("\n\t廚余垃圾表:\n");printf("\t--------------------------------------------------\n");printf("\t|編號 \t|名字\t\t|描述\t\t\t|\n");printf("\t--------------------------------------------------\n");while (p
!= g_head
){if (0 == strcmp(getTypeOne
, p
->garbage
.garbageType
)){printf("\t| %d\t|%s\t\t|%s\t\t\t|\n",i
,p
->garbage
.garbageName
,p
->garbage
.garbageDescribe
);i
++;} p
= p
->next
;}printf("\t--------------------------------------------------");printf("\n");break;case 2:p
= g_head
->next
;printf("\n\t可回收垃圾表:\n");printf("\t--------------------------------------------------\n");printf("\t|編號 \t|名字\t\t|描述\t\t\t|\n");printf("\t--------------------------------------------------\n");while (p
!= g_head
){if (0 == strcmp(getTypeTow
, p
->garbage
.garbageType
)){printf("\t| %d\t|%s\t\t|%s\t\t\t|\n",i
,p
->garbage
.garbageName
,p
->garbage
.garbageDescribe
);i
++;}p
= p
->next
;}printf("\t--------------------------------------------------");printf("\n");break;case 3:p
= g_head
->next
;printf("\n\t有毒有害垃圾表:\n");printf("\t--------------------------------------------------\n");printf("\t|編號 \t|名字\t\t|描述\t\t\t|\n");printf("\t--------------------------------------------------\n");while (p
!= g_head
){if (0 == strcmp(getTypeThree
, p
->garbage
.garbageType
)){printf("\t| %d\t|%s\t\t|%s\t\t\t\n",i
,p
->garbage
.garbageName
,p
->garbage
.garbageDescribe
);i
++;}p
= p
->next
;}printf("\t--------------------------------------------------");printf("\n");break;case 4:p
= g_head
->next
;printf("\n\t不可回收垃圾表:\n");printf("\t--------------------------------------------------\n");printf("\t|編號 \t|名字\t\t|描述\t\t\t|\n");printf("\t--------------------------------------------------\n");while (p
!= g_head
){if (0 == strcmp(getTypeFour
, p
->garbage
.garbageType
)){printf("\t| %d\t|%s\t\t|%s\t\t\t|\n",i
,p
->garbage
.garbageName
,p
->garbage
.garbageDescribe
);i
++;}p
= p
->next
;}printf("\t--------------------------------------------------");printf("\n");break;case 5:p
= g_head
->next
;printf("\n\t總垃圾表:\n");printf("\t------------------------------------------------------------\n");printf("\t|編號 \t|名字\t\t|類型\t\t\t|描述\t\t\n");printf("\t------------------------------------------------------------\n");while (p
!= g_head
){if (0 == strcmp(getTypeOne
, p
->garbage
.garbageType
)){printf("\t| %d\t|%s\t\t|%s\t\t|%s\t\t\n",i
,p
->garbage
.garbageName
,p
->garbage
.garbageType
,p
->garbage
.garbageDescribe
);i
++;}p
= p
->next
;}p
= g_head
->next
;while (p
!= g_head
){if (0 == strcmp(getTypeTow
, p
->garbage
.garbageType
)){printf("\t| %d\t|%s\t\t|%s\t\t|%s\t\t\n",i
,p
->garbage
.garbageName
,p
->garbage
.garbageType
,p
->garbage
.garbageDescribe
);i
++;}p
= p
->next
;}p
= g_head
->next
;while (p
!= g_head
){if (0 == strcmp(getTypeThree
, p
->garbage
.garbageType
)){printf("\t| %d\t|%s\t\t|%s\t\t|%s\t\t\n",i
,p
->garbage
.garbageName
,p
->garbage
.garbageType
,p
->garbage
.garbageDescribe
);i
++;}p
= p
->next
;}p
= g_head
->next
;while (p
!= g_head
){if (0 == strcmp(getTypeFour
, p
->garbage
.garbageType
)){printf("\t| %d\t|%s\t\t|%s\t\t|%s\t\t\n",i
,p
->garbage
.garbageName
,p
->garbage
.garbageType
,p
->garbage
.garbageDescribe
);i
++;}p
= p
->next
;}printf("\t------------------------------------------------------------");printf("\n");break;default:printf("\t|輸入不規范,請重新選擇\n");goto reBrowse
;break;}printf("\t|繼續瀏覽其他垃圾表請按 1 , 按其他鍵將返回主菜單:\n");printf("\t|請選擇: ");scanf("%d", &rebrowse
);if (rebrowse
== 1){goto reBrowse
;}else {system("pause");printf("\t|按 1 返回主菜單\n\t|按其他鍵退出系統\n");printf("\t|請選擇: ");scanf("%d", &reback
);if (reback
== 1){system("cls");return reback
;}else{exit(0);}}
}
void menu()
{printf("**********************************************************\n");printf("*************** 歡迎使用垃圾分類系統 *********************\n");printf("**********************************************************\n");printf("\t------------------------------------------\n");printf("\t|\t1.\t新增\t\t\t|\n");printf("\t------------------------------------------\n");printf("\t|\t2.\t刪除\t\t\t|\n");printf("\t------------------------------------------\n");printf("\t|\t3.\t修改\t\t\t|\n");printf("\t------------------------------------------\n");printf("\t|\t4.\t查找\t\t\t|\n");printf("\t------------------------------------------\n");printf("\t|\t5.\t瀏覽\t\t\t|\n");printf("\t------------------------------------------\n");printf("\n");printf("\t| 請選擇以上操作 ^_^ : ");}int main()
{
int reback
; FILE
* fp
;GarbageNode
* g_head
;GarbageNode
* gNewNode
, * tail
;g_head
= (GarbageNode
*)malloc(sizeof(GarbageNode
));g_head
->next
= g_head
;g_head
->prior
= g_head
;tail
= g_head
; fp
= fopen("garbage.txt", "rt");if (fp
== NULL){printf("找不到文件");exit(0);}else{gNewNode
= (GarbageNode
*)malloc(sizeof(GarbageNode
));fscanf(fp
, "%s\t\t%s\t\t%s",gNewNode
->garbage
.garbageName
,gNewNode
->garbage
.garbageType
,gNewNode
->garbage
.garbageDescribe
);while (!feof(fp
)){tail
->next
= gNewNode
;gNewNode
->prior
= tail
;tail
= gNewNode
;tail
->next
= g_head
;g_head
->prior
= tail
;gNewNode
= (GarbageNode
*)malloc(sizeof(GarbageNode
));fscanf(fp
, "%s\t%s\t%s",gNewNode
->garbage
.garbageName
,gNewNode
->garbage
.garbageType
,gNewNode
->garbage
.garbageDescribe
); }fclose(fp
);}do{system("color 5F");menu();int ch
;scanf("%d", &ch
);switch (ch
){case 1:reback
= add(g_head
, tail
);break;case 2:reback
= delet(g_head
, tail
);break;case 3:reback
= update(g_head
, tail
);break;case 4:reback
= select(g_head
, tail
);break;case 5:reback
= browse(g_head
, tail
);break;default:system("cls");printf("**********輸入不規范*************\n");printf("\n請重新選擇\n\n");system("pause");system("cls");reback
= 1;break;}} while (reback
);return 0;
}
5結語
問題回顧:1.起初對項目不知從何下手,對文件交互式不是很了解,但是努力找資料學習并實驗,對知識有更深體會和理解,兩天后開始了自己的垃圾分類項目。2.在對于系統中函數單獨調用后不能返回主菜單而苦惱,后來想到了調用函數后返回參數,再配合while循環即可達到目的,只有不滿足條件時才退出系統。3.在寫刪除操作前是用單向鏈表的,但是刪除必須要回到前一個結點才能連接其前后結點,自己再釋放掉,所以又改成了雙向鏈表。
鏈表的時間復雜度為O(n)。最開始是每個子函數都在堆空間創建一條鏈,再去讀取文件放入結點中,這樣很浪費運行時間和堆內存空間。為了不在每次調用子函數時都去文件那里讀取完再放進鏈表,我選擇將讀取的位置放在main()函數的最開始,然后再將鏈頭鏈尾傳給其他子函數,這樣我的系統從頭到尾就只有一條鏈,每次的增刪改查操作都在同一塊堆空間進行操作,這樣文件和鏈表的變化就能同步起來,只要去遍歷鏈表就相當于遍歷了文件,我是用readLink()函數讀取修改后堆空間鏈表。
功能擴展:系統除了最基本的增刪改查,我還擴展了瀏覽的功能。
對數據結構課程的認識:在開始做課程設計之前我復習數據結構,仔細比較線性表,單鏈表和雙向鏈表的各種不同及其適用的應用場景,更深刻體理解鏈表的使用極其在堆空間的存在,還有它的生命周期。
這次的課程設計對我的C語言基礎幫助很大,而且更是清楚了計算機中一些內存的分配,實踐和邏輯也得到質的提升了。
總結
以上是生活随笔為你收集整理的c语言垃圾分类系统的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。