C++二级操作题真题【内附解析】

0 阅读1分钟

来源题库:cpp.code2ji.cn 在这里插入图片描述


题目一:总评计算(Score 类)

题目描述: 设计一个 Score 类,用于存储课程名、学号、平时成绩、期中成绩、期末成绩。

  • 通过构造函数初始化全部数据
  • 修正学号拷贝错误
  • 实现 getFinal() 函数计算总评成绩,公式为: 平时20% + 期中30% + 期末50%,结果四舍五入为整数
  • 输出学号、课程名和总评成绩

题库出处cpp.code2ji.cn 在这里插入图片描述


初始代码(带空格待填)

#include <iostream>
#include <cstring>
using namespace std;

class Score {
public:
    Score(const char *the_course, const char *the_id, int the_normal, int the_midterm, int the_end_of_term)
        : course(the_course), normal(the_normal), midterm(the_midterm), end_of_term(the_end_of_term) {
        // ERROR **********found**********
        strcpy(__1__, __2__);
    }
    const char *getCourse() const { return course; }
    // ERROR **********found**********
    const char *getID() const { return __3__; }
    int getNormal() const { return normal; }
    int getMidterm() const { return midterm; }
    int getEndOfTerm() const { return end_of_term; }
    int getFinal() const;  // 计算总评成绩

private:
    const char *course;     // 课程名称
    char student_id[12];    // 学号
    int normal;             // 平时成绩
    int midterm;            // 期中成绩
    int end_of_term;        // 期末成绩
};

// ERROR **********found**********
int Score::getFinal() const {
    return (int)(normal * 0.2 + midterm * 0.3 + end_of_term * 0.5 + 0.5);
}

int main() {
    char English[] = "英语";
    Score score(English, "12345678", 68, 83, 92);
    cout << "学号:" << score.getID() << "   ";
    cout << "课程:" << score.getCourse() << "   ";
    cout << "总评成绩:" << score.getFinal() << endl;
    return 0;
}

填空答案

位置填写内容说明
1student_id目标数组,存放学号字符串
2the_id源字符串,构造函数参数传入的学号
3student_id返回学号成员变量地址,注意是数组名代表指针

代码逐句讲解

Score(const char *the_course, const char *the_id, int the_normal, int the_midterm, int the_end_of_term)
    : course(the_course), normal(the_normal), midterm(the_midterm), end_of_term(the_end_of_term) {
    strcpy(student_id, the_id);
}
  • 构造函数使用初始化列表给课程名和成绩赋值。
  • 注意学号是 char student_id[12],数组不能直接赋值,必须用 strcpy 复制字符串。
  • 这里传入的学号 the_id 复制到成员数组 student_id
const char *getID() const { return student_id; }
  • 返回学号字符数组的地址,student_id 本身是指向字符数组的指针。
  • 注意不要写成 &student_id,那是数组地址,不是字符串指针。
int Score::getFinal() const {
    return (int)(normal * 0.2 + midterm * 0.3 + end_of_term * 0.5 + 0.5);
}
  • 计算总评成绩,平时占20%,期中占30%,期末占50%。
  • 最后加 0.5 是四舍五入技巧,转换成整数。
int main() {
    char English[] = "英语";
    Score score(English, "12345678", 68, 83, 92);
    cout << "学号:" << score.getID() << "   ";
    cout << "课程:" << score.getCourse() << "   ";
    cout << "总评成绩:" << score.getFinal() << endl;
    return 0;
}
  • 测试程序,构造一个英语课程成绩对象。
  • 依次输出学号、课程和总评成绩。

题目二:图形解码(抽象类与派生类)

题目描述: 给定抽象类 Shape 和派生类 Triangle,实现一个 show(Shape *shape) 函数,输出图形名称、周长、面积。

题库出处cpp.code2ji.cn 在这里插入图片描述


初始代码(需补全 show 函数)

#include "shape.h"
#include <iostream>
using namespace std;

Triangle::Triangle(Point a, Point b, Point c) : p1(a), p2(b), p3(c) {}

string Triangle::name() {
    return "三角形";
}

double Triangle::perimeter() {
    return p1.distance(p2) + p2.distance(p3) + p3.distance(p1);
}

double Triangle::area() {
    double a = p1.distance(p2);
    double b = p2.distance(p3);
    double c = p3.distance(p1);
    double s = (a + b + c) / 2.0;
    return sqrt(s * (s - a) * (s - b) * (s - c));
}

//**********found**********
void show(Shape *shape) {
    cout << "此图形是一个" << shape->name()
         << ",  周长=" << shape->perimeter()
         << ",  面积=" << shape->area() << endl;
}

int main() {
    Triangle *tri = new Triangle(Point(0,2), Point(2,0), Point(0,0));
    show(tri);
    return 0;
}

代码逐句讲解

void show(Shape *shape) {
  • 接收一个 Shape 类指针,可以指向基类或派生类对象。
cout << "此图形是一个" << shape->name()
     << ",  周长=" << shape->perimeter()
     << ",  面积=" << shape->area() << endl;
  • 通过虚函数调用输出图形名称、周长、面积。
  • 多态机制:shape->name() 实际调用派生类 Trianglename()
int main() {
    Triangle *tri = new Triangle(Point(0,2), Point(2,0), Point(0,0));
    show(tri);
    return 0;
}
  • 创建一个三角形对象并调用 show,演示多态输出。

题目三:链式栈操作

题目描述: 实现基于链表的栈类 Stack,支持 pushpop

  • 数据项类 Entry 包含数据和指向下一结点的指针。
  • 主函数中将数组元素依次压栈,再依次弹栈输出。

题库出处cpp.code2ji.cn

1.00


初始代码(待填空)

#include <iostream>
using namespace std;

class Entry {
public:
    Entry* next;
    int data;

    //**********found**********
    Entry(Entry* n, int d) : next(n), data(d) { }
};

class Stack {
    Entry* top;
public:
    Stack() : top(0) { }
    ~Stack() {
        while (top != 0) {
            Entry* tmp = top;
            //**********found**********
            top = top->next;
            delete tmp;
        }
    }
    void push(int data) {
        //**********found**********
        top = new Entry(top, data);
    }
    int pop() {
        if (top == 0) return 0;
        //**********found**********
        int result = top->data;
        top = top->next;
        return result;
    }
};

int main() {
    int a[] = {0,1,2,3,4,5,6,7,8,9};
    Stack s;

    for (int i = 0; i < 10; i++) {
        cout << a[i] << ' ';
        s.push(a[i]);
    }
    cout << endl;

    for (int i = 0; i < 10; i++) {
        cout << s.pop() << ' ';
    }
    cout << endl;
    return 0;
}

代码逐句讲解

Entry(Entry* n, int d) : next(n), data(d) { }
  • 构造函数初始化,next 指向传入的结点,data 存储传入数据。
~Stack() {
    while (top != 0) {
        Entry* tmp = top;
        top = top->next;
        delete tmp;
    }
}
  • 析构函数循环释放所有结点,避免内存泄漏。
void push(int data) {
    top = new Entry(top, data);
}
  • 新结点成为栈顶,next 指向旧栈顶。
int pop() {
    if (top == 0) return 0;
    int result = top->data;
    top = top->next;
    return result;
}
  • 弹出栈顶数据,更新栈顶指针。

题目四:灯光开关(多态和控制)

题目描述: 基类 Switchable 表示可开关的设备,派生类 Lamp 表示灯,Button 控制设备开关。

  • 实现 Switchable::isOn() 返回设备状态
  • Switchable::getDeviceName() 为纯虚函数
  • Button 构造函数正确初始化设备指针
  • Button::push() 切换设备状态

题库出处cpp.code2ji.cn 在这里插入图片描述


初始代码(待补全)

#include <iostream>
using namespace std;

class Switchable {
    bool is_on;
public:
    Switchable() : is_on(false) { }
    void switchOn() { is_on = true; }
    void switchOff() { is_on = false; }
    //**********found**********
    bool isOn() { return is_on; }
    //**********found**********
    virtual const char *getDeviceName() = 0;
};

class Lamp : public Switchable {
public:
    const char *getDeviceName() { return "Lamp"; }
};

class Button {
    Switchable *device;
public:
    //**********found**********
    Button(Switchable &dev) : device(&dev) { }
    bool isOn() { return device->isOn(); }
    void push() {
        //**********found**********
        if (isOn())
            device->switchOff();
        else
            device->switchOn();
    }
};

int main() {
    Lamp lamp;
    Button button(lamp);
    cout << "灯的状态:" << (lamp.isOn() ? "开" : "关") << endl;
    cout << "按钮的状态:" << (button.isOn() ? "开" : "关") << endl;

    button.push();

    cout << "灯的状态:" << (lamp.isOn() ? "开" : "关") << endl;
    cout << "按钮的状态:" << (button.isOn() ? "开" : "关") << endl;

    return 0;
}

代码逐句讲解

bool isOn() { return is_on; }
  • 返回当前设备状态,true 为开,false 为关。
virtual const char *getDeviceName() = 0;
  • 纯虚函数,派生类必须重写。
Button(Switchable &dev) : device(&dev) { }
  • 构造函数用设备引用初始化设备指针。
void push() {
    if (isOn())
        device->switchOff();
    else
        device->switchOn();
}
  • 按钮按下时,切换设备状态。