uva 492 Pig_Latin 题目详解及面向过程,面向对象的编程思想的粗略讲解
2014-04-16 15:11
579 查看
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 shouldbe converted to Pig Latin according to the following rules (non-words should be output exactly as they appear in the input):
Words that begin with a vowel (a, e, i, o, 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''.
Words that begin with a consonant (any letter than is not A, a, E, e, I, i, O, o, U 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''.
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]); } } }*/
相关文章推荐
- Java第二课 Java面向对象编程,面向对象编程和面向过程编程的区别,我们如何才能掌握面向对象的编程,类和对象的关系;讲解了Java中的构造、重载、this和super变量、静态变量、Java中的常
- 从零开始学java:面向过程,面向对象等编程思想个人理解
- 编程思想:面向对象和面向过程
- UVa 492 - Pig-Latin
- 从面向过程到面向对象,我的编程之路 推荐
- 【C/C++学院】0817-递归汉诺塔 双层递归 /CPP结构体 /面向过程与面向对象的编程模式/类的常识共用体实现一个类的特征/QT应用于类以及类的常识
- 面向对象/面向过程编程
- 面向过程和面向对象的编程----话题讲座
- 面向过程和面向对象的编程
- 编程思想:面向对象和面向过程
- 深入理解面向过程与面向对象的思想差异与各自特色
- 连载:面向对象葵花宝典:思想、技巧与实践(3) - 面向过程 vs 面向对象
- 两种重要的思想(面向对象 & 面向过程)
- 什么是对象,什么是类,什么是面向对象的编程语言,什么是面向面向对象的编程思想?
- 编程思想:面向对象和面向过程
- [置顶] 编程思想:面向对象和面向过程
- 面向接口编程详解——思想基础
- 【学习笔记】面向过程的编程与面向对象的编程的区别——我的理解
- 面向接口设计和编程——(面向对象、面向接口、面向过程、面向实现)
- 面向对象的形式,面向过程的思想