问题描述
我正在尝试对包含以下信息的文件进行排序
Easy,Category 3 hurricane,5-Sep,1950
King,Category 4 hurricane,18-Oct,1950
Florence,Category 1 hurricane,26-Sep,1953
Hazel,9-Oct,1953
Flossy,24-Sep,1956
Donna,10-Sep,1960
Cleo,Category 2 hurricane,27-Aug,1964
Dora,1964
Isbell,14-Oct,1964
Betsy,8-Sep,1965
Alma,9-Jun,1966
Inez,8-Oct,1966
Gladys,19-Oct,1968
Agnes,19-Jun,1972
Eloise,23-Sep,1975
David,3-Sep,1979
Elena,1-Sep,1985
Kate,21-Nov,1985
Floyd,12-Oct,1987
Andrew,Category 5 hurricane,24-Aug,1992
Erin,3-Aug,1995
Opal,4-Oct,1995
Earl,1998
Georges,25-Sep,1998
Irene,15-Oct,1999
Charley,13-Aug,2004
Frances,2004
Ivan,16-Sep,2004
Jeanne,2004
Dennis,10-Jul,2005
Katrina,25-Aug,2005
Rita,20-Sep,2005
Wilma,24-Oct,2005
Hermine,2-Sep,2016
Matthew,7-Oct,2016
Irma,2017
Michael,10-Oct,2018
我正在使用此代码。
#include <stdio.h>
#include <string.h>
#define NUM_LINES 37
#define LINE_LENGTH 60
void print(void);
void select_sort_str(char list[][LINE_LENGTH],int n);
int alpha_first(char list[][LINE_LENGTH],int min_sub,int max_sub);
int main (void){
//store each line in an array of strings
FILE *inp;
FILE *outp;
char hurr[NUM_LINES][LINE_LENGTH];
inp = fopen("hurricanes.csv","r");
outp = fopen("out.txt","w");
int num;
//read in lines from file
for (int i = 0; i<NUM_LINES; i++){
fgets(hurr[i],LINE_LENGTH,inp);
}
inp = fopen("hurricanes.cvs","r");
//printf("%s",hurr[0]);
//define function
select_sort_str(hurr,NUM_LINES);
return(0);
}
int
alpha_first(char list[][LINE_LENGTH],// input - array of pointers to strings
int min_sub,// input - min and max subscripts of
int max_sub) // portion of list to consider
{
int first,i;
first = min_sub;
for (i = min_sub + 1; i <= max_sub; ++i) {
if (strcmp(list[i],list[first]) < 0) {
first = i;
}
}
return (first);
}
/*
* Orders the pointers in an array list so they access strings in
* alphabetical order
* Pre: first n elements of list reference string of uniform case;
* n >= 0
*/
void
select_sort_str(char list[][LINE_LENGTH],// input/output - array of pointers being
// ordered to acces strings alphabetically
int n) // input - number of elements to sort
{
int fill,// index of element to contain next string in order
index_of_min; // index of next string in order
char *temp;
for (fill = 0; fill < n - 1; ++fill) {
index_of_min = alpha_first(list,fill,n - 1);
if (index_of_min != fill) {
temp = list[index_of_min];
list[index_of_min][LINE_LENGTH] = list[fill][LINE_LENGTH];
list[fill][LINE_LENGTH] = *temp;
}
}
for( int i =0; i<NUM_LINES; i++){
printf("%s",list[i]);
}
}
但是当我执行代码时,输出没有正确排序。输出看起来像这样
Easy,1950
Aing,1950
Alorence,1953
Aazel,1953
Flossy,1956
Aonna,1960
Aleo,1964
Aora,1964
Asbell,1964
Aetsy,1965
Alma,1966
Anez,1966
Aladys,1968
Agnes,1972
Aloise,1975
David,1979
Alena,1985
Kate,1985
Aloyd,1987
Andrew,1992
Frin,1995
Cpal,1995
Carl,1998
Georges,1998
Crene,1999
Charley,2004
Crances,2004
Ivan,2004
Deanne,2004
Dennis,2005
Jatrina,2005
Hita,2005
Hilma,2005
Hermine,2016
Hatthew,2016
Hrma,2017
Michael,2018
我可以看出我的代码正在以某种方式对其进行排序,但我无法确定它没有按名称按字母顺序排序的原因。请让我知道您的想法。
编译也没有错误,只有几个我没用过的变量的警告。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)