7-1 友元很简单2016final
C++考试正在进行。请设计一个学生类student,学号、本次考试成绩是其私有数据成员,同时有一个求本次考试成绩最高分的学生的友元函数 student* average(student *p,int count)
以上类名和友元函数的形式,均须按照题目要求,不得修改。
输入格式:
输入是 学号([00001,99999])和成绩,以0结束。(不超过100个学生)
输出格式:
输出是最高分学生的 学号 。 提示:如果是并列最高分,需要将并列最高分学生的学号都输出,以一个空格间隔。
输入样例:
在这里给出一组输入。例如:
10001 90
10002 93
0
输出样例:
在这里给出相应的输出。例如:
10002
#include<iostream>
using namespace std;
class student
{
private:
int num;
double score;
public:
student(){}
student(int a,double b):num(a),score(b){}
friend student* average(student* p, int count);
};
student* average(student *p,int count)
{
int max = p[0].score,i,bj = 1;
for(i = 1;i < count;i++)
{
if(p[i].score > max)
max = p[i].score;
}
for(i = 0;i < count;i++)
{
if(p[i].score == max)
{
if(bj)
{
cout<<p[i].num;
bj = 0;
}
else
cout<<" "<<p[i].num;
}
}
return p;
}
int main()
{
student s[100];
int no,score,i;
cin>>no;
while(no != 0)
{
cin>>score;
s[i++] = student(no,score);
cin>>no;
}
average(s,i);
return 0;
}
7-2 2018Final友元函数(体检表)
体育已经成为一门重要的中考科目,但身体存在某些疾病的同学可以免考。
为此,凤湖中学每年需要整理一下毕业班同学(学生数不超过300人)的体检表中的体检结论,筛选出需要免考的同学,建议其申请免考。
要求建立一个类Exam,每个Exam类对象存储一位同学的体检信息,包括私有数据成员:
string num;//学号
int disease[10];//疾病信息
int count;//疾病数
该类还包括一些必要的公有成员函数。
此外,该类还有一个友元函数void print(Exam*e, int count, int *d)
用于输出需要申请免考的学生信息,这里e是一个指向Exam类对象数组元素的指针,count是学生数量,d是一个指向值得关注病病数组元素的指针。
输入格式:
首先输入学生的体检信息,每位学生一行,以0结束输入。首先输入学生的学号(000-999,每位同学的学号各不相同);接着输入疾病数T(T<=10);然后是T个疾病信息,每种疾病用一个正整数编号(100-999)表示,每种疾病对应的编号不同。
在输入学生体检信息后,输入一行值得关注的疾病编号(相邻的编号以单个空格隔开),最后一个编号为0,标识结束输入病病编号。
输出格式:
输出由于患有值得关注疾病,需要申请免考的同学的学号,以及所患疾病编号(在存在多个值得关注的疾病时,仅输出编号最小的那个),每位同学一行,按照输入顺序输出。
输入样例:
103 0
109 1 101
002 3 105 101 107
825 4 108 775 109 104
0
105 101 0
输出样例:
109 101
002 101
#include<bits/stdc++.h>
using namespace std;
int n = 0;
class exam
{
private:
string num;
int disease[10];
int count;
public:
exam()
{
num = "";
memset(disease, 0, sizeof(disease));
count = 0;
}
exam(string a,int b[],int c)
{
num = a;
for(int i = 0;i < c;i++)
{
disease[i] = b[i];
}
count = c;
}
void set(string a, int b[], int c)
{
num = a;
for(int i=0;i<c;i++)
{
disease[i] = b[i];
}
count = c;
}
friend void print(exam* stu, int cnt, int* jibing)
{
for(int i = 0;i < cnt;i++)
{
int k = 0,flag = 0;
while(stu[i].disease[k] != 0)
k++;
if(k == 0)
continue;
for(int j = 0;j < n;j++)
{
for(int l = 0;l < k;l++)
{
if(stu[i].disease[l] == jibing[j])
{
flag = 1;
cout<<stu[i].num<<" "<<jibing[j]<<endl;
break;
}
}
if(flag)
break;
}
}
}
};
int main()
{
exam stu[300];
string num;
int counts,disease[10],cnt = 0,jibing[1000],bing;
while(1)
{
cin>>num;
if(num == "0")
break;
else
{
cin>>counts;
for(int i = 0;i < counts;i++)
cin>>disease[i];
//stu[cnt++] = exam(num,disease,counts); //错误
stu[cnt++].set(num,disease,counts);
}
}
while(1)
{
cin>>bing;
if(bing == 0)
break;
else
jibing[n++] = bing;
}
sort(jibing,jibing+n);
print(stu,cnt,jibing);
return 0;
}
7-3 复数类的操作
1、声明一个复数类Complex(类私有数据成员为double型的real和image)
2、定义构造函数,用于指定复数的实部与虚部。
3、定义取反成员函数,调用时能返回该复数的相反数(实部、虚部分别是原数的相反数)。
4、定义成员函数Print(),调用该函数时,以格式(real, image)输出当前对象。
5、编写加法友元函数,以复数对象c1,c2为参数,求两个复数对象相加之和。
6、主程序实现:
(1)读入两个实数,用于初始化对象c1。
(2)读入两个实数,用于初始化对象c2。
(3)计算c1与c2相加结果,并输出。
(4)计算c2的相反数与c1相加结果,并输出。
输入格式:
输入有两行:
第一行是复数c1的实部与虚部,以空格分隔;
第二行是复数c2的实部与虚部,以空格分隔。
输出格式:
输出共三行:
第一行是c1与c2之和;
第二行是c2的相反数与c1之和;
第三行是c2 。
输入样例:
在这里给出一组输入。例如:
2.5 3.7
4.2 6.5
输出样例:
在这里给出相应的输出。例如:
(6.7, 10.2)
(-1.7, -2.8)
(4.2, 6.5)
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double image;
public:
Complex()
{
real = 0;
image = 0;
}
Complex(double r,double i)
{
real=r;
image=i;
}
void qufan(Complex c)
{
cout <<"("<<real - c.real << ", "<< image - c.image<<")" << endl;
}
void print()
{
cout<<"("<<real<<", "<<image<<")"<<endl;
}
friend void add(Complex a,Complex b);
};
void add(Complex a,Complex b)
{
cout << "(" << a.real + b.real << ", "<< a.image + b.image << ")" << endl;
}
int main()
{
double r1,r2,i1,i2;
cin>>r1>>i1>>r2>>i2;
Complex a(r1,i1),b(r2,i2);
add(a,b);
a.qufan(b);
b.print();
return 0;
}
7-4 计算高考状元
高考成绩已经公布,大家正在填报志愿。设计一个学生类student,四门学科成绩是其私有成员,分别是语文、数学、英语、综合。有个计算高考状元的函数是其友元函数,其形式是 student top(const student *p, int count) 。
以上类名和友元函数的形式,均须按照题目要求,不得修改。
输入是姓名 和 四科成绩,以0结束。 (不超过100个学生)
输出是状元的总分。
输入样例:
Alice 105 107 107 230
Bob 112 120 120 250
0
输出样例:
602
#include<iostream>
#include<string>
using namespace std;
class student
{
string name;
int yu,shu,yin,zong;
public:
student(string n = "666",int y = 0,int s = 0,int e = 0,int z = 0)
{
name = n;
yu = y;
shu = s;
yin = e;
zong = z;
}
friend student top(const student *p,int count);
};
student top(const student *p,int count)
{
int max = 0,sum = 0,i;
for(i = 0;i < count;i++)
{
sum = p[i].yu + p[i].shu + p[i].yin + p[i].zong;
if(sum > max)
{
max = sum;
}
}
cout<<max;
return *p;
}
int main()
{
student stu[100];
string name;
int yu,shu,yin,zong,n = 0;
cin>>name;
while(name != "0")
{
cin>>yu>>shu>>yin>>zong;
stu[n++] = student(name,yu,shu,yin,zong);
cin>>name;
}
top(stu,n);
return 0;
}
7-5 友元函数的练习
定义Boat与Car两个类,两者都有私有的整型weight属性,定义两者的一个友元函数getTotalWeight(),计算二者的重量和。
参考主函数:
int main()
{
int n,m;
cin>>n>>m;
Boat boat(n);
Car car(m);
cout<<"船和汽车共重"<<getTotalWeight(boat,car)<<"吨"<<endl;
}
输入格式:
请在这里写输入格式。例如:输入在一行中给出2个整数m和n。
输出格式:
请在这里描述输出格式。例如:对每一组输入,在一行中输出:船和汽车共重M+n吨值。
输入样例:
在这里给出一组输入。例如:
40 30
输出样例:
在这里给出相应的输出。例如:
船和汽车共重70吨
#include <iostream>
using namespace std;
class Car;
class Boat
{
private:
int weight1;
public:
Boat(int w)
{
weight1 = w;
}
friend int getTotalWeight(Boat &b,Car &c);
};
class Car
{
private:
int weight2;
public:
Car(int w)
{
weight2 = w;
}
friend int getTotalWeight(Boat &b,Car &c);
};
int getTotalWeight(Boat &b,Car &c)
{
return b.weight1+c.weight2;
}
int main()
{
int n,m;
cin>>n>>m;
Boat boat(n);
Car car(m);
cout<<"船和汽车共重"<<getTotalWeight(boat,car)<<"吨"<<endl;
return 0;
}
7-6 最小栈设计并实现一个MinStack类
最小栈设计并实现一个MinStack类,要求至少含有以下三种方法
一、push方法:将一个整数进行压栈
二、pop方法:弹出栈顶元素
三、getmin方法:输出栈内最小元素
输入格式:
第一行输入一个N,表示接下来要操作的数量
随后N行表示操作:
push方法后跟要压栈的元素
pop方法、getmin方法后不跟任何元素
说明:假设不包含其他无效输入,以及在执行getmin、pop方法时栈不为空
输出样例:
输出栈内最小元素
输入样例:
5
push 2
push 3
push 1
pop
getmin
输出样例:
2
#include <iostream>
#include <string>
using namespace std;
class MinStack
{
public:
int cnt;
int a[100000];
MinStack()
{
cnt = 0;
a[100000] = 0;
}
void push(int x)
{
a[cnt++] = x;
}
int pop()
{
return a[--cnt];
}
int getmin()
{
int min = a[0];
for(int i = 1;i < cnt;i++)
{
if(a[i] < min)
min = a[i];
}
return min;
}
};
int main()
{
MinStack s;
int i,n;
cin>>n;
for(i = 0;i < n;i++)
{
string op;
cin>>op;
if(op == "push")
{
int x;
cin>>x;
s.push(x);
}
else if(op == "pop")
s.pop();
else if(op == "getmin")
cout<<s.getmin()<<endl;
}
return 0;
}
7-7 计算点到直线的距离一一友元函数的应用
计算点到直线的距离。首先设计一个点类Point,它有2 个私有数据成员x和y,表示点的坐标。另一个类为直线类Line,它有3 个私有数据成员a,b和c,表示直线方程ax+by+c= 0。这两个类中都说明了一个友元函数dist,用于计算一个点到直线的距离。点(x.y)到直线ax+by+c=0的距离d的计算公式如下:
语法要点: 友元函数的特点。
这是一个编程题模板。请在这里写题目描述。例如:本题目要求读入2个整数A和B,然后输出它们的和。
输入格式:
输入两行,第一行输入两个整数,表示点坐标x,y的值
在第二行中输入直线三个参数,表示直线方程的三个洗漱a,b,c.
输出格式:
计算点到直线的距离保留两位小数。
输入样例:
在这里给出一组输入。例如:
5 5
2 4 3
输出样例:
在这里给出相应的输出。例如:
The distance is: 7.38
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class line;
class point
{
private:
double x,y;
public:
point(double a,double b):x(a),y(b){}
friend double dist(point &,line &);
};
class line
{
private:
double a,b,c;
public:
line(double m,double n,double p):a(m),b(n),c(p){}
friend double dist(point &,line &);
};
double dist(point &p, line &l)
{
double s;
s = abs((l.a*p.x + l.b*p.y + l.c)/sqrt(l.a*l.a + l.b*l.b));
return s;
}
int main()
{
int x,y,a,b,c;
cin>>x>>y>>a>>b>>c;
point p(x,y);
line l(a,b,c);
if(dist(p,l) == 0)
cout<<"The distance is: 0"<<endl;
else
cout<<"The distance is: "<<fixed<<setprecision(2)<<dist(p,l)<<endl;
return 0;
}
7-8 集合的模拟实现(类模板)
我们可以用一个类来模拟集合及集合运算,add运算用以实现集合元素的增加,delete运算用于实现集合元素的删除,find运算用以实现集合元素的查找,但是目前集合元素类型未知,可以是int、char、double等基本数据类型,也可以是String、Time、Student等对象类型,要求采用类模板实现集合及集合运算,包括集合元素的增加、删除和查找的等基本功能。
集合模板类MySet包括数据如下:
T data[100];//用数组来存放所有的集合元素,最多不超过100个元素
int count;//表示目前集合中有多少个元素
包括成员函数如下:
构造函数若干个,集合运算函数如下:
int addSet( T elem)
int deleSet(T elem)
int findElem(T elem)
其中,addSet向集合中添加一个元素,deleSet从集合中删除一个元素,findElem判断elem是否是集合成员,三个函数分别返回元素插入位置,删除位置和存在位置。
主函数有如下数据成员 :
MySet<\int>\ intSet;(反斜杠是转义字符,使用时去掉)
MySet<\double>\ douSet;
MySet<\string>\ strSet;
分别是int类型、double类型、String的集合。
完成上述类模板和主函数,主函数根据输入的信息,建立初始的三种不同类型的空集合对象,调用成员函数分别对intSet、douSet和StrSet执行相应的操作,并输出对应的集合信息。
输入格式:
每一行为一个集合操作,每行的第一个数字为集合元素类型,1为整型元素,2为浮点型元素,3为String类型,第二个数字为集合操作类型,1为插入,2为删除,3为查找,第三个为集合元素,集合元素类型视第一个数字给定的集合元素类型而定。输入0时标志输入结束。
输出格式:
输出当前操作的执行位置(插入位置、删除位置和存在位置)
删除操作时,如果元素X不存在,输出“X is not exist!”。
插入操作时,如果集合已满,输出“Full Set.”若元素已存在,输出“X is already exist!”
查找操作时,如果找不到元素,输出“X is not exist!”。
输入:
1 1 1
1 1 2
1 3 1
1 2 1
1 2 3
1 3 1
2 1 1.1
2 1 2.2
2 1 3.3
2 3 1.1
2 2 2.2
2 2 2.2
3 1 abc
3 1 bcd
3 3 abc
3 2 abc
3 3 abc
0
输出:
0
1
0
0
3 is not exist!
1 is not exist!
0
1
2
0
1
2.2 is not exist!
0
1
0
0
abc is not exist!
#include<bits/stdc++.h>
using namespace std;
template<class T>
class MySet
{
private:
T data[100];
static int count;
public:
MySet(){}
int addSet(T elem)
{
if(count==100)
{
cout<<"Full Set."<<endl;
return 0;
}
for(int i = 0;i < count;i++)
{
if(data[i] == elem)
{
cout<<elem<<" is already exist!"<<endl;
return 0;
}
}
cout<<count<<endl;
data[count++] = elem;
return 0;
}
int deleSet(T elem)
{
int bj = 0,i;
for(i = 0;i < count;i++)
{
if(data[i]==elem)
{
cout<<i<<endl;
bj = 1;
count--;
break;
}
}
if(bj)
{
for(int j = i;j <= count-1;j++)
{
data[j]=data[j+1];
}
}
else
cout<<elem<<" is not exist!"<<endl;
return 0;
}
int findElem(T elem)
{
int bj = 0;
for(int i = 0;i < count;i++)
{
if(data[i] == elem)
{
cout<<i<<endl;
bj = 1;
}
}
if(bj == 0)
cout<<elem<<" is not exist!"<<endl;
return 0;
}
};
template<class T>
int MySet<T>::count = 0;
int main()
{
int n;
cin>>n;
MySet<int>intSet;
MySet<double>douSet;
MySet<string>strSet;
while(n)
{
if(n == 1)
{
int op,x;
cin>>op>>x;
switch(op)
{
case 1:intSet.addSet(x);break;
case 2:intSet.deleSet(x);break;
case 3:intSet.findElem(x);break;
}
}
else if(n == 2)
{
int op;
double x;
cin>>op>>x;
switch(op)
{
case 1:douSet.addSet(x);break;
case 2:douSet.deleSet(x);break;
case 3:douSet.findElem(x);break;
}
}
else if(n == 3)
{
int op;
string x;
cin>>op>>x;
switch(op)
{
case 1:strSet.addSet(x);break;
case 2:strSet.deleSet(x);break;
case 3:strSet.findElem(x);break;
}
}
cin>>n;
}
return 0;
}
7-9 编写一个友元函数,求两个日期之间相差的天数。
设计一个日期类Date,包括日期的年份、月份和日号,编写一个友元函数,求两个日期之间相差的天数。该类中设计有3个友元函数;count_day()函数,它有两个参数,第2个参数是一个标志,当其值等于1 时,计算一年的开始到某日期的天数;否则计算某日期到年尾的天数。leap()函数用于判断指定的年份是否为闰年。subs()函数用于计算两个日期之间的天数。 当时间输入不正确时,输出“time error!”
输入样例:
在这里给出一组输入。例如:
2000 1 1
2002 10 1
输出样例:
在这里给出相应的输出。例如:
1004
#include<iostream>
using namespace std;
class date
{
int y,m,d;
public:
date(int y1,int m1,int d1):y(y1),m(m1),d(d1){}
friend int leap(int a);
friend int countday(date &a,int flag);
friend int subs(date &a,date &b);
};
int leap(int a)
{
if((a%4 == 0 && a%100 != 0)||(a%400 == 0))
return 1;
else
return 0;
}
int countday(date &a,int flag)
{
int A[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int bj = leap(a.y);
int sum = 0,i;
if (flag == 1)
{
for (i = 0; i < a.m - 1; i++)
sum += A[i];
sum += a.d + bj;
}
else
{
for (i = 11; i >= a.m - 1; i--)
{
sum += A[i];
if (i == 1 && bj)
sum += 1;
}
sum -= a.d;
}
return sum;
}
int subs(date &a,date &b)
{
int sum = 0,i;
sum += countday(a,0) + countday(b,1);
if(a.y == b.y)
{
int bj = leap(a.y);
if(bj)
sum -= 366;
else
sum -= 365;
return sum;
}
if((b.y-a.y)>=2)//年份大于等于两年
{
for(i = a.y+1;i <= b.y-1;i++)
{
if(leap(i))
sum+= 366;
else
sum+=365;
}
}
return sum;
}
int main()
{
int y1,y2,m1,m2,d1,d2;
cin>>y1>>m1>>d1>>y2>>m2>>d2;
if((y2-y1)<0 || (m2-m1)<0 || (d2-d1)<0)
cout<<"time error!";
else
{
date a(y1,m1,d1),b(y2,m2,d2);
cout<<subs(a,b)<<endl;
}
return 0;
}
7-10 程序猿和产品狗
在公司里面,程序猿经常有一堆todolist要做,而这些todolist是产品经理分配给他们的。但是当程序员遇到不懂技术的产品狗时,就悲剧了。产品经理经常修改他们的todolist,比如:添加,减少他们的todolist。
请设计一个类CodeMonkey ,表示程序猿,另一个类ProductDog,表示产品经理。
CodeMonkey类有私有成员 name,todolist。
构造函数初始化姓名和todolist,公有函数 int sizeof_todolist(), 来自ProductDog类的友元函数 add_todolist(CodeMonkey&,int),reduce_todolist(CodeMonkey& , int);
ProductDog类有公有函数 add_todolist(CodeMonkey&,int),reduce_todolist(CodeMonkey& , int)
Input Specification
每个测试文件包含一组测试用例,对于每个测试用例,第一行输入 n (1<= n <= 10^6)表示有n个程序员,接下去n行,每行为: name x 表示名为name的程序员的todolist的长度为x。 接下去一行 m (1<=m<=10^6),表示产品经理分配任务的次数。 接下去m行,每行为 name opt x , opt 为 0 表示名为name的程序猿的todolist增加x,opt为 1 表示减少x。 输入数据保证合法性。
Output Specification
对于每个程序员,请输出 name x,x表示最终的todolist长度。按输入顺序输出。
Sample Input:
3
Jack 1
Luck 2
Tom 3
4
Tom 0 100
Luck 0 50
Jack 0 25
Tom 1 50
Sample Output:
Jack 26
Luck 52
Tom 53
#include<bits/stdc++.h>
using namespace std;
class CodeMonkey;
class ProductDog
{
public:
void add_todolist(CodeMonkey&,int);
void reduce_todolist(CodeMonkey&,int);
};
class CodeMonkey
{
public:
string name;
int todolist;
CodeMonkey(){}
CodeMonkey(string n,int t):name(n),todolist(t){}
int sizeof_todolist()
{
return this->todolist;
}
void print()
{
cout<<this->name<<" "<<this->todolist<<endl;
}
friend void ProductDog::add_todolist(CodeMonkey&,int);
friend void ProductDog::reduce_todolist(CodeMonkey&,int);
};
void ProductDog::add_todolist(CodeMonkey& mk,int n)
{
mk.todolist = mk.todolist+n;
}
void ProductDog::reduce_todolist(CodeMonkey& mk,int n)
{
mk.todolist = mk.todolist-n;
}
int main()
{
ProductDog pd;
string name,name1;
int n,m,td,i,op,x,j;
cin>>n;
CodeMonkey ck[n+1];
for(i = 0;i < n;i++)
{
cin>>name>>td;
ck[i] = CodeMonkey(name,td);
}
cin>>m;
for(i = 0;i < m;i++)
{
cin>>name1>>op>>x;
for(j = 0;j < n;j++)
{
if(ck[j].name == name1)
{
if(op)
pd.reduce_todolist(ck[j],x);
else
pd.add_todolist(ck[j],x);
break;
}
}
}
for(i = 0;i < n;i++)
ck[i].print();
return 0;
}
7-11 2017final函数模板
数据的间距问题(函数模板) 类point有三个数据成员:x、y和z, 分别代表x坐标、y坐标和z坐标,并有若干构造函数和一个重载-(减号,计算两点距离)的成员函数。 要求设计一个函数模板,
template < class T> double dist(T a, T b)
对int,float,point或者其他类型的数据,返回间距。
输入格式:
每一行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为point类型,若为整型元素,接着输入两个整型数据,若为浮点型元素,接着输入两个浮点型数据,若为point型元素,输入两个point型数据(x1 y1 z1 x2 y2 z2),输入0时标志输入结束。
输出格式:
对每个输入,每行输出一个间距值。
输入样例:
1 2 5
3 2 4 7 5 9 7
2 2.2 9.9
0
输出样例:
3
5.83095
7.7
#include<iostream>
#include<cmath>
using namespace std;
class point
{
double x,y,z;
public:
point(){}
point(double a,double b,double c):x(a),y(b),z(c){}
friend double operator -(point a,point b);
};
double operator -(point a,point b)
{
double s = sqrt(pow((a.x-b.x),2)+pow((a.y-b.y),2)+pow((a.z-b.z),2));
return s;
}
template< class T >
double dist(T a, T b)
{
double s;
s = fabs(a-b);
return s;
}
int main()
{
int flag;
while(1)
{
cin>>flag;
if(!flag)
break;
switch(flag)
{
case 1:
{
int x,y;
cin>>x>>y;
cout<<dist(x,y)<<endl;
break;
}
case 2:
{
double x,y;
cin>>x>>y;
cout<<dist(x,y);
break;
}
case 3:
{
point x,y;
double xa,xb,xc,ya,yb,yc;
cin>>xa>>xb>>xc>>ya>>yb>>yc;
x=point(xa,xb,xc);
y=point(ya,yb,yc);
cout<<dist(x,y)<<endl;
break;
}
}
}
return 0;
}
7-12 2018final复数求模的类模板
有一个复数的类模板,有两个私有数据成员,分别是 实部和虚部。有一个成员函数是求该复数的模。 请添加适当的构造函数。
输入格式:
输入仅一行,分别是三个数,以空格间隔。 第一个数是1或2或3(1表示int型,2表示float型,3表示double型),第二个数是该复数的实部,第三个数是该复数的虚部。
输出格式:
输出仅一行,输出该复数的模(in型的复数,输出int型的模; float型的复数,输出float型的模;double型的复数,输出double型的模)。
输入样例:
在这里给出一组输入。例如:
2 2.5 -3.1
输出样例:
在这里给出相应的输出。例如:
3.98246
#include<iostream>
#include<cmath>
using namespace std;
template <class T>
void qumo(T x,T y)
{
cout<<sqrt(pow(x,2)+pow(y,2))<<endl;
}
int main()
{
int r1,i1,op;
float r2,i2;
double r3,i3;
cin>>op;
if(op == 1)
{
cin>>r1>>i1;
qumo(r1,i1);
}
else if(op == 2)
{
cin>>r2>>i2;
qumo(r2,i2);
}
else if(op == 3)
{
cin>>r3>>i3;
qumo(r3,i3);
}
return 0;
}
7-13 函数模板
数据的间距问题(函数模板)
类point有两个数据成员:x和y, 分别代表x坐标和y坐标,并有若干构造函数和一个重载-(减号,计算两点距离)的成员函数。
要求设计一个函数模板
template < class T >
double dist(T a, T b)
对int,float,point或者其他类型的数据,返回间距。
输入格式:
每一行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为point类型,若为整型元素,接着输入两个整型数据,若为浮点型元素,接着输入两个浮点型数据,若为point型元素,输入两个point型数据(x1 y1 x2 y2),输入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 point
{
double x,y;
public:
point(){}
point(double a,double b):x(a),y(b){}
friend double operator -(point a,point b);
};
double operator -(point a,point b)
{
double s;
s = sqrt(pow((a.x-b.x),2)+pow((a.y-b.y),2));
return s;
}
template< class T >
double dist(T a, T b)
{
double s;
s = fabs(a-b);
return s;
}
int main()
{
int flag,x,y,x1,y1,s1;
double x2,y2,s2;
cin>>flag;
while(flag)
{
if(flag == 1)
{
cin>>x>>y;
s1 = dist(x,y);
cout<<s1<<endl;
}
else if(flag == 2)
{
cin>>x2>>y2;
s2 = dist(x2,y2);
cout<<s2<<endl;
}
else if(flag == 3)
{
cin>>x>>y>>x1>>y1;
point a(x,y),b(x1,y1);
s2 = dist(a,b);
cout<<s2<<endl;
}
cin>>flag;
}
return 0;
}
7-14 数据的最大值问题(重载+函数模板)
两个类如下设计:类time有三个数据成员,hh,mm,ss,分别代表时,分和秒,并有若干构造函数和一个重载-(减号)的成员函数。类date有三个数据成员,year,month,day分别代表年月日,并有若干构造函数和一个重载>(<)(大于号或者小于号)的成员函数。
要求设计一个函数模板template <\class T>\ double maxn(T x[], int len) 对int,float,time和date或者其他类型的数据,返回最大值。
主函数有如下数据成员:
int intArray[100];
double douArray[100];time timeArray[100];
date dateArray[100];
其中,hh = 3600 * ss, mm = 60 * ss, year = 365 * day, month = 30 * day,对于time和date类型,数据在转换成ss或者day后进行运算。
输入格式:
每行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为time类型,4为date类型,若为整型元素,接着输入整型数据,以0结束。若为浮点型元素,接着输入浮点型数据,以0结束。若为time型元素, 输入time型数据(hh1 mm1 ss1 hh2 mm2 ss2),以0结束。若为date型数据,输入date型数据(year1 month1 day1 year2 month2 day2),以0结束。输入-1时表示全体输入结束。
输出格式:
对每次输入,输出一个最大值。
样例输入:
1 4 5 9 3 7 0
2 4.4 5.5 6.9 3.2 2.7 0
3 18 21 22 18 20 31 18 21 49 0
4 2013 5 14 2013 5 15 2013 4 1 0
-1
样例输出:
9
6.9
18 21 49
2013 5 15
7-14 数据的最大值问题(重载+函数模板)
分数 50
作者 余春艳
单位 浙江大学
两个类如下设计:类time有三个数据成员,hh,mm,ss,分别代表时,分和秒,并有若干构造函数和一个重载-(减号)的成员函数。类date有三个数据成员,year,month,day分别代表年月日,并有若干构造函数和一个重载>(<)(大于号或者小于号)的成员函数。
要求设计一个函数模板template <\class T>\ double maxn(T x[], int len) 对int,float,time和date或者其他类型的数据,返回最大值。
主函数有如下数据成员:
int intArray[100];
double douArray[100];time timeArray[100];
date dateArray[100];
其中,hh = 3600 * ss, mm = 60 * ss, year = 365 * day, month = 30 * day,对于time和date类型,数据在转换成ss或者day后进行运算。
输入格式:
每行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为time类型,4为date类型,若为整型元素,接着输入整型数据,以0结束。若为浮点型元素,接着输入浮点型数据,以0结束。若为time型元素, 输入time型数据(hh1 mm1 ss1 hh2 mm2 ss2),以0结束。若为date型数据,输入date型数据(year1 month1 day1 year2 month2 day2),以0结束。输入-1时表示全体输入结束。
输出格式:
对每次输入,输出一个最大值。
样例输入:
1 4 5 9 3 7 0
2 4.4 5.5 6.9 3.2 2.7 0
3 18 21 22 18 20 31 18 21 49 0
4 2013 5 14 2013 5 15 2013 4 1 0
-1
样例输出:
9
6.9
18 21 49
2013 5 15
7-15 数据的间距问题(重载+函数模板)
三个类如下设计:类cTime有三个数据成员,hh,mm,ss,分别代表时,分和秒,并有若干构造函数和一个重载-(减号)的成员函数。类point有两个数据成员,x,y分别坐标,并有若干构造函数和一个重载-(减号)的成员函数。类date有三个数据成员,year,month,day分别代表年月日,并有若干构造函数和一个重载-(减号)的成员函数。
要求设计一个函数模板template <\class T>\ double dist(T a, T b) 对int,float,cTime,point和date或者其他类型的数据,返回间距。
其中,hh = 3600 * ss, mm = 60 * ss, year = 365 * day, month = 30 * day,对于cTime和date类型,数据在转换成ss或者day后进行运算。
输入格式:
每一行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为point类型,4,为time类型,5为date类型,若为整型元素,接着输入两个整型数据,
若为浮点型元素,接着输入两个浮点型数据,若为point型元素,输入两个point型数据(x1 y1 x2 y2),若为time型元素, 输入两个cTime型数据(hh1 mm1 ss1 hh2 mm2 ss2),若为date型数据,输入两个date型数据(year1 month1 day1 year2 month2 day2)。输入0时标志输入结束。
输出格式:
对每个输入,每行输出一个间距值。
样例输入:
1 2 5
4 18 21 22 18 20 31
3 2 4 5 9
5 2013 5 14 2013 5 15
2 2.2 9.9
0
样例输出:
3
51
5.83095
1
7.7
#include<bits/stdc++.h>
using namespace std;
class Time
{
private:
int hh,mm,ss,total;
public:
Time(){}
Time(int h,int m,int s):hh(h),mm(m),ss(s)
{
total = hh*3600 + mm*60 + ss;
}
int gettotal()
{
return this->total;
}
int operator-(Time &t)
{
return this->total - t.gettotal();
}
};
class point
{
private:
double x,y;
public:
point(){}
point(double x1,double y1):x(x1),y(y1){}
double operator-(point& o)
{
return sqrt((this->x-o.x)*(this->x-o.x)+(this->y-o.y)*(this->y-o.y));
}
};
class date
{
private:
int year,month,day,total;
public:
date(){}
date(int y,int m,int d):year(y),month(m),day(d)
{
total = year*365 + month*30 + day;
}
int gettotal()
{
return this->total;
}
int operator-(date& d)
{
return this->total-d.gettotal();
}
};
template <class T>
double dist(T a,T b)
{
return abs(a-b);
}
int main()
{
int op;
while(1)
{
cin>>op;
if(op == 0)
break;
switch(op)
{
case 1:
{
int x,y;
cin>>x>>y;
cout<<dist(x,y)<<endl;
break;
}
case 2:
{
double x,y;
cin>>x>>y;
cout<<dist(x,y)<<endl;
break;
}
case 3:
{
double ax,ay,bx,by;
cin>>ax>>ay>>bx>>by;
point x(ax,ay),y(bx,by);
cout<<dist(x,y)<<endl;
break;
}
case 4:
{
int h1,h2,m1,s1,m2,s2;
cin>>h1>>m1>>s1>>h2>>m2>>s2;
Time t1(h1,m1,s1),t2(h2,m2,s2);
cout<<dist(t1,t2)<<endl;
break;
}
case 5:
{
int y1,y2,m1,m2,d1,d2;
cin>>y1>>m1>>d1>>y2>>m2>>d2;
date da1(y1,m1,d1),da2(y2,m2,d2);
cout<<dist(da1,da2)<<endl;
break;
}
}
}
return 0;
}