c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PRODUCTS 50
typedef struct {
char number[3];
char name[20];
float price;
} Product;
typedef struct {
char number[3];
int quantity;
} Purchase;
typedef struct {
char user[10];
Purchase purchases[MAX_PRODUCTS];
int numPurchases;
} Customer;
void readProducts(Product products[], int *numProducts);
void readCustomers(Customer customers[], int *numCustomers);
void printTotalAmount(Customer customers[], int numCustomers, Product products[], int numProducts);
void printTotalQuantityAndAmount(Product products[], int numProducts, Customer customers[], int numCustomers);
void printTop3Quantity(Product products[], int numProducts, Customer customers[], int numCustomers);
void printTop3Amount(Product products[], int numProducts, Customer customers[], int numCustomers);
void printPurchasedProducts(Product products[], int numProducts, Customer customers[], int numCustomers);
void printCommonProducts(Product products[], int numProducts, Customer customers[], int numCustomers);
void printNoPurchasedProducts(Product products[], int numProducts, Customer customers[], int numCustomers);
int main() {
Product products[MAX_PRODUCTS];
Customer customers[MAX_PRODUCTS];
int numProducts, numCustomers;
// 读取商品信息和用户购买清单
readProducts(products, &numProducts);
readCustomers(customers, &numCustomers);
// 统计并打印相关信息
printf("每个客户的购物总额:\n");
printTotalAmount(customers, numCustomers, products, numProducts);
printf("\n");
printf("每种商品的销售总数量和销售总金额:\n");
printTotalQuantityAndAmount(products, numProducts, customers, numCustomers);
printf("\n");
printf("销售总数量最多的三种商品及数量:\n");
printTop3Quantity(products, numProducts, customers, numCustomers);
printf("\n");
printf("销售总金额最多的三种商品及数量:\n");
printTop3Amount(products, numProducts, customers, numCustomers);
printf("\n");
printf("有人购买的商品编号和名称:\n");
printPurchasedProducts(products, numProducts, customers, numCustomers);
printf("\n");
printf("每个人都购买的商品编号和名称:\n");
printCommonProducts(products, numProducts, customers, numCustomers);
printf("\n");
printf("无人购买的商品编号和名称:\n");
printNoPurchasedProducts(products, numProducts, customers, numCustomers);
printf("\n");
return 0;
}
// 读取商品信息
void readProducts(Product products[], int *numProducts) {
FILE *file = fopen("product.txt", "r");
if (file == NULL) {
printf("无法打开文件product.txt");
exit(1);
}
*numProducts = 0;
while (!feof(file)) {
fscanf(file, "%s %s %f", products[*numProducts].number, products[*numProducts].name, &products[*numProducts].price);
(*numProducts)++;
}
fclose(file);
}
// 读取用户购买清单
void readCustomers(Customer customers[], int *numCustomers) {
FILE *file = fopen("shopping.txt", "r");
if (file == NULL) {
printf("无法打开文件shopping.txt");
exit(1);
}
*numCustomers = 0;
char buffer[100];
while (!feof(file)) {
if (fgets(buffer, sizeof(buffer), file) != NULL) {
if (buffer[0] != '\n') {
sscanf(buffer, "%[^:]", customers[*numCustomers].user);
customers[*numCustomers].numPurchases = 0;
while (fgets(buffer, sizeof(buffer), file) != NULL) {
if (buffer[0] == '\n') {
break;
}
sscanf(buffer, "%s %d", customers[*numCustomers].purchases[customers[*numCustomers].numPurchases].number, &customers[*numCustomers].purchases[customers[*numCustomers].numPurchases].quantity);
customers[*numCustomers].numPurchases++;
}
(*numCustomers)++;
}
}
}
fclose(file);
}
// 打印每个客户的购物总额
void printTotalAmount(Customer customers[], int numCustomers, Product products[], int numProducts) {
for (int i = 0; i < numCustomers; i++) {
float totalAmount = 0;
for (int j = 0; j < customers[i].numPurchases; j++) {
for (int k = 0; k < numProducts; k++) {
if (strcmp(customers[i].purchases[j].number, products[k].number) == 0) {
totalAmount += products[k].price * customers[i].purchases[j].quantity;
}
}
}
printf("%s 的购物总额为:%.2f\n", customers[i].user, totalAmount);
}
}
// 打印每种商品的销售总数量和销售总金额
void printTotalQuantityAndAmount(Product products[], int numProducts, Customer customers[], int numCustomers) {
for (int i = 0; i < numProducts; i++) {
int totalQuantity = 0;
float totalAmount = 0;
for (int j = 0; j < numCustomers; j++) {
for (int k = 0; k < customers[j].numPurchases; k++) {
if (strcmp(customers[j].purchases[k].number, products[i].number) == 0) {
totalQuantity += customers[j].purchases[k].quantity;
totalAmount += products[i].price * customers[j].purchases[k].quantity;
}
}
}
printf("商品编号:%s,名称:%s,销售总数量:%d,销售总金额:%.2f\n", products[i].number, products[i].name, totalQuantity, totalAmount);
}
}
// 打印销售总数量最多的三种商品及数量
void printTop3Quantity(Product products[], int numProducts, Customer customers[], int numCustomers) {
int maxQuantity[3] = {0};
int maxQuantityIndex[3] = {-1, -1, -1};
for (int i = 0; i < numProducts; i++) {
int totalQuantity = 0;
for (int j = 0; j < numCustomers; j++) {
for (int k = 0; k < customers[j].numPurchases; k++) {
if (strcmp(customers[j].purchases[k].number, products[i].number) == 0) {
totalQuantity += customers[j].purchases[k].quantity;
}
}
}
for (int j = 0; j < 3; j++) {
if (totalQuantity > maxQuantity[j]) {
for (int k = 2; k > j; k--) {
maxQuantity[k] = maxQuantity[k - 1];
maxQuantityIndex[k] = maxQuantityIndex[k - 1];
}
maxQuantity[j] = totalQuantity;
maxQuantityIndex[j] = i;
break;
}
}
}
for (int i = 0; i < 3; i++) {
if (maxAmountIndex[i] >= 0) {
printf("商品编号:%s,名称:%s,销售总数量:%d\n", products[maxQuantityIndex[i]].number, products[maxQuantityIndex[i]].name, maxQuantity[i]);
}
}
}
// 打印销售总金额最多的三种商品及数量
void printTop3Amount(Product products[], int numProducts, Customer customers[], int numCustomers) {
float maxAmount[3] = {0};
int maxAmountIndex[3] = {-1, -1, -1};
for (int i = 0; i < numProducts; i++) {
float totalAmount = 0;
for (int j = 0; j < numCustomers; j++) {
for (int k = 0; k < customers[j].numPurchases; k++) {
if (strcmp(customers[j].purchases[k].number, products[i].number) == 0) {
totalAmount += products[i].price * customers[j].purchases[k].quantity;
}
}
}
for (int j = 0; j < 3; j++) {
if (totalAmount > maxAmount[j]) {
for (int k = 2; k > j; k--) {
maxAmount[k] = maxAmount[k - 1];
maxAmountIndex[k] = maxAmountIndex[k - 1];
}
maxAmount[j] = totalAmount;
maxAmountIndex[j] = i;
break;
}
}
}
for (int i = 0; i < 3; i++) {
if (maxAmountIndex[i] >= 0) {
printf("商品编号:%s,名称:%s,销售总金额:%.2f\n", products[maxAmountIndex[i]].number, products[maxAmountIndex[i]].name, maxAmount[i]);
}
}
}
// 打印有人购买的商品编号和名称
void printPurchasedProducts(Product products[], int numProducts, Customer customers[], int numCustomers) {
int purchased[MAX_PRODUCTS] = {0};
for (int i = 0; i < numCustomers; i++) {
for (int j = 0; j < customers[i].numPurchases; j++) {
int index = atoi(customers[i].purchases[j].number) - 1;
purchased[index] = 1;
}
}
for (int i = 0; i < numProducts; i++) {
if (purchased[i] == 1) {
printf("商品编号:%s,名称:%s\n", products[i].number, products[i].name);
}
}
}
// 打印每个人都购买的商品编号和名称
void printCommonProducts(Product products[], int numProducts, Customer customers[], int numCustomers) {
int purchased[MAX_PRODUCTS] = {0};
for (int i = 0; i < numCustomers; i++) {
for (int j = 0; j < customers[i].numPurchases; j++) {
int index = atoi(customers[i].purchases[j].number) - 1;
purchased[index]++;
}
}
for (int i = 0; i < numProducts; i++) {
if (purchased[i] == numCustomers) {
printf("商品编号:%s,名称:%s\n", products[i].number, products[i].name);
}
}
}
// 打印无人购买的商品编号和名称
void printNoPurchasedProducts(Product products[], int numProducts, Customer customers[], int numCustomers) {
int purchased[MAX_PRODUCTS] = {0};
for (int i = 0; i < numCustomers; i++) {
for (int j = 0; j < customers[i].numPurchases; j++) {
int index = atoi(customers[i].purchases[j].number) - 1;
purchased[index] = 1;
}
}
for (int i = 0; i < numProducts; i++) {
if (purchased[i] == 0) {
printf("商品编号:%s,名称:%s\n", products[i].number, products[i].name);
}
}
}