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; }}