C++计算机二级考试:操作题训练六套 试题版

该文主要包含一系列C++编程操作题目,涉及类的构造与析构、对象初始化、数组操作、链表结构、多项式计算以及图形处理等概念。题目涵盖了基本操作、简单应用和综合应用三个层次,测试了编程者对C++语法和数据结构的理解与运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

操作题训练(一)

一、基本操作题

img

// proj1.cpp
#include<iostream>
#pragma warning (disable:4996)
using namespace std;
 
class Salary{
public:
  Salary(const char *id, double the_base, double the_bonus, double the_tax)
  // ERROR **********found********** 
     : the_base(base), the_bonus(bonus), the_tax(tax)
  {
     staff_id=new char[strlen(id)+1];
     strcpy(staff_id,id);
  }
  // ERROR **********found********** 
  ~Salary(){ delete *staff_id; }
  double getGrossPay()const{ return base+bonus; }          //返回应发项合计
  double getNetPay()const{ return getGrossPay()-tax; }   //返回实发工资额
private:
  char *staff_id;    //职工号
  double base;       //基本工资
  double bonus;      //奖金
  double tax;        //代扣个人所得税
};
 
int main() {
  Salary pay("888888", 3000.0, 500.0, 67.50);
  cout<<"应发合计:"<<pay.getGrossPay()<<"   ";
  cout<<"应扣合计:"<<pay.getGrossPay()-pay.getNetPay()<<"   ";
  // ERROR **********found********** 
  cout<<"实发工资:"<<pay::getNetPay()<<endl;
  return 0;
}

二、简单应用题

img

// proj2.cpp
#include <iostream>
using namespace std;
 
class Array {
public:
  Array(int size)  // 构造函数
  {
//**********found**********
    _p = __________;
    _size = size;
  }
  ~Array() { delete [] _p; }  // 析构函数
  void SetValue(int index, int value)  // 设置指定元素的值
  {
    if ( IsOutOfRange(index) ) {
      cerr << "Index out of range!" << endl;
      return;
    }
//**********found**********
    __________;
  }
  int GetValue(int index) const  // 获取指定元素的值
  {
    if ( IsOutOfRange(index) ) {
      cerr << "Index out of range!" << endl;
      return -1;
    }
//**********found**********
    __________;
  }
  int GetLength() const { return _size; }  // 获取元素个数
private:
  int *_p;
  int _size;
  bool IsOutOfRange(int index) const  // 检查索引是否越界
  {
//**********found**********
    if (index < 0 || __________)
      return true;
    else return false;
  }
};
 
int main()
{
  Array a(10);
  for (int i = 0; i < a.GetLength(); i++)
    a.SetValue(i, i+1);
  for (int j = 0; j <  a.GetLength()-1; j++)
    cout << a.GetValue(j) << ", ";
  cout << a.GetValue(a.GetLength()-1) << endl;
  return 0;
}

三、综合应用题

img

//Polynomial.h
#include<iostream>
#include<string>
using namespace std;
class Polynomial{   //“多项式”类
public:
    Polynomial(double coef[], int num):coef(new double[num]),num_of_terms(num){
        for(int i=0;i<num_of_terms;i++) this->coef[i]=coef[i];
    }
    ~Polynomial(){ delete[] coef; }
    //返回指定次数项的系数
    double getCoefficient(int power)const{ return coef[power]; }
    //返回在x等于指定值时多项式的值
    double getValue(double x)const;
private:
//系数数组,coef[0]为0次项(常数项)系数,coef[1]为1次项系数,coef[2]为2次项(平方项)系数,余类推。
    double *coef;   
    int num_of_terms;
};
void writeToFile(string path);
//Polynomial.cpp
#include"Polynomial.h"
double Polynomial::getValue(double x)const{
       // 多项式的值value为各次项的累加和
    double value=coef[0];
       // 用value累计多项式的值,初值为常数项值
    //********333********
    //********666********
}
//proj3.cpp
#include "Polynomial.h"
int main(){
    double p1[]={5.0, 3.4, -4.0, 8.0}, p2[]={0.0, -5.4, 0.0, 3.0, 2.0};
    Polynomial  poly1(p1, sizeof(p1)/sizeof(double)),
                poly2(p2, sizeof(p2)/sizeof(double));
    cout<<"Value of p1 when x=2.5 : "<<poly1.getValue(2.5)<<endl;
    cout<<"Value of p2 when x=3.0 : "<<poly2.getValue(3.0)<<endl;
    writeToFile(".\\");
    return 0;
}

操作题训练(二)

一、基本操作题

img

//proj1.cpp
#include<iostream>
#pragma warning (disable:4996)
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(the_id,student_id);
    }
    const char *getCourse()const{ return course; }      //返回课程名称
    // ERROR **********found********** 
    const char *getID()const{ return &student_id; } //返回学号
    int getNormal()const{ return normal; }              //返回平时成绩
    int getMidterm()const{ return midterm; }            //返回期中考试成绩
    int getEndOfTerm()const{ return end_of_term; }      //返回期末考试成绩
    int
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值