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);
	// 加入链表 
	(*end) -> next = newp;
	(*end) = (*end) -> next;
	// 写入文件
	FILE* fp;
	fp = fopen("data.txt","a");
	fprintf(fp, "\n%d\n", newp -> p.number);
	fprintf(fp, "%s\n", newp -> p.name);
	fprintf(fp, "%f\n", newp -> p.inprice);
	fprintf(fp, "%f\n", newp -> p.outprice);
	fprintf(fp, "%d", newp -> p.count);
	
	printf("成功添加\n");
	fclose(fp);
	getchar();
	getchar();
	return;
}

/*
删除不需要的商品:4 
*/
void delProduct(struct data* head){
	int n;
	printf("输入要删除的商品的编号:\n");
	scanf("%d", &n);
	int flag = 0; // 标记是否存在 
	struct data* tmp = head, *head_t = head;
	head = head -> next;
	while(head != NULL){
		// 找到商品 
		if(head -> p.number == n){
			tmp -> next = head -> next;
			flag = 1;
			break;
		}
		tmp = head;
		head = head -> next;
	}
	if(flag){
		write(head_t);
		printf("删除成功\n"); 
		head_t = head_t -> next;
		while(head_t != NULL){
			printf("%d\n", head_t -> p.number);
			printf("%s\n", head_t -> p.name);
			printf("%f\n", head_t -> p.inprice);
			printf("%f\n", head_t -> p.outprice);
			printf("%d\n", head_t -> p.count);
			head_t = head_t -> next;
		}
	}else{
		printf("该商品不存在\n");
	}
	getchar();
	return;
} 
/*
void getTime(FILE *fp){
	//获取当前系统时间
	time_t tTime;//距离1900年1月1日的秒数
	time(&tTime);
	fprintf(fp, "%lld", tTime);
}
*/
  • 15
    点赞
  • 123
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
由于商品销售管理系统包含多个功能模块和复杂的业务逻辑,因此提供完整的C语言代码是不现实的。不过,我可以给你提供一个简单的示例代码,供你参考: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_ITEMS 1000 typedef struct { char name[50]; float price; int quantity; } Item; Item items[MAX_ITEMS]; int num_items = 0; void add_item() { Item item; printf("Enter name: "); scanf("%s", item.name); printf("Enter price: "); scanf("%f", &item.price); printf("Enter quantity: "); scanf("%d", &item.quantity); items[num_items++] = item; printf("Item added successfully.\n"); } void list_items() { printf("Name\tPrice\tQuantity\n"); for (int i = 0; i < num_items; i++) { printf("%s\t%.2f\t%d\n", items[i].name, items[i].price, items[i].quantity); } } void sell_item() { char name[50]; printf("Enter name of item to sell: "); scanf("%s", name); for (int i = 0; i < num_items; i++) { if (strcmp(items[i].name, name) == 0) { int quantity; printf("Enter quantity to sell: "); scanf("%d", &quantity); if (quantity > items[i].quantity) { printf("Not enough stock.\n"); } else { items[i].quantity -= quantity; printf("Sale completed successfully.\n"); } return; } } printf("Item not found.\n"); } int main() { int choice; while (1) { printf("1. Add item\n"); printf("2. List items\n"); printf("3. Sell item\n"); printf("4. Quit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: add_item(); break; case 2: list_items(); break; case 3: sell_item(); break; case 4: exit(0); default: printf("Invalid choice.\n"); } } return 0; } ``` 该程序实现了商品销售管理系统的基本功能,包括添加商品、列出商品、销售商品和退出系统。当用户选择添加商品时,程序会让用户输入商品的名称、价格和数量,并将其添加到系统中。当用户选择列出商品时,程序会列出系统中所有的商品信息。当用户选择销售商品时,程序会询问用户要出售的商品名称和数量,并根据库存更新商品数量。如果库存不足,则销售操作失败。当用户选择退出系统时,程序会退出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程ID

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值