Tutorial 12: Problem C: addition and subtraction of overloaded characters, and problem addition and subtraction

Source: Internet
Author: User

Tutorial 12: Problem C: addition and subtraction of overloaded characters, and problem addition and subtraction

Home Web Board ProblemSet Standing Status Statistics
  Problem C: addition and subtraction of overloaded characters Problem C: Add/Subtract Time Limit of overloaded characters: 1 Sec Memory Limit: 128 MB
Submit: 493 Solved: 248
[Submit] [Status] [Web Board] Description

Defines a Character class Character and has only one data member of the char type.

The plus (+), minus (-), minus (<), and minus (>) operators are reloaded. The second operand of "+" and "-" is an integer n of the int type. "+" Is used to return the object whose nth character after the current character is the attribute value, and "-" is used to return the object whose nth character before the current character is the attribute value. As shown in the sample.

 

Input

Row N> 0 indicates the number of test cases.

Each test uses an integer of the 1 character (lowercase English letter) and an int type.

 

Output

There are N rows of output, and each line of input corresponds to one line of output. each row of output includes the nth character after the corresponding Input character and the nth character before the character. For example, if the input character of the 2nd use cases is "a" and the integer is "1", the 1st characters after "a" are "B", before "a", the first 1st characters are "z". Note: The input integer may be a negative number.

 

Sample Input3a 0a 1a-1 Sample Outputa AB zz bHINT Append Codeappend. cc, [Submit] [Status] [Web Board]

Please refer to the following link for more information:
All Copyright Reserved 2010-2011 SDUSTOJ TEAM
GPL2.0 2003-2011 HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin: admin

#include<iostream>using namespace std;class Character{public:    char c;    friend istream& operator >> (istream&,Character&);    friend char operator + (Character&, int);    friend char operator - (Character&, int);};istream& operator >> (istream& is,Character& c){    is>>c.c;    return is;}char operator + (Character& c, int o2){    return (c.c  + o2 % 26 - 'a' + 26 ) % 26 + 'a';}char operator - (Character& c, int o2){    return (c.c  - o2 % 26 - 'a' + 26 ) % 26 + 'a';}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;    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.