C/C++
Помогите решить задачу C++
Робот может перемещаться в четырех направлениях ("N" - север, "W" запад, "S" - юг, "E" - восток) и принимать три цифровые команды: 0 -продолжать движение, 1 - поворот налево, -1 - поворот направо. Данный символ C - исходное направление робота и число N - посланная ему команда. Вывести направление робота после выполнения полученной команды.
#include "Bounds.h"
#include "Size.h"
#include "Point.h"
#include
using std::min;
using std::max;
ROBOT_NS_BEGIN
//----------------------------------------------------------------------------//
// Macros //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
#define NORM( l, r, t, b, x, y, w, h )\
int32 l = x, r = x, t = y, b = y;\
if (w < 0) l += w; else r += w;\
if (h < 0) t += h; else b += h;
//----------------------------------------------------------------------------//
// Constructors Bounds //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
Bounds::Bounds (int32 value)
{
X = value;
Y = value;
W = value;
H = value;
}
////////////////////////////////////////////////////////////////////////////////
Bounds::Bounds (int32 x, int32 y, int32 w, int32 h)
{
X = x;
Y = y;
W = w;
H = h;
}
////////////////////////////////////////////////////////////////////////////////
Bounds::Bounds (const Point& p, const Size& s)
{
X = p.X;
Y = p.Y;
W = s.W;
H = s.H;
}
//----------------------------------------------------------------------------//
// Functions Bounds //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
bool Bounds::IsZero (void) const
{
return X == 0 && Y == 0 &&
W == 0 && H == 0;
}
////////////////////////////////////////////////////////////////////////////////
bool Bounds::IsEmpty (void) const
{
return W == 0 || H == 0;
}
////////////////////////////////////////////////////////////////////////////////
bool Bounds::IsValid (void) const
{
return W > 0 && H > 0;
}
////////////////////////////////////////////////////////////////////////////////
int32 Bounds::GetLeft (void) const
{
return X;
}
////////////////////////////////////////////////////////////////////////////////
int32 Bounds::GetTop (void) const
{
return Y;
}
////////////////////////////////////////////////////////////////////////////////
int32 Bounds::GetRight (void) const
{
return X + W;
}
////////////////////////////////////////////////////////////////////////////////
int32 Bounds::GetBottom (void) const
{
return Y + H;
}
////////////////////////////////////////////////////////////////////////////////
void Bounds::SetLeft (int32 l)
{
X = l;
}
////////////////////////////////////////////////////////////////////////////////
void Bounds::SetTop (int32 t)
{
Y = t;
}
////////////////////////////////////////////////////////////////////////////////
void Bounds::SetRight (int32 r)
{
W = r - X;
}
#include "Size.h"
#include "Point.h"
#include
using std::min;
using std::max;
ROBOT_NS_BEGIN
//----------------------------------------------------------------------------//
// Macros //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
#define NORM( l, r, t, b, x, y, w, h )\
int32 l = x, r = x, t = y, b = y;\
if (w < 0) l += w; else r += w;\
if (h < 0) t += h; else b += h;
//----------------------------------------------------------------------------//
// Constructors Bounds //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
Bounds::Bounds (int32 value)
{
X = value;
Y = value;
W = value;
H = value;
}
////////////////////////////////////////////////////////////////////////////////
Bounds::Bounds (int32 x, int32 y, int32 w, int32 h)
{
X = x;
Y = y;
W = w;
H = h;
}
////////////////////////////////////////////////////////////////////////////////
Bounds::Bounds (const Point& p, const Size& s)
{
X = p.X;
Y = p.Y;
W = s.W;
H = s.H;
}
//----------------------------------------------------------------------------//
// Functions Bounds //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
bool Bounds::IsZero (void) const
{
return X == 0 && Y == 0 &&
W == 0 && H == 0;
}
////////////////////////////////////////////////////////////////////////////////
bool Bounds::IsEmpty (void) const
{
return W == 0 || H == 0;
}
////////////////////////////////////////////////////////////////////////////////
bool Bounds::IsValid (void) const
{
return W > 0 && H > 0;
}
////////////////////////////////////////////////////////////////////////////////
int32 Bounds::GetLeft (void) const
{
return X;
}
////////////////////////////////////////////////////////////////////////////////
int32 Bounds::GetTop (void) const
{
return Y;
}
////////////////////////////////////////////////////////////////////////////////
int32 Bounds::GetRight (void) const
{
return X + W;
}
////////////////////////////////////////////////////////////////////////////////
int32 Bounds::GetBottom (void) const
{
return Y + H;
}
////////////////////////////////////////////////////////////////////////////////
void Bounds::SetLeft (int32 l)
{
X = l;
}
////////////////////////////////////////////////////////////////////////////////
void Bounds::SetTop (int32 t)
{
Y = t;
}
////////////////////////////////////////////////////////////////////////////////
void Bounds::SetRight (int32 r)
{
W = r - X;
}
#include <algorithm>
#include <iostream>
using namespace std;
class Robot {
public:
Robot(char dir, int step) : step(step) {
index = find(begin(box), end(box), dir) - begin(box);
shift();
}
void move(int step) {
this->step = step;
shift();
}
char direction()const {
return box[index];
}
private:
int step;
int index;
inline static const char box[] = { 'N', 'W', 'S', 'E' };
void shift() {
index += step;
if (index < 0) index = size(box) - 1;
else if (index == size(box)) index = 0;
}
};
char direction() {
static const char box[] = { 'N', 'W', 'S', 'E' };
char c;
do c = cin.get(); while (find(begin(box), end(box), c) == end(box));
return c;
}
int step() {
int n;
do cin >> n; while (n < -1 || n > 1);
cin.ignore(cin.rdbuf()->in_avail());
return n;
}
int main() {
auto c = direction();
auto n = step();
Robot robot(c, n);
cout << robot.direction() << '\n';
system("pause > nul");
}
#include <iostream>
using namespace std;
class Robot {
public:
Robot(char dir, int step) : step(step) {
index = find(begin(box), end(box), dir) - begin(box);
shift();
}
void move(int step) {
this->step = step;
shift();
}
char direction()const {
return box[index];
}
private:
int step;
int index;
inline static const char box[] = { 'N', 'W', 'S', 'E' };
void shift() {
index += step;
if (index < 0) index = size(box) - 1;
else if (index == size(box)) index = 0;
}
};
char direction() {
static const char box[] = { 'N', 'W', 'S', 'E' };
char c;
do c = cin.get(); while (find(begin(box), end(box), c) == end(box));
return c;
}
int step() {
int n;
do cin >> n; while (n < -1 || n > 1);
cin.ignore(cin.rdbuf()->in_avail());
return n;
}
int main() {
auto c = direction();
auto n = step();
Robot robot(c, n);
cout << robot.direction() << '\n';
system("pause > nul");
}
Похожие вопросы
- Помогите решить задачу, c++, функции
- Помогите решить задачу C++
- Помогите решить задачу c++
- Помогите решить задачу C++, пожалуйста.
- Помогите решить задачу C++
- Помогите решить задачу C++
- Помогите решить задачу . C++
- Пожалуйста, помогите решить задачу! C++
- Помогите решить задачу по программированию на C++
- Помогите решить задачу по C++!