把注释删光,然后看看编译能通过不?
将注释用“手打”一个一个字符的敲上去,看看编译能通过不?
如果能编译通过,说明你拷贝的注释里面含有 GB2312 之外的编码;如果依然不能编译通过,把你的xx.c以文件形式发上来让大家看看。
程序代码:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu
{
char name[30];
int rank;
};
int comp_int(const void* e1, const void* e2)
{
//return *(int*)e1 - *(int*)e2; 错误:可能会溢出
if( *(int*)e1 < *(int*)e2 ) return -1;
if( *(int*)e1 > *(int*)e2 ) return +1;
return 0;
}
int comp_float(const void* e1, const void* e2)
{
// 错误:非规格化数,既不小于,也不大于,更不等于
//if (*(float*)e1 == *(float*)e2)
// return 0;
//else if (*(float*)e1 > *(float*)e2)
// return 1;
//else
// return -1;
if( *(float*)e1 < *(float*)e2 ) return -1;
if( *(float*)e1 > *(float*)e2 ) return +1;
return 0;
}
int comp_struct_by_rank(const void* e1, const void* e2)
{
// return ((struct stu*)e1)->rank - ((struct stu*)e2)->rank; 错误:可能会溢出
return comp_int( &(*(struct stu*)e1).rank, &(*(struct stu*)e2).rank );
}
int comp_struct_by_name(const void* e1, const void* e2)
{
return strcmp( ((struct stu*)e1)->name, ((struct stu*)e2)->name );
}
void exchange( char* e1, char* e2, size_t width ) // size_t
{
for( size_t i=0; i!=width; ++i )
{
char temp = e1[i];
e1[i] = e2[i];
e2[i] = temp;
}
}
void bubble( void* arr, size_t num, size_t width, int (*comp)(const void* e1,const void* e2) ) // size_t
{
for( size_t i=0; i+1<num; ++i )
{
for( size_t j=0; j+1+i<num; ++j )
{
if( comp( (char*)arr + j*width, (char*)arr + (j+1)*width ) > 0 )
{
exchange( (char*)arr + j*width, (char*)arr + (j+1)*width, width );
}
}
}
}
void text1_int( void )
{
int arr[10] = { 2,6,4,8,7,9,5,1,3,0 };
size_t num = sizeof(arr) / sizeof(arr[0]);
qsort( arr, num, sizeof(arr[0]), comp_int );
for( size_t i=0; i!=num; ++i )
printf( "%d%c", arr[i], " \n"[i+1==num] );
}
void text2_float( void )
{
float arr[10] = { 2.6f,6.3f,4.5f,8.4f,7.9f,9.5f,5.3f,1.4f,3.7f,0.8f };
size_t num = sizeof(arr) / sizeof(arr[0]);
qsort( arr, num, sizeof(arr[0]), comp_float );
for( size_t i=0; i!=num; ++i )
printf( "%f%c", arr[i], " \n"[i+1==num] );
}
void text3_struct_rank( void )
{
struct stu arr[6] = { {"h",6} ,{"hh",4},{"hhh",3}, {"hhhh",7},{"hhhhh",5} ,{"hhhhhh",8} };
size_t num = sizeof(arr) / sizeof(arr[0]);
qsort( arr, num, sizeof(arr[0]), comp_struct_by_rank );
for( size_t i=0; i!=num; ++i )
printf( "%d%c", arr[i].rank, " \n"[i+1==num] );
}
void text4_struct_name( void )
{
struct stu arr[6] = { {"asd",6} ,{"fgh",4},{"jkl",3}, {"zxc",7},{"vbn",5} ,{"qwe",8} };
size_t num = sizeof(arr) / sizeof(arr[0]);
qsort( arr, num, sizeof(arr[0]), comp_struct_by_name );
for( size_t i=0; i!=num; ++i )
printf("%s%c", arr[i].name, " \n"[i+1==num] );
}
void text5_bubble_int( void )
{
int arr[10] = { 2,6,4,8,7,9,5,1,3,0 };
size_t num = sizeof(arr) / sizeof(arr[0]);
bubble( arr, num, sizeof(arr[0]), comp_int );
for( size_t i=0; i!=num; ++i )
printf( "%d%c", arr[i], " \n"[i+1==num] );
}
int main( void )
{
text1_int();
text2_float();
text3_struct_rank();
text4_struct_name();
text5_bubble_int();
/*bubble*/
}[此贴子已经被作者于2022-9-26 11:11编辑过]