超市总营业额分析程序要求:使用文件,结构体输出每天的销售记录,包括(1)每笔销售记录存储3个数据,例如:得力固体胶22.5。它们代表品名,数量(个)单价(元)。(2)将每天的销售记录通过文件进行存储(3)每周末进行盘存,从文件读取数据,并汇总出周销售额。
时间: 2023-12-20 13:03:26 浏览: 26
好的,这个程序可以使用 C 语言编写。以下是一个简单的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RECORDS 1000 // 最大销售记录数
#define MAX_NAME_LEN 20 // 品名的最大长度
// 销售记录结构体
typedef struct {
char name[MAX_NAME_LEN];
int quantity;
float price;
} Record;
// 读取销售记录文件,返回记录数
int read_records(Record *records) {
FILE *fp = fopen("sales.txt", "r");
if (!fp) {
printf("Failed to open file.\n");
return 0;
}
int count = 0;
while (fscanf(fp, "%s %d %f", records[count].name, &records[count].quantity, &records[count].price) == 3) {
count++;
}
fclose(fp);
return count;
}
// 写入销售记录文件
void write_records(Record *records, int count) {
FILE *fp = fopen("sales.txt", "w");
if (!fp) {
printf("Failed to open file.\n");
return;
}
for (int i = 0; i < count; i++) {
fprintf(fp, "%s %d %.2f\n", records[i].name, records[i].quantity, records[i].price);
}
fclose(fp);
}
// 添加一条销售记录
void add_record(Record *records, int *count) {
if (*count >= MAX_RECORDS) {
printf("Failed to add record: maximum number of records reached.\n");
return;
}
Record r;
printf("Enter name, quantity and price (separated by spaces): ");
scanf("%s %d %f", r.name, &r.quantity, &r.price);
records[*count] = r;
(*count)++;
write_records(records, *count);
}
// 显示所有销售记录
void show_records(Record *records, int count) {
printf("Name\tQuantity\tPrice\n");
for (int i = 0; i < count; i++) {
printf("%s\t%d\t%.2f\n", records[i].name, records[i].quantity, records[i].price);
}
}
// 计算周销售额
float calculate_weekly_revenue(Record *records, int count) {
float revenue = 0;
for (int i = 0; i < count; i++) {
revenue += records[i].quantity * records[i].price;
}
return revenue;
}
int main() {
Record records[MAX_RECORDS];
int count = read_records(records);
while (1) {
int choice;
printf("\n1. Add record\n2. Show records\n3. Calculate weekly revenue\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_record(records, &count);
break;
case 2:
show_records(records, count);
break;
case 3:
printf("Weekly revenue: %.2f\n", calculate_weekly_revenue(records, count));
break;
case 4:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
```
这个程序使用一个 `Record` 结构体来存储每个销售记录,同时定义了一些函数来实现添加、显示、读取、写入销售记录文件以及计算周销售额等功能。在 `main` 函数中,使用一个无限循环来接受用户的输入,根据用户的选择进行相应的操作。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)