C++语言题库(三)—— PAT

目录

1. 打印点、圆、圆柱信息

2. 国际贸易统计

3. 设计一个类CRectangle

4. 定义一个时间类

5. 定义一个Date类

6. 定义一个Time类

7. 设计一个People类

8. 平均成绩

9. 计算若干个学生的总成绩及平均成绩

11. 使用面向对象的方法求长方形的周长


1. 打印点、圆、圆柱信息

定义平面二维点类CPoint,有数据成员x坐标,y坐标,函数成员(构造函数、虚函数求面积GetArea, 虚函数求体积函数GetVolume、输出点信息函数print。
由CPoint类派生出圆类Cirle类(新增数据成员半径radius),函数成员(构造函数、求面积GetArea,虚函数求体积函数GetVolume、输出圆面积信息函数print。
再由Ccirle类派生出圆柱体Ccylinder类(新增数据成员高度height),函数成员(构造函数、求表面积GetArea ,求体积函数GetVolume、输出圆柱体体积信息函数print。在主函数测试这个这三个类。
打印数据保留小数点后2位。

答案:

#include<iostream>
#define PI 3.14
using namespace std;

class CPoint{
    private:
        double x,y;
    public:
        void print();
        CPoint(double,double);
};
CPoint::CPoint(double x,double y){
    this->x=x;
    this->y=y;
}
void CPoint::print(){
    cout<<"CPoint:"<<x<<","<<y<<endl;
}
class Circle:public CPoint{
    private:
        double r;
    public:
        Circle(double,double,double);
        double GetR();
        double GetArea();
        void print() ;
};
Circle::Circle(double x,double y,double r):CPoint(x,y){
    this->r=r;
}
double Circle::GetArea(){
    return PI*r*r;
}
double Circle::GetR(){
    return r;
} 
void Circle::print(){
    printf("CirleArea:%.2lf\n",GetArea());
}
class Ccylinder:public Circle{
    private:
        double h;
    public:
        Ccylinder(double,double,double,double);
        double GetArea();
        double GetVolume();
        void print();
};
Ccylinder::Ccylinder(double x,double y,double r,double h):Circle(x,y,r){
    this->h=h;
}
double Ccylinder::GetArea(){
    return PI*GetR()*GetR();
}
double Ccylinder::GetVolume(){
    return GetArea()*h;
}
void Ccylinder::print(){
    printf("CcylinderVolume:%.2lf\n",GetVolume());
}
int main(){
    double x,y,r,h;
    cin>>x>>y>>r>>h;
    CPoint c(x,y);
    Circle c1(x,y,r);
    Ccylinder c2(x,y,r,h);
    c.print();
    c1.print();
    c2.print();
    return 0;
}

2. 国际贸易统计

给出N个国家之间进行国际贸易的记录,请你统计这些国家进行国际贸易的收益。

答案:

#include<iostream>
using namespace std;
class country
{
    public:
    int num;
    int getings;
    int counts;
};
int main()
{
    int n;
    cin>>n;
    country cty[n+1];
    for(int i=1;i<n+1;i++)
    {
        cty[i].counts=0;
        cty[i].getings=0;
        cty[i].num=i;
    }
    for(int i=1;i<n+1;i++)
    {
        int *in=new int;
        cin>>*in;
        cty[i].counts+=*in;
        for(int j=0;j<*in;j++)
        {
            int *id=new int;
            cin>>*id;
            cty[*id].counts++;
            int *inin=new int;
            cin>>*inin;
            cty[i].getings+=*inin;
            cty[*id].getings-=*inin;
            delete id;
            delete inin;
        }
        delete in;
    }
    for(int i=1;i<n+1;i++)
    {
        for(int j=1;j<n;j++)
        {
            if(cty[j].getings==cty[j+1].getings&&cty[j].counts==cty[j+1].counts&&cty[j].num>cty[j+1].num)
            {
                country *p=new country;
                *p=cty[j];
                cty[j]=cty[j+1];
                cty[j+1]=*p;
                delete p;
            }
            else if(cty[j].getings==cty[j+1].getings&&cty[j].counts<cty[j+1].counts)
            {
                country *p=new country;
                *p=cty[j];
                cty[j]=cty[j+1];
                cty[j+1]=*p;
                delete p;
            }   
            else if(cty[j].getings<cty[j+1].getings)
            {
                country *p=new country;
                *p=cty[j];
                cty[j]=cty[j+1];
                cty[j+1]=*p;
                delete p;
            }    
        }
    }
    for(int i=1;i<n+1;i++)
    {
        cout<<cty[i].num<<" "<<cty[i].getings<<endl;
    }
    return 0;
}

3. 设计一个类CRectangle

设计一个类CRectangle,要求如下所述:
(1) 该类中的私有成员变量存放CRectangle的长和宽,并且设置它们的默认值为1.
(2) 通过成员函数设置其长和宽,并确保长和宽都在(0,50)范围之内。
(3) 求周长Perimeter

答案:

#include<iostream>
using namespace std;
 
class CRectangle{
public:
    void setlw();
    float getper();
private:
    float l, w;
};

void CRectangle::setlw(){
    cin >> l;
    if (l > 50)l = 1;
    cin >> w;
    if (w > 50)w = 1;
}

float CRectangle::getper(){
    return 2*(l+w);
}
 
int main(){
    CRectangle a;
    a.setlw();
    cout << a.getper();
}

4. 定义一个时间类

定义一个时间类Time,能提供和设置由时、分、秒组成的时间,并编写应用程序,定义时间对象,设置时间和输出该对象提供的时间。

答案:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
 
class Time{
public:
    void timing();
    void output();
private:
    int h, m, s;
};

void Time::timing(){
    cin>>h>>m>>s;
    if (m > 60){
        h+=1;
        m=m-60;}
    if (s > 60){
        m+=1;
        s=s-60;}
}

void Time::output()
{
cout<<setw(2)<<setfill('0')<<h<<"-"<<setw(2)<<setfill('0')<<m<<"-"<<setw(2)<<setfill('0')<<s<<endl; 
}

int main(){
    Time a;
    a.timing();
    a.output();
    return 0;
}

5. 定义一个Date类

定义一个满足如下要求的Date类
用下列的数据输出数据
年-月-日

答案:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
 
class Date{
public:
    void timing();
    void output();
private:
    int n, y, r;
};

void Date::timing(){
    cin>>n>>y>>r;
}

void Date::output()
{
cout<<setw(2)<<setfill('0')<<n<<"-"<<setw(2)<<setfill('0')<<y<<"-"<<setw(2)<<setfill('0')<<r<<endl; 
}

int main(){
    Date a;
    a.timing();
    a.output();
    return 0;
}

6. 定义一个Time类

定义一个时间类,能够提供和设置由时、分、秒组成的时间,并按照如下的格式输出时间:
08-09-24
12-23-59

答案:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
 
class TIME{
public:
    void timing();
    void output();
private:
    int n, y, r;
};

void TIME::timing(){
    cin>>n>>y>>r;
}

void TIME::output()
{
cout<<setw(2)<<setfill('0')<<n<<"-"<<setw(2)<<setfill('0')<<y<<"-"<<setw(2)<<setfill('0')<<r<<endl; 
}

int main(){
    TIME a;
    a.timing();
    a.output();
    return 0;
}

7. 设计一个People类

设计一个People 类,该类的数据成员有姓名、年龄、身高、体重和人数,其中人数为静态数据成员,成员函数有构造函数、显示和显示人数。其中构造函数由参数姓名、年龄、身高和体重来构造对象;显示函数用于显示人的姓名、年龄、身高和体重;显示人数函数为静态成员函数,用于显示总的人数。

答案:

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
class people{
private:
    string name;
    int age;
    int height;
    int weight;
    static int number;
public:
    people(string name, int age, int height, int weight){
        this->name = name;
        this->age = age;
        this->height = height;
        this->weight = weight;
    }
    void num_add()const{
        number++;
    }
    static void get_num(){
        cout << number << endl;
    }
};
int people::number = 0;
int main()
{
    string str;
    while (1){
        cin >> str;
        if (str == "exit")
            break;
        int age, height, weight;
        cin >> age >> height >> weight;
        people peo(str, age, height, weight);
        peo.num_add();
    }
    people::get_num();
    return 0;
}

8. 平均成绩

输入n个学生的姓名及其3门功课成绩,要求按输入的逆序逐行输出每个学生的姓名、3门课成绩和平均成绩。若有学生平均成绩低于60分,则不输出该学生信息。

答案:

#include<iostream>
#include<cstdio>
using namespace std;
struct student
{
    char name[20];
    int a;
    int b;
    int c;
    float avg;
};
int main()
{
    int n;
    while(1){
        cin>>n;
        student stu[n];
        for(int i = 0; i < n; i++){
        scanf("%s %d %d %d",stu[i].name, &stu[i].a,&stu[i].b,&stu[i].c);
            stu[i].avg = (stu[i].a+stu[i].b+stu[i].c)/3.0;
        }
        for(int i=n-1; i >= 0; i--){
            if(stu[i].avg >= 60){
                printf("%s %d %d %d %.2f\n",stu[i].name, stu[i].a,stu[i].b,stu[i].c,stu[i].avg);
            }
        }
        break;
    }
    return 0;
}

9. 计算若干个学生的总成绩及平均成绩

定义一个类Student,记录学生C++课程的成绩。要求使用静态数据成员或静态成员函数计算若干个学生C++课程的总成绩和平均成绩。

答案:

#include <iostream>
using namespace std;
class Student{
    protected:
    int studentscore;
    static int sum;
    static double avg;
    public:
    Student(){};
    Student(int a):studentscore(a){
        sum+=a;
        avg=sum/5;
    };
    void disp(){
        cout<<sum<<endl;
        cout<<avg;
    }
};
int Student::sum=0;
double Student::avg=0;
int main(){
    int score,count;cin>>count;
    Student *p;
    for(int i=0;i<count;i++){
        cin>>score;
        p=new Student(score);
    }
    p->disp();
    return 0;
}

11. 使用面向对象的方法求长方形的周长

本题目要求读入2个实数A和B,作为长方形的长和宽,计算并输出长方形的周长。

面向对象的方法,先要定义你要处理的数据类型——类,类就是现实中一个事物的抽象,在本题中就是长方形。

接着抽象长方形的属性和方法。

属性就是长方形用哪些数据来描述,要结合具体的应用场景,在本题中长方形有长和宽两个属性。属性就是类中的成员变量。

方法就是长方形具有哪些行为或者说可以对长方形进行哪些操作,要结合具体的应用场景,在本题中就是求长方形的周长。方法就是类中的成员函数。

类体现了面向对象的抽象和封装,将数据(成员变量)和对数据进行操作的函数(成员函数)封装在一起。

成员变量和成员函数统称为类的成员,成员需要指定访问权限,如果未指定访问权限,默认为私有权限。一般成员变量设为私有,成员函数设为共有。

类定义好之后,在主函数中就可以使用类定义一个具体的长方形,称为对象,定义对象的时候会自动调用构造函数完成初始化,即给该对象的长和宽赋值,接着就可以调用该对象的求周长函数求其周长。

答案:

#include<iostream>
#include<cstdio>
using namespace std;
class REC{
    double a;
    double b;
public:
    REC(double a1, double b1){
    a=a1;
    b=b1;
    }
    void circum(){
        double arr=2*(a+b);
        printf("%.2lf\n",arr);
    }
};

int main(){
    double a,b;
    cin>>a>>b;
    REC stu(a, b);
    stu.circum ();
}
  • 19
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕卿扬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值