#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAXSIZE 10000
#define WORDSIZE 20
bool IsWord(char c); //字符C是否为字母 a~z A~Z
void SortStr(char word[MAXSIZE][WORDSIZE], int n); //字符串数组排序
bool IsBig(char w1[WORDSIZE], char w2[WORDSIZE]); //w1字符串是否应该排在w2字符串后面
void Exchange(char *w1, char *w2); //交换在字符串数组中的两个个字符串
void Print(char word[MAXSIZE][WORDSIZE], int n); //跳过冗余输出字符串
bool IsSame(char w1[WORDSIZE], char w2[WORDSIZE]); //判断两个字符串是否相同
int main(){
char str[MAXSIZE] = {'\0'}; //记录输入的字符数据
int len = 0;
char ch;
while( (ch = getchar()) != EOF){
str[len++] = ch;
}
char word[MAXSIZE][WORDSIZE] = {'\0'}; //将输入分解成单个单词存储
int i,j,k;
i = j = k =0;
while(k < len){
if(IsWord(str[k])){
word[i][j++] = str[k];
++k;
}else{
if(str[k] == '-'){
++k;
continue;
}
else{
word[i][j] = '\0';
while(!IsWord(str[k]))
++k;
++i; j = 0;
}
}
}
//printf("%s\n", str);
SortStr(word, i); //排序
Print(word, i); //不冗余输出
return 0;
}
bool IsWord(char c){ //字符C是否为字母 a~z A~Z
if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return true;
return false;
}
void SortStr(char word[MAXSIZE][WORDSIZE], int n){ //字符串数组排序
int i,j;
for(i = 0; i < n - 1; i++){
for(j = i + 1; j < n; j++){
if(IsBig(word[i], word[j])){ //w1字符串是否应该排在w2字符串后面
Exchange(word[i], word[j]); //交换在字符串数组中的两个个字符串
}
}
}
}
bool IsBig(char w1[WORDSIZE], char w2[WORDSIZE]){ //w1字符串是否应该排在w2字符串后面
int len1 = strlen(w1);
int len2 = strlen(w2);
int l = (len1 < len2) ? len1 : len2;
int i;
for(i = 0; i < l;){
if(tolower(w1[i]) < tolower(w2[i]))
return false;
else if(tolower(w1[i]) == tolower(w2[i]))
i++;
else if(tolower(w1[i]) > tolower(w2[i]))
return true;
}
if(i == l){
if(len1 < len2)
return false;
else
return true;
}
}
void Exchange(char *w1, char *w2){ //交换在字符串数组中的两个个字符串
char temp[WORDSIZE];
strcpy(temp,w1);
strcpy(w1,w2);
strcpy(w2,temp);
}
void Print(char word[MAXSIZE][WORDSIZE], int n){ //跳过冗余输出字符串
if(n == 0)
return ;
printf("%s\n", word[0]);
char *temp = word[0];
for(int i = 1; i < n;i++){
if( !IsSame(word[i], temp) ){
printf("%s\n", word[i]);
temp = word[i];
}
}
}
bool IsSame(char w1[WORDSIZE], char w2[WORDSIZE]){ //判断两个字符串是否相同
int len1 = strlen(w1);
int len2 = strlen(w2);
if(len1 != len2)
return false;
else{
for(int i = 0; i < len1; i++){
if(tolower(w1[i]) != tolower(w2[i]))
return false;
}
}
return true;
}15-3
最新推荐文章于 2023-09-22 19:38:53 发布
2922

被折叠的 条评论
为什么被折叠?



