我是靠谱客的博主 健忘荔枝,最近开发中收集的这篇文章主要介绍运算符重载在ACM中的运用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

运算符重载问题是C++中经典的问题,使用运算符重载可以实现不同的类型数据的直接操作,比如复数问题等

针对此部分进行联系

练习题目:

A+B in Hogwarts

问题描述:

If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- as Hagrid explained it to Harry, "Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it's easy enough." Your job is to write a program to compute A+B where A and B are given in the standard form of "Galleon.Sickle.Knut" (Galleon is an integer in [0, 107], Sickle is an integer in [0, 17), and Knut is an integer in [0, 29)).

Input Specification:

Each input file contains one test case which occupies a line with A and B in the standard form, separated by one space.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input.

Sample Input:
3.2.1 10.16.27
Sample Output:
14.1.28


源码:

#include <iostream>
#include <cstdio>

using namespace std;

class strange_num
{
private:
	long long Galleon;
	int Sickle;
	int Knut;

public:
	strange_num():Galleon(0),Sickle(0),Knut(0){}
	strange_num(long long,int, int);
	~strange_num(){};
	friend strange_num operator+( strange_num& aa, strange_num& pp);
	long long get_Galleon()
	{
		return Galleon;
	}
	int get_Sickle()
	{
		return Sickle;
	}
	int get_Knut()
	{
		return Knut;
	}
	
};

strange_num::strange_num(long long Galleons,int Sickles, int Knuts)
{
	int rr=0;
	rr=Knuts/29;
	this->Knut=Knuts%29;

	this->Sickle=(Sickles+rr)%17;
	rr=(Sickles+rr)/17;
	
	this->Galleon=Galleons+rr;
	
}
strange_num operator+( strange_num& aa, strange_num& pp)
{
	return strange_num (aa.Galleon+pp.Galleon,aa.Sickle+pp.Sickle,aa.Knut+pp.Knut);
}

int main(void)
{
	long long a;
	int b,c;
	scanf("%lld.%d.%d",&a,&b,&c);
	strange_num tt(a,b,c);
	scanf("%lld.%d.%d",&a,&b,&c);
	strange_num cc(a,b,c);
	strange_num mm=tt+cc;

	printf("%lld.%d.%d",mm.get_Galleon(),mm.get_Sickle(),mm.get_Knut());
	return 0;
}


最后

以上就是健忘荔枝为你收集整理的运算符重载在ACM中的运用的全部内容,希望文章能够帮你解决运算符重载在ACM中的运用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部