diff --git a/data/data1.csv b/data/data1.csv new file mode 100644 index 0000000..e69de29 diff --git a/data/data2.csv b/data/data2.csv new file mode 100644 index 0000000..e69de29 diff --git a/images/Show.drawio.svg b/images/Show.drawio.svg new file mode 100644 index 0000000..512a24f --- /dev/null +++ b/images/Show.drawio.svg @@ -0,0 +1,66 @@ + + + + + + + +
+
+
+ 图表模块 +
+
+
+
+ + 图表模块 + +
+
+ + + + + +
+
+
+ 打印一个图表格式 +
+
+
+
+ + 打印一个图表格式 + +
+
+ + + + + +
+
+
+ 调用打印函数 +
+
+
+
+ + 调用打印函数 + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/images/delete_from_list.drawio.svg b/images/delete_from_list.drawio.svg new file mode 100644 index 0000000..a9ec896 --- /dev/null +++ b/images/delete_from_list.drawio.svg @@ -0,0 +1,103 @@ + + + + + + + +
+
+
+ 删除模块 +
+
+
+
+ + 删除模块 + +
+
+ + + + + +
+
+
+ 判断链表并返回信息 +
+
+
+
+ + 判断链表并返回信息 + +
+
+ + + + + +
+
+
+ 定义数组进行id比对 +
+
+
+
+ + 定义数组进行id比对 + +
+
+ + + + + + + +
+
+
+ 不存在则返回错误信息 +
+
+
+
+ + 不存在则返回错误信息 + +
+
+ + + + +
+
+
+ 存在则释放对应结点并更新结点 +
+
+
+
+ + 存在则释放对应结点并更新结点 + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/images/exit.drawio.svg b/images/exit.drawio.svg new file mode 100644 index 0000000..c0b7c6f --- /dev/null +++ b/images/exit.drawio.svg @@ -0,0 +1,48 @@ + + + + + + + +
+
+
+ 退出模块 +
+
+
+
+ + 退出模块 + +
+
+ + + + + +
+
+
+ 调用保存函数并打印退出信息 +
+
+
+
+ + 调用保存函数并打印退出信息 + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/images/find_goods.drawio.svg b/images/find_goods.drawio.svg new file mode 100644 index 0000000..d15a582 --- /dev/null +++ b/images/find_goods.drawio.svg @@ -0,0 +1,102 @@ + + + + + + + +
+
+
+ 查找模块 +
+
+
+
+ + 查找模块 + +
+
+ + + + + +
+
+
+ 判断链表是否存在并返回信息 +
+
+
+
+ + 判断链表是否存在并返回信息 + +
+
+ + + + + +
+
+
+ 创建数组进行id比对 +
+
+
+
+ + 创建数组进行id比对 + +
+
+ + + + + + +
+
+
+ 若不存在则返回错误信息 +
+
+
+
+ + 若不存在则返回错误信息 + +
+
+ + + + +
+
+
+ 存在则链表返回物品信息 +
+
+
+
+ + 存在则链表返回物品信息 + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/images/increase_count.drawio.svg b/images/increase_count.drawio.svg new file mode 100644 index 0000000..2b61434 --- /dev/null +++ b/images/increase_count.drawio.svg @@ -0,0 +1,120 @@ + + + + + + + +
+
+
+ 修改模块 +
+
+
+
+ + 修改模块 + +
+
+ + + + + +
+
+
+ 判断指针并返回信息 +
+
+
+
+ + 判断指针并返回信息 + +
+
+ + + + + +
+
+
+ 定义数组用于id比对 +
+
+
+
+ + 定义数组用于id比对 + +
+
+ + + + + +
+
+
+ 输入id进行比对 +
+
+
+
+ + 输入id进行比对 + +
+
+ + + + + + +
+
+
+ 若没有则返回信息 +
+
+
+
+ + 若没有则返回信息 + +
+
+ + + + +
+
+
+ 若有则通过链表修改信息 +
+
+
+
+ + 若有则通过链表修改信息 + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/images/insert.drawio.svg b/images/insert.drawio.svg new file mode 100644 index 0000000..3648733 --- /dev/null +++ b/images/insert.drawio.svg @@ -0,0 +1,102 @@ + + + + + + + +
+
+
+ 添加模块 +
+
+
+
+ + 添加模块 + +
+
+ + + + +
+
+
+ 判断链表是否存在 +
+
+
+
+ + 判断链表是否存在 + +
+
+ + + + + + +
+
+
+ 不存在返回error +
+
+
+
+ + 不存在返回error + +
+
+ + + + + +
+
+
+ 存在则头插法输入数据 +
+
+
+
+ + 存在则头插法输入数据 + +
+
+ + + + + +
+
+
+ 输入结束返回ok +
+
+
+
+ + 输入结束返回ok + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/images/main.drawio.svg b/images/main.drawio.svg new file mode 100644 index 0000000..511f7f2 --- /dev/null +++ b/images/main.drawio.svg @@ -0,0 +1,848 @@ + + + + + + + + + +
+
+
+ main +
+
+
+
+ + main + +
+
+ + + + + + + + +
+
+
+ init +
+
+
+
+ + init + +
+
+ + + + + + + +
+
+
+ display_menu +
+
+
+
+ + display_menu + +
+
+ + + + + + + +
+
+
+ c = make_choice +
+
+
+
+ + c = make_choice + +
+
+ + + + + +
+
+
+ Y +
+
+
+
+ + Y + +
+
+ + + + +
+
+
+ c == 1 +
+
+
+
+ + c == 1 + +
+
+ + + + + + + +
+
+
+ read_data +
+
+
+
+ + read_data + +
+
+ + + + + + + +
+
+
+ Y +
+
+
+
+ + Y + +
+
+ + + + +
+
+
+ c == 2 +
+
+
+
+ + c == 2 + +
+
+ + + + + + + +
+
+
+ savedata +
+
+
+
+ + savedata + +
+
+ + + + + + + +
+
+
+ Y +
+
+
+
+ + Y + +
+
+ + + + +
+
+
+ c == 3 +
+
+
+
+ + c == 3 + +
+
+ + + + + + + +
+
+
+
+ show_goods +
+
+
+
+
+ + show_goods + +
+
+ + + + + + + +
+
+
+ Y +
+
+
+
+ + Y + +
+
+ + + + +
+
+
+ c == 4 +
+
+
+
+ + c == 4 + +
+
+ + + + + + + +
+
+
+
+ + find_goods + +
+
+
+
+
+ + find_goods + +
+
+ + + + + + + +
+
+
+ Y +
+
+
+
+ + Y + +
+
+ + + + +
+
+
+ c == 5 +
+
+
+
+ + c == 5 + +
+
+ + + + + + + +
+
+
+
+ insert +
+
+
+
+
+ + insert + +
+
+ + + + + + + +
+
+
+ Y +
+
+
+
+ + Y + +
+
+ + + + +
+
+
+ c == 6 +
+
+
+
+ + c == 6 + +
+
+ + + + + + + +
+
+
+
+ increase_count +
+
+
+
+
+ + increase_count + +
+
+ + + + + + + +
+
+
+ Y +
+
+
+
+ + Y + +
+
+ + + + +
+
+
+ c == 7 +
+
+
+
+ + c == 7 + +
+
+ + + + + + + +
+
+
+
+ delete_from_list +
+
+
+
+
+ + delete_from_list + +
+
+ + + + + + + +
+
+
+ Y +
+
+
+
+ + Y + +
+
+ + + + +
+
+
+ c == 8 +
+
+
+
+ + c == 8 + +
+
+ + + + + + + +
+
+
+
+ sort +
+
+
+
+
+ + sort + +
+
+ + + + + + + + + + +
+
+
+ c == CMD_QUIT +
+
+
+
+ + c == CMD_QUIT + +
+
+ + + + + + + +
+
+
+ quit +
+
+
+
+ + quit + +
+
+ + + + + + + + +
+
+
+ 确定退出 +
+
+
+
+ + 确定退出 + +
+
+ + + + + + +
+
+
+ 命令错误,请重新选择 +
+
+
+
+ + 命令错误,请重新选择 + +
+
+ + + + +
+
+
+ N +
+
+
+
+ + N + +
+
+ + + + +
+
+
+ N +
+
+
+
+ + N + +
+
+ + + + +
+
+
+ N +
+
+
+
+ + N + +
+
+ + + + +
+
+
+ N +
+
+
+
+ + N + +
+
+ + + + +
+
+
+ N +
+
+
+
+ + N + +
+
+ + + + +
+
+
+ N +
+
+
+
+ + N + +
+
+ + + + +
+
+
+ N +
+
+
+
+ + N + +
+
+ + + + +
+
+
+ N +
+
+
+
+ + N + +
+
+ + + + +
+
+
+ N +
+
+
+
+ + N + +
+
+ + + + +
+
+
+ N +
+
+
+
+ + N + +
+
+ + + + +
+
+
+ Y +
+
+
+
+ + Y + +
+
+ + + + +
+
+
+ N +
+
+
+
+ + N + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/images/readData.drawio.svg b/images/readData.drawio.svg new file mode 100644 index 0000000..b6de3dd --- /dev/null +++ b/images/readData.drawio.svg @@ -0,0 +1,102 @@ + + + + + + + +
+
+
+ 读取文件 +
+
+
+
+ + 读取文件 + +
+
+ + + + + +
+
+
+ 判断头指针 +
+
+
+
+ + 判断头指针 + +
+
+ + + + + +
+
+
+ 为空返回error +
+
+
+
+ + 为空返回error + +
+
+ + + + + +
+
+
+ 不为空文件指针指向文件 并打开 +
+
+
+
+ + 不为空文件指针指向文件 并打开 + +
+
+ + + + + +
+
+
+ 若文件内没有数据返回无信息 +
+
+
+
+ + 若文件内没有数据返回无信息 + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/images/saveData.drawio.svg b/images/saveData.drawio.svg new file mode 100644 index 0000000..0af3f30 --- /dev/null +++ b/images/saveData.drawio.svg @@ -0,0 +1,152 @@ + + + + + + + + + +
+
+
+ save_data +
+
+
+
+ + save_data + +
+
+ + + + + + +
+
+
+ 输入文件名 +
+
+
+
+ + 输入文件名 + +
+
+ + + + + + +
+
+
+ 打开文件 +
+
+
+
+ + 打开文件 + +
+
+ + + + + + + + +
+
+
+ 打开文件成功? +
+
+
+
+ + 打开文件成功? + +
+
+ + + + + + +
+
+
+ 把所有库存记录 +
+ 写入文件 +
+
+
+
+ + 把所有库存记录 +写入文件 + +
+
+ + + + + + +
+
+
+ 关闭文件 +
+
+
+
+ + 关闭文件 + +
+
+ + + + + + + +
+
+
+ 提示打开文件失败 +
+
+
+
+ + 提示打开文件失败 + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/images/save_data.drawio.svg b/images/save_data.drawio.svg new file mode 100644 index 0000000..e69de29 diff --git a/images/show_goods.drawio.svg b/images/show_goods.drawio.svg new file mode 100644 index 0000000..47b0ea3 --- /dev/null +++ b/images/show_goods.drawio.svg @@ -0,0 +1,102 @@ + + + + + + + +
+
+
+ 打印模块 +
+
+
+
+ + 打印模块 + +
+
+ + + + + +
+
+
+ 判断头指针 +
+
+
+
+ + 判断头指针 + +
+
+ + + + + +
+
+
+ 为空返回错误信息 +
+
+
+
+ + 为空返回错误信息 + +
+
+ + + + + +
+
+
+ 不为空定义指针变量 +
+
+
+
+ + 不为空定义指针变量 + +
+
+ + + + + +
+
+
+ 打印商品信息 +
+
+
+
+ + 打印商品信息 + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/images/sort.drawio.svg b/images/sort.drawio.svg new file mode 100644 index 0000000..6370643 --- /dev/null +++ b/images/sort.drawio.svg @@ -0,0 +1,66 @@ + + + + + + + +
+
+
+ 排序模块 +
+
+
+
+ + 排序模块 + +
+
+ + + + + +
+
+
+ 链表的冒泡排序 +
+
+
+
+ + 链表的冒泡排序 + +
+
+ + + + + +
+
+
+ 定义前后指针对物品id进行从小到大的排序 +
+
+
+
+ + 定义前后指针对物品id进行从小到大的排序 + +
+
+
+ + + + + Viewer does not support full SVG 1.1 + + + +
\ No newline at end of file diff --git a/src/system.c b/src/system.c new file mode 100644 index 0000000..4c8d49b --- /dev/null +++ b/src/system.c @@ -0,0 +1,45 @@ + +#include +#include +#include +#include +#include "void.c" + +int main () +{ + LIST *pList = NULL; + pList = creation ( ); + readData2 ( pList ); + + while ( 1 ) + { + int choice = menu ( ); + int c1; + getchar(); + switch ( choice ) + { + case 1: + c1= menu1(); + if(c1==1) readData1 ( pList ); + if(c1==2) saveData(pList); + if(c1==3) show_goods(pList); + if(c1==4) find_goods(pList); + if(c1==5) insert(pList); + if(c1==6) increase_count(pList); + if(c1==7) delete_from_list(pList); + if(c1==8) sort(pList); + if(c1==9) Show(pList); + if(c1==0) {saveData(pList); printf("感谢使用");} + break; + case 2: + saveData ( pList ); + printf("感谢使用"); + return 0; + break; + default: + printf ("输入的信息有误,请重新输入!!!\r\n"); + break; + } + } + return 0; +} diff --git a/src/system.cpp b/src/system.cpp new file mode 100644 index 0000000..e780dd1 --- /dev/null +++ b/src/system.cpp @@ -0,0 +1,418 @@ +//sjandy sjandsy sjandsy +#include +#define Rl 1000 +using namespace std; + +#include +#include +#include +#include + +//常量定义 +#define SIZE 20 + +enum judge +{ + ERROR = -1, + OK, +}; + + +//类型定义 +struct goods +{ int id; + char name [SIZE]; + int count; +}; + +typedef struct goods dataType; + +typedef struct list +{ + dataType data; + struct list *pNext; +}LIST; + +LIST lis; + + +LIST * creation ( ) +{ + LIST *pList = NULL; + pList = ( LIST * ) malloc ( sizeof ( LIST ) ); + if ( NULL == pList ) + { + return NULL; + } + memset ( pList, 0, sizeof ( LIST ) ); + + return pList; +} + +bool add_goods(int id,char name[],int count,LIST *p){ + if ( NULL == p ) + { + return false; + } + //赋值 + strcpy(p->data.name,name); + p->data.count = count; + p->data.id=id; + return true; +} + +int insert ( LIST * pList ) +{ + //判断链表是否存在 + if ( NULL == pList ) + { + return ERROR; + } + LIST *p = creation ( ); + char name[20]; + int count=0; + int id=0; + printf("请输入id:\n"); + scanf("%d",&id); + printf("请输入商品名:\n"); + scanf("%s",&name); + printf("请输入商品数量:\n"); + scanf("%d",&count); + if(add_goods(id,name,count,p)){ + //头插法插入节点 + p->pNext = pList->pNext; + pList->pNext = p; + printf ( "商品 %s 已经添加成功\r\n", p->data.name ); + return OK; + } +} + + + + +int delete_from_list ( LIST * pList ) +{ + //判断链表是否存在,是否为空 + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "数据库数据为空,请先添加商品信息\r\n" ); + return ERROR; + } + int s[SIZE] = {0}; + printf( "请输入需要删除的商品id\r\n" ); + scanf ( "%d", s ); + + LIST *p = pList->pNext; + LIST *q = pList; + + while ( NULL != p ) + { + if ( p->data.id == s[0] ) + { + q->pNext = p->pNext; + free ( p ); + p = NULL; + printf ( "商品 %s 信息已被删除!!\r\n", s ); + return OK; + } + p = p->pNext; + q = q->pNext; + + } + printf ( "您输入的商品名称没有找到!!\r\n" ); + return ERROR; +} + + + +int find_goods ( LIST *pList ) +{ + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "数据库数据为空,请先添加商品信息\r\n" ); + return ERROR; + } + int s[SIZE] = {0}; + printf( "请输入需要查找的商品id\r\n" ); + scanf ( "%d", s ); + + //定义指针变量 + LIST *p = pList->pNext; + while ( NULL != p ) + { + if ( p->data.id==s[0] ) + { printf ("商品的id:%d\r\n ",p->data.id); + printf ( "商品的名称:%s\r\n", p->data.name ); + printf ( "商品的数量:%d\r\n", p->data.count ); + return OK; + } + p = p->pNext; + } + printf ( "输入的商品信息无效!!!\r\n" ); + return ERROR; +} + + +int increase_count ( LIST *pList ) +{ + //判断链表是否存在,是否为空 + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "数据库数据为空,请先添加商品信息\r\n" ); + return ERROR; + } + + int s[SIZE] = {0}; + printf( "请输入需修改的商品id\r\n" ); + scanf ( "%d", s ); + + + LIST *p = pList->pNext; + while ( NULL != p ) + { + + if ( p->data.id==s[0] ) + { + + printf ( "原商品的数量:%d\r\n", p->data.count ); + printf ( "请输入修改的商品数量\r\n" ); + int xiugai; + scanf ( "%d", &xiugai ); + p->data.count+=xiugai; + printf ( "商品数量修改成功 *_* \r\n" ); + return OK; + } + p = p->pNext; + } + printf ( "您输入的商品名称没有找到!!\r\n"); + return ERROR; +} + + +int menu ( ) +{ + + printf ( " ==欢迎来到商品管理系统===\r\n" ); + printf("1.功能菜单 | 2.退出 \n"); + + printf ( "请输入想要操作的选项*-*\r\n" ); + int choice = 0; + + scanf ( "%d", &choice ); + + return choice; +} + +int menu1 ( ) +{ + + printf ( " ==欢迎来到商品管理系统===\r\n" ); + printf ( "1.读取 | 2. 保存 | 3. 打印 | 4. 查询 | 5. 添加\n" + "6.修改 | 7.删除 | 8.排序 | 9.图表 | 0.退出\n\n"); + + //输入选项 + printf ( "请输入想要操作的选项*-*\r\n" ); + int choice = 0; + + scanf ( "%d", &choice ); + return choice; +} + + +int show_goods ( LIST *pList ) +{ + //判断 + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "您的数据库内没有商品!\r\n" ); + return ERROR; + } + + int count = 0; + LIST *p = pList->pNext; + while ( NULL != p ) + { + count++; + printf ( "商品的id: %d\r\n",p->data.id); + printf ( "商品的名称:%s\r\n", p->data.name ); + printf ( "商品的数量:%d\r\n", p->data.count ); + //printf ( "商品的价格:%f\r\n", p->data.price ); + printf ( "\r\n" ); + p = p->pNext; + } + printf ( "商品的种类为 %d \r\n", count ); + return count; +} + + + +void saveData ( LIST *pList ) +{ + //判断 + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "您没有商品信息不用保存\r\n" ); + return ; + } + + FILE *fp =NULL; + fp = fopen ( "ck.csv", "w+" ); + if ( NULL == fp ) + { + printf( "文件打开失败\r\n" ); + return ; + } + LIST *p = NULL; + int count = 0; + + while ( NULL != pList->pNext ) + { + p = pList->pNext; + pList->pNext = p->pNext; + fwrite(&p->data,sizeof(p->data),1,fp); + free (p); + p = NULL; + count++; + } + free ( pList ); + pList = NULL; + + fclose ( fp ); + printf ( "已经保存 %d 种商品信息\r\n" , count ); + + return; +} + +void readData ( LIST *pList ) +{ + //判断 + if ( NULL == pList ) + { + printf ( "读取数据失败!\r\n" ); + return ; + } + FILE *fp = fopen ( "ck.csv", "rb" ); + if ( NULL == fp ) + { + printf( "注意:您的数据库内没有商品信息\r\n" ); + return ; + } + + int count = 0; + int tmp = 1; + LIST *p = NULL; + + p = creation ( ); + fread(&p->data,sizeof(p->data),1,fp); + + while ( !feof(fp) ) + { + //头插法插入节点 + p->pNext = pList->pNext; + pList->pNext = p; + count++; + p = creation ( ); + fread(&p->data,sizeof(p->data),1,fp); + } + printf ( "数据库中有 %d 种商品信息\r\n", count ); + fclose ( fp ); + + return; +} + +int Show( LIST *pList ) +{printf("ID | NAME Amount\n"); + //判断 + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "您的数据库内没有商品!\r\n" ); + return ERROR; + } + + int count = 0; + LIST *p = pList->pNext; + while ( NULL != p ) + { + count++; + printf ( "%d ",p->data.id); + printf ( " %s ", p->data.name ); + printf ( " %d ", p->data.count ); + printf ( "\r\n" ); + p = p->pNext; + } +} +void sort(LIST *pList) +{ + int i ,count = 0, num; + LIST *p, *q, *tail; + p = pList; + while(p->pNext != NULL) + { + count++; + p = p->pNext; + } + for(i = 0; i < count - 1; i++) + { + num = count - i - 1; + q = pList->pNext; + p = q->pNext; + tail = pList; + while(num--) + { + if(q->data.id > p->data.id) + { + q->pNext = p->pNext; + p->pNext = q; + tail->pNext = p; + } + tail = tail->pNext; + q = tail->pNext; + p = q->pNext; + } + } +} + +int main ( ) +{ + LIST *pList = NULL; + pList = creation ( ); + readData ( pList ); + + while ( 1 ) + { + int choice = menu ( ); + int c1; + getchar(); + switch ( choice ) + { + case 1: + c1= menu1(); + if(c1==1) readData ( pList ); + if(c1==2) saveData(pList); + if(c1==3) show_goods(pList); + if(c1==4) find_goods(pList); + if(c1==5) insert(pList); + if(c1==6) increase_count(pList); + if(c1==7) delete_from_list(pList); + if(c1==8) sort(pList); + if(c1==9) Show(pList); + if(c1==0) {saveData(pList); printf("感谢使用");} + break; + case 2: + saveData ( pList ); + printf("感谢使用"); + return 0; + break; + default: + printf ("输入的信息有误,请重新输入!!!\r\n"); + break; + } + } + return 0; +} diff --git a/src/void.c b/src/void.c new file mode 100644 index 0000000..3e17bbe --- /dev/null +++ b/src/void.c @@ -0,0 +1,413 @@ +#include +#include +#include +#include +//常量定义 +#define SIZE 20 + +enum judge +{ + ERROR = -1, + OK, +}; + + +//类型定义 +struct goods +{ int id; + char name [SIZE]; + int count; +}; + +typedef struct goods dataType; + +typedef struct list +{ + dataType data; + struct list *pNext; +}LIST; + +LIST lis; + + +LIST * creation ( ) +{ + LIST *pList = NULL; + pList = ( LIST * ) malloc ( sizeof ( LIST ) ); + if ( NULL == pList ) + { + return NULL; + } + memset ( pList, 0, sizeof ( LIST ) ); + + return pList; +} + +bool add_goods(int id,char name[],int count,LIST *p){ + if ( NULL == p ) + { + return false; + } + //赋值 + strcpy(p->data.name,name); + p->data.count = count; + p->data.id=id; + return true; +} + +int insert ( LIST * pList ) +{ + //判断链表是否存在 + if ( NULL == pList ) + { + return ERROR; + } + LIST *p = creation ( ); + char name[20]; + int count=0; + int id=0; + printf("请输入id:\n"); + scanf("%d",&id); + printf("请输入商品名:\n"); + scanf("%s",&name); + printf("请输入商品数量:\n"); + scanf("%d",&count); + if(add_goods(id,name,count,p)){ + //头插法插入节点 + p->pNext = pList->pNext; + pList->pNext = p; + printf ( "商品 %s 已经添加成功\r\n", p->data.name ); + return OK; + } +} + + + + +int delete_from_list ( LIST * pList ) +{ + //判断链表是否存在,是否为空 + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "数据库数据为空,请先添加商品信息\r\n" ); + return ERROR; + } + int s[SIZE] = {0}; + printf( "请输入需要删除的商品id\r\n" ); + scanf ( "%d", s ); + + LIST *p = pList->pNext; + LIST *q = pList; + + while ( NULL != p ) + { + if ( p->data.id == s[0] ) + { + q->pNext = p->pNext; + free ( p ); + p = NULL; + printf ( "商品 %s 信息已被删除!!\r\n", s ); + return OK; + } + p = p->pNext; + q = q->pNext; + + } + printf ( "您输入的商品名称没有找到!!\r\n" ); + return ERROR; +} + + + +int find_goods ( LIST *pList ) +{ + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "数据库数据为空,请先添加商品信息\r\n" ); + return ERROR; + } + int s[SIZE] = {0}; + printf( "请输入需要查找的商品id\r\n" ); + scanf ( "%d", s ); + + //定义指针变量 + LIST *p = pList->pNext; + while ( NULL != p ) + { + if ( p->data.id==s[0] ) + { printf ("商品的id:%d\r\n ",p->data.id); + printf ( "商品的名称:%s\r\n", p->data.name ); + printf ( "商品的数量:%d\r\n", p->data.count ); + return OK; + } + p = p->pNext; + } + printf ( "输入的商品信息无效!!!\r\n" ); + return ERROR; +} + + +int increase_count ( LIST *pList ) +{ + //判断链表是否存在,是否为空 + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "数据库数据为空,请先添加商品信息\r\n" ); + return ERROR; + } + + int s[SIZE] = {0}; + printf( "请输入需修改的商品id\r\n" ); + scanf ( "%d", s ); + + + LIST *p = pList->pNext; + while ( NULL != p ) + { + + if ( p->data.id==s[0] ) + { + + printf ( "原商品的数量:%d\r\n", p->data.count ); + printf ( "请输入修改的商品数量\r\n" ); + int xiugai; + scanf ( "%d", &xiugai ); + p->data.count+=xiugai; + printf ( "商品数量修改成功 *_* \r\n" ); + return OK; + } + p = p->pNext; + } + printf ( "您输入的商品名称没有找到!!\r\n"); + return ERROR; +} + + +int menu ( ) +{ + + printf ( " ==欢迎来到商品管理系统===\r\n" ); + printf("1.功能菜单 | 2.退出 \n"); + + printf ( "请输入想要操作的选项*-*\r\n" ); + int choice = 0; + + scanf ( "%d", &choice ); + + return choice; +} + +int menu1 ( ) +{ + + printf ( " ==欢迎来到商品管理系统===\r\n" ); + printf ( "1.读取 | 2. 保存 | 3. 打印 | 4. 查询 | 5. 添加\n" + "6.修改 | 7.删除 | 8.排序 | 9.图表 | 0.退出\n\n"); + + //输入选项 + printf ( "请输入想要操作的选项*-*\r\n" ); + int choice = 0; + + scanf ( "%d", &choice ); + return choice; +} + + +int show_goods ( LIST *pList ) +{ + //判断 + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "您的数据库内没有商品!\r\n" ); + return ERROR; + } + + int count = 0; + LIST *p = pList->pNext; + while ( NULL != p ) + { + count++; + printf ( "商品的id: %d\r\n",p->data.id); + printf ( "商品的名称:%s\r\n", p->data.name ); + printf ( "商品的数量:%d\r\n", p->data.count ); + //printf ( "商品的价格:%f\r\n", p->data.price ); + printf ( "\r\n" ); + p = p->pNext; + } + printf ( "商品的种类为 %d \r\n", count ); + return count; +} + + + +void saveData ( LIST *pList ) +{ + //判断 + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "您没有商品信息不用保存\r\n" ); + return ; + } + printf("输入文件名称: "); + char str=getchar(); + FILE *fp =NULL; + fp = fopen ( "data2.csv", "w+" ); + if ( NULL == fp ) + { + printf( "文件打开失败\r\n" ); + return ; + } + LIST *p = NULL; + int count = 0; + + while ( NULL != pList->pNext ) + { + p = pList->pNext; + pList->pNext = p->pNext; + fwrite(&p->data,sizeof(p->data),1,fp); + free (p); + p = NULL; + count++; + } + free ( pList ); + pList = NULL; + + fclose ( fp ); + printf ( "已经保存 %d 种商品信息\r\n" , count ); + + return; +} + +void readData1 ( LIST *pList ) +{ + //判断 + if ( NULL == pList ) + { + printf ( "读取数据失败!\r\n" ); + return ; + } + FILE *fp = fopen ( "data1.csv", "rb" ); + if ( NULL == fp ) + { + printf( "注意:您的数据库内没有商品信息\r\n" ); + return ; + } + + int count = 0; + int tmp = 1; + LIST *p = NULL; + + p = creation ( ); + fread(&p->data,sizeof(p->data),1,fp); + + while ( !feof(fp) ) + { + //头插法插入节点 + p->pNext = pList->pNext; + pList->pNext = p; + count++; + p = creation ( ); + fread(&p->data,sizeof(p->data),1,fp); + } + printf ( "数据库中有 %d 种商品信息\r\n", count ); + fclose ( fp ); + + return; +} +void readData2 ( LIST *pList ) +{ + //判断 + if ( NULL == pList ) + { + printf ( "读取数据失败!\r\n" ); + return ; + } + + printf("输入文件名称: "); + char str=getchar(); + FILE *fp = fopen ( "data2.csv", "rb" ); + if ( NULL == fp ) + { + printf( "注意:您的数据库内没有商品信息\r\n" ); + return ; + } + + int count = 0; + int tmp = 1; + LIST *p = NULL; + + p = creation ( ); + fread(&p->data,sizeof(p->data),1,fp); + + while ( !feof(fp) ) + { + //头插法插入节点 + p->pNext = pList->pNext; + pList->pNext = p; + count++; + p = creation ( ); + fread(&p->data,sizeof(p->data),1,fp); + } + printf ( "数据库中有 %d 种商品信息\r\n", count ); + fclose ( fp ); + + return; +} + +int Show( LIST *pList ) +{printf("ID | NAME Amount\n"); + //判断 + if ( NULL == pList + || NULL == pList->pNext ) + { + printf ( "您的数据库内没有商品!\r\n" ); + return ERROR; + } + + int count = 0; + LIST *p = pList->pNext; + while ( NULL != p ) + { + count++; + printf ( "%d ",p->data.id); + printf ( " %s ", p->data.name ); + printf ( " %d ", p->data.count ); + printf ( "\r\n" ); + p = p->pNext; + } +} +void sort(LIST *pList) +{ + int i ,count = 0, num; + LIST *p, *q, *tail; + p = pList; + while(p->pNext != NULL) + { + count++; + p = p->pNext; + } + for(i = 0; i < count - 1; i++) + { + num = count - i - 1; + q = pList->pNext; + p = q->pNext; + tail = pList; + while(num--) + { + if(q->data.id > p->data.id) + { + q->pNext = p->pNext; + p->pNext = q; + tail->pNext = p; + } + tail = tail->pNext; + q = tail->pNext; + p = q->pNext; + } + } +}