第1关:游戏角色变身
任务描述
本关任务:编写一个能实现游戏角色变身的小程序。
相关知识
为了完成本关任务,你需要掌握:1.如何定义抽象类(接口),2.如何使用抽象类。
题目内容
已知游戏中常见人物状态State抽象类,包括站立(Idle),行走(Run),攻击(Attack),死亡(Dead)等动作。在游戏中有角色hero类,拥有人物状态同时还拥有血量,攻击力,防御力等属性,有怪物类Monster,怪物有血量,攻击力,防御力,怪物类型等属性。 已知角色可以变身为怪物,现在main函数中,模拟某个角色在正常情况下以及在变身状态下的各种状态操作(输入1为正常hero,输入2则变身为monster)。 要求,使用抽象类及的基类指针实现。
编程要求
根据提示,在右侧编辑器补充代码,完成程序。
测试说明
平台会对你编写的代码进行测试:
测试输入:
1
预期输出:
hero is idleling...
hero is attack...
hero is running...
hero is dead...
测试输入:
2
预期输出:
Monster is idleling...
Monster is attack...
Monster is running...
Monster is dead...
上答案:
#include <iostream>
using namespace std;
#include<string>
#include"time.h"
#include"math.h"
//请在begin和end之间完成完成代码编写
/*************begin***********/
class State//声明抽象基类 State
{
public:
virtual void idle()=0;
virtual void run() = 0;
virtual void attack() = 0;
virtual void dead() = 0;
};
class hero :public State
{
public:
hero(int hp, int af, int df)
{
hp = hp;
af = af;
df = df;
}
virtual void idle()
{
cout << "hero is idleling..." << endl;
}
virtual void run()
{
cout << "hero is running..." << endl;
}
virtual void attack()
{
cout << "hero is attack..." << endl;
}
virtual void dead()
{
cout << "hero is dead..." << endl;
}
private:
int hp;
int af;
int df;
};
class Monster :public State
{
public:
Monster(int hp = 0, int af = 0, int df = 0, int type = 0) {};
virtual void idle()
{
cout << "Monster is idleling..." << endl;
}
virtual void run()
{
cout << "Monster is running..." << endl;
}
virtual void attack()
{
cout << "Monster is attack..." << endl;
}
virtual void dead()
{
cout << "Monster is dead..." << endl;
}
private:
int hp;
int af;
int df;
string type;
};
/*************end**********/
int main() {
State* s;
/*************begin*****************/
hero a(1200, 1, 1);
Monster b;
int m;
cin >> m;
if (m == 1)
{
s = &a;
}
else
{
s = &b;
}
/**************end***************/
s->idle();
s->attack();
s->run();
s->dead();
return 1;
}
第2关:形状数组
任务描述
本关任务:编写一个能计算不同形状的面积数组的小程序。
相关知识
为了完成本关任务,你需要掌握:1.抽象类的定义,2.上转型的使用。
题目要求
写一个程序,定义抽象类Shape,由它派生出5个派生类:Circle(圆),Square(正方形),Rectangle(矩形),Trapezoid(梯形),Triangle(三角形),用虚函数area()分别计算几种图形的面积,并求他们的和,要求使用基类指针数组,使它的每个元素都指向一个派生类对象
编程要求
根据提示,在右侧编辑器补充代码。
测试说明
平台会对你编写的代码进行测试:
测试输入:
10
10
15 14 13
10 8
15 8 5
预期输出:
area sum=635.659
测试输入:
13
12
11 10 5
12 5
18 9 7
预期输出:
area sum=854.409
上答案:
#include <iostream>
using namespace std;
#include<string>
#include"time.h"
#include"math.h"
#define PI 3.1415926;
//请在begin和end之间填入代码
/**************begin*****************/
class Shape
{
public:
virtual double area() = 0;
};
class Circle:public Shape
{
public:
Circle(double d)
{
r = d;
}
virtual double area()
{
return 3.14159 * r * r;
}
private:
double r;
};
class Square :public Shape
{
public:
Square(double le)
{
l = le;
}
virtual double area()
{
return l * l;
}
double l;
};
class Rectangle :public Shape
{
public:
Rectangle(double m, double n)
{
w= m;
h = n;
}
virtual double area()
{
return w*h;
}
double w;
double h;
};
class Trapezoid :public Shape
{
public:
Trapezoid(double a, double b, double c)
{
eup = a;
ebo = b;
h = c;
}
virtual double area()
{
return ((eup + ebo) * h / 2);
}
double eup;
double ebo;
double h;
};
class Triangle :public Shape
{
public:
Triangle(double a, double b, double c)
{
e1 = a;
e2 = b;
e3 = c;
}
virtual double area()
{
double p;
p = (e1 + e2 + e3) / 2.0;
return sqrt(p * (p - e1) * (p - e2) * (p - e3));
}
double e1;
double e2;
double e3;
};
/****************end**************/
int main() {
double r, h, e1, e2, e3, eup, ebo, w;
cin >> r;
Circle c(r);
cin >> w;
Square sq(w);
cin >> e1 >> e2 >> e3;
Triangle tr(e1, e2, e3);
cin >> w >> h;
Rectangle re(w, h);
cin >> eup >> ebo >> h;
Trapezoid trap(eup, ebo, h);
double sum = 0;
/*****************begin*******************/
Shape* p[5];
p[0] = &c;
sum += p[0]->area();
p[1] = &sq;
sum += p[1]->area();
p[2] = &tr;
sum += p[2]->area();
p[3] = &re;
sum += p[3]->area();
p[4] = &trap;
sum += p[4]->area();
/******************end*****************/
cout << "area sum=" << sum;
}
第3关:多功能线性表
任务描述
本关任务:编写一个能为栈也能为队列的线性表。
相关知识
为了完成本关任务,你需要掌握:1.如何定义抽象类,2.如何封装栈和队列,3.如何实现类模板。
题目内容
设置一个vlist线性表类,抽象函数in(),out(),表示进表和出表,由Stack类和Quene类继承vlist,实现vlist指针既能是栈也可以是队列的功能。同时还可对不同的数据类型进行进表和出表。
代码提示
template
class vlist//定义抽象类vlist,
{
public:
virtual void in(T &e)=0;
virtual T out()=0;
};
template class Element//定义线性表元素,用于存储数据
{
public:
T data;
Element *next;
Element(){}
};
编程要求
根据提示,在右侧编辑器补充代码。
测试说明
平台会对你编写的代码进行测试:
测试输入:
my name is
i am jack
预期输出:
432234
is name my
i am jack
上答案:
#include <iostream>
using namespace std;
#include<string>
#include"math.h"
template<class T>
class vlist//线性表抽象类
{
public:
virtual void in(T& e) = 0;
virtual T out() = 0;
};
template<class T>
class Element//线性表存储元素
{
public:
T data;
Element<T>* next;
Element() {next=NULL;}
};
//在beign和end之间完成SStack和Quene类的定义及实现,其继承vlist
/***************begin****************/
template<class T>
class SStack:public vlist<T>
{
private:
Element<T>* top;
public:
SStack() { top = NULL; }
virtual void in(T&e)
{
Element<T>* p = new Element<T>;
p->data = e;
p->next = top;
top = p;
}
virtual T out()
{
T e;
if (top != NULL)
{
e = top->data;
Element<T>* t = top;
top = top->next;
delete t;
return e;
}
else
{
cerr << "栈为空!" << endl;
exit(1);
}
}
};
template<class T>
class Quene:public vlist<T>
{
private:
Element<T>* head;
Element<T>* tail;
public://入队
Quene(){head=tail=NULL;}
virtual void in(T&e)
{
Element<T>* p = new Element<T>;
p->data = e;
p->next = NULL;
if (head == NULL)
{
head = tail = p;
}
else {
tail->next = p;
tail = p;
}
}
virtual T out()
{
T x=head->data;
Element<T>* p = head;
head = head->next;
if (head == NULL)
tail = NULL;
delete p;
return x;
}
};
/***************end******************/
int main() {
vlist<int>* intp;
vlist<string>* strp;
SStack<int> ints;
Quene<int> intq;
SStack<string> strs;
Quene<string> strq;
intp = &ints;
int t = 2;
intp->in(t);
t = 3;
intp->in(t);
t = 4;
intp->in(t);
for (int i = 0; i < 3; i++)
cout << intp->out();
intp = &intq;
t = 2;
intp->in(t);
t = 3;
intp->in(t);
t = 4;
intp->in(t);
for (int i = 0; i < 3; i++)
cout << intp->out();
strp = &strs;
string str;
cout << endl;
for (int i = 0; i < 3; i++)
{
cin >> str;
strp->in(str);
}
for (int i = 0; i < 3; i++)
cout << strp->out() << " ";
cout << endl;
strp = &strq;
for (int i = 0; i < 3; i++)
{
cin >> str;
strp->in(str);
}
for (int i = 0; i < 3; i++)
cout << strp->out() << " ";
return 0;
}