C语言-商品销售管理系统_c语言编程商场销售管理系统代码

C语言-商品销售管理系统

全部代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
#include<time.h> 
// 商品结构体 
typedef struct
{
	int number;
	char name[20];
	float inprice;
	float outprice;
	int count;
}product;

// 存放数据的链表 
struct data{
	product p;
	struct data \* next;
};

/\*
函数声明 
\*/
void menu(int\* choose);											//功能菜单 
struct data\* load(struct data\* head);					//加载数据 
void addProduct(struct data\*\* end);						//添加商品 
void delProduct(struct data\* head);						//删除商品 
void write(struct data\* head);							//写入数据 
void purchase(struct data\* head);						//商品进货 
void settlement(struct data\* head, struct data\* res);	//商品结算
//void getTime(FILE \*fp);
void writelist(struct data\* res);						//写入清单 
/\*
主函数 
\*/ 
int main(){
	// 模式 
	int choose;
	// 销售清单 
	struct data\* res = (struct data\*) malloc(sizeof(struct data));
	res -> next = NULL;
	
	struct data\* head = (struct data\* )malloc(sizeof(struct data));
	struct data\* end = NULL;
	head -> next = NULL;
	while(1){
		menu(&choose);
		end = load(head);
		if(choose == 1){
			purchase(head);
		}else if(choose == 2){
			settlement(head, res);
			writelist(res); 
		}else if(choose == 3){
			addProduct(&end);
		}else if(choose == 4){
			delProduct(head);
		}
	}
	
	return 0;
}

/\*
功能菜单:
 进货:1
 结算:2
 添加:3
 删除:4 
\*/
void menu(int\* choose){
	// 清屏 
	system("cls");
	// 交互界面 
	printf("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\n");
	printf("\* 商店销售管理系统 \*\n"); 
	printf("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\n");
	printf("\* 输入 1 进货 \*\n");
	printf("\* 输入 2 结算 \*\n");
	printf("\* 输入 3 添加新的商品 \*\n");
	printf("\* 输入 4 删除不需要的商品 \*\n");
	printf("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\n");
	// 输入模式 
	scanf("%d", choose);
	return; 
}
/\*
读取数据 
\*/
struct data\* load(struct data \*head){
	FILE\* fp;
	if((fp = fopen("data.txt","r")) == NULL){
		printf("加载存档时发生错误\n");
		exit(1);
	}
	// 临时存放文件数据 
	int number\_t;
	char name\_t[20];
	float inprice\_t;
	float outprice\_t;
	int count\_t;
	struct data \*q = head;
	// 读取 
	while(!feof(fp)){
		fscanf(fp, "%d", &number\_t);
		fscanf(fp, "%s", name\_t);
		fscanf(fp, "%f", &inprice\_t);
		fscanf(fp, "%f", &outprice\_t);
		fscanf(fp, "%d", &count\_t);
		struct data \*tmp = (struct data\* )malloc(sizeof(struct data));
		tmp -> next = NULL;
		// 存入链表 
		tmp -> p.number = number\_t;
		strcpy(tmp -> p.name, name\_t);
		tmp -> p.inprice = inprice\_t;
		tmp -> p.outprice = outprice\_t;
		tmp -> p.count = count\_t;
		q -> next = tmp;
		q = q -> next;
		//printf("%d %s %f %f %d", number\_t, name\_t, inprice\_t, outprice\_t, count\_t);
		
	}
	fclose(fp);
	return q; 
} 
/\*
清单写入磁盘 
\*/
void writelist(struct data\* res){
	FILE\* fp;
	fp = fopen("list.txt","a");
	res = res -> next;
	while(res != NULL){
		fprintf(fp, "\n%d %s %f %f %d \n", res -> p.number, res -> p.name, res -> p.inprice, res -> p.outprice, res -> p.count);
		//fprintf(fp, "%s\n", res -> p.name);
		//fprintf(fp, "%f\n", res -> p.inprice);
		//fprintf(fp, "%f\n", res -> p.outprice);
		//fprintf(fp, "%d\n", res -> p.count);
		res = res -> next;
	}
	
	fclose(fp);
	printf("数据写入成功\n");
	return;
}
/\*
数据写入磁盘 
\*/
void write(struct data\* head){
	system("del data.txt");
	FILE\* fp;
	fp = fopen("data.txt","w+");
	head = head -> next;
	while(head != NULL){
		fprintf(fp, "\n%d\n", head -> p.number);
		fprintf(fp, "%s\n", head -> p.name);
		fprintf(fp, "%f\n", head -> p.inprice);
		fprintf(fp, "%f\n", head -> p.outprice);
		fprintf(fp, "%d", head -> p.count);
		head = head -> next;
	}
	
	fclose(fp);
	printf("数据写入成功\n");
	return;
}

/\*
进货:1 
\*/
void purchase(struct data\* head){
	printf("提示:输入-1完成进货\n");
	int n, cnt;
	while(1){
		printf("输入商品的编号:\n");
		scanf("%d", &n);
		if(n == -1) break;
		printf("输入进货数量:\n"); 
		scanf("%d", &cnt) ;
		
		struct data\* q = head;
		q = q -> next;
		while(q != NULL){
			if(q -> p.number == n){
				q -> p.count += cnt;
				break;
			}
			q = q -> next;
		}
	}
	write(head);
	printf("进货完成\n");
	getchar();
	getchar();
	
}

/\*
结算 
\*/
void settlement(struct data\* head, struct data\* res){
	// 总售价 
	double money_all = 0;
	printf("提示:输入-1进行结算\n");
	int n,cnt;
	
	struct data\* tmp;
	// 工作指针 
	tmp = res;
	while(1){
		printf("输入商品编号:\n");
		scanf("%d", &n);
		// 结算
		if(n == -1){
			struct data\* tmp2 = res;
			tmp2 = tmp2 -> next;
			int i = 1;
			while(tmp2 != NULL){
				printf("项目%d 编号:%d 商品名称:%s 售价:%f 数量:%d\n", i, tmp2->p.number, tmp2->p.name, tmp2->p.outprice, tmp2->p.count);
				money_all += (tmp2 -> p.outprice) \* (tmp2 -> p.count);
				i++;
				tmp2 = tmp2 -> next;
			}
			printf("总计:%lf元\n",money_all);
			break;
		} 
		printf("输入购买数量:\n"); 
		scanf("%d", &cnt);
		
		// 寻找商品信息 
		struct data\* q = head;
		q = q -> next;
		while(q != NULL){
			if(q -> p.number == n){
				q -> p.count -= cnt;
				struct data\* newp = (struct data\*) malloc(sizeof(struct data));
				newp -> next = NULL;
				newp -> p.number = q -> p.number;
				strcpy(newp -> p.name, q -> p.name);
				newp -> p.inprice = q -> p.inprice;
				newp -> p.outprice = q -> p.outprice;
				// 此时count代表购买数量 
				newp -> p.count = cnt;
				tmp -> next = newp;
				tmp = tmp -> next;
				break;
			}
			q = q -> next;
		}
	}
	write(head);
	//return res; 
	getchar();
	getchar();
	return;
	
}


/\*
添加新的商品: 3 
\*/
void addProduct(struct data\*\* end){
	struct data \*newp = (struct data\* )malloc(sizeof(struct data));
	newp -> next =  NULL;
	printf("输入新商品的编号:\n");
	scanf("%d", &newp -> p.number);
	printf("输入新商品的名字:\n");
	scanf("%s", newp -> p.name);
	printf("输入新商品的进价:\n");
	scanf("%f", &newp -> p.inprice);
	printf("输入新商品的售价:\n");
	scanf("%f", &newp -> p.outprice);
	printf("输入新商品的库存数量:\n");
	scanf("%d", &newp -> p.count);
	// 加入链表 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值