6-1 使用成员函数重载复数类的运算符+
类Complex声明了一个复数类,有两个数据成员realPart(代表复数的实部)和imgPart(代表复数的虚部),并定义了成员函数实现了重载运算符“+”以实现两个复数对象的相加操作。成员函数Show用来输出复数的实部和虚部。请完成对运算符“+”的重载操作。
函数接口定义:
Complex& Complex::operator+(Complex& com);
参数com为复数类Complex的对象的引用,函数的返回值为当前对象与com对象相加后的值。
裁判测试程序样例:
#include<iostream>
using namespace std;
class Complex {
public:
Complex(double realPart = 0, double imgPart = 0) {
this->realPart = realPart;
this->imgPart = imgPart;
}
Complex& operator+(Complex& com);
void Show() {
cout << realPart << " " << imgPart << endl;
}
private:
double realPart, imgPart;
};
int main() {
int r1, i1; //第1个复数对象的实部和虚部
int r2, i2; //第1个复数对象的实部和虚部
cin >> r1 >> i1;
cin >> r2 >> i2;
Complex c1(r1, i1); //构造第1个复数对象c1
Complex c2(r2, i2); //构造第2个复数对象c2
c1 = c1 + c2;
c1.Show();
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3 4
10 20
输出样例:
13 24
参考答案
Complex& Complex::operator+(Complex& c1) {
c1.realPart += this->realPart;
c1.imgPart += this->imgPart;
return c1;
}
6-2 复数的加法*
作者 李祥
单位 湖北经济学院
请设计复数类COMPLEX,实现复数的输入、输出和加法运算。
#include <iostream>
#include <iomanip>
using namespace std;
/* 你提交的代码将被嵌在这里 */
int main()
{
COMPLEX a, b, c;
cin >> a >> b;
c = a + b;
cout << c << endl;
return 0;
}
输入样例
3.2+4.9i
2.5-7.3i
输出样例
5.7-2.4i
要求:
- 设计构造函数,达到以下效果。
COMPLEX a;cout << a << endl; // 输出: 0+0i
COMPLEX a(3);cout << a << endl; // 输出: 3+0i
COMPLEX a(3, 4);cout << a << endl; // 输出: 3+4i
- 重载输出运算符函数,达到以下效果。
COMPLEX a(3, 4);cout << a << endl; // 输出: 3+4i;
COMPLEX a(3, 4), b(2, -7);cout << a << ' ' << b << endl; // 输出: 3+4i 2-7i
- 重载输入运算符函数,达到以下效果。
COMPLEX a;cin >> a; // 输入: 3+4i
COMPLEX a, b;cin >> a >> b; // 输入: 3+4i 2-7icout << a << ' ' << b << endl; // 输出: 3+4i 2-7i
- 重载算术加运算符,达到以下效果。
COMPLEX a(3, 4), b(2, -7), c;c = a + b;cout << c << endl; // 输出: 5-3i
COMPLEX a(3, 4), b(2, -7), c(-1, 8), d;d = a + b + c;cout << d << endl; // 输出: 4+5i
参考答案:
class COMPLEX {
private:
float image,real;
public:
COMPLEX(float a= 0,float b=0){
real = a;
image = b;
}
COMPLEX operator+( COMPLEX &a){
return COMPLEX(a.real+real,a.image+image);
}
COMPLEX operator-(COMPLEX &a){}
friend COMPLEX operator+(float a,COMPLEX &b)
{
COMPLEX p(a+b.real,b.image);
return p;
}
friend COMPLEX operator+(COMPLEX &b,float a)
{
COMPLEX p(a+b.real,b.image);
return p;
}
friend ostream&operator<<(ostream&out,COMPLEX&a) {
out<<a.real;
if(a.image>=0) out<<'+';
out<<a.image<<'i';
return out;
}
friend istream&operator>>(istream&in,COMPLEX&s) {
char sign;
in>>s.real>>s.image;
in>>sign;
return in;
}
};
6-3 学生成绩的输入和输出(运算符重载)
现在需要输入一组学生的姓名和成绩,然后输出这些学生的姓名和等级。
输入时,首先要输入学生数(正整数)N。接着输入N组学生成绩,每组成绩包括两项:第一项是学生姓名,第二项是学生的成绩(整数)。
输出时,依次输出各个学生的序号(从1开始顺序编号),学生姓名,成绩等级(不小于60为PASS,否则为FAIL)
函数接口定义:
面向Student类对象的流插入和流提取运算符
裁判测试程序样例:
#include <iostream>
#include <string>
using namespace std;
/* 请在这里填写答案 */
int main(){
int i, repeat;
Student st;
cin>>repeat;
for(i=0;i<repeat;i++){
cin>>st;
cout<<st<<endl;
}
return 0;
}
输入样例:
3
Li 75
Zhang 50
Yang 99
输出样例:
1. Li PASS
2. Zhang FAIL
3. Yang PASS
参考答案
class Student{
int no; // 序号
string name; //姓名
int grade; // 成绩
public:
Student():no(0){ } // 初始化序号为 0
friend istream & operator>>(istream &cin, Student &stu); //声明为友元,才能访问Student对象的私有成员
friend ostream & operator<<(ostream &cout,const Student &stu); // 声明为友元,才能访问Student对象的私有成员
};
istream & operator>>(istream &cin, Student &stu){
cin>>stu.name>>stu.grade; // 输入姓名和成绩
stu.no++; // 序号加 1
return cin;
}
ostream & operator<<(ostream &cout,const Student &stu){
cout<<stu.no<<". "<<stu.name<<" "<<(stu.grade<60?"FAIL":"PASS");
return cout;
}
6-4 时钟模拟
一个Time类,数据成员有时、分、秒。要求模拟秒表,每次走一秒,满60秒进位,秒又从零开始计数。满60分进位,分又从零开始计数。输出时、分和秒的值。(使用重载++运算符实现)
时间类定义:
class MyTime
测试程序样例:
/* 请在这里填写答案 */int main(){ MyTime t1,t2(23,59,59),t3; cin>>t3; ++t1; cout<<t1<<endl; ++t2; cout<<t2<<endl; ++t3; cout<<t3<<endl; return 0;}
输入样例:
12 35 59
输出样例:
0:0:1
0:0:0
12:36:0
参考答案
#include <iostream>
using namespace std;
class MyTime
{
public:
MyTime(int h = 0, int m = 0, int s = 0) :h(h), m(m), s(s){}
friend istream& operator>>(istream&, MyTime&);
friend ostream& operator<<(ostream&, MyTime&);
MyTime operator++();
private:
int h, m, s;
};
istream& operator>>(istream& is, MyTime& t){
is >> t.h >> t.m >> t.s;
return is;
}
ostream& operator<<(ostream& os, MyTime& t) {
os << t.h << ":" << t.m << ":" << t.s;
return os;
}
MyTime MyTime::operator++() {
s += 1;
if (s == 60) {
s = 0;
m += 1;
if (m == 60) {
m = 0;
h += 1;
if (h == 24)h = 0;
}
}
return MyTime(h, m, s);
}
6-5 大整数求和(运算符重载)
BigInt类表示不超过100位的无符号大整数。试重载>>,<<和+,以支持无符号大整数的输入、输出与求和(假设结果仍是一个不超过100位的无符号大整数)。
重载面向BigInt类对象的运算符:
>><<+
裁判测试程序样例:
#include <iostream>
#include <string>
using namespace std;
/* 请在这里填写答案 */
int main(){
BigInt a, b, c;
cin>>a>>b;
c=a+b;
cout<<a<<"+"<<b<<"="<<c<<endl;
return 0;
}
输入样例:
123456789
987654321
输出样例:
123456789+987654321=1111111110
参考答案:
class BigInt
{
public:
string str;
BigInt() {}
BigInt(string s)
{
str=s;
}
friend ostream&operator<<(ostream &os, BigInt &bigint)
{
os<<bigint.str;
return os;
}
friend istream&operator>>(istream &is,BigInt &bigint)
{
is>>bigint.str;
return is;
}
friend BigInt operator+( BigInt &bigint1, BigInt &bigint2);
};
BigInt operator+( BigInt &bigint1, BigInt &bigint2)
{
string a=bigint1.str;
string b=bigint2.str;
string temp;
if(a.size()>b.size())
{
temp=a;
a=b;
b=temp;
}
char *sum=new char[b.size()+2];
int carry=0;
int count=0;
int m,n,k;
int al,bl;
for (al=a.size()-1,bl=b.size()-1; al>=0; al--,bl--)
{
m=a[al]-'0';
n=b[bl]-'0';
k=m+n+carry;
if(k > 9)
{
carry=1;
k-=10;
sum[count]=k+'0';
}
else
{
sum[count]=k+'0';
carry=0;
}
count++;
}
if(a.size()==b.size() && carry==1) sum[count]='1';
if(a.size()==b.size() && carry==0) count--;
int sizeDif=b.size()-a.size();
int i=sizeDif-1;
for(bl!=0; i>=0; i--)
{
k=b[i]-'0'+carry;
if(k>9)
{
carry=1;
k-=10;
sum[count]=k+'0';
}
else
{
sum[count]=k+'0';
carry=0;
}
count++;
if(i==0 && carry==1) sum[count]='1';
if(i==0 && carry==0) count--;
}
sum[count+1]='\0';
int j;
char t;
int x=count;
for(j=0; j<=count/2; j++)
{
t=sum[j];
sum[j]=sum[x];
sum[x]=t;
x--;
}
string st(sum);
return BigInt(st);
}