C++ pta编程题
6-2 类的声明和成员函数的实现–this指针 (10 分)
本题要求声明和实现一个Car类,包括实现其成员函数。
要求如下:
类和函数接口定义:
- 声明一个Car类;
- 三个public成员函数:
(1) disp_welcomemsg(),无返回类型;
(2) get_wheels(),返回一个Car类的数据成员m_nWheels的值;
(3) set_wheels(int),用指定的形参初始化数据成员m_nWheels的值; - 一个私有数据成员m_nWheels,整数类型,代表汽车的车轮数量。
其中,成员函数disp_welcomemsg()显示一条欢迎信息“Welcome to the car world!”。 成员函数get_wheels()返回Car类的私有数据成员m_nWheels。 成员函数set_wheels(int)用指定的形参初始化数据成员m_nWheels。
#include <iostream>
using namespace std;
/* 请在这里填写答案 */
int main()
{
int n;
cin >> n;
Car myfstcar, myseccar; //定义类对象
myfstcar.disp_welcomemsg();//访问成员函数,显示欢迎信息
myfstcar.set_wheels(n); //访问成员函数,设置车轮数量
myseccar.set_wheels(n+4); //访问成员函数,设置车轮数量
//访问成员函数,显示车轮数量
cout << "my first car wheels num = " << myfstcar.get_wheels() << endl;
cout << "my second car wheels num = " << myseccar.get_wheels() << endl;
return 0;
}
class Car{
int m_nWheels;
public:
void disp_welcomemsg()
{
cout<<"Welcome to the car world!"<<endl;
}
int get_wheels()
{
return m_nWheels;
}
void set_wheels(int n)
{
m_nWheels=n;
}
};
6-11 定义有静态成员的C++学生类Student(10分) (10 分)
本程序中学生Student类中有学号 number,姓名 name,成绩 score 等数据成员,另外有静态变量:学生对象个数 count 和总分sum。静态成员函数average( )用来计算学生的平均分。
Student类构造函数的原型如下:
Student(int number1, String name1, float score1);
/* 请在这里填写答案 */
int main( )
{
// Student::init( );
Student stu1(1,"Bill",87);
stu1.print( );
Student stu2(2,"Adam",91);
stu2.print( );
Student stu3(3,"David",96);
stu3.print( );
Student::average( ); //静态成员函数的调用
return 0;
}
#include<iostream>
//#include<string>
using namespace std;
class Student
{
public: //成员变量
int number;
string name;
float score;
static int count; //静态成员变量 的类内声明
static float sum;
public:
Student(int number1, string name1, float score1)
{
number=number1;
name=name1;
score=score1;
//因为静态成员变量是所有对象共享
//因此count和sum在每定义一个对象之后都进行操作是有效的
count++;
sum = sum+score;
}
static float average() //静态成员函数 只能 访问静态成员变量
{
cout << "sum is " << sum << endl;
cout << "count is " << count << endl;
cout << "average is " << sum/count<< endl;
}
void print()
{
cout << "number: " << number << " name: " << name << " score: " << score << " count: " << count << endl;
}
};
//静态成员变量 必须 类外初始化
int Student::count=0;
float Student::sum=0;
6-12 2017final友元函数之全班同学的平均绩点 (10 分)
一个学生类,有三个私有成员:名字name、课程学分指针score、课程成绩指针grade。定义一个友元函数,求全班同学的平均绩点。单门课程的学分绩点=学分绩点=学分(成绩/10-5) ; 全班同学的平均绩点是 所有同学的全部课程的学分绩点之和/所有同学学分数之和。单个同学的课程数不超过100门。全班同学人数不超过100名。
输入说明:
输入若干行。
每行一个学生的信息:第一个输入是学生的名字,第二个输入是第一门课程的学分,第三个输入是第一门课程的成绩,第四个输入是第二门课程的学分,第五个输入是第二门课程的成绩,以此类推,最后以-1表示该行输入结束。每个学生的课程数不超过100门。
最后以 no 表示输入结束。
输出一行,即该全班同学的平均绩点。
函数接口定义:
这是求全部同学平均绩点的友元函数的声明:
friend double averagegrade(student *stu, int count)
#include<iostream>
#include<string>
using namespace std;
class student{
private:
double *grade;
double *score;
string name;
public:
student( )
{
grade=NULL;
score=NULL;
}
student(string n, double *g, double *s)
{
name=n;
grade=g;
score=s;
}
friend double averagegrade(student *stu, int count);
};
/* 请在这里填写答案 */
int main()
{
student stu[100];
double s[100][100], g[100][100];
int count=0;
string n;
for(int i=0;i<100;i++)
{
cin>>n;
if(n=="no") break;
count++;
for(int j=0;j<100;j++)
{
cin>>s[i][j];
if(s[i][j]==-1) break;
cin>>g[i][j];
}
stu[i]=student(n, g[i], s[i]);
}
cout<<averagegrade(stu, count);
return 0;
}
double averagegrade(student *stu, int count)
{
int i,j;
double sum1=0,sum2=0,sum;
for(i==0;i<count;i++)
for(j=0;stu[i].score[j]!=-1;j++)
{
sum1+=stu[i].score[j]*(stu[i].grade[j]/10-5);
sum2+=stu[i].score[j];
}
if(sum2==0||sum1==0)
return 0;
return sum1/sum2;
}
7-1 求两点之间距离 (10 分)
定义一个Point类,有两个数据成员:x和y, 分别代表x坐标和y坐标,并有若干成员函数。 定义一个函数Distance(), 用于求两点之间的距离。
#include<iostream>
#include<cmath>
#include<stdio.h>
using namespace std;
class Point{
private:
double x,y;
public:
Point(double x,double y)
{
this->x = x;
this->y = y;
}
double Getx()
{
return x;
}
double Gety()
{
return y;
}
double Distance(const Point &p) //定义拷贝构造函数
{
x -= p.x;
y -= p.y;
return sqrt(x*x+y*y);
}
void ShowPoint()
{
cout << "<" << Getx() << "," << Gety() << ">" << endl;
}
};
int main()
{
double x1,y1,x2,y2;
double x;
cin >> x1 >> y1 >> x2 >> y2;
Point P1(x1,y1);
Point P2(x2,y2);
x=P1.Distance(P2);
cout.precision(2);
cout.setf(ios::fixed);
cout << x << endl;
return 0;
}
6-1 对象指针与对象数组(拉丁舞) (7 分)
对象指针与对象数组(拉丁舞)
怡山小学毕业文艺晚会上,拉丁舞是最受欢迎的节目。不过,每年为了排练这个节目,舞蹈组都会出现一些纠纷。有些同学特别受欢迎,有些却少人问津,因此安排舞伴成为舞蹈组陈老师最头疼的问题。
为了解决这一问题,今年陈老师决定让按先男生后女生,先低号后高号的顺序,每个人先报上自己期待的舞伴,每人报两位,先报最期待的舞伴。接下来按先男生后女生,先低号后高号的顺序,依次按以下规则匹配舞伴:
(1)每个人均按志愿顺序从前到后确定舞伴。如果第一志愿匹配不成功,则考虑第二志愿。
(2)如果A的当前志愿为B,则如果B未匹配舞伴,且有以下情形之一者,A和B匹配成功:
2a) B的期待名单中A。
2b) B的期待名单中没有A,但B期待的两位舞伴均已匹配成功,所以B只能与A凑合。
输入时先输入男生数m, 接下来m行,第一项为学生的姓名,后两项为期待舞伴的编号,编号从0开始,最大为女生数减1。接下来输入女生数f,接下来f行,第一项为学生的姓名,后两项为期待舞伴的编号,编号从0开始,最大为男生数减1。
输出时按男生的编号顺序输出 姓名:舞伴姓名
注意两个姓名间有冒号隔开
#include <iostream>
#include <string>
using namespace std;
const int K=2;
const int N=20;
class Student{
string name;
Student *welcome[K];
Student *pair;
public:
void init(string &name, Student *a, Student *b) {
this->name=name;
welcome[0]=a;
welcome[1]=b;
pair=NULL;
}
void printPair();
void addPair();
};
/* 请在这里填写答案 */
int main(){
Student male[N], female[N];
int m, f, i, j, a, b;
string name;
cin>>m;
for(i=0;i<m;i++){
cin>>name>>a>>b;
male[i].init(name, &female[a], &female[b]);
}
cin>>f;
for(i=0;i<f;i++){
cin>>name>>a>>b;
female[i].init(name, &male[a], &male[b]);
}
for(i=0;i<m;i++) male[i].addPair();
for(i=0;i<f;i++) female[i].addPair();
for(i=0;i<m;i++) male[i].printPair();
return 0;
}
void Student::addPair()
{
if(this->pair!=NULL)return;
for(int i=0;i<2;i++)
{
if(this->welcome[i]->pair!=NULL)continue;
for(int j=0;j<2;j++)
{
if(this->welcome[i]->welcome[j]==this)
{
this->pair=this->welcome[i];
this->welcome[i]->pair=this;
return;
}
}
for(int j=0;j<2;j++) //如果期待的名单上未匹配成功,则考虑凑合情况
{
if(this->welcome[j]->welcome[0]->pair!=NULL && this->welcome[j]->welcome[1]->pair!=NULL && this->welcome[j]->pair==NULL)
{
this->pair=this->welcome[j];
this->welcome[j]->pair=this;
return;
}
}
}
}
void Student::printPair()
{
if(this->pair)
cout<<this->name<<":"<<this->pair->name<<endl;
}
6-1 从shape类派生出一个正六边形类RHexagon (10 分)
从下列的抽象类shape类派生出一个正六边形(regular hexagon)RHexagon类。RHexagon类将正六边形的边长作为私有成员,类中包含初始化这个值的构造函数。
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
#include <iostream>
#include <cmath>
using namespace std;
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
//你提交的代码将嵌入到这里
int main()
{
double s;
cin>>s;
RHexagon p(s);
cout<<p.getArea()<<endl;
cout<<p.getPerimeter()<<endl;
}
class RHexagon:public shape {
private :
double length;
public:
RHexagon(double r):length(r){
}
double getArea(){
return 3*sqrt(3)*length*length/2;
}
double getPerimeter(){
return 6*length;
}
};
6-2 派生类的定义和使用 (10 分)
按要求完成下面的程序:
1、定义一个Animal类,包含一个void类型的无参的speak方法,输出“animal language!”。
2、定义一个Cat类,公有继承自Animal类,其成员包括:
(1)私有string类型的成员m_strName;
(2)带参数的构造函数,用指定形参对私有数据成员进行初始化;
(3)公有的成员函数print_name,无形参,void类型,功能是输出成员m_strName的值,具体输出格式参见main函数和样例输出。
#include <iostream>
#include <string>
using namespace std;
/* 请在这里填写答案 */
int main()
{
Cat cat("Persian"); //定义派生类对象
cat.print_name(); //派生类对象使用本类成员函数
cat.speak(); //派生类对象使用基类成员函数
return 0;
}
class Animal
{
private:
public:
void speak()
{
cout<<"animal language!";
}
};
class Cat:public Animal
{
private:
string m_strName;
public:
Cat(string name)
{m_strName=name;}
string Getname()
{return m_strName;}
void print_name()
{cout<<"cat name: "<<Getname()<<endl;}
};
6-3 学生派生类 (10 分)
根据所给的类Student定义其派生类,并利用构造函数进行数据初始化,使程序能按照"样例"的格式进行输出
#include <iostream>
#include<string>
using namespace std;
class Student
{public:
Student(int n,string nam,char s )
{num=n;
name=nam;
sex=s; }
~Student( ) { }
protected:
int num;
string name;
char sex ;
};
/* 请在这里添加派生类定义 */
int main( )
{Student1 stud1(10010,"Wang-li",'f',19,"115 Beijing Road,Shanghai");
Student1 stud2(10011,"Zhang-fun",'m',21,"213 Shanghai Road,Beijing");
stud1.show( );
stud2.show( );
return 0;
}
class Student1:public Student
{
protected:
int age;
string address;
public:
//派生类后括号内传入基类与派生类的数据,传递值(划重点!!!)
Student1(int n,string nam,char s,int a,string ad):Student(n,nam,s),age(a),address(ad){}
~Student1(){}
void show()
{
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
cout<<"age: "<<age<<endl;
cout<<"address: "<<address<<'\n'<<endl;
}
};
6-1 使用成员函数重载复数类的运算符+ (4 分)
类Complex声明了一个复数类,有两个数据成员realPart(代表复数的实部)和imgPart(代表复数的虚部),并定义了成员函数实现了重载运算符“+”以实现两个复数对象的相加操作。成员函数Show用来输出复数的实部和虚部。请完成对运算符“+”的重载操作。
#include<iostream>
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;
}
/* 你的代码将被嵌在这里 */
Complex& Complex::operator+(Complex& com){
com.imgPart=com.imgPart+this->imgPart;
com.realPart=com.realPart+this->realPart;
return com;
}
6-2 单目运算符重载(时钟类) (15 分)
本题已给出时钟类及其成员函数实现,要求补充完整运算符++重载函数(前置和后置),使之能够实现时钟对象自增1秒。
#include<iostream>
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<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
/*---------请在这里填写答案-----------*/
int main() {
int h, m, s;
cin>>h>>m>>s;
Clock a(h,m,s);
(++a).ShowTime();
(a++).ShowTime();
a.ShowTime();
return 0;
}
Clock operator++(Clock& op) {
op.Second++;
if (op.Second == 60) {
op.Second = 0;
op.Minute++;
}
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.Second = 0;
op.Minute++;
}
if (op.Minute == 60) {
op.Minute = 0;
op.Hour++;
}
if (op.Hour == 24) {
op.Hour = 0;
}
return t;
}
6-11 从抽象类shape类派生出一个正五边形类(C++) (10 分)
从下列的抽象类shape类派生出一个正五边形(regular pentagon)类RPentagon,这个类将正五边形的边长作为私有成员,类中包含初始化这个值的构造函数。
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
#include <iostream>
#include <cmath>
using namespace std;
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
//你提交的代码将嵌入到这里
int main()
{
double s;
cin>>s;
RPentagon p(s);
cout<<p.getArea()<<endl;
cout<<p.getPerimeter()<<endl;
}
class RPentagon: public shape{
private:
double a;//定义边长
public:
RPentagon(double a1)
{
a = a1;
}
double getArea() // 求面积
{
return pow(a,2)*(sqrt(25+10*sqrt(5)))/4;
}
double getPerimeter() // 求周长
{
return 5*a;
}
};
6-12 从shape类派生出一个直角三角形类RTriangle (10 分)
从shape类派生出一个直角三角形类RTriangle类(regular triangle)。两条直角边长作为RTriangle类的私有成员,类中包含参数为直角边的构造方法。
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
###直角三角形类名:RTriangle
直角三角形类的构造函数原型如下: RTriangle(double a, double b);
其中 a 和 b 都是直角三角形的两条直角边。
#include <iostream>
#include <cmath>
using namespace std;
class shape {// 抽象类
public:
virtual double getArea()=0;// 求面积
virtual double getPerimeter()=0; // 求周长
};
//你提交的代码将嵌入到这里
int main()
{
double a,b;
cin>>a>>b;
RTriangle t(a,b);
cout<<t.getArea()<<endl;
cout<<t.getPerimeter()<<endl;
}
class RTriangle:public shape{
private:
double a;
double b;
public:
RTriangle(double a1,double b1) {
a = a1;
b = b1;
}
double getArea() {
return 0.5 * a * b;
}
double getPerimeter() {
return a + b + sqrt(a*a + b*b);
}
};