typedef struct //定义的用于表示教师信息 { int id; // 编号 int company_id; // 企业编号 char company_name[50]; // 企业名称 char phone[20]; // 联系电话 double low_elec; // 谷电量 double high_elec; // 峰电量 double low_price; // 谷价 double high_price; // 峰价 char date[20]; // 年月 double total_elec; // 合计电量 double total_price; // 合计电费 } ElectricityInfo; typedef struct { ElectricityInfo *data; // 存储用电情况信息的数组 int length; // 当前存储的用电情况信息数量 int capacity; // 顺序表最大容量 } SeqList; ElectricityInfo *get(SeqList *list, char company_name[]) void insert(SeqList *list, ElectricityInfo) void delete(SeqList *list, int company-id) void sort(SeqList *list) ElectricityInfo *find(SeqList *list, int company-id) void update(SeqList *list, int id) void statistics(SeqList *list) void display(SeqList *list)根据上述内容继续编写代码
时间: 2023-08-08 17:04:51 浏览: 109
各种单片机的比较(avr 8051 stc 等)
以下是完整的代码,包括结构体定义和函数实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int id; // 编号
int company_id; // 企业编号
char company_name[50]; // 企业名称
char phone[20]; // 联系电话
double low_elec; // 谷电量
double high_elec; // 峰电量
double low_price; // 谷价
double high_price; // 峰价
char date[20]; // 年月
double total_elec; // 合计电量
double total_price; // 合计电费
} ElectricityInfo;
typedef struct {
ElectricityInfo *data; // 存储用电情况信息的数组
int length; // 当前存储的用电情况信息数量
int capacity; // 顺序表最大容量
} SeqList;
// 根据企业名称查找用电情况信息,返回指针
ElectricityInfo *get(SeqList *list, char company_name[]) {
for (int i = 0; i < list->length; i++) {
if (strcmp(list->data[i].company_name, company_name) == 0) {
return &(list->data[i]);
}
}
return NULL;
}
// 在顺序表末尾插入一条用电情况信息
void insert(SeqList *list, ElectricityInfo info) {
if (list->length >= list->capacity) {
printf("Error: SeqList is full!\n");
return;
}
list->data[list->length++] = info;
}
// 删除指定企业编号的用电情况信息
void delete(SeqList *list, int company_id) {
for (int i = 0; i < list->length; i++) {
if (list->data[i].company_id == company_id) {
for (int j = i; j < list->length - 1; j++) {
list->data[j] = list->data[j + 1];
}
list->length--;
return;
}
}
printf("Error: Company with ID %d not found!\n", company_id);
}
// 按照合计电费排序
void sort(SeqList *list) {
for (int i = 0; i < list->length - 1; i++) {
for (int j = 0; j < list->length - i - 1; j++) {
if (list->data[j].total_price > list->data[j + 1].total_price) {
ElectricityInfo temp = list->data[j];
list->data[j] = list->data[j + 1];
list->data[j + 1] = temp;
}
}
}
}
// 根据企业编号查找用电情况信息,返回指针
ElectricityInfo *find(SeqList *list, int company_id) {
for (int i = 0; i < list->length; i++) {
if (list->data[i].company_id == company_id) {
return &(list->data[i]);
}
}
return NULL;
}
// 修改指定编号的用电情况信息
void update(SeqList *list, int id) {
ElectricityInfo *info = find(list, id);
if (info == NULL) {
printf("Error: ElectricityInfo with ID %d not found!\n", id);
return;
}
printf("Enter new values:\n");
printf("Company ID: ");
scanf("%d", &(info->company_id));
printf("Company name: ");
scanf("%s", info->company_name);
printf("Phone: ");
scanf("%s", info->phone);
printf("Low electricity: ");
scanf("%lf", &(info->low_elec));
printf("High electricity: ");
scanf("%lf", &(info->high_elec));
printf("Low price: ");
scanf("%lf", &(info->low_price));
printf("High price: ");
scanf("%lf", &(info->high_price));
printf("Date: ");
scanf("%s", info->date);
info->total_elec = info->low_elec + info->high_elec;
info->total_price = info->low_elec * info->low_price + info->high_elec * info->high_price;
}
// 统计所有用电情况信息的合计电费和合计电量
void statistics(SeqList *list) {
double total_elec = 0;
double total_price = 0;
for (int i = 0; i < list->length; i++) {
total_elec += list->data[i].total_elec;
total_price += list->data[i].total_price;
}
printf("Total electricity: %.2lf\n", total_elec);
printf("Total price: %.2lf\n", total_price);
}
// 显示所有用电情况信息
void display(SeqList *list) {
printf("%-5s %-10s %-20s %-15s %-10s %-10s %-10s %-10s %-10s %-15s %-15s\n",
"ID", "CompanyID", "CompanyName", "Phone", "LowElec", "HighElec",
"LowPrice", "HighPrice", "Date", "TotalElec", "TotalPrice");
for (int i = 0; i < list->length; i++) {
ElectricityInfo *info = &(list->data[i]);
printf("%-5d %-10d %-20s %-15s %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10s %-15.2lf %-15.2lf\n",
info->id, info->company_id, info->company_name, info->phone,
info->low_elec, info->high_elec, info->low_price, info->high_price,
info->date, info->total_elec, info->total_price);
}
}
int main() {
SeqList list;
list.data = (ElectricityInfo *) malloc(sizeof(ElectricityInfo) * 100);
list.length = 0;
list.capacity = 100;
int choice;
do {
printf("\n========== Electricity Management System ==========\n");
printf("1. Insert electricity info\n");
printf("2. Delete electricity info\n");
printf("3. Sort by total price\n");
printf("4. Find by company ID\n");
printf("5. Update by ID\n");
printf("6. Statistics\n");
printf("7. Display all info\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
ElectricityInfo info;
printf("Enter values:\n");
printf("Company ID: ");
scanf("%d", &(info.company_id));
printf("Company name: ");
scanf("%s", info.company_name);
printf("Phone: ");
scanf("%s", info.phone);
printf("Low electricity: ");
scanf("%lf", &(info.low_elec));
printf("High electricity: ");
scanf("%lf", &(info.high_elec));
printf("Low price: ");
scanf("%lf", &(info.low_price));
printf("High price: ");
scanf("%lf", &(info.high_price));
printf("Date: ");
scanf("%s", info.date);
info.id = list.length + 1;
info.total_elec = info.low_elec + info.high_elec;
info.total_price = info.low_elec * info.low_price + info.high_elec * info.high_price;
insert(&list, info);
printf("Electricity info inserted successfully!\n");
break;
}
case 2: {
int company_id;
printf("Enter company ID to delete: ");
scanf("%d", &company_id);
delete(&list, company_id);
printf("Electricity info deleted successfully!\n");
break;
}
case 3: {
sort(&list);
printf("Electricity info sorted by total price!\n");
break;
}
case 4: {
int company_id;
printf("Enter company ID to find: ");
scanf("%d", &company_id);
ElectricityInfo *info = find(&list, company_id);
if (info == NULL) {
printf("ElectricityInfo with ID %d not found!\n", company_id);
} else {
printf("%-5s %-10s %-20s %-15s %-10s %-10s %-10s %-10s %-10s %-15s %-15s\n",
"ID", "CompanyID", "CompanyName", "Phone", "LowElec", "HighElec",
"LowPrice", "HighPrice", "Date", "TotalElec", "TotalPrice");
printf("%-5d %-10d %-20s %-15s %-10.2lf %-10.2lf %-10.2lf %-10.2lf %-10s %-15.2lf %-15.2lf\n",
info->id, info->company_id, info->company_name, info->phone,
info->low_elec, info->high_elec, info->low_price, info->high_price,
info->date, info->total_elec, info->total_price);
}
break;
}
case 5: {
int id;
printf("Enter ID to update: ");
scanf("%d", &id);
update(&list, id);
printf("Electricity info updated successfully!\n");
break;
}
case 6: {
statistics(&list);
break;
}
case 7: {
display(&list);
break;
}
case 0: {
break;
}
default: {
printf("Error: Invalid choice, please try again!\n");
break;
}
}
} while (choice != 0);
free(list.data);
return 0;
}
```
这个程序实现了一个简单的用电情况管理系统,可以插入、删除、排序、查找、更新和统计用电情况信息,并且支持将所有信息显示出来。顺序表是用来存储用电情况信息的,每个用电情况信息包含编号、企业编号、企业名称、联系电话、谷电量、峰电量、谷价、峰价、年月、合计电量和合计电费等属性。各个函数的功能如下:
- `get`:根据企业名称查找用电情况信息,返回指针。
- `insert`:在顺序表末尾插入一条用电情况信息。
- `delete`:删除指定企业编号的用电情况信息。
- `sort`:按照合计电费排序。
- `find`:根据企业编号查找用电情况信息,返回指针。
- `update`:修改指定编号的用电情况信息。
- `statistics`:统计所有用电情况信息的合计电费和合计电量。
- `display`:显示所有用电情况信息。
主函数使用一个循环来接收用户的输入,并且根据用户选择调用相应的函数。用户可以选择插入、删除、排序、查找、更新、统计或显示所有用电情况信息,或者退出程序。每个函数实现了相应的功能,并且在完成后输出提示信息。最后,在退出程序之前需要释放动态分配的内存。
阅读全文