当前位置:网站首页>PTA:7-69 数据的间距问题
PTA:7-69 数据的间距问题
2022-06-23 03:45:00 【Sy_Faker】
复数类Complex有两个数据成员:a和b, 分别代表复数的实部和虚部,并有若干构造函数和一个重载-(减号,用于计算两个复数的距离)的成员函数。 要求设计一个函数模板
template < class T >
double dist(T a, T b)
对int,float,Complex或者其他类型的数据,返回两个数据的间距。
以上类名和函数模板的形式,均须按照题目要求,不得修改
输入格式:
每一行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为Complex类型,若为整型元素,接着输入两个整型数据,若为浮点型元素,接着输入两个浮点型数据,若为Complex型元素,输入两个Complex型数据(a1 b1 a2 b2),输入0时标志输入结束。
输出格式:
对每个输入,每行输出一个间距值。
输入样例:
1 2 5
3 2 4 5 9
2 2.2 9.9
0
输出样例:
3
5.83095
7.7
#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
int a;
int b;
public:
Complex()
{
a=0;
b=0;
}
Complex(int x,int y)
{
a=x;
b=y;
}
float operator -(Complex&c)
{
return (sqrt((a-c.a)*(a-c.a)+(b-c.b)*(b-c.b)));
}
};
template<class T>
double dist(T a,T b)
{
return fabs(a-b);
}
int main()
{
int type;
cin>>type;
int i1,i2;
float f1,f2;
int x1,x2,y1,y2;
while(type!=0)
{
switch(type)
{
case 1:
cin>>i1>>i2;
cout<<dist(i1,i2)<<endl;
break;
case 2:
cin>>f1>>f2;
cout<<dist(f1,f2)<<endl;
break;
case 3:
cin>>x1>>y1>>x2>>y2;
Complex c1(x1,y1),c2(x2,y2);
cout<<c1-c2<<endl;
break;
}
cin>>type;
}
}
边栏推荐
猜你喜欢

Black horse PostgreSQL, why is it black in the end

基于HAProxy实现网页动静分离

QMainWindow
![[two points] leetcode1011 Capacity To Ship Packages Within D Days](/img/fd/c6f31a44ebaf41bd5ab2a342f10d06.png)
[two points] leetcode1011 Capacity To Ship Packages Within D Days

背景彩带动画插件ribbon.js

Compilation, installation and global configuration section description of haproxy

photoshop PS 查看像素坐标、像素颜色、像素HSB颜色
![[OWT] OWT client native P2P E2E test vs2017 build 2: test unit construction and operation](/img/b0/4ea8069a88ce19ca7dbfa67ac9fcba.png)
[OWT] OWT client native P2P E2E test vs2017 build 2: test unit construction and operation

【二叉树进阶】AVLTree - 平衡二叉搜索树

Insert sort directly
随机推荐
Common events for elements
[OWT] OWT client native P2P E2E test vs2017 build 2: test unit construction and operation
TDesign update weekly report (the second week of January 2022)
A summary of PostgreSQL data types. All the people are here
Select sort method
Efficient remote office experience | community essay solicitation
Tcapulusdb Jun · industry news collection (V)
Two ways to improve the writing efficiency of hard disk storage data
JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology
[machine learning] wuenda's machine learning assignment ex2 logistic regression matlab implementation
软件项目管理 8.4.软件项目质量计划
Bug STM32 interrupt (everyone knows)
在word里,如何让页码从指定页开始编号
虫子 日期类 下 太子语言
聊聊内存模型和内存序
最新编程语言排行榜
How to realize data transaction
会话和守护进程
Using jhipster to build microservice architecture
自媒体时代的贤内助——AI 视频云