10082—WERTYU

Problem C: WERTYU

A common typingerror is to place the hands on the keyboard one row to the right of the correctposition. So "Q" is typed as "W" and "J" is typedas "K" and so on. You are to decode a message typed in this manner.

Input consists ofseveral lines of text. Each line may contain digits, spaces, upper case letters(except Q, A, Z), or punctuation shown above [except back-quote (`)]. Keyslabelled with words [Tab, BackSp, Control,etc.] are not represented inthe input. You are to replace each letter or punction symbol by the oneimmediately to its left on the QWERTY keyboard shown above. Spaces in the inputshould be echoed in the output.

Sample Input

O S, GOMR YPFSU/

Outputfor Sample Input

I AM FINE TODAY.

 

代码:

#include<iostream>

#include<cstdio>//使用EOF

using namespace std;

int main()

{

   char ch[]="`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./";//将//键盘的内容存在一个常量数组中

   char c;

   while((c=cin.get())!=EOF)//每输入一个便判读并输出一次

    {

       int i;

       for(i=0;ch[i]&&ch[i]!=c;i++);

/*char型数组的特点是均以’\0’结尾,该字符的码值为0,用于判断的时候,相当于FALSE.for循环没有循环体,只有for循环的头部,判断的理由是for循环最后有一个分号,表示是一个单独的语句.for循环的功能是找出字符的情况.所有字符分两类.一类是键盘上不存在的字符,如$等等,对于这类字符,要求是直接输出,逻辑判断中的ch[i]的功能就在于此,这类字符在常量数组中无法找到,也就是对于每个我们可见的字符数组ch中的字符均满足ch[i]!=c,那么这类字符靠ch[i]退出for循环,当遍历完可见字符后,ch[i]变成了’\0‘,而该字符表示:FALSE,所以第一类字符判断完成,退出循环,执行else语句,原样输出输入的字符。第二类字符,即键盘上存在的字符,使用ch[i]!=c来定位,当ch[i]==c时,退出循环,执行if语句,输出ch[i-1]。题目解决。*/

       if(ch[i])

       {

           cout<<ch[i-1];//第二类字符

       }

       else

       {

           cout<<c;//第一类字符

       }

    }

   return 0;

}

程序分析:该思路远比使用几十个if语句或者switch语句简单.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值