您的位置:首页 > 其它

商品信息管理系统

2015-01-29 21:05 330 查看
大一时候的东西,今天又翻了出来,现在看看其实很简单,没有使用复杂的数据结构,数据是存在文件里面的,当时觉得挺深奥的。算了,还是发个博客,算作对过去的一种纪念吧。

注:本系统使用Microsoft Visual Studio 2013编译,其他编译器不能保证顺利编译

本系统主要分三部分,主文件“commoditymanage.cpp”,功能文件"Funtion.h",声明文件"State.h",代码如下:

commoditymanage.cpp

#include"State.h"
#include"Funtion.h"
int main(int argc, char* argv[])
{
	int n = 1;
	while (n)
	{
		system("CLS");
		menu();
		scanf_s("%d", &n);
		switch (n)
		{
		case 1:	in(); break;
		case 2:	search();break;
		case 3:	del();break;
		case 4:	modify();break;
		case 5:	sort();break;
		case 6:toal(); break;
		case 0:break;
		default:
			printf_s("\tYour num is fail."); 
			system("pause"); 
			break;
		}
	}
	printf_s("\tThe system will quit!\n");
	fcloseall;
	return 0;
}


Funtion.h

//菜单选项栏
void menu()
{
	system("cls");
	printf_s("\n\n");
	printf_s("\t|-------------------commodity management---------------------|\n");
	printf_s("\t|\t1. 录入记录                                          |\n");
	printf_s("\t|\t2. 查找记录                                          |\n");
	printf_s("\t|\t3. 删除记录                                          |\n");
	printf_s("\t|\t4. 修改记录                                          |\n");
	printf_s("\t|\t5. 排序记录                                          |\n");
	printf_s("\t|\t6. 统计记录                                          |\n");
	printf_s("\t|\t0. 退出系统                                          |\n");
	printf_s("\t|------------------------------------------------------------|\n");
	printf_s("\tPlease input num( 0 - 6 ): ");
}

//录入商品信息
void in()
{
	FILE *fp;
	int m;
	int j;
	int flag = 0;
	char ch;
	m = openfilerb("ab+");
	if (m == 0)
	{
		printf_s("\nHave no goods.");
	}
	else
	{
		system("CLS");
		show(m);
	}
	getchar();
	printf_s("\nWhether to need input data?(Y/N) :");
	scanf_s("%c", &ch, 1);
	if (ch == 'Y' || ch == 'y')
	{
		flag = 1;
	}
	for (int i = m; i <= N && flag; i++)
	{
		printf_s("Num: ");
		scanf_s("%d", &comm[i].num);
		getchar();
		for (j = 0; j < m; j++)
		{
			if (comm[i].num == comm[j].num)
			{
				printf_s("The num Repetition.Please input again.\n");
				break;
			}
		}
		if (j < m)
		{
			continue;
		}
		printf_s("Name: ");
		gets_s(comm[i].name);
		printf_s("Price: ");
		scanf_s("%f", &comm[i].price);
		printf_s("count: ");
		scanf_s("%f", &comm[i].count);
		getchar();
		comm[i].toal = comm[i].price*comm[i].count;
		fopen_s(&fp, "data.dat", "ab+");
		fwrite(&comm[i], LEN, 1, fp);
		fclose(fp);
		printf_s("data(%s) save success.\n", comm[i].name);
		printf_s("Weather input again(y/n): ");
		scanf_s("%c", &ch, 1);
		getchar();
		if (ch == 'Y' || ch == 'y')
		{
			continue;
		}
		flag = 0;
	}
}

//查找商品信息
void search()
{
	errno_t err;
	FILE *fp;
	int m = 0, snum, flag = 1;
	int i;
	err = fopen_s(&fp, "data.dat", "rb");
	if (err)
	{
		printf_s("The file open fail.");
		return;
	}
	while (!feof(fp))
	{
		if (fread(&comm[m], LEN, 1, fp) == 1)
		{
			m++;
		}
	}
	if (m == 0)
	{
		printf_s("The file empty!");
		return;
	}
	system("CLS");
	while (flag)
	{
		printf_s("Please input num (When input '-1' quit): ");
		scanf_s("%d", &snum);
		if (snum == -1)
		{
			flag = 0;
		}
		for (i = 0; i < m; i++)
		{
			if (comm[i].num == snum)
			{
				printf_s("Num: %d\n", comm[i].num);
				printf_s("Name: %s\n", comm[i].name);
				printf_s("Price: %.2f\n", comm[i].price);
				printf_s("Count: %.2f\n", comm[i].count);
				printf_s("Toal: %.2f\n", comm[i].toal);
				break;
			}
		}
		if (i == m)
		{
			printf_s("It find no data!\n");
		}
	}
	fclose(fp);
}

//删除商品信息
void del()
{
	errno_t err;
	FILE *fp;
	int m , snum, flag = 1, i;
	char ch;
	m = openfilerb("rb");
	system("CLS");
	if (m == 0)
	{
		printf_s("The file empty.\n");
		return;
	}
	while (flag)
	{
		printf_s("Please input the num that you will delete: ");
		scanf_s("%d", &snum);
		for (i = 0; i < m; i++)
		{
			if (comm[i].num == snum)
			{
				for (int j = i; j < m; j++)
				{
					comm[j] = comm[j + 1];
				}
				m--;
				break;
			}
		}
		if (i == m)
		{
			printf_s("It can't find num! \n");
		}
		else
		{
			openfilewb(m, "wb");
			printf_s("Delete success.\n");
		}
		getchar();
		printf_s("Weather to delete again[Y/N]:");
		scanf_s("%c", &ch, 1);
		if (ch == 'Y' || ch == 'y')
		{
			continue;
		}
		flag = 0;
	}
}

//修改记录
void modify()
{
	int m = 0, i;
	int flag = 1, snum;
	char ch;
	m = openfilerb("ab+");
	if (m == 0)
	{
		printf_s("There is no data.");
		return;
	}
	system("CLS");
	while (flag)
	{
		printf_s("Input the num that you will modify: ");
		scanf_s("%d", &snum);
		getchar();
		for (i = 0; i < m; i++)
		{
			if (comm[i].num == snum)
			{
				printf_s("\nNmae: ");
				gets_s(comm[i].name);
				printf_s("Price: ");
				scanf_s("%f", &comm[i].price);
				printf_s("Count: ");
				scanf_s("%f", &comm[i].count);
				comm[i].toal = comm[i].price*comm[i].count;
				break;
			}
		}
		if (i == m)
		{
			printf_s("It find not your num!\n");
		}
		else
		{
			openfilewb(m,"wb");
			printf_s("modify success.\n");
		}
		printf_s("Weather modify others?(Y/N): ");
		scanf_s("%c", &ch, 1);
		getchar();
		if (ch == 'Y' || ch == 'y')
		{
			continue;
		}
		flag = 0; 
	}
}

//排序记录
void sort()
{
	int m;
	m = openfilerb("rb");
	if (m == 0)
	{
		printf_s("There is no data.");
		return;
	}
	struct commdity temp;
	for (int i = 1; i < m; i++)
	{
		for (int j = 0; j < m - 1; j++)
		{
			if (comm[j].num>comm[j + 1].num)
			{
				temp = comm[j];
				comm[j] = comm[j + 1];
				comm[j + 1] = temp;
			}
		}
	}
	openfilewb(m, "wb");
	system("CLS");
	printf_s("Sort success.\n");
	system("pause");
}

//统计记录
void toal()
{
	int m;
	m = openfilerb("rb");
	if (m == 0)
	{
		system("CLS");
		printf_s("There is no goods.");
	}
	else
	{
		system("CLS");
		printf_s("It have %d puchase.", m);
	}
	system("pause");
}

//----------------------------------------------------

//显示记录
void show(int m)
{
	printf_s("\n|Num    |Name   |Price  |Count  |Toal[Price*count]\n");
	for (int i = 0; i < m; i++)
	{
		printf_s(" %d\t", comm[i].num);
		printf_s(" %s\t", comm[i].name);
		printf_s(" %.2f\t", comm[i].price);
		printf_s(" %.2f\t", comm[i].count);
		printf_s(" %.2f\n", comm[i].toal);
	}
}

//以"rb"方式打开文件
int openfilerb(char way[])
{
	errno_t err;
	FILE *fp;
	int m = 0;
	err = fopen_s(&fp, "data.dat", way);
	if (err)
	{
		printf_s("The file open fail.");
		exit(0);
	}
	while (!feof(fp))
	{
		if (fread(&comm[m], LEN, 1, fp))
		{
			m++;
		}
	}
	fclose(fp);
	return m;
}

//以"wb"方式或"ab+"方式打开文件并写入
void openfilewb(int m, char way[])
{
	errno_t err, war;
	FILE *fp;
	err = fopen_s(&fp, "data.dat", way);
	if (err)
	{
		printf_s("Open file fail.\n");
		return;
	}
	for (int i = 0; i < m; i++)
	{
		war = fwrite(&comm[i], LEN, 1, fp);
		if (war != 1)
		{
			printf_s("Write data.dat fail.");
			break;
		}
	}
	fclose(fp);
}


State.h

#include"stdio.h"
#include"stdlib.h"

struct commdity
{
	int num;
	char name[15];
	float price;
	float count;
	float toal;
};
struct commdity comm[50];

#define N 50
#define LEN sizeof(struct commdity)

void show(int);
int openfilerb(char *);
void openfilewb(int,char*);

void in();
void search();
void del();
void modify();
void sort();
void toal();


效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: