用C语言中的顺序表编写完成用电情况信息的查询(按照企业编号和企业名称)、更新、插入、删除、排序等功能。(其中包含了,编号、企业编号、企业名称、联系电话、谷电量、峰电量、谷价、峰价、年月、合计电量、合计电费等信息)
时间: 2023-11-08 20:47:19 浏览: 35
很抱歉,C语言中没有内置的顺序表数据结构,需要自己实现。下面是一个示例代码,仅供参考。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RECORDS 1000 // 最大记录数
// 电力用量记录
typedef struct {
int id; // 记录编号
int enterprise_id; // 企业编号
char enterprise_name[50]; // 企业名称
char phone[20]; // 联系电话
int valley_amount; // 谷电量
int peak_amount; // 峰电量
float valley_price; // 谷价
float peak_price; // 峰价
int year_month; // 年月
int total_amount; // 合计电量
float total_price; // 合计电费
} Record;
// 顺序表
typedef struct {
Record* data[MAX_RECORDS]; // 记录指针数组
int length; // 当前记录数
} SeqList;
// 初始化顺序表
void init(SeqList* list) {
list->length = 0;
}
// 按照企业编号和企业名称查找记录
Record* search(SeqList* list, int enterprise_id, char* enterprise_name) {
for (int i = 0; i < list->length; i++) {
Record* record = list->data[i];
if (record->enterprise_id == enterprise_id && strcmp(record->enterprise_name, enterprise_name) == 0) {
return record;
}
}
return NULL;
}
// 更新记录
void update(SeqList* list, int id, int enterprise_id, char* enterprise_name, char* phone, int valley_amount, int peak_amount, float valley_price, float peak_price, int year_month, int total_amount, float total_price) {
Record* record = list->data[id - 1];
record->enterprise_id = enterprise_id;
strcpy(record->enterprise_name, enterprise_name);
strcpy(record->phone, phone);
record->valley_amount = valley_amount;
record->peak_amount = peak_amount;
record->valley_price = valley_price;
record->peak_price = peak_price;
record->year_month = year_month;
record->total_amount = total_amount;
record->total_price = total_price;
}
// 插入记录
void insert(SeqList* list, int enterprise_id, char* enterprise_name, char* phone, int valley_amount, int peak_amount, float valley_price, float peak_price, int year_month, int total_amount, float total_price) {
if (list->length >= MAX_RECORDS) {
printf("ERROR: The list is full!\n");
return;
}
Record* record = (Record*)malloc(sizeof(Record));
record->id = list->length + 1;
record->enterprise_id = enterprise_id;
strcpy(record->enterprise_name, enterprise_name);
strcpy(record->phone, phone);
record->valley_amount = valley_amount;
record->peak_amount = peak_amount;
record->valley_price = valley_price;
record->peak_price = peak_price;
record->year_month = year_month;
record->total_amount = total_amount;
record->total_price = total_price;
list->data[list->length] = record;
list->length++;
}
// 删除记录
void del(SeqList* list, int id) {
if (id < 1 || id > list->length) {
printf("ERROR: Invalid id!\n");
return;
}
Record* record = list->data[id - 1];
free(record);
for (int i = id - 1; i < list->length - 1; i++) {
list->data[i] = list->data[i + 1];
list->data[i]->id--;
}
list->length--;
}
// 按照企业名称排序
void sort_by_name(SeqList* list) {
for (int i = 0; i < list->length - 1; i++) {
for (int j = 0; j < list->length - i - 1; j++) {
if (strcmp(list->data[j]->enterprise_name, list->data[j + 1]->enterprise_name) > 0) {
Record* tmp = list->data[j];
list->data[j] = list->data[j + 1];
list->data[j + 1] = tmp;
}
}
}
}
// 按照企业编号排序
void sort_by_id(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]->enterprise_id > list->data[j + 1]->enterprise_id) {
Record* tmp = list->data[j];
list->data[j] = list->data[j + 1];
list->data[j + 1] = tmp;
}
}
}
}
// 打印记录
void print_record(Record* record) {
printf("%d,%d,%s,%s,%d,%d,%.2f,%.2f,%d,%d,%.2f\n", record->id, record->enterprise_id, record->enterprise_name, record->phone, record->valley_amount, record->peak_amount, record->valley_price, record->peak_price, record->year_month, record->total_amount, record->total_price);
}
// 打印所有记录
void print_all(SeqList* list) {
for (int i = 0; i < list->length; i++) {
print_record(list->data[i]);
}
}
int main() {
SeqList list;
init(&list);
insert(&list, 1001, "企业1", "12345678", 100, 200, 0.5, 1.0, 202201, 300, 250.0);
insert(&list, 1002, "企业2", "23456789", 200, 300, 0.6, 1.2, 202201, 500, 450.0);
insert(&list, 1003, "企业3", "34567890", 300, 400, 0.7, 1.4, 202201, 700, 650.0);
printf("All records:\n");
print_all(&list);
printf("Sorted by enterprise name:\n");
sort_by_name(&list);
print_all(&list);
printf("Sorted by enterprise id:\n");
sort_by_id(&list);
print_all(&list);
printf("Record with id 2: ");
Record* record = search(&list, 1002, "企业2");
if (record) {
print_record(record);
} else {
printf("Not found!\n");
}
update(&list, 2, 1002, "企业2-更新", "98765432", 400, 500, 0.8, 1.6, 202201, 900, 850.0);
printf("After update:\n");
print_all(&list);
del(&list, 1);
printf("After delete:\n");
print_all(&list);
return 0;
}
```