当前位置:网站首页>PTA: spacing of 7-69 data

PTA: spacing of 7-69 data

2022-06-23 04:26:00 Sy_ Faker

Plural Complex There are two data members :a and b, Represent the real part and imaginary part of a complex number respectively , There are several constructors and an overload -( minus sign , Used to calculate the distance between two complex numbers ) Member function of . It is required to design a function template

template < class T >

double dist(T a, T b)

Yes int,float,Complex Or other types of data , Returns the spacing between two data .

The above class name and function template form , Must be in accordance with the requirements of the topic , Do not modify

Input format :
Each action is an operation , The first number in each line is the element type ,1 Is an integer element ,2 Is a floating point element ,3 by Complex type , If integer element , Then enter two integer data , If it is a floating-point element , Then enter two floating-point data , if Complex Type element , Enter two Complex Type data (a1 b1 a2 b2), Input 0 Mark the end of input .

Output format :
For each input , Output one spacing value per line .

sample input :
1 2 5
3 2 4 5 9
2 2.2 9.9
0

sample output :
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;
	}
}
原网站

版权声明
本文为[Sy_ Faker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206222259538314.html