6-1 使用成员函数重载复数类的运算符+

分数 2

全屏浏览题目

切换布局

作者 李廷元

单位 中国民用航空飞行学院

类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& com)
{
    com.realPart=com.realPart+this->realPart;
    com.imgPart=com.imgPart+this->imgPart;
    return com;
}

 

6-2 重载+-运算符

分数 3

全屏浏览题目

切换布局

作者 李廷元

单位 中国民用航空飞行学院

请根据程序的输出结果,重载类A的+和-运算符。

类和函数接口定义:


class A { public: A(int x = 0, int y = 0) : x(x), y(y) {} void show() const; A operator+(A& a); //重载+运算符 A operator-(A& a); //重载-运算符 private: int x, y; };

裁判测试程序样例:


#include <iostream> using namespace std; class A { public: A(int x = 0, int y = 0) : x(x), y(y) {} void show() const; A operator+(A& a); A operator-(A& a); private: int x, y; }; void A::show() const { cout << "(x,y) = " << "(" << x << "," << y << ")" << endl; } /* 请在这里填写答案 */ int main() { A a1(1, 2); A a2(4, 5); A a; cout << "a1:"; a1.show(); cout << "a2:"; a2.show(); a = a1 + a2; cout << "a:"; a.show(); a = a1 - a2; cout << "a:"; a.show(); return 0; }

输入样例:

本题无输入。

输出样例:

a1:(x,y) = (1,2)
a2:(x,y) = (4,5)
a:(x,y) = (5,7)
a:(x,y) = (-3,-3)
A A::operator+(A& a)
{
    A temp;
    temp.x=x+a.x;
    temp.y=y+a.y;
    return temp;
}
A A::operator-(A& a)
{
    A temp;
    temp.x=x-a.x;
    temp.y=y-a.y;
    return temp;
}

 

6-3 大整数求和(运算符重载)

分数 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
{
private:
    string str;
public:
    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);
};

 

6-4 学生成绩的输入和输出(运算符重载)

分数 5

全屏浏览题目

切换布局

作者 何振峰

单位 福州大学

现在需要输入一组学生的姓名和成绩,然后输出这些学生的姓名和等级。

输入时,首先要输入学生数(正整数)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{
public :
    Student ( string ,int);

    friend istream & operator >>(istream&,Student &);
    friend ostream & operator <<(ostream&,Student &);
private:
    string name;
    int score;
};


Student::Student( string name="def",int score=0){}

istream &operator >>(istream & is ,Student &s )
{
is>> s.name>>s.score;
return is;
}
ostream &operator <<(ostream & os ,Student &s)
{
 static int sum;sum=sum+1;
os<<sum<<". "<<s.name<<" ";
if(s.score>=60)os<<"PASS";
else{os<<"FAIL";}

return os;
}

 

6-5 Point类的运算

分数 5

全屏浏览题目

切换布局

作者 杨军

单位 四川师范大学

定义Point类,有坐标x,y两个私有成员变量;对Point类重载“+”(相加)、“-”(相减)和“==”(相等)运算符,实现对坐标的改变,要求用友元函数和成员函数两种方法实现。对Point类重载<<运算符,以使得代码 Point p; cout<<p<<endl;可以输出该点对象的坐标。

函数接口定义:

实现Point类。

裁判测试程序样例:


/* 请在这里填写答案 */ int main(int argc, char const *argv[]) { Point p1(2,3); cout<<p1<<endl; Point p2(4,5); cout<<p2<<endl; Point p3 = p1+p2; cout<<p3<<endl; p3 = p2-p1; cout<<p3<<endl; p1 += p2; cout<<p1<<endl; cout<<(p1==p2)<<endl; return 0; }

输入样例:

输出样例:

在这里给出相应的输出。例如:

2,3
4,5
6,8
2,2
6,8
0
#include<bits/stdc++.h>
using namespace std;
class Point
{
	int x,y;
public:
	Point(int xx=0, int yy=0):x(xx),y(yy)  {}
	friend Point operator+(Point &c1, Point &c2);
	friend Point operator-(Point &c1, Point &c2);
    friend Point operator+=(Point &c1, Point &c2);
	friend bool operator==(Point &c1, Point &c2);
	friend ostream &operator<<(ostream &out, Point &c); 
};

Point operator+(Point &c1, Point &c2)
{
	return Point(c1.x+c2.x, c1.y+c2.y);
}

Point operator-(Point &c1, Point &c2)
{
	return Point(c1.x-c2.x, c1.y-c2.y);
}

Point operator+=(Point &c1, Point &c2)
{
	return Point(c1.x+=c2.x, c1.y+=c2.y);
}

bool operator==(Point &c1, Point &c2)
{
	if(c1.x==c2.x && c1.y==c2.y)  return 1;
	else  return 0;
}
	
ostream &operator<<(ostream &out, Point &c) 
{
	out<<c.x<<","<<c.y;
	return out;
}

6-7 矩阵运算

分数 10

全屏浏览题目

切换布局

作者 范鹏程

单位 内蒙古师范大学

根据main函数中矩阵对象的定义与使用,定义相关的矩阵类Array,并利用运算符重载的方法实现矩阵的加法与输入输出操作。(为简化问题,矩阵中元素为2位以内整数,要求矩阵按照行列的格式输出,每个元素占3位宽度)

类定义:


class Array /* 请在这里填写答案 */

测试程序样例:


int main() { Array arr1,arr2,arr3; cin>>arr1; cin>>arr2; cout<<arr1<<endl; cout<<arr2<<endl; arr3=arr1+arr2; cout<<arr3; return 0; }

输入样例:

1 2 3 4 5 6
7 8 9 1 11 12

输出样例:

  1  2  3
  4  5  6

  7  8  9
  1 11 12

  8 10 12

 

#include<bits/stdc++.h>
using namespace std;
class Array
{
    private:
    int a,b,c,d,e,f;
    public:
    Array(int A=0,int B=0,int C=0,int D=0,int E=0,int F=0)
    {
        a=A;
        b=B;
        c=C;
        d=D;
        e=E;
        f=F;
    }
    friend istream & operator>>(istream &in,Array &A)
    {
        in>>A.a>>A.b>>A.c>>A.d>>A.e>>A.f;
        return in;
    }
    friend ostream & operator<<(ostream &out,Array &A)
    {
        out<<setw(3)<<setfill(' ')<<A.a<<setw(3)<<setfill(' ')<<A.b<<setw(3)<<setfill(' ')<<A.c<<endl<<setw(3)<<setfill(' ')<<A.d<<setw(3)<<setfill(' ')<<A.e<<setw(3)<<setfill(' ')<<A.f<<endl;
    }
    friend Array operator+(Array &a1, Array &a2)
    {
        return Array (a1.a+a2.a,a1.b+a2.b,a1.c+a2.c,a1.d+a2.d,a1.e+a2.e,a1.f+a2.f);
    }
};

6-8 时钟模拟

分数 10

全屏浏览题目

切换布局

作者 范鹏程

单位 内蒙古师范大学

一个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

 

#include<bits/stdc++.h>
using namespace std;
class MyTime 
{
private:
    int h, m, s;
public:
    MyTime() 
    {
        h = 0;
        m = 0;
        s = 0;
    }
    MyTime(int h,int m,int s):h(h),m(m),s(s)
    {}
    MyTime operator++() 
    {
        this->s++;
        if (this->s >= 60) 
        {
            this->s = 0;
            this->m++;
        }
        if (this->m >= 60) 
        {
            this->m = 0;
            this->h++;
        }
        if (this->h >= 24) 
        {
            this->h= 0;
        }
    }
    friend istream& operator>>(istream& is, MyTime& t) 
    {
        is >> t.h >> t.m >> t.s;
        return is;
    }
    friend ostream& operator<<(ostream& os, MyTime& t) 
    {
        os << t.h <<":" << t.m<<":" << t.s;
    }
};

6-9 日期类的设计与实现

分数 10

全屏浏览题目

切换布局

作者 范鹏程

单位 内蒙古师范大学

使用重载运算符(++,+=,<<等)实现日期类的操作。功能包括:
1)设置日期,如果日期设置不符合实际,则设置为默认日期(1900年1月1日)
2)在日期对象中向日期添加1或者加若干天(加入日期值后根据实际的日期进行月份、年份的变化)
3)重载流插入运算符进行日期的输出,其中月份要用名称表示
###定义类MyDate:


class MyDate

主测试程序样例:


#include <string> #include <iostream> using namespace std; /* 请在这里填写答案 */ int main() { int m,d,y; MyDate d1,d2,d3; cin>>m>>d>>y; d1.setDate(m,d,y); cin>>m>>d>>y; d2.setDate(m,d,y); cin>>m>>d>>y; d3.setDate(m,d,y); cout << "d1 is " << d1 << "\nd2 is " << d2; cout << "\n\nd1 += 7 is " << ( d1 += 7 ); cout << "\n\n d2 is " << d2; cout << "\n++d2 is " << ++d2; cout << "\n\nTesting the prefix increment operator:\n"<< " d3 is " << d3 << endl; cout << "++d3 is " << ++d3 << endl; cout << " d3 is " << d3; cout << "\n\nTesting the postfix increment operator:\n"<< " d3 is " << d3 << endl; cout << "d3++ is " << d3++ << endl; cout << " d3 is " << d3 <<endl; }

输入样例:

在这里给出一组输入。例如:

13 38 100
12 31 2009
2 28 2000

输出样例:

在这里给出相应的输出。例如:

d1 is January 1, 1900
d2 is December 31, 2009

d1 += 7 is January 8, 1900

d2 is December 31, 2009
++d2 is January 1, 2010

Testing the prefix increment operator:
d3 is February 28, 2000
++d3 is February 29, 2000
d3 is February 29, 2000

Testing the postfix increment operator:
d3 is February 29, 2000
d3++ is February 29, 2000
d3 is March 1, 2000
class MyDate{
private:
    int m,d,y;
public:
    MyDate(int pm=1,int pd=1,int py=1900)
    {
        m=pm;d=pd;y=py;
    }
    void setDate(int m,int pd,int year)
    {
        bool rn=false;
        rn = ((0 == year % 4) && (0 != year % 100)) || (0 == year % 400);
        bool err=((m==1||m==3||m==5||m==7||m==8||m==10||m==12)&&pd>31)||
                 ((m==4||m==6||m==9||m==11)&&pd>30)||(m==2&&rn&&pd>29||!rn&&m==2&&pd>28)||m>12;
        if(err)
        {
            m=1;d=1;y=1900;
        }else
        {
            this->m=m;d=pd;y=year;
        }
    }
    MyDate operator++()
    {
        bool rn=false;
        rn = ((0 == y % 4) && (0 != y % 100)) || (0 == y % 400);
        ++d;
        bool err=((m==1||m==3||m==5||m==7||m==8||m==10||m==12)&&d>31)||
                 ((m==4||m==6||m==9||m==11)&&d>30)||(m==2&&rn&&d>29||!rn&&m==2&&d>28);
        if(err)
        {
            d=1;
            m++;
            if(m>12)
            {
                m=1;
                y++;
            }
        }
        return *this;
    }
    MyDate operator++(int)
    {
        MyDate md=*this;
        operator++();
        return md;
    }
    MyDate operator+=(int day)
    {
        for (int i = 0; i <day ; ++i) 
        {
            operator++();
        }
        return *this;
    }
    friend ostream& operator<<(ostream & outd,MyDate md);
};
ostream& operator<<(ostream & outd,MyDate md)
{
    string month[12]={"January","February","March","April","May","June","July",
                        "August","September","October","November","December"};
    outd<<month[md.m-1]<<' '<<md.d<<", "<<md.y;
    return outd;
}

 

 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐