34 求两点间距离
注:最开始的时候我在getDistance(…)这个友元函数的定义上费了很大的劲,我想把一个模板函数定义成一个模板类的友元,但是似乎不行,只好在定义函数的时候把它实例化。
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
template <class Tp>
class CPoint {
private:
Tp x,y;
public:
CPoint(Tp _x,Tp _y) {
x=_x;
y=_y;
}
Tp getX() {
return x;
}
Tp getY() {
return y;
}
friend Tp getDistance(CPoint<Tp> a,CPoint<Tp> b);
};
double getDistance(CPoint<double> a,CPoint<double> b) {
return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}
int main() {
double x,y;
cin>>x>>y;
CPoint <double>a(x,y);
cin>>x>>y;
CPoint <double>b(x,y);
cout<<"Distance="<<getDistance(a,b)<<endl;
return 0;
}
35 统计单词个数
注:这道题我考虑得挺多的,结果交了才发现数据给的很简单,我考虑了多重空格、标点前后的空格(通过真正地鉴别单词的个数而不是通过空格),其实还没考虑缩音词。
#include <iostream>
#include <iomanip>
using namespace std;
class Counter{
private:
int _c;
char lastChar,thisChar;
bool isChar(char c) {
return (c<='Z'&&c>='A')||(c<='z'&&c>='a')||c=='\'';
}
public:
Counter() {
_c=0;
lastChar=' ';
}
void countWord() {
while (lastChar!='\n') {
thisChar=cin.get();
if (isChar(thisChar)&&!isChar(lastChar))
_c++;
lastChar=thisChar;
}
}
void show() {
cout<<"Words="<<_c<<endl;
}
};
int main() {
Counter counter;
counter.countWord();
counter.show();
return 0;
}
36 显示时间
注:这个题要容错,于是我就专门写了个容错函数,谁让我懒呢。
#include <iostream>
using namespace std;
template <class Tp>
class Time {
private:
Tp hour,minute,second;
Tp fixByRange(Tp __data,Tp __floor,Tp __ceil) {
if (__data<=__ceil&&__data>=__floor)
return __data;
else
return 0;
}
public:
Time() {
hour=0;
minute=0;
second=0;
}
void setHour(Tp __hour) {
hour=fixByRange(__hour,0,23);
}
void setMinute(Tp __minute) {
minute=fixByRange(__minute,0,59);
}
void setSecond(Tp __second) {
second=fixByRange(__second,0,59);
}
void setTime() {
Tp tmpC;
cin>>tmpC;
setHour(tmpC);
cin>>tmpC;
setMinute(tmpC);
cin>>tmpC;
setSecond(tmpC);
}
void print() {
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
};
int main() {
Time<int> objTime;
objTime.setTime();
objTime.print();
system("pause");
return 0;
}
37 整数集合运算
核心思想:这道题写得有点累,重载其实在外面也可以,只是后来懒得再写了。
#include <iostream>
using namespace std;
template <class Tpi,class Tp>
class Set {
private:
Tp array[1000];
Tpi length;
public:
Set(Tp __length=0) {
//array[__length];
length=__length;
Tpi i;
for (i=0;i<__length;i++)
array[i]=0;
}
void get() {
cin>>length;
Tpi i;
for (i=0;i<length;i++)
cin>>array[i];
}
void put() {
Tpi i;
for (i=0;i<length;i++)
cout<<array[i]<<" ";
cout<<endl;
}
Tpi getLength() {
return length;
}
/*Set<Tpi,Tp> operator+(Set<Tpi,Tp> & __Set) {
Tpi i,j;
Set<int,int> ret=this;
for (j=0;j<__Set.length;j++) {
for (i=0;i<ret.length;i++)
if (ret.array[i]!=__Set.array[j])
break;
ret.array[length++]=__Set.array[j];
}
return ret;
}*/
friend Set<Tpi,Tp> operator+(Set<Tpi,Tp> & _s1,Set<Tpi,Tp> & _s2) {
int i,j;
Set<Tpi,Tp> ret;
for (j=0;j<_s1.length;j++) {
for (i=0;i<ret.length;i++)
if (ret.array[i]==_s1.array[j])
break;
if (i==ret.length)
ret.array[ret.length++]=_s1.array[j];
}
for (j=0;j<_s2.length;j++) {
for (i=0;i<ret.length;i++)
if (ret.array[i]==_s2.array[j])
break;
if (i==ret.length)
ret.array[ret.length++]=_s2.array[j];
}
return ret;
}
friend Set<Tpi,Tp> operator-(Set<Tpi,Tp> & _s1,Set<Tpi,Tp> & _s2) {
int i,j;
Set<Tpi,Tp> ret;
for (i=0;i<_s1.length;i++) {
for (j=0;j<_s2.length;j++)
if (_s1.array[i]==_s2.array[j])
break;
if (j==_s2.length)
ret.array[ret.length++]=_s1.array[i];
}
return ret;
}
};
/*template <class Tpi,class Tp>
Set<Tpi,Tp> operator+(Set<Tpi,Tp> & _s1,Set<Tpi,Tp> & _s2) {
int i,j;
Set<Tpi,Tp> ret();
for (j=0;j<_s2.length;j++) {
for (i=0;i<_s1.length;i++)
if (_s1.array[i]!=_s2.array[j])
break;
ret.array[ret.length++]=_s2.array[j];
}
return ret;
}
template <class Tpi,class Tp>
Set<Tpi,Tp> operator-(Set<Tpi,Tp> & _s1,Set<Tpi,Tp> & _s2) {
int i,j;
Set<Tpi,Tp> ret();
for (i=0;i<_s1.length;i++) {
for (j=0;j<_s2.length;j++)
if (_s1.array[i]==_s2.array[j])
break;
ret.array[ret.length++]=_s1.array[i];
}
return ret;
}*/
int main() {
Set<int,int> s1,s2;
s1.get();
s2.get();
Set<int,int> ret1=s1+s2;
Set<int,int> ret2=s1-s2;
cout<<"int1+int2=";
ret1.put();
cout<<"int1-int2=";
ret2.put();
system("pause");
return 0;
}
Leave a Reply
You must be logged in to post a comment.