码迷,mamicode.com
首页 > 其他好文 > 详细

Problem A: 重载字符的加减法

时间:2017-04-30 01:01:52      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:返回   container   line   desc   esc   public   php   ring   ios   

Description

定义一个字符类Character,只有一个char类型的数据成员。

重载它的+、-、<<和>>运算符,其中+、-的第二个操作数是int类型的整数n。“+”用于返回以当前字符之后的第n个字符为属性值的对象,“-”用于返回当前字符之前的第n个字符为属性值的对象。如样例所示。

Input

第1行N>0表示测试用例个数。

每个测试用包括1个字符(小写英文字母)和1个int类型的整数。

Output

输出有N行,每行输入对应一行输出,每行输出包括对应输入字符之后的第n个字符,以及该字符之前的第n个字符。如样例中第2个用例输入字符是“a”,整数是“1”,那么“a”之后的第1个字符是”b“,"a"之前的第1个字符是”z“;注意:输入的整数可能是负数。

Sample Input

3 a 0 a 1 a -1

Sample Output

a a b z z b

HINT

Append Code

int main()
{
    int cases, data;
    Character ch;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>ch;
        cin>>data;
        cout<<(ch + data)<<" "<<(ch - data)<<endl;
    }
}
 
 
代码
#include<iostream>
using namespace std;
class Character
{

public:char s;
    Character(char s = 0):s(s) {}
   char operator +(int n)
   {
       int temp = s+n;
       int t;
       if(temp > ‘z‘)
            t = s+(temp - ‘z‘ -1 )%26;
        else if(temp < ‘a‘)
            t = ‘z‘ - (s - temp -1)%26;
        else
            t = temp;
        return t;
   }
    char operator-(int n)
    {
        return (*this)+(-n);
    }
    friend ostream & operator <<(ostream &os,Character &c);
    friend  istream & operator >>(istream &is ,Character &c);
};
ostream & operator <<(ostream &os,Character &c)
{
    os<<c.s;
    return os;
}
istream & operator >>(istream &is ,Character &c)
{
    is>>c.s;
    return is;
}
int main()
{
    int cases, data;
    Character ch;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>ch;
        cin>>data;
        cout<<(ch + data)<<" "<<(ch - data)<<endl;
    }
}

Problem A: 重载字符的加减法

标签:返回   container   line   desc   esc   public   php   ring   ios   

原文地址:http://www.cnblogs.com/go-ahead-TT/p/6786590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!