7-1 计算平均值(10 分)
现在为若干组整数分别计算平均值。
已知这些整数的绝对值都小于100,每组整数的数量不少于1个,不大于20个。
输入格式:首先输入K(不小于2,不大于20)。接下来每一行输入一组数据(至少有一组数据),每组至少有一个数据,在有多个数据时,两个数据之间有1到3个空格。最后一行输入100,标志输入的结束。
输出格式:对于每一组数据,输出其前K个数据的均值,如果该组数据个数少于K时,则输出该组所有数据的均值。输出的均值只输出整数部分,直接忽略小数部分。
样例输入:
3
10 30 20 40
-10 17 10
10 9
100
样例输出:
20
5
9
#include
#include
using namespace std;
int main()
{
int i, j, k, n, m, s, t, a[30];
char c[1000];
cin >> n;
fgets(c, 1000, stdin);
while (1 > 0)
{
fgets(c, 1000, stdin);
j = -1;
i = 0;
while (i < strlen(c))
{
if ((c[i] >= '0') && (c[i] <= '9'))
{
m = 1;
if (c[i - 1] == '-') m = -1;
t = 0;
while ((c[i] >= '0') && (c[i] <= '9'))
{
t = 10 * t + c[i] - '0';
i++;
}
j++;
a[j] = t*m;
}
i++;
}
if ((j == 0) && (a[0] == 100)) break;
j++;
s = 0;
if (j > n)
{
for (i = 0; i < n; i++)
s += a[i];
cout << s / n << endl;
}
else
{
for (i = 0; i < j; i++)
s += a[i];
cout << s / j << endl;
}
}
return 0;
}
7-2 计算正五边形的面积和周长(10 分)
从键盘输入一个数作为正五边形的边长,计算并输出该正五边形的周长和面积。
计算正五边形的面积公式为: S=a2×√25+10×√5/4
输入格式:
输入正五边形的边长。例如:
5
输出格式:
输出正五边形的面积和周长。第一行输出面积,第二行输出周长。例如:
43.0119
25
输入样例:
16.8
#include
#include
using namespace std;
int main()
{
float a,area,length;
cin>>a;
area=a*a*sqrt(25+10*sqrt(5))/4;
length=5*a;
cout<cout<return 0;
}
6-2 求最大值和最小值(10 分)
本题要求实现一个函数f,可找出10个整数中最大值max和最小值min。
函数接口定义:
在主函数中将以下列形式调用该函数
f(a,10,max,min);
例如:其中a是数组名,max用来保存最大值,min用来保存最小值。
裁判测试程序样例:
#include
using namespace std;
/* 你提交的代码将被嵌入到这里 */
int main( )
{
int a[10];
int max,min,i;
for(i=0;i<10;i++){
cin>>a[i];
}
f(a,10,max,min);
cout<<"Max: "<cout<<"Min: "<return 0;
}
输入样例:
2 5 8 1 4 7 3 6 9 0
输出样例:
Max: 9
Min: 0
int f(int a[],int n,int &max,int &min)
{
int i;
for(i=0;i<=n;i++)
{
if(a[i]>=max) max=a[i];
if(a[i]<=min) min=a[i];
}
}
7-1 类的定义和使用(10 分)
定义一个日期类Date,内有数据成员年、月、日,另有成员函数:构造函数用于初始化数据成员,输出,闰年的判断。编写主函数:创建日期对象,计算并输出该日是该年的第几天。
输入:
每组测试数据仅包含一个测试用例,每个测试用例占一行包括三个数,分别表示年、月、日。
输出:
该日是该年的第几天。
输入样例:
2006 3 5
输出样例:
64 (2006年3月5日是该年的第64天)
#include
using namespace std;
class Date//日期类
{
private:
int year,month,day,flag,sum;
public:
Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
flag = 0;
sum =0;
switch(month)
{
case 1: sum = 0;break;
case 2: sum = 31;break;
case 3: sum = 59;break;
case 4: sum = 90;break;
case 5: sum = 120;break;
case 6: sum = 151;break;
case 7: sum = 181;break;
case 8: sum = 212;break;
case 9: sum = 243;break;
case 10: sum = 273;break;
case 11: sum = 304;break;
case 12: sum = 334;break;
}
sum=sum+day;
if((year%4==0 && year%100!=0)||year%400==0)
{
flag = 1;
}
if(flag==1&&month>2)
sum++;
cout << sum << endl;
}
};
int main()
{
int y,m,d;
cin >> y >> m >>d;
Date r(y,m,d);
return 0;
}
7-1 友元函数(10 分)
C++考试正在进行。请设计一个学生类student,学号、本次考试成绩是其私有数据成员,同时有一个计算本次考试平均成绩的友元函数 double average(student *p,int count)
以上类名和友元函数的形式,均须按照题目要求,不得修改。
输入是 学号([00001,99999])和成绩,以0结束。(不超过100个学生)
输出是平均成绩。
输入样例:
10001 90
10002 93
0
输出样例:
91.5
#include
#include
using namespace std;
class student
{
long name;
double yu;
public:
student()
{
name=0;yu=0;
};
void set(long name1,double yu1)
{
name=name1;yu=yu1;
}
friend double average(const student *p,int count );
};
double average(const student *p,int count )
{
double top1;
double add=0;
for(int i=0;ireturn top1;
}
int main()
{
student St[100];
long name;
int num=0;
double yu;
double yes;
while((cin>>name)&&name!=0&&name>=00001&&name<=99999)
{ cin>>yu;
St[num].set(name,yu);
num++;
}
yes=average(St,num);
cout<return 0;
}
7-2 求两点之间距离(10 分)
定义一个Point类,有两个数据成员:x和y, 分别代表x坐标和y坐标,并有若干成员函数。 定义一个函数Distance(), 用于求两点之间的距离。
输入格式:
输入有两行: 第一行是第一个点的x坐标和y坐标; 第二行是第二个点的x坐标和y坐标。
输出格式:
输出两个点之间的距离,保留两位小数。
输入样例:
0 9 3 -4
输出样例:
13.34
#include
#include
#include
using namespace std;
class Point
{
double x,y;
public:
Point(double x,double y)
{ this->x=x;this->y=y;}
friend double Distance(Point &a,Point &b);
};
double Distance(Point &a,Point &b)
{ double dx=a.x-b.x;
double dy=a.y-b.y;
return sqrt(dx*dx+dy*dy);
}
int main()
{ double m,n,p,q;
cin>>m>>n;
cin>>p>>q;
Point a(m,n),b(p,q);
double d=Distance(a,b);
cout <2) << d <return 0;
}
7-1 复数类的操作(10 分)
1、声明一个复数类Complex(类私有数据成员为double型的real和image) 2、定义构造函数,用于指定复数的实部与虚部。 3、定义取反成员函数,调用时能返回该复数的相反数(实部、虚部分别是原数的相反数)。 4、定义成员函数Print(),调用该函数时,以格式(real,image)输出当前对象。 5、定义静态数据成员count,用于统计该类一共定义了多少个对象。 6、编写加法友元函数,以复数对象c1,c2为参数,求两个复数对象相加之和。 7、主程序实现: (1)读入两个实数,用于初始化对象c1。 (2)读入两个实数,用于初始化对象c2。 (3)输出class类中count的值。 (4)计算c1与c2相加结果,并输出。 (5)计算c2的相反数与c1相加结果,并输出。
输入格式:
输入有两行: 第一行是复数c1的实部与虚部,以空格分隔; 第二行是复数c2的实部与虚部,以空格分隔;
输出格式:
输出共三行: 第一行是对象个数; 第二行是c1与c2之和; 第三行是c2的相反数与c1之和。
输入样例:
在这里给出一组输入。例如:
2.5 3.7
4.2 6.5
输出样例:
在这里给出相应的输出。例如:
2
(6.7,10.2)
(-1.7,-2.8)
#include
using namespace std;
class Complex
{
double real,image;
public:
Complex(double a,double b);
void norcomplex();
void Print();
static int count;
friend void add(Complex &a,Complex &b);
};
Complex::Complex(double a,double b)
{
real=a;image=b;
count++;
}
void Complex::norcomplex()
{
real=-real;image=-image;
}
void Complex::Print()
{
printf("(%.1f,%.1f)\n",real,image);
}
void add(Complex &a,Complex &b)
{
a.real+=b.real;
a.image+=b.image;
a.Print();
a.real-=b.real;
a.image-=b.image;
}
int Complex::count=0;
int main()
{ double m,n,p,q;
cin>>m>>n;
cin>>p>>q;
Complex c1(m,n);
Complex c2(p,q);
printf("%d\n",c1.count);
add(c1,c2);
c2.norcomplex();
add(c1,c2);
return 0;
}
7-1 字符串替换(10 分)
将文本文件中指定的字符串替换成新字符串。 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束。end后面有两个字符串,要求用第二个字符串替换文本中所有的第一个字符串。
输入格式:
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology. The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end (表示结束)
Institute (第一个字符串,要求用第二个字符串替换)
University (第二个字符串)
输出格式:
Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
输入样例:
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end
Institute
University
输出样例:
Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
#include
#include
using namespace std;
int main()
{
string a,m,b,c;
int found,i;
getline(cin,a);
while(1)
{getline(cin,m);
i=m.compare("end");
if(i==0)
{
break;
}
a+='\n';
a+=m;
}
a+='\n';
cin>>b;
cin>>c;
found=a.find(b);
while(found !=-1)
{
a.replace(found,b.length(),c);
found=a.find(b,found+1);
}
cout<return 0;
}
7-2 求解给定字符串的前缀(10 分)
求解给定字符串的前缀。
输入格式:
输入数目不定的多对字符串,每行两个,以空格分开。 例如:
filename filepath
Tom Jack
输出格式:
返回两个字符串的最大前缀,例如:
The common prefix is file
No common prefix
输入样例:
filename filepath
Tom Jack
输出样例:
The common prefix is file
No common prefix
#include
#include
#include
using namespace std;
int main(){
char a[100],b[100];
int i;
int n,m,t;
n=strlen(a);
m=strlen(b);
if(m>n){
t=n;
n=m;
m=t;
}
while(scanf("%s",a)!=EOF && scanf("%s",b)!= EOF){
for(i=0;iif(a[i]!=b[i]){
break;
}
}
if(i==0)
cout<<"No common prefix"<else {
cout<<"The common prefix is ";
for(int j=0;jcout<cout<
7-3 学号解析(10 分)
川师的学号的某些位有特殊的含义,如从2016110101中可以看出该学生为2016级,就读于11系,班级为1班。根据输入的学号,利用程序进行解析,输出对应的信息。
输入格式:
一个学号
输出格式:
相关信息
输入样例:
在这里给出一组输入。例如:
2016110101
输出样例:
在这里给出相应的输出。例如:
year:2016
department:11
class:01
#include
#include
using namespace std;
int main(){
char a[100];
cin>>a;
cout<<"year:";
for(int i=0;i<4;i++){
cout<cout<cout<<"department:";
for(int i=4;i<6;i++){
cout<cout<cout<<"class:";
for(int i=6;i<8;i++){
cout<cout<return 0;
}
7-1 分离目录路径和文件名(10 分)
输入文件目录路径和文件名,要求分离成目录路径和文件名分别输出
输入格式:
例如:输入
c:\windows\winhelp.exe
输出格式:
c:\windows (目录路径)
winhelp.exe (文件名)
输入样例:
/usr/bin/man
输出样例:
/usr/bin
man
#include
#include
using namespace std;
void file(string &str)
{
int found=str.find_last_of("/\\");
cout<0,found)<cout<1)<int main()
{
string str1;
getline(cin,str1);
file(str1);
return 0;
}
6-4 狗的继承(10 分)
完成两个类,一个类Animal,表示动物类,有一个成员表示年龄。一个类Dog,继承自Animal,有一个新的数据成员表示颜色,合理设计这两个类,使得测试程序可以运行并得到正确的结果。
函数接口定义:
按照要求实现类
裁判测试程序样例:
/* 请在这里填写答案 */
int main(){
Animal ani(5);
cout<<"age of ani:"<5,"black");
cout<<"infor of dog:"<
输入样例:
无
输出样例:
age of ani:5
infor of dog:
age:5
color:black
#include
#include
using namespace std;
class Animal{
protected:
int age;
public:
Animal(int age1){ age=age1;}
~Animal(){}
int getAge(){ return age;}
};
class Dog:public Animal{
private:
char color[100];
int age1;
public:
Dog(int age1,char color1[]):Animal(age1)
{ age=age1;
strcpy(color,color1);
}
~Dog(){}
void showInfor()
{
cout<<"age:"<cout<<"color:"<
7-1 学生CPP成绩计算(10 分)
给出下面的人员基类框架:
class Person {
protected:
string name;
int age;
public:
Person();
Person (string p_name, int p_age);
void display () {cout<
建立一个派生类student,增加以下成员数据:
int ID;//学号
float cpp_score;//cpp上机成绩
float cpp_count;//cpp上机考勤
float cpp_grade;//cpp总评成绩
//总评成绩计算规则:cpp_grade = cpp_score * 0.9 + cpp_count * 2;
增加以下成员函数:
student类的无参构造函数
student类的参数化构造函数//注意cpp_grade为上机成绩和考勤的计算结果
void print()//输出当前student的信息
//其中cpp_grade输出保留一位小数
//输出格式为ID name cpp_grade
生成上述类并编写主函数,根据输入的学生基本信息,建立一个学生对象,计算其cpp总评成绩,并输出其学号、姓名、总评成绩。
输入格式: 测试输入包含若干测试用例,每个测试用例占一行(学生姓名 学号 年龄 cpp成绩 cpp考勤)。当读入0时输入结束,相应的结果不要输出。
输入样例:
Bob 10001 18 75.5 4
Mike 10005 17 95.0 5
0
输出样例:
10001 Bob 75.9
10005 Mike 95.5
#include
#include
#include
using namespace std;
class Person
{
protected:
string name;
int age;
public:
Person(){};
Person (string p_name, int p_age){name=p_name; age=p_age;};
~Person(){ }
void display () {cout<":"<class student:public Person
{ private:
int ID;//学号
float cpp_score;//cpp上机成绩
float cpp_count;//cpp上机考勤
float cpp_grade;//cpp总评成绩
public: //总评成绩计算规则:cpp_grade = cpp_score * 0.9 + cpp_count * 2;
student(){}
student(string n,int i,int a,float p,float q):Person(n,a)
{name=n;ID=i;age=a;cpp_score=p;cpp_count=q;cpp_grade=p*0.9+q*2;}
~student(){}
void print()
{
cout<" "<" "<1)<int main()
{ int a,i;string n;float p,q;
while(1)
{ cin>>n;
if(n=="0") return 0;
else{
cin>>i;
cin>>a;
cin>>p;
cin>>q;
student s(n,i,a,p,q);
s.print();
}
}
return 0;
}
7-2 验证手机号码(C++ Java)(10 分)
某系统在新用户注册时必须输入手机号,为了提高系统效率,防止输错手机号,需要对手机号进行验证。 验证规则为: (1)长度为11位 (2)由数字0~9组成 (3)必须是1开头 以上3个条件同时满足,则验证通过,否则为不通过。
输入格式:
在一行中一个字符串,长度不超过50个字符。例如: 13802988920
输出格式:
如果验证通过则输出Yes,否则输出No。
输入样例:
13812345678
输出样例:
Yes
#include
#include
#include
using namespace std;
int main(){
int i,j,tag=0,m;
char a[50];
gets(a);
m=strlen(a);
for(i=0;iif(!(a[i]>='0' && a[i]<='9')){
tag=1;
break;
}
}
if(m==11 && a[0]=='1' && tag==0){
cout<<"Yes"<else cout<<"No"<
6-3 一个简单的队列类模板(10 分)
请按照下列简单的整数队列类创建一个简单的队列类模板。
整数队列类如下:
const int SIZE=100;
//整数队列类
class Queue {
int q[SIZE];
int front; //队列头
int rear; //队列尾
public:
Queue( )
{ front = rear = 0; }
void put(int i); // 在队尾放置一个数据
int get( ); // 从队列头部取一个数据
};
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
#include
#include
using namespace std;
// 你提交的代码将嵌入到这里
int main()
{
Queue<int> a; // 创建一个整数队列
int m,n;
cin>>m>>n;
a.put(m);
a.put(n);
cout << a.get() << " ";
cout << a.get() << endl;
Queue<double> d; // 创建一个双精度浮点数队列
double x,y;
cin>>x>>y;
d.put(x);
d.put(y);
cout << d.get() << " ";
cout << d.get() << endl;
Queue<string> qs;// 创建一个字符串队列
string s1,s2,s3;
cin>>s1>>s2>>s3;
qs.put(s1);
qs.put(s2);
qs.put(s3);
cout << qs.get() << " ";
cout << qs.get() << " ";
cout << qs.get() << endl;
return 0;
}
输入样例:
6 9
3.14159 2.781828
ShenZhen Beijing HangZhou
输出样例:
6 9
3.14159 2.78183
ShenZhen Beijing HangZhou
const int SIZE=100;
//创建队列模板
template class Queue{
T q[SIZE];
int front,rear;
public:
Queue(){front=rear=0;}
void put(T i);
T get();
};
//在队尾放置元素
template void Queue::put(T i)
{
if(front==SIZE) exit(0);
rear++;
q[rear]=i;
}
//在队头取元素
template T Queue::get()
{
if(rear==front) return 0;
front++;
return q[front];
}
6-2 编写一个函数模板 sort 实现选择法排序的功能(10 分)
编写一个函数模板 sort 实现选择法排序的功能。
函数模板实例化:
Mysort(a,m);
其中 a 和 n 都是用户传入的参数。 a 表示数组名; m 表示数组元素的个数。
裁判测试程序样例:
#include
using namespace std;
//你提交的代码将被嵌入到这里
int main()
{
int m,n,a[20],i,j;
float b[20];
cin>>m;
for(i=0;icin>>a[i];
cin>>n;
for(j=0;jcin>>b[j];
Mysort(a,m);
Mysort(b,n);
for(i=0;icout<' ';
cout<for(j=0;jcout<' ';
cout<return 0;
}
输入样例:
在这里填写一组输入
6
1 4 2 6 3 5
5
1.5 1.2 1.1 1.3 1.4
输出样例:
在这里填写相应的输出
1 2 3 4 5 6
1.1 1.2 1.3 1.4 1.5
template <typename T> void Mysort(T a[],int m)
{
T temp;int i,j,k;
for(i=0;ifor(j=i+1;jif(a[j]if(i!=k) {temp=a[i];a[i]=a[k];a[k]=temp;}
}
}
7-1 人名地名排序(10 分)
从键盘输入若干人名、地名或者国家名,要求按照升序排序之后输出。
输入格式:
7(表示将输入7个人名或者地名)
Zhang3
Li4
Wang5
Ma6
Chen7
Shu8
Ren9
输出格式:
Chen7
Li4
Ma6
Ren9
Shu8
Wang5
Zhang3
输入样例:
5
Xi’an
HanZhong
BaoJi
Yan’an
WeiNan
输出样例:
BaoJi
HanZhong
WeiNan
Xi’an
Yan’an
#include
#include
#include
#include
using namespace std;
void load(vector<string>&,int );
void print(vector<string>,int);
int main()
{ int n;
cin>>n;
vector<string>v(n);
load(v,n);
sort(v.begin(),v.end());
print(v,n);
return 0;
}
void load(vector<string>&v,int n)
{ int i;
for(i=0;icin>>v[i];
}
void print(vector<string> v,int n)
{ int i;
for(i=0;icout<
7-2 对字符串进行排序输出(10 分)
给定一个字符串,对该字符串进行排序,请输出排好序的字符串。要求能够连续输入输出的字符串。
输入格式:
在一行输入一个字符串
输出格式:
输出排好序的字符串的序列
输入样例:
fecbad
输出样例:
abcdef
#include
#include
#include
#include
#include
using namespace std;
int main()
{
string s;
cin>>s;
sort(s.begin(),s.end());
cout<return 0;
}
7-1 查找成绩并折算后输出(10 分)
文件:期中考试成绩.txt中有若干学生的姓名和数学期中考试成绩。 Smith 67 Anderson 75 Lewis 83 Cook 58 David 96 请你编写一个简单的查询成绩程序,当从键盘输入一个姓名时查找到他的数学期中考试分数并按照21%折算后输出。如果没找到则显示Not found. 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和成绩,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其成绩。
输入格式:
Smith 67
Anderson 75
Lewis 83
Cook 58
David 96
noname (表示结束)
Bill
输出格式:
Not found.
输入样例:
Smith 67
Anderson 75
Lewis 83
Cook 58
David 96
noname
Lewis
输出样例:
17.43
#include
#include
#include
using namespace std;
int main()
{
map<string,int> m;
string a;int b;
while(1)
{
cin>>a;
if(a=="noname") break;
cin>>b;
m.insert(pair<string,int>(a,b));
}
string s;
cin>>s;
map<string,int>::iterator p;
p=m.find(s);
if(p!=m.end())
cout<<(p->second)*0.21<else cout<<"Not found."<return 0;
}
7-2 电话号码同步(10 分)
文件phonebook1.txt和phonebook2.txt中有若干联系人的姓名和电话号码。请你设计一个程序,将这两个文件中的电话号码同步。(所谓同步,就是将两个文件中的电话号码合并后剔除相同的人名和电话号码。请将同步后的电话号码按照姓名拼音顺序排序后保存到文件phonebook3.txt中。)
由于目前的OJ系统暂时不能支持用户读入文件和写文件,我们编写程序从键盘输入文件中的姓名和电话号码,当输入的单词为end时,表示文件结束。将同步后的电话号码按照姓名拼音顺序排序后输出。
输入格式:
张三 13012345678
李四 13112340000
王五 13212341111
马六 13312342222
陈七 13412343333
孙悟空 13512345555
end (表示文件phonebook1.txt结束) 张三 13012345678
孙悟空 13512345555
王五 13212341111
陈七 13412343333
唐三藏 13612346666
猪悟能 13712347777
沙悟净 13812348888
end (表示文件phonebook2.txt结束)
输出格式:
陈七 13412343333
李四 13112340000
马六 13312342222
沙悟净 13812348888
孙悟空 13512345555
唐三藏 13612346666
王五 13212341111
张三 13012345678
猪悟能 13712347777
#include
#include
#include
#include
#include
using namespace std;
int main()
{
set<string> A;
string s1;
set<string>::iterator p;
for(int i=0;i<2;i++)
{
getline(cin,s1);
while(s1!="end")
{
A.insert(s1);
getline(cin,s1);
}
}
p=A.begin();
while(p!=A.end())
cout<<*p++<
6-1 使用成员函数重载复数类的运算符+(4 分)
类Complex声明了一个复数类,有两个数据成员realPart(代表复数的实部)和imgPart(代表复数的虚部),并定义了成员函数实现了重载运算符“+”以实现两个复数对象的相加操作。成员函数Show用来输出复数的实部和虚部。请完成对运算符“+”的重载操作。
函数接口定义:
Complex& Complex::operator+(Complex& com);
参数com为复数类Complex的对象的引用,函数的返回值为当前对象与com对象相加后的值。
裁判测试程序样例:
#include
using namespace std;
class Complex {
public:
Complex(double realPart = 0, double imgPart = 0) {
this->realPart = realPart;
this->imgPart = imgPart;
}
Complex& operator+(Complex& com);
void Show() {
cout << realPart << " " << imgPart << endl;
}
private:
double realPart, imgPart;
};
int main() {
int r1, i1; //第1个复数对象的实部和虚部
int r2, i2; //第1个复数对象的实部和虚部
cin >> r1 >> i1;
cin >> r2 >> i2;
Complex c1(r1, i1); //构造第1个复数对象c1
Complex c2(r2, i2); //构造第2个复数对象c2
c1 = c1 + c2;
c1.Show();
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3 4
10 20
输出样例:
13 24
Complex& Complex::operator+(Complex& com)
{
com.realPart+=realPart;
com.imgPart+=imgPart;
return com;
}
6-2 单目运算符重载(时钟类)(15 分)
本题已给出时钟类及其成员函数实现,要求补充完整运算符++重载函数(前置和后置),使之能够实现时钟对象自增1秒。
时钟类定义如下:
class Clock {
public:
Clock(int NewH=0, int NewM=0, int NewS=0);
void ShowTime();
friend Clock operator++(Clock& op); //前置单目运算符重载
friend Clock operator++(Clock& op,int); //后置单目运算符重载
private:
int Hour, Minute, Second;
};
裁判测试程序样例:
#include
using namespace std;
class Clock {
public:
Clock(int NewH=0, int NewM=0, int NewS=0);
void ShowTime();
friend Clock operator++(Clock& op); //前置单目运算符重载
friend Clock operator++(Clock& op,int); //后置单目运算符重载
private:
int Hour, Minute, Second;
};
Clock::Clock(int NewH, int NewM, int NewS) {
Hour=NewH;
Minute=NewM;
Second=NewS;
}
void Clock::ShowTime() {
cout<":"<":"</*---------请在这里填写答案-----------*/
int main() {
int h, m, s;
cin>>h>>m>>s;
Clock a(h,m,s);
(++a).ShowTime();
(a++).ShowTime();
a.ShowTime();
return 0;
}
输入样例:
在这里给出一组输入。例如:
10 10 10
输出样例:
在这里给出相应的输出。例如:
10:10:11
10:10:11
10:10:12
Clock operator++(Clock& op) //前置单目运算符重载
{
op.Second++;
if(op.Second==60)
{
op.Minute++;
op.Second=0;
if(op.Minute==60)
{
op.Minute=0;
op.Hour++;
if(op.Hour==24)
op.Hour=0;
}
}
return op;
}
Clock operator++(Clock& op,int) //后置单目运算符重载
{
Clock t=op;
op.Second++;
if(op.Second==60)
{
op.Minute++;
op.Second=0;
if(op.Minute==60)
{
op.Minute=0;
op.Hour++;
if(op.Hour==24)
op.Hour=0;
}
}
return t;
}
6-1 使用成员函数重载复数类的运算符+(4 分)
类Complex声明了一个复数类,有两个数据成员realPart(代表复数的实部)和imgPart(代表复数的虚部),并定义了成员函数实现了重载运算符“+”以实现两个复数对象的相加操作。成员函数Show用来输出复数的实部和虚部。请完成对运算符“+”的重载操作。
函数接口定义:
Complex& Complex::operator+(Complex& com);
参数com为复数类Complex的对象的引用,函数的返回值为当前对象与com对象相加后的值。
裁判测试程序样例:
#include
using namespace std;
class Complex {
public:
Complex(double realPart = 0, double imgPart = 0) {
this->realPart = realPart;
this->imgPart = imgPart;
}
Complex& operator+(Complex& com);
void Show() {
cout << realPart << " " << imgPart << endl;
}
private:
double realPart, imgPart;
};
int main() {
int r1, i1; //第1个复数对象的实部和虚部
int r2, i2; //第1个复数对象的实部和虚部
cin >> r1 >> i1;
cin >> r2 >> i2;
Complex c1(r1, i1); //构造第1个复数对象c1
Complex c2(r2, i2); //构造第2个复数对象c2
c1 = c1 + c2;
c1.Show();
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3 4
10 20
输出样例:
13 24
Complex& Complex::operator+(Complex& com)
{
com.realPart+=realPart;
com.imgPart+=imgPart;
return com;
}
6-2 对学生对象按照成绩升序排序(10 分)
下面这段代码要实现对学生对象按照成绩升序排序。 仔细阅读代码,要求实现编程实现输出运算符“<<”和小于“<”运算符,使本程序能完成预定的排序功能。
裁判测试程序样例:
#include
#include
#include
using namespace std;
class Student {
string name;
char sex;
int score;
string grade;
public:
Student(string name, char sex, int score, string grade);
friend ostream &operator<< (ostream& os, Student st) ;
friend bool operator<(Student &st1, Student &st2);
};
//你提交的代码将被嵌入到这里
Student::Student(string name, char sex, int score, string grade) {
this->name = name;
this->sex = sex;
this->score = score;
this->grade = grade;
}
int main() {
list st;
string name, grade;
char sex; int score;
for(int i = 0; i < 5; i++) {
cin>>name; cin>>sex;
cin>>score; cin>>grade;
st.push_back(Student(name, sex, score, grade));
}
st.sort();
list ::iterator p = st.begin();
while(p != st.end()) {
cout<<*p;
p++;
}
return 0;
}
输入样例:
Bill M 86 JK1501
David M 98 JK1502
Mary F 78 JK1503
Adam M 83 JK1504
Rose F 96 JK1505
输出样例:
Mary F 78 JK1503
Adam M 83 JK1504
Bill M 86 JK1501
Rose F 96 JK1505
David M 98 JK1502
ostream &operator<< (ostream& os, Student st)
{
os<<st.name<<" "<<st.sex<<" "<<st.score<<" "<<st.grade<;
}
bool operator<(Student &st1, Student &st2)
{
if(st1.score.score)
return true;
return false;
}
6-3 虚函数的应用(15 分)
补充下列代码,使得程序的输出为:
A:3
A:15
B:5
3
15
5
类和函数接口定义:
参见裁判测试程序样例中的类和函数接口。
裁判测试程序样例:
#include
using namespace std;
class CMyClassA {
int val;
public:
CMyClassA(int);
void virtual print();
};
CMyClassA::CMyClassA(int arg) {
val = arg;
printf("A:%d\n", val);
}
void CMyClassA::print() {
printf("%d\n", val);
return;
}
/* 在这里填写代码 */
int main(int argc, char** argv) {
CMyClassA a(3), *ptr;
CMyClassB b(5);
ptr = &a;
ptr->print();
a = b;
a.print();
ptr = &b;
ptr->print();
return 0;
}
输入样例:
None
输出样例:
A:3
A:15
B:5
3
15
5
来源:
openjudge.cn
class CMyClassB:public CMyClassA
{
public:
int val2;
CMyClassB(int x):CMyClassA(3*x)
{
val2=x;
printf("B:%d\n",val2);
}
void print()
{
printf("%d\n",val2);
}
};
6-1 学生成绩的输入和输出(运算符重载)(10 分)
现在需要输入一组学生的姓名和成绩,然后输出这些学生的姓名和等级。
输入时,首先要输入学生数(正整数)N。接着输入N组学生成绩,每组成绩包括两项:第一项是学生姓名,第二项是学生的成绩(整数)。
输出时,依次输出各个学生的序号(从1开始顺序编号),学生姓名,成绩等级(不小于60为PASS,否则为FAIL)
函数接口定义:
面向Student类对象的流插入和流提取运算符
裁判测试程序样例:
#include
#include
using namespace std;
/* 请在这里填写答案 */
int main(){
int i, repeat;
Student st;
cin>>repeat;
for(i=0;icin>>st;
cout<return 0;
}
输入样例:
3
Li 75
Zhang 50
Yang 99
输出样例:
class Student
{
private:
string name;
int score;
string level;
static int n;
public:
Student(string n="noname",int score=0){ }
~Student(){}
friend istream &operator >>(istream &input,Student &stu)
{
input>>stu.name>>stu.score;
if(stu.score<60) stu.level="FAIL";
else stu.level="PASS";
return input;
}
friend ostream &operator <<(ostream &output,Student &stu)
{ n=n+1;
output<". " <" "<return output;
}
};
int Student::n=0;
7-1 数字格式异常(10 分)
(NumberFormatException数字格式异常)编写一个程序,提示用户读取两个整数,然后显示他们的和。程序应该在输入不正确时提示用户再次输入数字。
输入格式:
i 9 (第1次输入) l 8 (第2次输入) 5 6 (第3次输入)
输出格式:
Incorrect input and re-enter two integers: (第1次输出提示) Incorrect input and re-enter two integers: (第2次输出提示) Sum is 11 (输出结果)
输入样例:
i 9
l 8
5 6
#include
#include
using namespace std;
int f;
int toNumb(char co[])
{
int len=strlen(co);
int i;
int sum=0;
int w=1;
for(i=len-1;i>=0;i--)
{
if(!(co[i]<='9' && co[i]>='0'))
{
f=1;
return 0;
}
sum+=w*(co[i]-'0');
w*=10;
}
return sum;
}
int main ()
{
string a,b;
while(1)
{
cin>>a>>b;
f=0;
int m=toNumb(&a[0]);
int n=toNumb(&b[0]);
if(f)
{
cout<<"Incorrect input and re-enter two integers:"<continue;
}
else
{
cout<<"Sum is "<break;
}
}
}
6-1 学生成绩的快速录入(构造函数)(10 分)
现在需要录入一批学生的成绩(学号,成绩)。其中学号是正整数,并且录入时,后录入学生的学号会比前面的学号大;成绩分两等,通过(Pass,录入时用1代表),不通过(Fail,录入时用0代表)。
由于很多学号都是相邻的,并且学号相邻的学生成绩常常相同。所以在录入时,适当地加了速。如果当前学生的学号比前面的学号大1,且成绩与前面的成绩相同,则只输入0即可。
类定义:
完成Student类
裁判测试程序样例:
#include
using namespace std;
/* 请在这里填写答案 */
int main(){
const int size=100;
int i, N, no, score;
Student *st[size];
cin>>N;
for(i=0; icin>>no;
if(no>0){
cin>>score;
st[i]=new Student(no, score);
}
else
st[i]=new Student(*st[i-1]);
}
cout<" Students"<for(i=0;idisplay();
for(i=0;idelete st[i];
return 0;
}
输入样例:
5
3 0
0
7 1
0
12 1
输出样例:
5 Students
3 Fail
4 Fail
7 Pass
8 Pass
12 Pass
class Student
{
int id;
int score;
public:
Student(int n,int s)
{
id=n;score=s;count++;
}
Student(Student &st)
{
id=st.id+1;
score=st.score;
count++;
}
~Student()
{
}
void display()
{
cout<if(score==0) cout<<" Fail"<else cout<<" Pass"<static int count;
};
int Student::count=0;
6-2 有序数组(类模板)(10 分)
实现一个类模板,它可以接受一组数据,能对数据排序,也能输出数组的内容。
每行输入的第一个数字为0,1,2或3:为0时表示输入结束; 为1时表示将输入整数,为2时表示将输入有一位小数的浮点数,为3时表示输入字符。
如果第一个数字非0,则接下来将输入一个正整数,表示即将输入的数据的数量。
从每行第三个输入开始,依次输入指定类型的数据。
类模板:
template <class T>
class MyArray
裁判测试程序样例:
#include
using namespace std;
/* 请在这里填写答案 */
template<class T>
MyArray::~MyArray(){ delete[] data;}
template<class T>
bool MyArray::check(){
int i;
for(i=0;i1;i++)
if(data[i]>data[i+1]) { cout<<"ERROR!"<return false;}
return true;
}
int main( )
{
MyArray<int> *pI;
MyArray<float> *pF;
MyArray<char> *pC;
int ty, size;
cin>>ty;
while(ty>0){
cin>>size;
switch(ty){
case 1: pI = new MyArray<int>(size); pI->sort(); pI->check(); pI->display(); delete pI; break;
case 2: pF = new MyArray<float>(size); pF->sort(); pF->check(); pF->display(); delete pF; break;
case 3: pC = new MyArray<char>(size); pC->sort(); pC->check(); pC->display(); delete pC; break;
}
cin>>ty;
}
return 0;
}
输入样例:
1 3 2 3 1
2 4 1.5 2.6 3.7 0.5
3 2 A a
0
输出样例:
1 2 3
0.5 1.5 2.6 3.7
A a
#include
using namespace std;
template<class T>
class MyArray
{ T *data;
int size;
public:
MyArray(int s)
{size=s;data=new T[size];int i;
for(i=0;icin>>data[i];
}
void sort();
bool check();
~MyArray();
void display();
} ;
template <class T>
void MyArray::sort()
{ int i,j;
T temp;
for(i=0;i1;i++)
for(j=i+1;jif(data[i]>data[j])
{
temp=data[j];
data[j]=data[i];
data[i]=temp;
}
}
template<class T>
void MyArray::display()
{ int i;
for(i=0;icout<if(i1)
cout<<" ";
}
cout<template<class T>
MyArray::~MyArray(){ delete[] data;}
template<class T>
bool MyArray::check(){
int i;
for(i=0;i1;i++)
if(data[i]>data[i+1]) { cout<<"ERROR!"<return false;}
return true;
}
int main( )
{
MyArray<int> *pI;
MyArray<float> *pF;
MyArray<char> *pC;
int ty, size;
cin>>ty;
while(ty>0){
cin>>size;
switch(ty){
case 1: pI = new MyArray<int>(size); pI->sort(); pI->check(); pI->display(); delete pI; break;
case 2: pF = new MyArray<float>(size); pF->sort(); pF->check(); pF->display(); delete pF; break;
case 3: pC = new MyArray<char>(size); pC->sort(); pC->check(); pC->display(); delete pC; break;
}
cin>>ty;
}
return 0;
}