HW11
//Robot.h
#pragma once
class Robot {
private:
char *name;
int energy;
void errPrn();
public:
Robot();
~Robot();
Robot(const char *name, int energy = 0);
Robot(Robot &r);
void go();
void back();
void turn();
void jump();
void charge(int e);
char *getName();
void setName(const char *);
int getEnergy();
void setEnergy(int);
};
//Robot.cpp
#include <iostream>
#include "Robot.h"
using namespace std;
#pragma warning (disable : 4996)
void Robot::errPrn() {
cout << "에너지 부족!" << endl;
}
Robot::Robot() {
this->name = new char[1];
strcpy(this->name, "");
this->energy = 0;
}
Robot::~Robot() {
delete[] this->name;
}
Robot::Robot(const char *name, int energy) {
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->energy = energy;
}
Robot::Robot(Robot &r) {
this->name = new char[strlen(r.name) + 1];
strcpy(this->name, r.name);
this->energy = r.energy;
}
// 전진 메세지 출력
void Robot::go() {
if (this->energy < 10) {
this->errPrn();
}
else {
cout << this->name << "전진..." << endl << endl;
this->energy -= 10;
}
}
// 후진 메세지 출력
void Robot::back() {
if (this->energy < 20) {
this->errPrn();
}
else {
cout << this->name << "후진..." << endl << endl;
this->energy -= 20;
}
}
// 턴 메세지 출력
void Robot::turn() {
if (this->energy < 30) {
this->errPrn();
}
else {
cout << this->name << "턴..." << endl << endl;
this->energy -= 30;
}
}
// 점프 메세지 출력
void Robot::jump() {
if (this->energy < 40) {
this->errPrn();
}
else {
cout << this->name << "점프..." << endl << endl;
this->energy -= 40;
}
}
void Robot::charge(int e) {
if (e < 0) {
return;
}
else {
this->energy += e;
}
}
char *Robot::getName() {
return this->name;
}
void Robot::setName(const char *name) {
if (this->name != NULL) {
delete[] this->name;
}
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
int Robot::getEnergy() {
return this->energy;
}
void Robot::setEnergy(int e) {
if (e < 0) {
return;
}
else {
this->energy = e;
}
}
//HW11.cpp
#include<iostream>
#include<cstring>
#include "Robot.h"
using namespace std;
#pragma warning (disable : 4996)
//int main()
//{
// Robot r1;
// Robot r2("하랑이");
// Robot r3("댕댕이", 100);
// Robot r4(r2);
//
// cout << r1.getName() << " " << r2.getEnergy() << endl;
// cout << r2.getName() << " " << r2.getEnergy() << endl;
// cout << r3.getName() << " " << r3.getEnergy() << endl;
// cout << r4.getName() << " " << r4.getEnergy() << endl;
//
// r2.setName("로보트태권브이");
// r2.setEnergy(250);
// r2.setEnergy(-50);
// cout << r2.getName() << " " << r2.getEnergy() << endl;
//
// r3.go();
// r3.back();
// r3.turn();
// r3.jump();
// r3.charge(120);
// cout << r3.getName() << " " << r3.getEnergy() << endl;
//
// return 0;
//}
void input(Robot *rp, int rCnt);
void work(Robot *rp, int rCnt);
void output(Robot *rp, int rCnt);
void myflush();
int main() {
Robot *rp;
int rCnt;
cout << "구입할 로봇 대수를 입력하시오 : ";
cin >> rCnt;
rp = new Robot[rCnt];
input(rp, rCnt);
work(rp, rCnt);
output(rp, rCnt);
return 0;
}
void input(Robot *rp, int rCnt) {
char name[30];
int energy;
for (int i = 0; i < rCnt;i++) {
cout << endl << i + 1 << "번 로봇명을 입력하시오 : ";
cin >> name;
(rp + i)->setName(name);
cout << name << "의 에너지 양을 입력하시오 : ";
cin >> energy;
(rp + i)->setEnergy(energy);
}
}
void work(Robot *rp, int rCnt) {
myflush();
while (1) {
char name[30];
int num, energy;
cout << endl << "# 로봇명 선택(";
for (int i = 0; i < rCnt;i++) {
cout << (rp + i)->getName() << ", ";
}
cout << "\b\b) : ";
cin.getline(name, sizeof(name));
if (strcmp(name, "") == 0) return;
for (int i = 0; i < rCnt;i++) {
//cout << name << (rp + i)->getName() << strcmp(name, (rp + i)->getName()) << endl;
if (strcmp(name, (rp + i)->getName()) == 0) {
cout << "# 할일 선택(1.전진/2.후진/3.회전/4.점프/5.충전) : ";
cin >> num;
switch (num) {
case 1: (rp + i)->go(); break;
case 2: (rp + i)->back(); break;
case 3: (rp + i)->turn(); break;
case 4: (rp + i)->jump(); break;
case 5: {
cout << "# 충전할 에너지양 입력 : ";
cin >> energy;
(rp + i)->charge(energy); break;
}
default: break;
}
myflush();
break;
}
else {
if (i == rCnt - 1) {
cout << "올바르지 못한 로봇명입니다!" << endl;
}
}
}
}
}
void output(Robot *rp, int rCnt) {
cout << endl;
for (int i = 0; i < rCnt; i++) {
cout << i + 1 << ". " << (rp + i)->getName() << " : " << (rp + i)->getEnergy() << endl;
}
}
void myflush() {
while (cin.get() != '\n');
}