实验一:复数类及函数模板(switch语句)
#include<bits/stdc++.h>
using namespace std;
class Complex{
private:
int a,b;
public:
Complex(int _a,int _b):a(_a),b(_b){};
double operator -(Complex n)
{
return sqrt((a-n.a)*(a-n.a)+(b-n.b)*(b-n.b));
}
~Complex(){};
};
template<class T>
double dist(T a,T b)
{
return a-b>0?a-b:b-a;
};
int main()
{
int flag;
while(1)
{ cin>>flag;
switch(flag)
{
case 1:
{
int a,b;
cin>>a>>b;
cout<<dist(a,b)<<endl;
break;
}
case 2:
{
float a,b;
cin>>a>>b;
cout<<dist(a,b)<<endl;
break;
}
case 3:
{
int a,b,c,d;
cin>>a>>b>>c>>d;
Complex m=Complex(a,b);
Complex n=Complex(c,d);
cout<<m-n<<endl;
break;
}
case 0:
break;
}
}
return 0;
}
收获:
学习了如何在case语句中定义新变量。
复习了类中的运算符重载。
标签:case,cout,int,cin,pta,break,Complex,实验报告 From: https://www.cnblogs.com/djcf/p/17369597.html