[Error] cannot convert 'SeqList**' to 'SeqList*' for argument '1' to 'void quickSort(SeqList*, int, int)'
时间: 2024-06-14 09:03:50 浏览: 149
根据提供的引用内容,这个错误是由于将`SeqList**`类型的参数传递给了`SeqList*`类型的参数导致的。这个错误可以通过修改函数参数的类型来解决。
以下是一个示例代码,演示了如何修复这个错误:
```c++
void quickSort(SeqList* L, int low, int high) {
// 快速排序的实现
}
int main() {
SeqList* L;
int low = 0;
int high = L->length - 1;
quickSort(&L, low, high); // 将参数传递的地址改为指针类型
return 0;
}
```
修复后的代码将`SeqList**`类型的参数改为了`SeqList*`类型的参数,并且在调用`quickSort`函数时,传递了指针类型的参数。
相关问题
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)根据上述内容继续编写代码
以下是完整的代码,包括结构体定义和函数实现:
```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`:显示所有用电情况信息。
主函数使用一个循环来接收用户的输入,并且根据用户选择调用相应的函数。用户可以选择插入、删除、排序、查找、更新、统计或显示所有用电情况信息,或者退出程序。每个函数实现了相应的功能,并且在完成后输出提示信息。最后,在退出程序之前需要释放动态分配的内存。
线性表的基本操作的实现: 1.顺序表的定义CreateList(SeqList *L,int n); 2.顺序表的打印PrintList(SeqList *L); 3.顺序表的查找LocateList(SeqList *L,int x); 4.顺序表的插入InsertList(SeqList *L,int x,int i); 5.顺序表的删除DeleteList(SeqList *L,int i);
好的,以下是 CSDN 开发的 C知道 对线性表基本操作的实现:
1. 顺序表的定义:
typedef struct {
int *elem; // 存储空间基址
int length; // 当前长度
int size; // 当前分配的存储容量
} SeqList;
void CreateList(SeqList *L, int n) {
L->elem = (int *)malloc(n * sizeof(int));
if (!L->elem) {
printf("Memory allocation failed.\n");
exit(1);
}
L->length = 0;
L->size = n;
}
2. 顺序表的打印:
void PrintList(SeqList *L) {
int i;
for (i = 0; i < L->length; i++) {
printf("%d ", L->elem[i]);
}
printf("\n");
}
3. 顺序表的查找:
int LocateList(SeqList *L, int x) {
int i;
for (i = 0; i < L->length; i++) {
if (L->elem[i] == x) {
return i;
}
}
return -1;
}
4. 顺序表的插入:
int InsertList(SeqList *L, int x, int i) {
int j;
if (i < 1 || i > L->length + 1) {
return 0;
}
if (L->length >= L->size) {
return 0;
}
for (j = L->length; j >= i; j--) {
L->elem[j] = L->elem[j - 1];
}
L->elem[i - 1] = x;
L->length++;
return 1;
}
5. 顺序表的删除:
int DeleteList(SeqList *L, int i) {
int j;
if (i < 1 || i > L->length) {
return 0;
}
for (j = i; j < L->length; j++) {
L->elem[j - 1] = L->elem[j];
}
L->length--;
return 1;
}
希望这些代码能够帮助你实现线性表的基本操作。
阅读全文