我创建了一个“按日期排序”功能,该功能可以正常工作。但是,当尝试创建“按名称排序”功能时,我假设复制“按日期排序”功能并将变量yearTmp更改为nameTmp时,它将按名称进行排序。它不是。程序正在运行,但我在编译器中看到了这些错误...
[Warning] assignment makes integer from pointer without a cast
[Warning] assignment makes pointer from integer without a cast
我到目前为止的代码...
#include <stdio.h>
#include <string.h>
#define MAX 30
void sortByDate( int year[], char *name[], char *states[], int count);
void sortByName(int year[], char *name[], char *states[], int count);
int main()
{
int year[MAX];
int i, a;
int count = 0;
int choice;
char *name[MAX],
*states[MAX];
char b[MAX], c[MAX];
FILE *inp = fopen("hurricanes.txt","r"); /* defining file input */
for(i=0;i<MAX;i++)
{
if( feof(inp) )
{
break;
}
fscanf(inp, "%d %s %29[^\n]", &a, b, c);
year[i]=a;
name[i] = strdup(b);
states[i] = strdup(c);
++count;
printf("%d %s %s\n", year[i], name[i], states[i]);
}
printf("Press 0 to sort by date or 1 to sort by name: ");
scanf("%d", &choice);
if (choice == 0)
{
sortByDate(year, name, states, count);
}
else if ( choice == 1)
{
sortByName(year, name, states, count);
}
getch();
return 0;
}
void sortByDate( int year[], char *name[], char *states[], int count )
{
int d = 0;
int c = 0;
int yearTmp;
int order[count];
int tmp = 0;
FILE *outp = fopen("report.txt","w"); /* defining file output */
for (c = 0; c < count; ++c)
{
order[c] = c;
}
for (c = 0 ; c < ( count - 1 ); c++)
{
for (d = 0 ; d < count - c - 1; d++)
{
if (year[d] > year[d+1])
{
yearTmp = year[d];
year[d] = year[d+1];
year[d+1] = yearTmp;
tmp = order[d];
order[d] = order[d+1];
order[d+1] = tmp;
}
}
}
for (c = 0; c < count; ++c)
{
printf("%d %-10s %s\n", year[c], name[order[c]], states[order[c]]);
}
}
void sortByName(int year[], char *name[], char *states[], int count)
{
int d = 0;
int c = 0;
char nameTmp;
int order[count];
int tmp = 0;
FILE *outp = fopen("report.txt","w"); /* defining file output */
for (c = 0; c < count; ++c)
{
order[c] = c;
}
for (c = 0 ; c < ( count - 1 ); c++)
{
for (d = 0 ; d < count - c - 1; d++)
{
if (name[d] > name[d+1])
{
nameTmp = name[d];
name[d] = name[d+1];
name[d+1] = nameTmp;
tmp = order[d];
order[d] = order[d+1];
order[d+1] = tmp;
}
}
}
for (c = 0; c < count; ++c)
{
printf("%d %-10s %s\n", year[order[c]], name[c], states[order[c]]);
}
}
hurricanes.txt文件...
1960 Donna FL, NC
1969 Camille MS
1972 Agnes FL
1983 Alicia TX
1989 Hugo SC,NC
2005 Katrina FL, LA, MS
2005 Rita TX, LA
2005 Wilma FL
2008 Ike TX
2009 Ida MS
2011 Irene NC, NJ, MA, VT
2012 Isaac LA
1992 Andrew FL, LA
1995 Opal FL, AL
1999 Floyd NC
2003 Isabel NC, VA
2004 Charley FL, SC, NC
2004 Frances FL
2004 Ivan AL
2004 Jeanne FL
最佳答案
您从正确的道路开始,但是您不能像对待整数一样对待c字符串。
C中的字符串是由空终止符'\ 0'终止的字符数组。为了比较它们,您必须检查数组的每个元素。幸运的是,存在一个为您执行此操作的函数,即strcmp()
您传递给函数的是char *数组。这些索引中的每个索引都指向代表您的字符串的字符数组。要比较这些索引,您可以执行以下操作。
// Returns 0 if the are equal
// Returns something > 0 if pName[d] is lexicographically greater than pName[d+1]
// Returns something < 0 if pName[d] is lexicographically less than pName[d+1]
if(strcmp(pName[0], pName[d+1]) > 0)
两个字符串均以null终止或此函数具有未定义的行为非常重要。
要执行排序算法的交换部分,请交换指针。
char * pTemp = pName[d];
pName[d] = pName[d+1];
pName[d+1] = pTemp;
关于c - 使用字符串数组的排序功能-C中的错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36637010/