15-3

#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;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值