当前位置:网站首页>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;
}
}
边栏推荐
- QMainWindow
- 元素的常用事件
- D overloading nested functions
- 选择排序法
- 【owt】owt-client-native-p2p-e2e-test vs2017构建2 :测试单元构建及运行
- 靜態查找錶和靜態查找錶
- 粒子动画背景登录页面particles.js
- redisTemplate和cacheManager操作redis有什么不同
- Pytorch---Pytorch进行自定义Dataset
- [tcapulusdb knowledge base] [list table] example code for replacing the data at the specified location in the list
猜你喜欢

Two ways to improve the writing efficiency of hard disk storage data

Software project management 8.4 Software project quality plan

理想汽车×OceanBase:当造车新势力遇上数据库新势力

mysql如何删除表的一行数据

8 key indicators to measure technology debt in 2022

如何处理大体积 XLSX/CSV/TXT 文件?

两招提升硬盘存储数据的写入效率

【深度学习】深度学习推理框架 TensorRT MNN OpenVINO ONNXRuntime

炫酷鼠标跟随动画js插件5种

photoshop PS 查看像素坐标、像素颜色、像素HSB颜色
随机推荐
Pytorch---Pytorch进行自定义Dataset
自媒体时代的贤内助——AI 视频云
mysql存储引擎之Myisam和Innodb的区别
Tcapulusdb Jun · industry news collection (III)
What is the digital "true" twin? At last someone made it clear!
JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology
Swiftui component encyclopedia creating animated 3D card scrolling effects using Scrollview and geometryreader
Weekly Postgres world news 2022w02
虫子 STM32 中断 (懂的都懂)
粒子动画背景登录页面particles.js
Twitter cooperates with Shopify to introduce merchant products into twitter shopping
众昂矿业:新能源新材料产业链对萤石需求大增
静态查找表和静态查找表
redis 精讲系列介绍八 - 淘汰策略
深度学习 TensorFlow入门
炫酷鼠标跟随动画js插件5种
[tcapulusdb knowledge base] [list table] sample code for inserting data into the specified position in the list
[OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe
bubble sort
怎么使用Shell脚本实现监测文件变化