Home > Back-end >  C function template
C function template

Time:09-28

Plural kinds of Complex, there are two data members: a and b, respectively, on behalf of the plural of real part and imaginary part, and there are a number of constructor and an overloaded - (minus sign, used for calculating the distance of two plural) a member function, required to design a function template
The template & lt; The class T & gt;
Double dist (T, a, T, b)
For int, float, and Complex or other types of data, returns the two data spacing,
More than the name of the class and the form of a function template, must be made according to the topic request, shall not modify the
Input format:
Each behavior an operation, each row of the first number for the element type, 1 integer elements, 2 for floating-point elements, 3 for Complex types, for integer elements, then two integer data input, if floating-point elements, then two floating-point data input, for Complex type elements, enter two Complex data type (a1, a2, b1 b2), input zero mark input end,
The output format:
Each row for each input, the output value of a spacing,
Input the sample:
1 2 5
3, 2, 4, 5 and 9
2, 2.2 9.9
0

The output sample:
3
5.83095
7.7

CodePudding user response:

# include
# include
Using STD: : endl;
Using STD: : cout;
Using STD: : cin;

The class Complex
{
Private:
Double a;
Double b;
Public:
Complex ()=default;
Complex (double a=0, double b=0)
{
this-> A=a;
this-> B=b;
}
Double operator - Complex (c1)
{
Double dis.
Double (x=pow this - & gt; A - c1. A, 2);
Double y=pow (this - & gt; B - c1. B, 2);
Dis=pow (x + y, 0.5);
Return dis.
}
};

Template
Double dist (T, a, T, b)
{
Double dis=a - b;
Dis=abs (dis);
Return dis.
}
Int main ()
{
Int judge;
While (true)
{
Cin & gt;> Judge;
If (judge==1)
{
Int a, b;
Cin & gt;> A. & gt;> B;
Cout & lt; }
If (judge==3)
{
Double a1, b1 and a2, b2,
Cin & gt;> A1 & gt;> B1 & gt;> A2 & gt;> B2.
Complex c1 (a1, b1), c2 (a2, b2);
Cout & lt; }
If (judge==2)
{
Double a, b;
Cin & gt;> A. & gt;> b;
Cout & lt; }
If (judge==0)
break;
}
return 0;
}
  • Related