山东科技大学2020年5月21日实验题解
第一题: 新奇的加法运算
Description
定义类newInt,包括:
-
int类型的数据成员。
-
重载运算符“+”。计算规则为:将A、B对应位置上的数字相加,只保留个位数作为结果的对应位置上的数字。比如:876 + 543 = 319。注意:该运算不改变两个操作数的值。
-
重载输入和输出运算符,用于输入和输出对象的属性值。
-
无参构造函数和带参构造函数。
Input
第1行N>0,表示测试用例数量。
每个测试用例包括2个非负整数,用空格隔开。
Output
见样例。
Sample Input
4
876 543
999 9999
9 1999
199 88
Sample Output
876 + 543 = 319
999 + 9999 = 9888
9 + 1999 = 1998
199 + 88 = 177
题目给定代码
int main()
{
int cases;
newInt a, b, c;
cin>>cases;
for (int i = 0; i < cases; i++)
{
cin>>a>>b;
c = a + b;
cout<<a<<" + "<<b<<" = "<<c<<endl;
}
return 0;
}
标程
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class newInt {
private:
int i;
public:
newInt operator + (newInt& n) {
int x = i;
int y = n.i;
int s = 0;
if (x >= y) {
int a = x, l = 1, mode = 1;
a /= 10;
while (a) {
a /= 10, ++ l;
}
for (R int j = 0; j < l; ++j) {
s += ((x % 10 + y % 10) % 10) * mode, mode *= 10, x /= 10, y /= 10;
}
}
else {
int a = y, l = 1, mode = 1;
a /= 10;
while (a) {
a /= 10, ++l;
}
for (R int j = 0; j < l; ++j) {
s += ((x % 10 + y % 10) % 10) * mode, mode *= 10, x /= 10, y /= 10;
}
}
return newInt(s);
}
friend istream& operator >> (istream& is, newInt& n) {
is >> n.i;
return is;
}
friend ostream& operator << (ostream & os, const newInt & n) {
os << n.i;
return os;
}
newInt(int a = 0) {
i = a;
}
};
int main()
{
int cases;
newInt a, b, c;
cin >> cases;
for (int i = 0; i < cases; i++)
{
cin >> a >> b;
c = a + b;
cout << a << " + " << b << " = " << c << endl;
}
return 0;
}
题目二: 重载字符的加减法
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
题目给定代码
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<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class Character {
private:
char c;
public:
Character(char ch = '\0') {
c = ch;
}
friend char operator + (Character ch, int n) {
n %= 26, ch.c += n;
if (ch.c > 'z') {
ch.c -= 26;
}
else if (ch.c < 'a') {
ch.c += 26;
}
return ch.c;
}
friend char operator - (Character ch, int n) {
n %= 26, ch.c -= n;
if (ch.c < 'a') {
ch.c += 26;
}
else if (ch.c > 'z') {
ch.c -= 26;
}
return ch.c;
}
friend istream& operator >> (istream& is, Character& ch) {
is >> ch.c;
return is;
}
friend ostream& operator << (ostream& os, Character& ch) {
os << ch.c;
return os;
}
};
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;
}
}
题目三: 成绩排序
Description
定义Student类:
-
数据成员string name和int score表示一个学生的姓名、成绩。
-
无参构造函数。
-
void setStudent(string,int)方法,用于设置一个学生的属性值。
-
重载>(大于运算符)。Student类的对象A和B的大小关系,A>B定义为A.score>B.score,或者A.score=B.score但A.name<B.name。
5.重载运算符<<用于输出学生信息:先输出成绩再输出姓名,中间用一个空格隔开。
Input
分多行。第一个M>0表示有M个学生信息。
之后有M行,每一行是一个学生信息。第一部分是学生姓名,第二部分是学生成绩。
Output
输出为M行,按照从大到小的顺序依次输出每个学生的成绩、姓名。假定不存在重名的学生。
Sample Input
5
Tom 98
Jack 97
Mary 98
Sherry 99
Dock 97
Sample Output
99 Sherry
98 Mary
98 Tom
97 Dock
97 Jack
题目给定代码
int main()
{
int cases;
string name;
int score;
cin>>cases;
Student students[cases], temp;
for (int i = 0; i < cases; i++)
{
cin>>name>>score;
students[i].setStudent(name, score);
}
for (int i = 0; i < cases; i++)
{
for (int j = 0; j < cases - i - 1; j++)
{
if (!(students[j] > students[j + 1]))
{
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
for (int i = 0; i < cases; i++)
cout<<students[i]<<endl;
return 0;
}
标程
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class Student {
private:
string name;
int score;
public:
Student() {
name = "", score = 0;
}
void setStudent(string s, int number) {
name = s, score = number;
}
int operator > (Student& a) {
if (score > a.score) {
return 1;
}
else if (score == a.score) {
if (name < a.name) {
return 1;
}
}
return 0;
}
friend ostream& operator << (ostream& os, const Student& S) {
os << S.score << " " << S.name;
return os;
}
};
int main()
{
int cases;
string name;
int score;
cin >> cases;
Student students[cases], temp;
for (int i = 0; i < cases; i++)
{
cin >> name >> score;
students[i].setStudent(name, score);
}
for (int i = 0; i < cases; i++)
{
for (int j = 0; j < cases - i - 1; j++)
{
if (!(students[j] > students[j + 1]))
{
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
for (int i = 0; i < cases; i++)
cout << students[i] << endl;
return 0;
}
题目四: 整型数组运算符重载
Description
定义Array类:
1.拥有数据成员int length和int *mems,分别是数组中元素的个数和元素列表。
-
无参构造函数,将mems设置为NULL,length为0。
-
重载==运算符,用于判断两个Array对象是否相等。相等包括两种情况:(1)两个对象是同一个对象,即它们拥有相同的地址(记住: this指针指向当前对象,是当前对象的地址);(2)两个对象的length相同,且mems中对应元素的值相同。其他情况均为不相等。
-
利用友元函数重载<<和>>运算符。输入、输出格式见下。
Input
输入分多行。
第一行是一个正整数M,表示有M个数组。
每个数组是一行,其中第一个非负整数N表示该数组的元素个数,之后有N个整数。
Output
输出有M行。
第一行输出即为第一个数组。
自第二行开始,首先输出对应的数组元素(两两之间用空格隔开,首尾不能有空格),如果数组为空,则不输出元素。之后根据这个数组与上个数组是否相同,输出“unequal to above.”(不相等)和“equal to above”(相等)。
Sample Input
5
3 1 2 3
3 1 2 3
0
7 1 2 3 4 5 6 7
7 1 2 3 4 5 6 8
Sample Output
1 2 3
1 2 3 equal to above.
unequal to above.
1 2 3 4 5 6 7 unequal to above.
1 2 3 4 5 6 8 unequal to above.
题目给定代码
int main()
{
int cases;
cin>>cases;
Array arraies[cases];
for (int i = 0; i < cases; i++)
{
cin>>arraies[i];
}
cout<<arraies[0]<<endl;
for (int i = 1; i < cases; i++)
{
if (arraies[i] == arraies[i - 1])
{
cout<<arraies[i]<<" "<<"equal to above."<<endl;
}
else
{
cout<<arraies[i]<<" "<<"unequal to above."<<endl;
}
}
return 0;
}
标程
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class Array {
private:
int length, * mems;
public:
Array() {
length = 0, mems = NULL;
}
bool operator == (Array& a) {
R int i;
for (i = 0; i < length; ++i) {
if (mems[i] != a.mems[i]) {
break;
}
}
if (this == &a) {
return 1;
}
else if (a.length == length && length == i) {
return 1;
}
else {
return 0;
}
}
friend ostream& operator << (ostream& os, Array& a) {
for (R int i = 0; i < a.length; ++i) {
if (i != a.length - 1) {
os << a.mems[i] << " ";
}
else {
os << a.mems[i];
}
}
return os;
}
friend istream& operator >> (istream& is, Array& a) {
is >> a.length;
a.mems = new int[a.length];
for (R int i = 0; i < a.length; ++i) {
is >> a.mems[i];
}
return is;
}
~Array() {
if (mems != NULL) {
delete[]mems;
}
}
};
int main()
{
int cases;
cin >> cases;
Array arraies[cases];
for (int i = 0; i < cases; i++)
{
cin >> arraies[i];
}
cout << arraies[0] << endl;
for (int i = 1; i < cases; i++)
{
if (arraies[i] == arraies[i - 1])
{
cout << arraies[i] << " " << "equal to above." << endl;
}
else
{
cout << arraies[i] << " " << "unequal to above." << endl;
}
}
return 0;
}
题目五: 时间类的流插入、提取和递增、递减运算
Description
封装一个时间类Time,在类上重载以下运算符,使得main()函数能够正确运行。
流插入操作符“>>”,按照输入格式从标准输入读取三个整数:hh,mm,ss,分别表示时、分、秒,其值在int范围内。
流提取操作符“<<”;按照“hh:mm:ss”输出Time类的对象,不合法的时间输出“error!!!”。
前置自增运算符“++”:把时间对象的秒数加1并返回。
前置自减运算符“–”:把时间对象的秒数减1并返回。
后置自增运算符“++”:把时间对象的秒数加1,返回原值。
后置自减运算符“–”:把时间对象的秒数减1,返回原值。
以上4个自增、自减仅对合法的时间操作,并且不会产生不合法的时间。比如:
若原时间对象为“00:00:00”,自减运算后的对象为“23:59:59”;
若原时间对象为“23:59:59”,自增运算后的对象为“00:00:00”;
若原时间对象为“24:60:60”,自增或自减运算后对象仍为“24:60:60”。
函数调用格式见append.cc。
append.cc中已给出main()函数
Input
输入的第一个整数n,表示有n组测试数据,每组3个整数:hh,mm,ss,分别表示时、分、秒,其值都在int范围内。
Output
输出一张表:每列8个字符宽,两列之间有一个空格。
首先,输出一个表头:“++t --t t t-- t++ t ”,
其次,对应每组测试数据在一行内依次以下内容:
前置++、前置–、原值、后置–、后置++、原值。
若输入的日期合法,输出格式为“hh:mm:ss”,不足两位的输出需要前面补0。如果输入的时间不合法,则输出“error!!!”。格式见sample。
Sample Input
6
0 0 1
0 59 59
1 1 60
23 0 0
23 59 59
24 1 0
Sample Output
++t --t t t-- t++ t
00:00:02 00:00:01 00:00:01 00:00:01 00:00:00 00:00:01
01:00:00 00:59:59 00:59:59 00:59:59 00:59:58 00:59:59
error!!! error!!! error!!! error!!! error!!! error!!!
23:00:01 23:00:00 23:00:00 23:00:00 22:59:59 23:00:00
00:00:00 23:59:59 23:59:59 23:59:59 23:59:58 23:59:59
error!!! error!!! error!!! error!!! error!!! error!!!
题目给定代码
int main()
{
Time t;
int cases;
cin>>cases;
cout<<setw(8)<<left<<"++t"<<" ";
cout<<setw(8)<<left<<"--t"<<" ";
cout<<setw(8)<<left<<"t"<<" ";
cout<<setw(8)<<left<<"t--"<<" ";
cout<<setw(8)<<left<<"t++"<<" ";
cout<<setw(8)<<left<<"t"<<right<<endl;
for(int i = 1; i <= cases; ++i)
{
cin>>t;
cout<<(++t)<<" ";
cout<<(--t)<<" ";
cout<<t<<" ";
cout<<t--<<" ";
cout<<t++<<" ";
cout<<t<<endl;
}
}
标程
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<iomanip>
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
#define pi 3.141
#define INF 1400000000
using namespace std;
class Time {
private:
int hour, minute, second;
public:
Time() {
hour = 0, minute = 0, second = 0;
}
friend ostream& operator << (ostream& os, const Time& T) {
if ((T.hour >= 0 && T.hour < 24) && (T.minute >= 0 && T.minute < 60) && (T.second >= 0 && T.second < 60)) {
os << setw(2) << setfill('0') << T.hour << ":" << setw(2) << setfill('0') << T.minute << ":" << setw(2) << setfill('0') << T.second;
}
else {
os << "error!!!";
}
return os;
}
friend istream& operator >> (istream& is, Time& T) {
is >> T.hour >> T.minute >> T.second;
return is;
}
Time& operator ++() {
if ((hour >= 0 && hour < 24) && (minute >= 0 && minute < 60) && (second >= 0 && second < 60)) {
++second;
if (second >= 60) {
second -= 60, ++minute;
}
if (minute >= 60) {
minute -= 60, ++hour;
}
if (hour >= 24) {
hour -= 24;
}
}
return *this;
}
Time operator ++(int) {
Time q = (*this);
if ((hour >= 0 && hour < 24) && (minute >= 0 && minute < 60) && (second >= 0 && second < 60)) {
++second;
if (second >= 60) {
second -= 60, ++minute;
}
if (minute >= 60) {
minute -= 60, ++hour;
}
if (hour >= 24) {
hour -= 24;
}
}
return q;
}
Time& operator --() {
if ((hour >= 0 && hour < 24) && (minute >= 0 && minute < 60) && (second >= 0 && second < 60)) {
--second;
if (second < 0) {
second += 60, --minute;
}
if (minute < 0) {
minute += 60, --hour;
}
if (hour < 0) {
hour += 24;
}
}
return *this;
}
Time operator --(int) {
Time q = (*this);
if ((hour >= 0 && hour < 24) && (minute >= 0 && minute < 60) && (second >= 0 && second < 60)) {
--second;
if (second < 0) {
second += 60, --minute;
}
if (minute < 0) {
minute += 60, --hour;
}
if (hour < 0) {
hour += 24;
}
}
return q;
}
};
int main()
{
Time t;
int cases;
cin >> cases;
cout << setw(8) << left << "++t" << " ";
cout << setw(8) << left << "--t" << " ";
cout << setw(8) << left << "t" << " ";
cout << setw(8) << left << "t--" << " ";
cout << setw(8) << left << "t++" << " ";
cout << setw(8) << left << "t" << right << endl;
for (int i = 1; i <= cases; ++i)
{
cin >> t;
cout << (++t) << " ";
cout << (--t) << " ";
cout << t << " ";
cout << t-- << " ";
cout << t++ << " ";
cout << t << endl;
}
}