栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++计算机二级操作题(一)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++计算机二级操作题(一)

基本操作 原题
// proj2.cpp
#include
#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<<"应发合计:"< 
 

 

基本操作 答案
// proj2.cpp
#include
#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********** 
     : base(the_base), bonus(the_bonus), tax(the_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<<"应发合计:"< 
简单应用 原题 
// proj2.cpp
#include 
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;
}

 简单应用 答案
// proj2.cpp
#include 
using namespace std;
class Array {
public:
  Array(int size)  // 构造函数
  {
//**********found**********
    _p = new int[size];
    _size = size;
  }
  ~Array() { delete [] _p; }  // 析构函数
  void SetValue(int index, int value)  // 设置指定元素的值
  {
    if ( IsOutOfRange(index) ) {
      cerr << "Index out of range!" << endl;
      return;
    }
//**********found**********
    _p[index]=value;
  }
  int GetValue(int index) const  // 获取指定元素的值
  {
    if ( IsOutOfRange(index) ) {
      cerr << "Index out of range!" << endl;
      return -1;
    }
//**********found**********
    return _p[index];
  }
  int GetLength() const { return _size; }  // 获取元素个数
private:
  int *_p;
  int _size;
  bool IsOutOfRange(int index) const  // 检查索引是否越界
  {
//**********found**********
    if (index < 0 || index>=_size)
      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;
}
综合应用 原题

//main.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 : "< 
//Polynomial.cpp
#include"Polynomial.h"
double Polynomial::getValue(double x)const{
       // 多项式的值value为各次项的累加和
	double value=coef[0];
       // 用value累计多项式的值,初值为常数项值
	//********333********
	//********666********
	return value;
}
//Polynomial.h
#include
#include
using namespace std;
class Polynomial{	//“多项式”类
public:
	Polynomial(double coef[], int num):coef(new double[num]),num_of_terms(num){
		for(int i=0;icoef[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********
    	for(int i=1;i 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976988.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号