c - 在C中对数据结构数组进行排序
问题描述
我的程序应该读取一个文件,存储该文件的记录,然后对每条记录进行排序。我正在使用插入排序算法对记录进行排序,但是在使其正常工作时遇到了一些问题。
例如,如果 thext 文件看起来像这样
string one; string one; string one; 1
string two; string two; string two; 2
string three; stringh three; string three; 3
我需要第三条记录是第一条,第二条是第二条,第一条是最后一条。由于 3>2>1。
我认为插入排序算法是正确的,因为我用一个简单的数组对其进行了测试并且它正在工作,但是我很难将它实现到我的程序中。我得到的错误是insertionSort’ makes pointer from integer without a cast
,我认为这是因为我使用数据结构来存储从文件扫描的数据。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100
struct Element
{
char one[100];
char two[100];
char three[100];
int st;
};
void insertionSort(int arr[]);
void printArray(int arr[]);
int main() {
int i;
struct Element elements[MAXLEN];
FILE * fpointer = fopen("clients.txt", "r");
char buffer[1024]; // Define a really big string to hold the line of text in
char *field;
int field_number;
while(fgets(buffer,1024,fpointer))
{
field_number=0;
field=strtok(buffer,";");
while(field)
{
switch(field_number)
{
case 0:
strcpy(elements[i].one,field);
break;
case 1:
strcpy(elements[i].two,field);
break;
case 2:
strcpy(elements[i].three,field);
break;
case 3:
elements[i].st=atoi(field);
break;
}
field=strtok(NULL,";"); // Get next field
field_number++;
}
i++; // Move the index for elements to the next one
}
insertionSort(elements[MAXLEN].st);
printArray(elements[MAXLEN].st);
fclose(fpointer);
return 0;
}
void insertionSort(int arr[])
{
struct Element elements[MAXLEN];
int i, key, j;
for (i = 1; i < 10; i++)
{
key = elements[i].st;
j = i-1;
while (j >= 0 && elements[j].st > key)
{
elements[j+1].st = elements[j].st;
j = j-1;
}
elements[j+1].st = key;
}
}
void printArray(int arr[])
{
int i;
for (i=0; i < 10; i++) {
printf("%d ", arr[i]);
printf("\n");
}
}
解决方案
您的代码中有以下问题。
您已经声明
了作为参数的void insertionSort(int arr[]); and void printArray(int arr[]);
函数,但您
只是作为参数pointer
int array
insertionSort(elements[MAXLEN].st); & printArray(elements[MAXLEN].st);
int
传递。
insertionSort
还将对st
字段进行 排序,struct Element elements[MAXLEN];
但不对elements
自身insertionSort
进行排序,并将按您想要的升序而不是降序对数组进行排序。
因此改变这个
while (j >= 0 && elements[j].st > key)
到
while (j >= 0 && elements[j].st < key.st)
以下面的代码为例。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100
struct Element
{
char one[100];
char two[100];
char three[100];
int st;
};
void insertionSort(struct Element elements[]);
void printArray(struct Element elements[]);
int main() {
int i;
struct Element elements[MAXLEN];
FILE * fpointer = fopen("clients.txt", "r");
char buffer[1024]; // Define a really big string to hold the line of text in
char *field;
int field_number;
while(fgets(buffer,1024,fpointer))
{
field_number=0;
field=strtok(buffer,";");
while(field)
{
switch(field_number)
{
case 0:
strcpy(elements[i].one,field);
break;
case 1:
strcpy(elements[i].two,field);
break;
case 2:
strcpy(elements[i].three,field);
break;
case 3:
elements[i].st=atoi(field);
break;
}
field=strtok(NULL,";"); // Get next field
field_number++;
}
i++; // Move the index for elements to the next one
}
insertionSort(elements);
printArray(elements);
fclose(fpointer);
return 0;
}
void insertionSort(struct Element elements[])
{
int i, j;
struct Element key;
for (i = 1; i < 10; i++)
{
key = elements[i];
j = i-1;
while (j >= 0 && elements[j].st < key.st)
{
elements[j+1] = elements[j];
j = j-1;
}
elements[j+1] = key;
}
}
void printArray(struct Element elements[])
{
int i;
for (i=0; i < 3; i++) {
printf("%s %s %s%d ", elements[i].one,elements[i].two,elements[i].three,elements[i].st);
printf("\n");
}
}
推荐阅读
- linux - 检查 csv 文件中列的任何值是否存在于第二个 csv 文件的列中
- r - (ggplot)geom_boxplots。我想要每个箱线图的平均值而不是中位数+置信区间
- php - 无法在会话值上添加前缀,在本地主机上很好
- javascript - 在使用纯 JS 生成的 word 文档中定位元素
- python - 从论文(PyTorch)中实现“并发”softmax 函数时遇到问题
- laravel - 服务器的安全问题
- pygame - W10中的Python 3,8 Pygame 2无法保存png文件
- vb.net - 从 vb.net 调用 dll 函数
- r - Web Scrape 不从下一页提取信息,只是重复第一页上的数据
- javascript - 圣经参考的正则表达式