72 lines
1.9 KiB
C
72 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int n;
|
|
char* p[1005];
|
|
char s[1005][1005];
|
|
|
|
// 比较函数,比较两个字符串的大小,不区分字母大小写
|
|
int compare(char* a, char* b) {
|
|
// 复制两个字符串到新的数组中
|
|
char c[1005];
|
|
char d[1005];
|
|
strcpy(c, a);
|
|
strcpy(d, b);
|
|
// 将两个字符串中的小写字母转换为大写字母
|
|
for (int i = 0; i < strlen(c); i++) {
|
|
if (c[i] >= 'a' && c[i] <= 'z')
|
|
c[i] -= 32;
|
|
}
|
|
for (int i = 0; i < strlen(d); i++) {
|
|
if (d[i] >= 'a' && d[i] <= 'z')
|
|
d[i] -= 32;
|
|
}
|
|
// 逐个比较两个字符串中的字符
|
|
for (int i = 0; i < (strlen(c) > strlen(d) ? strlen(d) : strlen(c)); i++) {
|
|
if (c[i] > d[i])
|
|
return 1;
|
|
else if (c[i] < d[i])
|
|
return -1;
|
|
}
|
|
// 如果两个字符串前面的字符都一样,那么短字符串排在前面
|
|
if (strlen(c) == strlen(d))
|
|
return 0;
|
|
else
|
|
return (strlen(c) > strlen(d)) ? 1 : -1;
|
|
}
|
|
|
|
// 字符串排序函数,使用比较函数进行排序
|
|
void sortstring(char** r, int n) {
|
|
for (int i = 0; i < n - 1; i++) {
|
|
for (int j = 0; j < n - i - 1; j++) {
|
|
if (compare(r[j], r[j + 1]) > 0) {
|
|
// 如果前面的字符串比后面的字符串大,那么交换两个字符串在数组中的位置
|
|
char* t = r[j];
|
|
r[j] = r[j + 1];
|
|
r[j + 1] = t;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
// 输入字符串的个数
|
|
scanf("%d", &n);
|
|
|
|
// 逐个输入字符串,并将字符串的首地址存入指针数组中
|
|
char* p[1005];
|
|
for (int i = 0; i < n; i++) {
|
|
scanf("%s", s[i]);
|
|
p[i] = s[i];
|
|
}
|
|
|
|
// 对字符串进行排序
|
|
sortstring(p, n);
|
|
|
|
// 输出排好序的字符串
|
|
for (int i = 0; i < n; i++) {
|
|
printf("%s\n", p[i]);
|
|
}
|
|
|
|
return 0;
|
|
} |