【C语言小训】1063-提取缩略词

Problem C: 提取缩略词

Description

在英文文献中,尤其是专业文献中,经常有很多的缩略词,如CPU代表Central Processing Unit等。为了方便学习,Qili决定从一批英文论文中提取出所有的缩略词以及它们的全称。

经过初步判断,这些文章的缩略词都是以全部大写字母的形式出现,而且每个缩略词之后会有一个空格,之后是它的全称。全称使用“()”括起来,左括号跟它后面的单词之间没有空格,右括号跟它前面的单词之间没有空格,全称的每个单词的首字母与缩略词的顺序是对应的。全称的单词之间可能有连字符“-”连接,如SSI (small-scale integration)。

你来帮Qili编写这个程序,将所有的缩略词和全称提取出来。

Input

一篇英文文章,每个缩略词第一次出现时,其后都跟有它的英文全称;同样的缩略词再出现时,将不再出现全称。每个缩略词和全称都不会太长。缩略词总数小于100。

Output

如果有缩略词,第一行输出“Abbreviation > Full Name”。之后每个缩略词和它的全称占一行,包括缩略词的序号(从1开始)、缩略词、分隔符(>)和全称。所有输出以分隔符(==>)分为两部分,右侧的全称左对齐,左侧的缩略词右对齐,但序号和第一行的“Abbreviation”是左对齐的。每个缩略词只输出一遍。

如果没有缩略词,则输出:There is no abbreviations in this text.

Sample Input

COMputers such as the ENIAC (Electronic Numerical Integrator And Computer) had to be physically rewired in order to perform
different tasks, which caused these machines to be called “fixed-program computers.”

Sample Output

Abbreviation ==> Full Name
1: ENIAC ==> Electronic Numerical Integrator And Computer

HINT

注意:大写的不一定都是缩写,有括号的不一定都是全称。可以做一个函数忽略字母的大小写判断字符相同。

提取缩略词时应注意:英文全称的单词之间不一定只用空格分开,有些英文单词是带连字符的,但是其首字母也会被提取出缩略词,如SSI (small-scale integration)。

Append Code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int JudgeIngonre(char a,char b){
    if(a >= 'A' && a <= 'Z')a = a + 32;
    if(b >= 'A' && b <= 'Z')b = b + 32;
    if(a == b)return 1;
    else return 0;
}

int main(){
    char s[1000];
    char a[100];//用于存放缩略词
    char b[100];//用于存放意思
    int flag = 0;//用于查看是否出现缩略词以及计数
    while(gets(s)){
        int i = 0;
        int n = strlen(s);
        for(i = 0;i < n;i++){
            memset(a,0,100);
            memset(b,0,100);
            int x = i;
            int judge = 1;//用于判断两个空格内的数是否全为大写;
            //把两个空格之间的数拿出来,看看是不是大写
            for(;s[i] != ' ';i++){
                a[i - x] = s[i];
                if(s[i] < 'A' || s[i] > 'Z'){
                    judge = 0;
                }
            }
            //如果前面都是大写,跳过空格,并且如果下一个是'('
            if(judge == 1){
                if(i + 2 < n && s[i + 1] == '('&&JudgeIngonre(s[i+2],a[0]) == 1){
                    int j = strlen(a);//缩略词大小
                    int m = 1;//遍历缩略词,因为已经判断了第一个
                    int k = 0;//遍历详细
                    //开始比对大写字母
                    i = i + 2;
                    for(k = 0;m < j && i < n;i++,k++){
                        b[k] = s[i];//开始向里放;
                        if((s[i - 1] == ' ' || s[i - 1] == '-' )&& JudgeIngonre(s[i],a[m]) == 0)break;
                        if((s[i - 1] == ' ' || s[i - 1] == '-' )&& JudgeIngonre(s[i],a[m]) == 1)m++;
                    }
                    //如果缩略词匹配成功,并且')'匹配成功
                    if(m == j){
                        for(;i < n && s[i] != ' ';i++,k++){
                            if(s[i] == ')')break;
                            b[k] = s[i];
                        }
                        //完全匹配成功!!!!!!!!打个重点
                        if(s[i] == ')'){
                            if(flag == 0)printf("Abbreviation ==> Full Name\n");
                            flag++;
                            printf("%d:",flag);
                            int r = 10 - j;
                            if(flag >= 10)r-=1;
                            while(r--)printf(" ");
                            int y = 0;
                            for(y = 0;y < j;y++)
                                printf("%c",toupper(a[y]));
                            printf(" ==> ");
                            for(y = 0;y < strlen(b);y++)
                                printf("%c",b[y]);
                            puts("");
                            i++;
                        }
                    }
                }
            }
        }

    }
    if(flag == 0)printf("There is no abbreviations in this text.\n");

}

//一个大大的问题
//最后输出的时候,下面的代码不对,但是输出和上面答案显示是一样的,一直没有找出错来
//哭了
//完全匹配成功,但是反正这个就显示格式错误……我……
                        if(s[i] == ')'){
                            if(flag == 0)printf("Abbreviation ==> Full Name\n");
                            flag++;
                            printf("%d:%10s ==> %s\n",flag,a,b);
                            i++;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值