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

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

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

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

基本操作 原题
#include
#include
using namespace std;

class Point{
	double x,y;
public:
	// ERROR **********found**********
	Point(double m=0.0, double n=0.0):m(x), n(y){}
	friend double distanceBetween(const Point& p1, const Point& p2);
};
// ERROR **********found**********
double Point::distanceBetween(const Point& p1, const Point& p2){//返回两点之间的距离
	double dx=p1.x-p2.x;
	double dy=p1.y-p2.y;
	return sqrt(dx*dx+dy*dy);
}

int main(){
	Point f1(0,0), f2(1,1);
	// ERROR **********found**********
	cout< 

基本操作 答案
#include
#include
using namespace std;

class Point{
	double x,y;
public:
	// ERROR **********found**********
	Point(double m=0.0, double n=0.0):x(m), y(n){}
	friend double distanceBetween(const Point& p1, const Point& p2);
};
// ERROR **********found**********
double distanceBetween(const Point& p1, const Point& p2){//返回两点之间的距离
	double dx=p1.x-p2.x;
	double dy=p1.y-p2.y;
	return sqrt(dx*dx+dy*dy);
}

int main(){
	Point f1(0,0), f2(1,1);
	// ERROR **********found**********
	cout< 
简单应用 原题 
#include
using namespace std;

class Switchable{	//具有开、关两种状态的设备
	bool is_on;		//为 ture 表示“开”,为 false 表示“关”
public:
	Switchable(): is_on(false){}
	void switchOn(){ is_on=true; }	//置为“开”状态
	//**********found**********
	void switchOff(){_________________________________}//置为“关”状态
	bool isOn(){ return is_on; }	//返回设备状态
	//**********found**********
	virtual const char *getDeviceName()__________;   //返回设备名称的纯虚函数
};

class Lamp: public Switchable{
public:
	//返回设备名称,用于覆盖基类中的纯虚函数
	const char *getDeviceName(){ return "Lamp"; }
};

class Button{		//按钮
	Switchable *device;	//按钮控制的设备
public:
	//**********found**********
	Button(Switchable &dev): _______________{}	//用参数变量的地址初始化device
	bool isOn(){ return device->isOn(); }	//按钮状态
	void push(){							//按一下按钮改变状态
		if(isOn()) device->switchOff();
		//**********found**********
		else ___________________________
	}
};

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

简单操作 答案
#include
using namespace std;

class Switchable{	//具有开、关两种状态的设备
	bool is_on;		//为 ture 表示“开”,为 false 表示“关”
public:
	Switchable(): is_on(false){}
	void switchOn(){ is_on=true; }	//置为“开”状态
	//**********found**********
	void switchOff(){is_on=false;}//置为“关”状态
	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){}	//用参数变量的地址初始化device
	bool isOn(){ return device->isOn(); }	//按钮状态
	void push(){							//按一下按钮改变状态
		if(isOn()) device->switchOff();
		//**********found**********
		else device->switchOn();

	}
};

int main(){
	Lamp lamp;
	Button button(lamp);
	cout<<"灯的状态:"<<(lamp.isOn()? "开" : "关")< 
 综合应用 原题 
//main.cpp
#include "MiniString.h"
#pragma warning (disable:4996)

ostream& operator<<(ostream& o,MiniString& s)
{
	int len=strlen(s.str);
	for(int i=0;i 
//MinString.h
#include 
#include 
#pragma warning (disable:4996)
using namespace std;

class MiniString
{
public:
	MiniString(const char* s)
	{
		str = new char[strlen(s)+1];
		strcpy(str,s);
	}
	~MiniString() { delete[] str; }
	//删除从pos处开始的n个字符,然后在pos处插入字符串p
	MiniString& replace(int pos,int n,char *p);	
	char* getStr() const { return str; }
	friend ostream& operator<<(ostream& o,MiniString& s);		//输出字符串str
private:
	char *str;
};

void writeToFile(const char *);
综合应用 答案
#include "MiniString.h"
#pragma warning (disable:4996)

ostream& operator<<(ostream& o,MiniString& s)
{
	int len=strlen(s.str);
	for(int i=0;i 

 

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

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

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