有谁知道为什么这种排序不对struct数组中的第一个元素进行排序?下面是struct,sort和一些示例输入输出的代码。我已经排除了填充数组的代码,因为我知道在排序之前打印数组,它是正确填充的。
struct Record
{
char *firstName;
char *lastName;
char *GPA;
unsigned int ID;
};
void newList(struct Record * Records, int amount, char* name)
{
int i;
int j;
struct Record tempR;
FILE *fp;
unsigned int temp;
//Order Array
for(i=0;i<amount;i++)
{
for(j=0;j<amount-1;j++)
{
if(strcmp(Records[j].firstName,Records[j+1].firstName)<0)
{
tempR=Records[j];
Records[j]=Records[j+1];
Records[j+1]=tempR;
}
}
}
//Make New Fle with Ordered Array
fp=fopen(name, "w+");
for(i=0;i<amount;i++)
{
fprintf(fp,"%s, %s, %s, %d, Records[i].firstName,Records[i].lastName,
Records[i].GPA,Records[i].ID);
}
}
此代码的示例输入和输出是以下输入:
Yblwtjbvtz,Eoztbzoqnz,2.6,1123268861
Blmhwgzjdd,Ojwfnlislc,3.1,1712113924
Gkmkbnotic,Mhzcakkugv,3.3,1966045151
Zsrwqdwkfo,Nciqixcamr,2.1,212426241
Vrekafrafk,Ixylzenhlc,2.2,297694159
Kzkaxpoeqg,Syawkuqbew,3.4,104209687
Ekdcfsifrw,Apvrwfshqm,1.4,799470314
Iybmcotvpf,Eqvcorjntu,0.6,1748600414
Jsfwiydnyt,Rhyaabwfdr,2.2,104800253
Mfqrukoytp,Urjsjcloau,3.8,1240702350"
输出:
Yblwtjbvtz, Eoztbzoqnz, 2.6, 1123268861
Zsrwqdwkfo, Nciqixcamr, 2.1, 212426241
Vrekafrafk, Ixylzenhlc, 2.2, 297694159
Mfqrukoytp, Urjsjcloau, 3.8, 1240702350
Kzkaxpoeqg, Syawkuqbew, 3.4, 104209687
Jsfwiydnyt, Rhyaabwfdr, 2.2, 104800253
Iybmcotvpf, Eqvcorjntu, 0.6, 1748600414
Gkmkbnotic, Mhzcakkugv, 3.3, 1966045151
Ekdcfsifrw, Apvrwfshqm, 1.4, 799470314
Blmhwgzjdd, Ojwfnlislc, 3.1, 1712113924
答案 0 :(得分:0)
这是一个用C语言编写的冒泡排序算法示例。
for (size_t c = 0 ; c < ( n - 1 ); c++)
{
for (size_t d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
所以发布的代码应该实现相同的算法,只使用不同的变量/数组名称
答案 1 :(得分:0)
以下代码实现所有注释,执行适当的错误检查,并执行所需的操作。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Record
{
char *firstName;
char *lastName;
char *GPA;
unsigned int ID;
};
static struct Record Records[] =
{
{"Yblwtjbvtz","Eoztbzoqnz","2.6",1123268861},
{"Blmhwgzjdd","Ojwfnlislc","3.1",1712113924},
{"Gkmkbnotic","Mhzcakkugv","3.3",1966045151},
{"Zsrwqdwkfo","Nciqixcamr","2.1",212426241},
{"Vrekafrafk","Ixylzenhlc","2.2",297694159},
{"Kzkaxpoeqg","Syawkuqbew","3.4",104209687},
{"Ekdcfsifrw","Apvrwfshqm","1.4",799470314},
{"Iybmcotvpf","Eqvcorjntu","0.6",1748600414},
{"Jsfwiydnyt","Rhyaabwfdr","2.2",104800253},
{"Mfqrukoytp","Urjsjcloau","3.8",1240702350}
};
//prototypes
void newList( char* filename );
void printList( char* filename );
int main( void )
{
newList( "sorted.txt" );
printList( "sorted.txt" );
return 0;
} // end function: main
void newList( char* filename )
{
struct Record swap;
size_t numRecords = sizeof( Records)/sizeof( struct Record);
//Order Array, ascending on firstName field
for (size_t c = 0 ; c < ( numRecords - 1 ); c++)
{
for (size_t d = 0 ; d < numRecords - c - 1; d++)
{
if ( 0 < strcmp(Records[d].firstName, Records[d+1].firstName) )
{
memcpy( &swap, &Records[d], sizeof( struct Record ) );
memcpy( &Records[d], &Records[d+1], sizeof( struct Record ) );
memcpy( &Records[d+1], &swap, sizeof( struct Record ) );
}
}
}
//Make New File with Ordered Array
FILE *fp = NULL;
if( NULL == (fp=fopen(filename, "w") ) )
{ // then, fopen failed
perror( "fopen sorted.txt for write failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
for(size_t i=0;i<numRecords;i++)
{
fwrite( &Records[i], sizeof( struct Record ), 1, fp );
}
fclose( fp );
} // end function: newList
void printList( char * filename )
{
FILE *fp = NULL;
if( NULL == (fp=fopen(filename, "r") ) )
{ // then, fopen failed
perror( "fopen sorted.txt for read failed" );
exit( EXIT_FAILURE );
}
struct Record oneRecord;
while(1 == fread( &oneRecord, sizeof( struct Record ), 1, fp) )
{
printf( "%s %s %s %u\n",
oneRecord.firstName,
oneRecord.lastName,
oneRecord.GPA,
oneRecord.ID );
}
fclose( fp );
} // end function: printList
这是该计划的输出:
Blmhwgzjdd Ojwfnlislc 3.1 1712113924
Ekdcfsifrw Apvrwfshqm 1.4 799470314
Gkmkbnotic Mhzcakkugv 3.3 1966045151
Iybmcotvpf Eqvcorjntu 0.6 1748600414
Jsfwiydnyt Rhyaabwfdr 2.2 104800253
Kzkaxpoeqg Syawkuqbew 3.4 104209687
Mfqrukoytp Urjsjcloau 3.8 1240702350
Vrekafrafk Ixylzenhlc 2.2 297694159
Yblwtjbvtz Eoztbzoqnz 2.6 1123268861
Zsrwqdwkfo Nciqixcamr 2.1 212426241