基本操作 原题
#include<iostream>
#include<cmath>
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<<distanceBetween(Point f1, Point f2)<<endl;
return 0;
}
基本操作 答案
#include<iostream>
#include<cmath>
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<<distanceBetween(f1, f2)<<endl;
return 0;
}
简单应用 原题
#include<iostream>
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()? "开" : "关")<<endl;
cout<<"按钮的状态:"<<(button.isOn()? "开" : "关")<<endl;
button.push(); //按一下按钮
cout<<"灯的状态:"<<(lamp.isOn()? "开" : "关")<<endl;
cout<<"按钮的状态:"<<(button.isOn()? "开" : "关")<<endl;
return 0;
}
简单操作 答案
#include<iostream>
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()? "开" : "关")<<endl;
cout<<"按钮的状态:"<<(button.isOn()? "开" : "关")<<endl;
button.push(); //按一下按钮
cout<<"灯的状态:"<<(lamp.isOn()? "开" : "关")<<endl;
cout<<"按钮的状态:"<<(button.isOn()? "开" : "关")<<endl;
return 0;
}
综合应用 原题
//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<len;i++)
cout<<s.str[i];
return o;
}
MiniString& MiniString::replace(int pos, int n, char *p )
{
MiniString temp(str); //创建一个副本以保存前字符串信息
delete[] str; //释放字符串数组
int lt=strlen(temp.str),lp=strlen(p);
int len=lt-n+lp+1; //计算存放结果字符串所需空间大小
str=new char[len]; //申请存放结果所需字符数组
//分别把temp.str中的前pos个字符和p中的lp个字符复制到str中
//********333********
//********666********
//把temp.str后面剩余的字符串复制到str中
for(int k=pos+n; k<lt; k++)
str[pos+lp++]=temp.str[k];
str[len-1]='\0';
return *this;
}
int main()
{
MiniString mStr("String handling C++ style.");
char iStr[]="STL Power";
cout << "Replace char* into MiniString:\n";
mStr.replace(7, 8, iStr);
cout << mStr << endl;
writeToFile("");
return 0;
}
//MinString.h
#include <iostream>
#include <string>
#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<len;i++)
cout<<s.str[i];
return o;
}
MiniString& MiniString::replace(int pos, int n, char *p )
{
MiniString temp(str); //创建一个副本以保存前字符串信息
delete[] str; //释放字符串数组
int lt=strlen(temp.str),lp=strlen(p);
int len=lt-n+lp+1; //计算存放结果字符串所需空间大小
str=new char[len]; //申请存放结果所需字符数组
//分别把temp.str中的前pos个字符和p中的lp个字符复制到str中
//********333********
strncpy(str,temp.str,pos); //将temp.str中的前pos个字符复制到字符数组str中
for(int i=pos,j=0; i<pos+lp; i++) {str[i]=p[j]; j++;} //将p中的lp个字符复制到字符数组str中
//********666********
//把temp.str后面剩余的字符串复制到str中
for(int k=pos+n; k<lt; k++)
str[pos+lp++]=temp.str[k];
str[len-1]='\0';
return *this;
}
int main()
{
MiniString mStr("String handling C++ style.");
char iStr[]="STL Power";
cout << "Replace char* into MiniString:\n";
mStr.replace(7, 8, iStr);
cout << mStr << endl;
writeToFile("");
return 0;
}