运算符重载基础OJ——2023-05-17

A. 复数的加减乘运算(运算符重载+友元)

题目描述

定义一个复数类,通过重载运算符:+、-、*,实现两个复数之间的各种运算。

要求如下:

1.实现Complex类,私有数据成员实部和虚部,都是int;

2.编写main函数,初始化两个Complex对象,计算它们之间的加减乘,并输出结果。

复数相乘的运算规则

设z1=a+bi,z2=c+di(a、b、c、d∈R)是任意两个复数,那么它们的积(a+bi)(c+di)=(ac-bd)+(bc+ad)i.

输入

第1行:输入两个数值,分别为第一个Complex对象的实部和虚部。

第2行:输入两个数值,分别为第二个Complex对象的实部和虚部。

输出

第1行:两个Complex对象相加后的输出结果。

第2行:两个Complex对象相减后的输出结果。

第3行:两个Complex对象相乘后的输出结果。

输入样例1:

10 20
50 40

输出样例1:

Real=60 Image=60\n
Real=-40 Image=-20\n
Real=-300 Image=1400\n

AC代码:

A.复数的加减乘运算
#include <iostream>
using namespace std;
class complex{
private:
    int real;
    int imag;
public:
    complex(){}
    complex(int r,int im):real(r),imag(im){}
    complex operator+(complex&c1)
    {
        complex c2;
        c2.real=real+c1.real;
        c2.imag=imag+c1.imag;
        return c2;
    }
    complex operator-(complex&c1)
    {
        complex c2;
        c2.imag=imag-c1.imag;
        c2.real=real-c1.real;
        return c2;
    }
    complex operator*(complex &c1)
    {
        complex c2;
        c2.real=real*c1.real-imag*c1.imag;
        c2.imag=imag*c1.real+c1.imag*real;
        return c2;
    }
    void dis()
    {
        cout<<"Real="<<real<<" Image="<<imag<<endl;
    }
};
int main()
{
    int r1,im1,r2,im2;
    cin>>r1>>im1>>r2>>im2;
    complex c1(r1,im1);
    complex c2(r2,im2);
    complex c3;
    c3=c1+c2;
    c3.dis();
    c3=c1-c2;
    c3.dis();
    c3=c1*c2;
    c3.dis();
    return 0;
}

B. 分数的加减乘除(运算符重载)

题目描述

Fraction类的基本形式如下:

要求如下:

1.实现Fraction类;common_divisor()和contracted()函数体可为空,不实现具体功能。

2.编写main函数,初始化两个Fraction对象的,计算它们之间的加减乘除。

输入

第1行:依次输入第1个和第2个Fraction对象的分子和分母值。

输出

每行依次分别输出加减乘除计算后的Fraction对象(直接输出分数值,不需要约简)

输入样例1:

1 3 2 5

输出样例1:

fraction=11/15\n
fraction=-1/15\n
fraction=2/15\n
fraction=5/6

AC代码:

B.分数的加减乘除
#include <iostream>
using namespace std;

class fraction{
private:
    int numerator,denominator;
    int common_divisor();        //计算最大公约数
    void contracted();      //分数化简
public:
    fraction(int=0,int=1);
    fraction(fraction&);
    fraction operator+(fraction);
    fraction operator-(fraction);
    fraction operator*(fraction);
    fraction operator/(fraction);
    void set(int=0,int=1);
    void disp()
    {
        
        int m=common_divisor();
        numerator/=m;
        denominator/=m;
        if(numerator!=0)
        cout<<"fraction="<<numerator<<"/"<<denominator<<endl;
    }
};

fraction::fraction(int a, int b)
{
    numerator=a;
    denominator=b;
}

fraction::fraction(fraction &f)
{
    numerator=f.numerator;
    denominator=f.denominator;
}

fraction fraction::operator+(fraction f1)
{
    fraction f;
    int m,i;
    for(i=1;;i++)
    {
        if(i%denominator==0&&i%f1.denominator==0)
        {
            break;
        }
    }
    int n1=numerator;
    int d1=denominator;
    n1=i/d1*n1;
    f1.numerator=i/f1.denominator*f1.numerator;
    d1=i;
    f1.denominator=i;
    f.numerator=n1+f1.numerator;
    f.denominator=d1;
    return f;
}
int fraction::common_divisor()
{
    int m=(denominator>numerator)?denominator:numerator;
    int s;
    for(int i=1;i<=m;i++)
    {
        if(denominator%i==0&&numerator%i==0)
        {
            s=i;
        }
    }
    return s;
}
fraction fraction::operator-(fraction f1)
{
    fraction f;
    int m,i;
    for(i=1;;i++)
    {
        if(i%denominator==0&&i%f1.denominator==0)
        {
            break;
        }
    }
    int n1=numerator;
    int d1=denominator;
    n1=i/d1*n1;
    f1.numerator=i/f1.denominator*f1.numerator;
    d1=i;
    f1.denominator=i;
    f.numerator=n1-f1.numerator;
    f.denominator=d1;
    return f;
}
fraction fraction::operator*(fraction f1) {
    fraction f;
    f.numerator=numerator*f1.numerator;
    f.denominator=denominator*f1.denominator;
    return f;
}

fraction fraction::operator/(fraction f1)
{
    fraction f;
    f.numerator=numerator*f1.denominator;
    f.denominator=denominator*f1.numerator;
    return f;
}

int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    fraction f1(a,b);
    fraction f2(c,d);
    fraction f;
    f=f1+f2;
    f.disp();
    f=f1-f2;
    f.disp();
    f=f1*f2;
    f.disp();
    f=f1/f2;
    f.disp();
    return 0;
}

C. 学生生日差值计算(运算符重载)***

题目描述

定义一个学生类Student,包含该学生的姓名、出生年、月、日 ,重定义 “-”号实现两个学生之间相差多少天的比较。并利用重载的“-”运算符,求所有学生中年龄相差最大的两个人的名字以及相差天数。

输入

第一行:输入所需要输入的学生个数;

第二行开始,依次输入每个学生的姓名、出生年、月、日。

输出

输出年龄相差最大的两个人的名字以及相差天数。

输入样例1:
3
Tom 1995 1 1
Joe 1995 2 28
Jimmy 1996 1 8

输出样例1:
Tom和Jimmy年龄相差最大,为372天。

AC代码:

C.学生生日差值计算(重点学如何求两个日期相差的天数)
#include <iostream>
using namespace std;

class student{
private:
    string name;
    int year;
    int month;
    int day;
public:
    student(){}
    student(string n,int y,int m,int d):name(n),year(y),month(m),day(d){}
    bool isLeapYear(int year)
    {
        if(year%4==0&&year%100!=0||year%400==0)
        {
            return true;
        }
        return false;
    }
    int getDayOfYear(const student &s1)
    {
        int daysInMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(s1.year))
        {
            daysInMonth[1]=29;
        }
        int dayOfYear=0;
        for(int i=0;i<s1.month-1;i++)
        {
            dayOfYear+=daysInMonth[i];
        } 
        dayOfYear+=s1.day;
        return dayOfYear;
    }
    int getDaysDifference(const student &s1,const student &s2)
    {
        int dayofyear1=getDayOfYear(s1);
        int dayofyear2=getDayOfYear(s2);
        int daysdiff=dayofyear2-dayofyear1;
        int yearsdiff=s2.year-s1.year;
        for(int i=s1.year;i<s2.year;i++)
        {
            if(isLeapYear(i))
            {
                daysdiff++;
            }
        }
        return yearsdiff*365+daysdiff;
    }
    int operator-(const student &s1)
    {
        return getDaysDifference(s1,*this);
    }
    string getname()
    {
        return name;
    }
};
struct num{
    string name1;
    string name2;
    int d;
};
int main()
{
    int n;
    cin>>n;
    student *s=new student[n];
    for(int i=0;i<n;i++)
    {
        string n;
        int y,m,d;
        cin>>n>>y>>m>>d;
        *(s+i)=student(n,y,m,d);
    }
    num max;
    max.d=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(i!=j)
            {
                int d;
                if(s[i]-s[j]>0)
                    d=s[i]-s[j];
                else
                    d=s[j]-s[i];
                if(d>max.d)
                {
                    max.d=d;
                    max.name1=s[i].getname();
                    max.name2=s[j].getname();
                }
            }
        }
    }
    cout<<max.name1<<" "<<max.name2<<" "<<max.d<<endl;
    return 0;
}

D. OOP矩阵类运算(运算符重载)

题目描述

利用重载运算符,实现矩阵的加法,减法。

请重载加减运算符并自定义矩阵类完成相应的操作

输入

第一行为测试数据数

对于每组测试数据

第一行为矩阵的行数和列数

接下来分别为两个矩阵的各个元素

输出

输出矩阵的和与差

输入样例1:

2
4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
9 8 7 6 5
4 3 2 1 0
-1 5 6 9 1
2 0 2 1 6
4 5
9 8 7 6 5
4 3 2 1 0
-1 5 6 9 1
2 0 2 1 6
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
输出样例1:
Add:
10 10 10 10 10 
10 10 10 10 10 
10 17 19 23 16 
18 17 20 20 26 
Minus:
-8 -6 -4 -2 0 
2 4 6 8 10 
12 7 7 5 14 
14 17 16 18 14 
-----------------
Add:
10 10 10 10 10 
10 10 10 10 10 
10 17 19 23 16 
18 17 20 20 26 
Minus:
8 6 4 2 0 
-2 -4 -6 -8 -10 
-12 -7 -7 -5 -14 
-14 -17 -16 -18 -14 
-----------------

AC代码:

D.OOP矩阵类运算
#include <iostream>
using namespace std;
class matrix{
private:
    int**p=NULL;
    int m;
    int n;
public:
    matrix(){}
    matrix(int**p,int m,int n):p(p),m(m),n(n){}
    matrix operator+(matrix &m1)
    {
        matrix m2;
        m2.m=m;
        m2.n=n;
        m2.p=new int*[m];
        for(int i=0;i<m;i++)
        {
            m2.p[i]=new int[n];
        }
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                m2.p[i][j]=p[i][j]+m1.p[i][j];
            }
        }
        return m2;
    }
    matrix operator-(matrix &m1)
    {
        matrix m2;
        m2.m=m;
        m2.n=n;
        m2.p=new int*[m];
        for(int i=0;i<m;i++)
        {
            m2.p[i]=new int[n];
        }
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                m2.p[i][j]=p[i][j]-m1.p[i][j];
            }
        }
        return m2;
    }
    void dis()
    {
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                cout<<p[i][j]<<" ";
            }
            cout<<endl;
        }
    }
};
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int m,n;
        cin>>m>>n;
        int **p=new int*[m];
        for(int i=0;i<m;i++)
        {
            p[i]=new int[n];
        }
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                cin>>p[i][j];
            }
        }
        matrix m1(p,m,n);
        int **p1=new int*[m];
        for(int i=0;i<m;i++)
        {
            p1[i]=new int[n];
        }
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                cin>>p1[i][j];
            }
        }
        matrix m2(p1,m,n);
        cout<<"Add:"<<endl;
        matrix m3;
        m3=m1+m2;
        m3.dis();
        cout<<"Minus:"<<endl;
        m3=m1-m2;
        m3.dis();
        cout<<"-----------------"<<endl;
    }
    return 0;
}

E. 四进制加法(运算符重载)

题目描述

定义一个四进制的类,重定义“+”号实现四进制数的累加。

输入

第一行输入所需要的四进制数的个数

第二行开始,依次输入四进制数

输出

所有输入四进制数累加的和

输入样例1:

3
13
2
21

输出样例1:
102

E.四进制加法
实现:把一个四进制数转化为十进制,累加运算之后的结果再化为四进制数
#include <iostream>
#include <math.h>
using namespace std;
class four{
private:
    char* num;
    int tnum;//转化的十进制数
public:
    four()
    {
        tnum=0;
    }
    four(char* p):num(p)
    {
        int k;
        for(k=0;num[k]!='\0';k++){}//重新计算数的长度(四进制)
        int len=k;
        int sum=0;
        int j;
        for(k=len-1,j=0;k>=0;k--,j++)
        {
            int n=num[k]-'0';
            sum+=n*(int)pow(4,j);
        }
        tnum=sum;
    }
    four operator+=(const four& f1)
    {
        tnum+=f1.tnum;
        return *this;
    }
    void getnum(int tnum)
    {
        if(tnum/4!=0)
        {
            getnum(tnum/4);//此处直接运用递归输出,联想十进制与其他数制的转换方法
        }
        cout<<tnum%4;
    }
    void dis()
    {
        getnum(tnum);
    }
};
int main()
{
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    int t;
    cin>>t;
    getchar();
    four sum;
    while(t--)
    {
        char *p=new char[99];//进行完+=操作后记得删除
        int k=0;
        while((p[k]=getchar())!='\n')
        {
            k++;
        }

        p[k]='\0';
        four f1(p);
        sum+=f1;
        delete []p;
    }
    sum.dis();
    return 0;
}

F. 【程序填空】三维点坐标平移(增量运算符重载)

题目描述

定义一个三维点Point类,利用友元函数重载"++"和"--"运算符,并区分这两种运算符的前置和后置运算。

++表示x\y\z坐标都+1,--表示x\y\z坐标都-1

请完成以下程序填空

输入

只有一行输入,输入三个整数,表示点的x/y/z坐标

输出

函数自行输出

#include <iostream>
using namespace std;

class Point;
Point operator -- (Point & );
Point operator -- (Point &, int);

class Point {
private:
	int x, y, z;
public:
	Point(int tx=0, int ty=0, int tz=0 )
	{	x = tx, y = ty, z = tz;	}
	Point operator ++ ();
	Point operator ++ (int);
	friend Point operator -- (Point & );
	friend Point operator -- (Point &, int);
	void print();
};
//完成以下填空
/********** Write your code here! **********/
Point Point::operator++()
{
    x++;
    y++;
    z++;
    return *this;
}
Point Point::operator++(int)
{
    Point t(x,y,z);
    x++;
    y++;
    z++;
    return t;
}
Point operator--(Point &p1)
{
    p1.x--;
    p1.y--;
    p1.z--;
    return p1;
}
Point operator --(Point &p1,int)
{
    Point t(p1);
    p1.x--;
    p1.y--;
    p1.z--;
    return t;
}
void Point::print()
{
    cout<<"x="<<x<<" y="<<y<<" z="<<z<<endl;
}
/*******************************************/
int main()
{	int tx, ty, tz;
	cin>>tx>>ty>>tz;
	Point p0(tx, ty, tz); //原值保存在p0
	Point p1, p2;	//临时赋值进行增量运算

	//第1行输出
	p1 = p0;
	p1++;;
	p1.print();
	//第2行输出
	p1 = p0;
	p2 = p1++;
	p2.print();
	//第3、4行输出,前置++
	p1 = p0;
	(++p1).print();
	p1.print();
	//第5、6行输出,后置--
	p1 = p0;
	p1--;
	p1.print();
	p0.print();
	//第7、8行输出,前置--
	p1 = p0;
	(--p1).print();
	p1.print();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值