uva 492 Pig_Latin 题目详解及面向过程,面向对象的编程思想的粗略讲解

Pig-Latin

You have decided that PGP encryptation is not strong enough for your email. You have decided to supplement it by first converting your clear text letter into Pig Latin before encrypting it with PGP.

Input and Output

You are to write a program that will take in an arbitrary number of lines of text and output it in Pig Latin. Each line of text will contain one or more words. A ``word'' is defined as a consecutive sequence of letters (upper and/or lower case). Words should be converted to Pig Latin according to the following rules (non-words should be output exactly as they appear in the input):

  1. Words that begin with a vowel (aeio, or u, and the capital versions of these) should just have the string ``ay'' (not including the quotes) appended to it. For example, ``apple'' becomes ``appleay''.
  2. Words that begin with a consonant (any letter than is not AaEeIiOoU or u) should have the first consonant removed and appended to the end of the word, and then appending ``ay'' as well. For example, ``hello'' becomes ``ellohay''.
  3. Do not change the case of any letter.

Sample Input

This is the input.

Sample Output

hisTay isay hetay inputay.



(以下很多名词由博文作者设计,旨在意会,不要深究.)
一.翻译:
翻译是没有必要的,这些题目的内容没有想象中那么困难,如果实在不会的,就用蹩脚的网络翻译,再加上一丢丢的英语基础,要完全理解内容是没有问题的.
技巧:先看输入输出样例,然后看输入输出要求.

二.本题亮点:
1. 如果不用编程只用想的话,这个题 非常非常简单.如果要编程的话......就 非常简单.
2.Pig-Latin:故意颠倒英语字母顺序拼凑而成的行话.
3.本题主要考察的是大家对于字符串的理解和运用能力.
三.设计解题代码: /*设计:即采用的语法方式*/
1.面向过程设计:不采用类,对象的经典语言方法.
四..比较:
1.面向对象和面向过程设计的好处和坏处?

#include<iostream>    /*面向过程思想的程序设计*/  
#include<string>  
#include<cctype>  
using namespace std;  
int isVowel(char alpa);  
  
int main()  
{  
    string st="";  
    while(getline(cin,st)){  
        int j;  
        for(int i=0;i<st.length();i++){  
            if(isalpha(st[i])){                   //如果是字符就进入循环判断
                if(!isVowel(st[i])){  
                    for(j=i+1;isalpha(st[j]);j++)     //j变量用于循环单词
                        cout<<st[j];        //输出字符
                    cout<<st[i]<<"ay";     //输出首字母和结尾ay
                }  
                else{  
                    for(j=i;isalpha(st[j]);j++)  
                        cout<<st[j];  
                    cout<<"ay";  
                }  
                i=j-1;    //因为循环有i++,故要记录上一个字符的序号
            }  
            else  
                cout<<st[i];     //不是字符就直接输出
        }  
        cout<<endl;          
    }  
    return 0;  
}  
int isVowel(char alpa){  
    switch(alpa){  
    case 'a':case 'e':case 'i':case 'o':case 'u':  
    case 'A':case 'E':case 'I':case 'O':case 'U':return 1;  
    default: return 0;  
    };  
}  



#include<iostream>           /*面向对象思想的程序设计*/  
#include<string>  
#include<cctype>  
using namespace std;  
int isVowel(char alpa);  
int main()  
{  
    string st="",buf="";  
    int i,j;  
    while(getline(cin,st)){  
        for(i=0;i<st.length();){  
            if(isalpha(st[i])){  
                for(j=0;isalpha(st[i]);){  
                    buf[j++]=st[i++];  
                }  
                buf[j]='\0';              /*记录一个单词*/  
                if(isVowel(buf[0])){  
                    cout<<&buf[0]<<"ay";  
                }  
                else if(!isVowel(buf[0])){  
                    cout<<&buf[1]<<buf[0]<<"ay";  
                }                      /*输出一个单词*/  
            }  
            else{  
                cout<<st[i++];  
            }  
        }  
        cout<<endl;  
    }  
    return 0;  
}  
int isVowel(char alpa){  
    switch(alpa){  
    case 'a':case 'e':case 'i':case 'o':case 'u':  
    case 'A':case 'E':case 'I':case 'O':case 'U':return 1;  
    default: return 0;  
    };  
}  


#include<iostream>           /*面向对象思想采用了类和对象的程序设计*/  
#include<string>  
#include<cctype>  
using namespace std;  
  
class Words{    //单词类
private:  
    string word;  
    bool isVowel(const char &alpa);  
public:  
    int Readword(const string &st,int i);  
    void Printword();  
};  
  
int main()  
{  
    string st="";  
    while(getline(cin,st)){  
        for(int i=0;i<st.length();){  
            if(isalpha(st[i])){  
                Words myword;  
                i=myword.Readword(st,i);  
                myword.Printword();  
            }  
            else{  
                cout<<st[i++];  
            }  
        }  
        cout<<endl;  
    }  
    return 0;  
}  
  
bool Words::isVowel(const char &alpa){  
    switch(alpa){  
        case 'a':case 'e':case 'i':case 'o':case 'u':  
        case 'A':case 'E':case 'I':case 'O':case 'U':return true;  
        default: return false;  
    };  
}  
int Words::Readword(const string &st,int i){  
    int j;  
    for(j=0;isalpha(st[i]);){  
        word[j++]=st[i++];  
    }  
    word[j]='\0';  
    return i;  
}  
void Words::Printword(){  
    if(isVowel(word[0])){  
        cout<<&word[0]<<"ay";  
    }  
    else{  
        cout<<&word[1]<<word[0]<<"ay";  
    }  
}  



======无法AC  WA or Runtime Error
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int isVowel(char alpa);  /*return 1 if it is a vowel else return 0*/
int main()
{
    char sentence[100000],buf[100000];
    int i,j;
    while(fgets(sentence,100000,stdin)){
        for(i=0;i<strlen(sentence);){
            if(isalpha(sentence[i])){
                for(j=0;isalpha(sentence[i]);){
                    buf[j++]=sentence[i++];
                }
                buf[j]='\0';
                if(isVowel(buf[0])){
                    printf("%say",&buf[0]);
                }
                else if(!isVowel(buf[0])){
                    printf("%s%cay",&buf[1],buf[0]);
                }
            }
            else{
                printf("%c",sentence[i++]);
            }
        }
    }
    return 0;
}
int isVowel(char alpa){
    switch(alpa){
    case 'a':case 'e':case 'i':case 'o':case 'u':
    case 'A':case 'E':case 'I':case 'O':case 'U':return 1;break;
    default: return 0;
    };
}



  /*  while(fgets(sentence,100000,stdin)){
        for(i=0;i<strlen(sentence);i++){
            if(isalpha(sentence[i])){
                if(isVowel(sentence[i])){
                    for(j=i;isalpha(sentence[j]);j++){
                        printf("%c",sentence[j]);
                    }
                    printf("ay");
                }
                else{
                    for(j=i+1;isalpha(sentence[j]);j++){
                        printf("%c",sentence[j]);
                    }
                    printf("%cay",sentence[i]);
                }
                i=j-1;
            }
            else{
                printf("%c",sentence[i]);
            }
        }
    }*/

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值