大一C语言课设---服装销售系统代码实现与总结

  大一以为期一个星期的课设结束,按课程需求,设计出了一个服装销售系统,在这里总结经验并分享。下面是具体需求:

   题目要求利用堆区创建数组实现,则在这里选用顺序表作为底层数据结构,来储存数据。

输入记录模块 

 定义一个结构体,其中存放服装信息,之后声明顺序表,即创建好了一个顺序表,其中包括一个结构体类型的数组,和两个整形一个记录顺序表的空间大小另一个记录顺序表当前有效的数据个数。再用顺序表的初始化函数对其进行初始化。之后用fopen等函数对新创建好的数据结构进行初始化。

查询记录模块 

   核心用字符串比较函数strcmp通过对顺序表里的数据访问并对要找出的服装编号或者尺码进行比较来确定下标,从而进行定位,最后挨个打印信息即可。

更新记录模块

  增加服装信息定义新的结构体再用顺序表的尾插即可,修改与删除操作需要先通过编号找到服装之后在进行删除修改操作。 

统计记录模块 

   统计库存数最多的寻找可以定义一个整形来记录最多库存数衣服的下标,之后遍历找到比之前大的的则更行这个整形,统计库存数不足的,只需正常访问每个服装的库存量与3比较,符合条件打印信息即可。

输出记录模块 

   挨个遍历输出即可。

下面是代码实现:

seqList.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"System.h"


//typedef int SLDataType;
typedef Info SLDataType;

typedef struct SeqList
{
	SLDataType* arr;//储存数据的底层结构
	int capacity;//记录顺序表的空间大小
	int size;//记录顺序表当前有效的数据个数
}SL;

//初始化和销毁
void SLInit(SL* ps);
void SLDestory(SL* ps);
void SLPrint(SL* ps);

//顺序表的头部/尾部插入
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);

//顺序表的头部/尾部删除
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);

//指定位置之前插入数据
//指定位置删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);

 seqList.cpp

#include"SeqList.h"
//初始化和销毁
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc");
			
		}
		ps->capacity = newCapacity;
		ps->arr = tmp;
	}
}
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	/*if (ps == NULL)
	{
		return;
	}*/

	//空间不够扩容
	SLCheckCapacity(ps);

	//空间足够,直接插入
	ps->arr[ps->size++] = x;
	// ps->size++;
}
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}


void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}

void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}




void SLDestory(SL* ps)
{
	assert(ps);
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}

 System.h

#pragma once
#define NUMBER_MAX 1000
#define NAME_MAX 10
#define SIZEMAX 3
typedef struct ClothInfo
{
	char number[NUMBER_MAX];
	char name[NAME_MAX];
	char size[SIZEMAX];
	int price;
	int amount;
}Info;



struct SeqList;
typedef struct SeqList Cloth;
void SystemInit(Cloth* pcon);
void LoadFile(Cloth* pcon);
void SystemDesTroy(Cloth* pcon);
void SystemAdd(Cloth* pcon);
void SystemDel(Cloth* pcon);
void SystemModify(Cloth* pcon);
void SystemFind(Cloth* pcon);
void SystemShow(Cloth* pcon);
void SystemFindBySize(Cloth* pcon);
void MostAmount(Cloth* pcon);
void LackAmount(Cloth* pcon);

 System.cpp

 

#include"System.h"
#include"SeqList.h"
#include<ctype.h>
#include<string.h>


void SystemInit(Cloth* pcon)
{
	SLInit(pcon);
}

void SystemDesTroy(Cloth* pcon)
{
	SLDestory(pcon);
}

void SystemAdd(Cloth* pcon)
{
	Info info;
	printf("请输入服装编号:\n");
	scanf("%s", info.number);
	printf("请输入服装名称:\n");
	scanf("%s", info.name);
	printf("请输入服装尺码:\n");
	scanf("%s", info.size);
	printf("请输入服装价格:\n");
	scanf("%d", &info.price);
	printf("请输入服装库存数量:\n");
	scanf("%d", &info.amount);

	SLPushBack(pcon, info);
}

int FindByNumber(Cloth* pcon, char number[])
{
	for (int i = 0; i < pcon->size; i++)
	{
		if (strcmp(pcon->arr[i].number, number) == 0)
		{
			return i;
		}
	}
	return -1;
}

void SystemDel(Cloth* pcon)
{
	printf("请输入要删除的服装编号:\n");
	char number[NUMBER_MAX];
	scanf("%s", number);

	int findIndex = FindByNumber(pcon, number);
	if (findIndex < 0)
	{
		printf("服装信息不存在!\n");
		return;
	}
	SLErase(pcon, findIndex);
	printf("服装信息删除成功!\n");
}

void SystemModify(Cloth* pcon)
{
	char number[NUMBER_MAX];
	printf("请输入要修改的服装信息的编号:\n");
	scanf("%s", number);
	int findIndex = FindByNumber(pcon, number);
	if (findIndex < 0)
	{
		printf("要修改的服装信息不存在!\n");
		return;
	}
	printf("请输入编号:\n");
	scanf("%s", pcon->arr[findIndex].number);
	printf("请输入名称:\n");
	scanf("%s", pcon->arr[findIndex].name);
	printf("请输入尺码:\n");
	scanf("%s", pcon->arr[findIndex].size);
	printf("请输入价格:\n");
	scanf("%d", &pcon->arr[findIndex].price);
	printf("请输入库存数量:\n");
	scanf("%d", &pcon->arr[findIndex].amount);
	printf("服装信息修改成功!\n");

}

void LoadFile(Cloth* pcon) {
	assert(pcon != NULL);
	FILE* pf = fopen("C:\\数据库.txt", "r");
	if (NULL == pf) {
		printf("open file fail\n");
		return;
	}
	int size;
	fscanf(pf, "%d", &size);
	for (int i = 0; i < size; i++) {
		Info g;
		fscanf(pf, "%s %s %s %d %d", g.number, g.name, g.size, &g.price, &g.amount);
		SLPushBack(pcon, g);
		// pcon[i] = g;
	}
	fclose(pf);
	pf = NULL;
}
void SystemShow(Cloth* pcon)
{
	
	printf("%s %s %s %s %s \n", "编号", "名称", "尺码", "价格", "库存数量");
	for (int i = 0; i < pcon->size; i++)
	{
		printf("%s %s %s %d %d\n",
			pcon->arr[i].number,
			pcon->arr[i].name,
			pcon->arr[i].size,
			pcon->arr[i].price,
			pcon->arr[i].amount);
	}
}
void SystemFind(Cloth* pcon)
{
	char number[NUMBER_MAX];
	printf("请输入要查找的服装编号:\n");
	scanf("%s", number);
	int findIndex = FindByNumber(pcon, number);
	if (findIndex < 0)
	{
		printf("该服装不存在!\n");
		return;
	}
	printf("%s %s %s %s %s \n", "编号", "名称", "尺码", "价格", "数量");
	printf("%s %s %s %d %d\n",
		pcon->arr[findIndex].number,
		pcon->arr[findIndex].name,
		pcon->arr[findIndex].size,
		pcon->arr[findIndex].price,
		pcon->arr[findIndex].amount);
}

int FindBySize(Cloth* pcon, char size[])
{
	bool flag = false;
	for (int i = 0; i < pcon->size; i++)
	{
		if (strcmp(pcon->arr[i].size, size) == 0)
		{
			printf("%s %s %s %s %s \n", "编号", "名称", "尺码", "价格", "数量");
			printf("%s %s %s %d %d\n",
				pcon->arr[i].number,
				pcon->arr[i].name,
				pcon->arr[i].size,
				pcon->arr[i].price,
				pcon->arr[i].amount);

			flag = true;
		}
	}
	return -1;
}

void SystemFindBySize(Cloth* pcon)
{
	char size[SIZEMAX];
	printf("请输入要查找的服装尺码:\n");
	scanf("%s", size);
    int findIndex=FindBySize(pcon, size);
	if (findIndex < 0)
	{
		/*printf("该服装不存在!\n");*/
		return;
	}
	/*printf("%s %s %s %s %s \n", "编号", "名称", "尺码", "价格", "数量");
	printf("%s %s %s %d %d\n",
		pcon->arr[findIndex].number,
		pcon->arr[findIndex].name,
		pcon->arr[findIndex].size,
		pcon->arr[findIndex].price,
		pcon->arr[findIndex].amount);*/
}

void MostAmount(Cloth* pcon)  
{
	int i;
	int most = pcon->arr[0].amount;
	int flag = 0;  //最多的  下标的标志
	for (i = 0; i < pcon->size; i++)
	{
		
		if (most < pcon->arr[i].amount)
		{
			most = pcon->arr[i].amount;
			flag = i;  //更新标志
		}
	}
	printf("%s %s %s %s %s \n", "编号", "名称", "尺码", "价格", "数量");
	printf("%s %s %s %d %d\n", 
		pcon->arr[flag].number, //最后根据flag来打印   
		pcon->arr[flag].name,
		pcon->arr[flag].size,
		pcon->arr[flag].price,
		pcon->arr[flag].amount);
}

void LackAmount(Cloth* pcon)
{
	int cont = 0;
	for (int i = 0; i < pcon->size; i++)
	{
		if (pcon->arr[i].amount <= 3)
		{
			cont++;
			printf("%s %s %s %s %s \n", "编号", "名称", "尺码", "价格", "数量");
			printf("%s %s %s %d %d\n",
				pcon->arr[i].number,
				pcon->arr[i].name,
				pcon->arr[i].size,
				pcon->arr[i].price,
				pcon->arr[i].amount);
		}
		else
		{
			printf("无库存短缺的服装。\n");
		}
	}
}

SysTest.c

#include"SeqList.h"

void menu()
{
	printf("*****************管理系统******************\n");
	printf("*****1.添加服装信息  2.删除服装信息********\n");
	printf("*****3.修改服装信息  4.编号查找信息********\n");
	printf("*****5.尺码查找信息  6.统计服装信息********\n");
	printf("*****7.查找库存最多  8.查找库存不足********\n");
	printf("*****0. 退    出     **********************\n");
}
int main()
{
	int op = -1;
	Cloth con;
	SystemInit(&con);
	LoadFile(&con);
	do
	{
		menu();
		printf("请输入你的操作:\n");
		scanf("%d", &op);
		switch (op)
		{
		case 1:
			SystemAdd(&con);
			break;
		case 2:
			SystemDel(&con);
			break;
		case 3:
			SystemModify(&con);
			break;
		case 4:
			SystemFind(&con);
			break;
		case 5:
			SystemFindBySize(&con);
			break;
		case 6:
			SystemShow(&con);
			break;
		case 7:
			MostAmount(&con);
			break;
		case 8:LackAmount(&con);
			break;
		case 0:
			printf("系统退出中...\n");
			break;
		default:
			break;
		}

	} while (op != 0);
	SystemDesTroy(&con);
	return 0;
}

 下面是数据库,这个代码缺点就是可以从数据库中读取数据,但是在进行增删查改等一系列操作后无法把更新后的数据储存到数据库中,读者如果有好的办法可以在评论区分享。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值