You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
419 lines
7.3 KiB
419 lines
7.3 KiB
//sjandy sjandsy sjandsy
|
|
#include <bits/stdc++.h>
|
|
#define Rl 1000
|
|
using namespace std;
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
//常量定义
|
|
#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;
|
|
}
|