C语言大作业 商品库存管理系统
生活随笔
收集整理的這篇文章主要介紹了
C语言大作业 商品库存管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
利用c語言做的課程設計(鏈表),在DEV C++等編譯器上可通過
可根據需求自行更改為圖書管理系統或者其他類似的系統
本課題的主要任務是設計和實現一個“商品庫存管理系統”,并滿足以下要求:
1.系統以菜單方式工作;
2.使用鏈表或結構數組對商品信息進行管理和維護;
3.使用二進制文件在磁盤上保存商品信息;
4. 鏈表中各結點或結構數組中各元素包括的商品信息: 商品編號、 商品名
稱、 商品類型(如食品、 體育用品、 生活用品、 兒童玩具、 音像制品等) 、
單價、 庫存數量、 是否進口等。
5.實現如下基本功能:
(1) 新增商品
(2) 商品瀏覽 (輸出所有商品信息)
(3) 商品刪除 (刪除指定編號的商品)
(4) 商品修改 (修改指定編號的商品信息)- 14 -
(5) 商品排序 ( 根據商品編號進行排序)
(6) 商品查詢統計 ( 提供商品類型、 是否進口方式兩種方式對商品進行
統計查詢功能)
(7)將商品信息保存到文件存盤 ( 將鏈表或結構數組中的數據以文件的
形式存盤)
(8)從文件中讀入商品信息 ( 將已存盤的文件讀入內存, 進行管理)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define FORMAT "%d%s%s%d%d%d" struct product { long num; //商品編號 char name[20]; //商品名稱 char type[20]; //商品類型 long price; //商品單價 long res; //庫存數量 long import; //是否進口,1代表進口,0代表非進口 struct product *next; }; int n=0;struct product *creat(); void print(struct product *head); void save_to_file(struct product *head); void change(int search_num,struct product *head); void insert(struct product *head,struct product *newpro); void del(int del_num,struct product *head); void swap(long *a,long *b); void sort(struct product *head); void type_inquire(struct product *head); void import_inquire(struct product *head);struct product *creat() /*創建鏈表*/ {struct product *head,*L;struct product *p1,*p2;FILE *fp1;fp1=fopen("D:\\goods.txt","r");n=0;head=(struct product *)malloc(sizeof(struct product));head->next=NULL;p2=head; p1=(struct product *)malloc(sizeof(struct product));fscanf(fp1,FORMAT,&p1->num,p1->name,p1->type,&p1->price,&p1->res,&p1->import); while (p1->num!=0){ n++;p2->next=p1;p2=p1;p1=(struct product *)malloc(sizeof(struct product)); //p1在前方開辟,p2來保存之前的節點 fscanf(fp1,FORMAT,&p1->num,p1->name,p1->type,&p1->price,&p1->res,&p1->import);}free(p1);p2->next=NULL; fclose(fp1); return(head); //head指向第一個空白節點,其內的元素為空 }void print(struct product *head) //用于將表在屏幕上輸出,一個漢字等于兩個空格 { struct product *p; printf("\n此表中有%d個記錄\n",n);p=head->next;if(p!=NULL){printf("┌────┬─────────────┬─────────────┬────────┬────────┬──────────┐\n");printf("│編號│ 商 品 名 稱 │ 商 品 類 型 │ 單 價 │ 庫存數 │ 是否進口 │\n");while(p!=NULL){printf("├────┼─────────────┼─────────────┼────────┼────────┼──────────┤\n");if(p->import) printf("│%4d│%13s│%13s│%8d│%8d│ 是 │\n",p->num,p->name,p->type,p->price,p->res);else printf("│%4d│%13s│%13s│%8d│%8d│ 否 │\n",p->num,p->name,p->type,p->price,p->res);p=p->next;}printf("└────┴─────────────┴─────────────┴────────┴────────┴──────────┘\n");} }void save_to_file(struct product *head) {struct product *p;p=head->next;FILE *fp;fp=fopen("D:\\goods.txt","w");while(p!=NULL){fprintf(fp,"%d %s %s %d %d %d \n",p->num,p->name,p->type,p->price,p->res,p->import);p=p->next;}fprintf(fp,"0 0 0 0 0 0"); //用于作為文件結束標記 fclose(fp); }void change(int search_num,struct product *head) {struct product *p;p=head->next;while(p!=NULL&&p->num!=search_num)p=p->next;if(p==NULL)printf("\n————————表中無所查商品————————\n");else {printf("請輸入新的商品信息,包括:\n編號 名稱 類型 單價 庫存數量 是否進口(用1表示進口,0表示非進口)\n"); scanf(FORMAT,&p->num,p->name,p->type,&p->price,&p->res,&p->import);printf("\n————————商品信息修改成功!————————\n");} } void insert(struct product *head,struct product *newpro) {struct product *p;p=head;while(p->next!=NULL) p=p->next;newpro->next=p->next;p->next=newpro;n++;printf("\n————————新增商品成功!————————\n"); }void del(int del_num,struct product *head) {struct product *p,*q;p=head;while(p->next&&p->next->num!=del_num) //查找要刪除的元素,最終用p指向要刪除的節點的 前驅 節點 p=p->next;if(p->next==NULL)printf("————————未找到要刪除的商品————————\n");else {q=p->next;p->next=q->next;free(q);n--;printf("————————商品刪除成功!————————");} }void swap(long *a,long *b) {long temp;temp=*a;*a=*b;*b=temp; }void sort(struct product *head) //鏈表的冒泡排序法 {struct product *p;struct product *pend=NULL;p=head->next;char temp[20];while(p!=pend){while(p->next!=pend){if(p->num>p->next->num){swap(&p->num,&p->next->num);strcpy(temp,p->name);strcpy(p->name,p->next->name);strcpy(p->next->name,temp);strcpy(temp,p->type);strcpy(p->type,p->next->type);strcpy(p->next->type,temp);swap(&p->price,&p->next->price);swap(&p->res,&p->next->res);swap(&p->import,&p->next->import);}p=p->next;}pend=p;p=head->next;}printf("————————商品排序成功!————————"); }void type_inquire(struct product *head) {struct product *p;int count=0;char get_type[20];p=head->next;printf("請輸入要統計的商品類型:");gets(get_type); printf("找到的所有此類型商品信息如下:\n");printf("┌────┬─────────────┬─────────────┬────────┬────────┬──────────┐\n");printf("│編號│ 商 品 名 稱 │ 商 品 類 型 │ 單 價 │ 庫存數 │ 是否進口 │\n");while(p!=NULL){if(strcmp(p->type,get_type)==0){ count++;printf("├────┼─────────────┼─────────────┼────────┼────────┼──────────┤\n");if(p->import) printf("│%4d│%13s│%13s│%8d│%8d│ 是 │\n",p->num,p->name,p->type,p->price,p->res);else printf("│%4d│%13s│%13s│%8d│%8d│ 否 │\n",p->num,p->name,p->type,p->price,p->res); }p=p->next;}printf("└────┴─────────────┴─────────────┴────────┴────────┴──────────┘\n");printf("——————該類型商品共有%d種——————\n",count);} void import_inquire(struct product *head) { struct product *p;int count=0,k;p=head->next;printf("統計非進口商品信息,輸入0\n統計進口商品信息,輸入1\n");printf("請輸入:");scanf("%d",&k);printf("找到的所有此類型商品信息如下:\n");printf("┌────┬─────────────┬─────────────┬────────┬────────┬──────────┐\n");printf("│編號│ 商 品 名 稱 │ 商 品 類 型 │ 單 價 │ 庫存數 │ 是否進口 │\n");while(p!=NULL){if(p->import==k){ count++;printf("├────┼─────────────┼─────────────┼────────┼────────┼──────────┤\n");if(p->import) printf("│%4d│%13s│%13s│%8d│%8d│ 是 │\n",p->num,p->name,p->type,p->price,p->res);else printf("│%4d│%13s│%13s│%8d│%8d│ 否 │\n",p->num,p->name,p->type,p->price,p->res); }p=p->next;}printf("└────┴─────────────┴─────────────┴────────┴────────┴──────────┘\n");printf("——————該類型商品共有%d種——————\n",count); } void main() {struct product *head,*newpro,*p;int c;char get_type[20];int len,del_num,change_num,flag=1;head=creat(); print(head); printf(" 1.新增商品\n");printf(" 2.商品瀏覽\n");printf(" 3.商品刪除\n");printf(" 4.商品修改\n");printf(" 5.商品排序(根據編號排序)\n");printf(" 6.商品查詢統計(按商品類型統計)\n");printf(" 7.商品查詢統計(按是否進口統計)\n");printf(" 8.將商品信息保存到文件存盤\n"); printf(" 9.退出系統\n"); while(1){printf(" 請輸入功能選項(1..9) :"); scanf("%d",&c);getchar(); switch(c){ case 1:printf("請輸入一種新產品的信息,系統將在表中新增該商品信息后輸出(用1表示進口,0表示非進口)\n");newpro=(struct product *)malloc(sizeof(struct product));scanf(FORMAT,&newpro->num,newpro->name,newpro->type,&newpro->price,&newpro->res,&newpro->import); insert(head,newpro);print(head);break;case 2:print(head); break;case 3:if(head->next==NULL){printf("表已空,無法刪除\n");break;}else{printf("請輸入要刪除的產品的編號:");scanf("%d",&del_num);del(del_num,head);print(head);break;}case 4:printf("請輸入要修改的商品編號:");scanf("%d",&change_num);change(change_num,head);break;case 5:sort(head);print(head);break;case 6:type_inquire(head); break;case 7:import_inquire(head); break;case 8:save_to_file(head);printf("數據已成功保存至指定文件中\n");break;case 9:flag=0;break;default:printf("輸入有誤\n");}if(flag==0)break;}printf("\n——————————已退出系統——————————\n");}代碼運行部分:增刪改查等操作根據已有格式即可
?注:此程序采用文件儲存信息,需自行根據要求建立一個txt文檔,根據fscanf函數,格式同上(同行用空格分開,隔行用回車),如在此程序中是在D盤
總結
以上是生活随笔為你收集整理的C语言大作业 商品库存管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java编写一个测试类_java写一个类
- 下一篇: 使用Python爬虫 爬取豆瓣top25