UVA 10815 - Andy's First Dictionary

本文展示了一个使用C++进行字符串处理与排序的例子,通过读取输入并按特定规则进行字符串比较来完成任务。该程序首先从标准输入读取文本,然后对文本中的单词进行标准化处理,最终对单词进行排序并输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

水题手速不都快   思维还是不是非常流畅   继续锻炼

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;

struct Words
{
    char s[205];
}words[5005];

int T;

int cmp(struct Words a, struct Words b)
{
    int i;
    for(i = 0; a.s[i]!='\0'&&b.s[i]!='\0'; i++)
        if(a.s[i] < b.s[i] )return 1;
        else if(a.s[i] > b.s[i] )return 0;
    if(a.s[i] == '\0') return 1;
    if(b.s[i] == '\0') return 0;
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    #endif // LOCAL
    char str[205];
    int i, j;
    while(gets(str)!=NULL)
    {
        if(str[0]=='\n')continue;
        char s[205];
        int len_str = strlen(str);
        for(i = 0; i < len_str; i++)
        {
            int t = 0;
            if(!isalpha(str[i]))continue;
            while(isalpha(str[i]))s[t++] = tolower(str[i++]);
            s[t] = '\0';
            if(T == 0){strcpy(words[T++].s,s);continue;}
            for(j = 0; j < T; j++)
                if(strcmp(words[j].s,s) == 0)break;
            if(j == T) strcpy(words[T++].s,s);
        }
    }
    sort(words,words+T,cmp);
    for(i = 0; i < T; i++)
        puts(words[i].s);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值