Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[과제/04]01/marahohc #32

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions 과제제출/marahohc/04/01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

class Node {
private:
char* word;
Node* next;
public:
Node() {
word = NULL;
next = NULL;
}
Node* getnext() { return next; }
void setnext(Node* NEXT) { next = NEXT; }
void setword(char* WORD) { word = WORD; }
char* getword() { return word; }
};

class List {
private:
Node* head;
public:
List(void) { head = NULL; }
~List(void);
void InsertNode(char* word);
void DisplayList();
Node* gethead() { return head; }
int Compare(char* word);

bool transword(char* firstword, char* addword) {
int len = strlen(firstword) - 1;
char fword = firstword[len] < 97 ? firstword[len] + 32 : firstword[len];
char aword = addword[0] < 97 ? addword[0] + 32 : addword[0];
if (fword == aword)
return true;
else
return false;
}
};

void List::DisplayList(void) {
Node* cur = NULL;
cur = head;

if (cur->getnext() == NULL)
cout << cur->getword() << "->" << endl;
else {
while (cur != NULL) {
cout << cur->getword() << "-> "; //wow ->
cur = cur->getnext();
}
}
cout << endl;
}
void List::InsertNode(char* word) {
//새로운 노드 생성
Node* newNode = new Node;
newNode->setword(word);
//curr노드가 head에서 시작
Node* currNode = head;
//head가 null인 경우 head를 new노드로 지정
if (head == NULL) {
head = newNode;
return;
}
//맨 끝에 위치한 노드를 찾아주기
while (currNode->getnext() != NULL) {
currNode = currNode->getnext();
}
//new노드를 마지막 노드의 끝에 붙여줌
currNode->setnext(newNode);
newNode->setnext(NULL);
}

int List::Compare(char* curword) {
Node* nodeinst = head;
char temp1[2] = { "\0" };
char temp2[2] = { "\0" };
if (nodeinst == NULL) {
bool comp = 1;

comp = strcasecmp(curword, nodeinst->getword());

if (comp == false) {
cout << "Already Exists" << endl;
return 0;
}
if (nodeinst->getword()[strlen(nodeinst->getword()) - 1] == curword[0])
return 1;
else if (nodeinst->getword()[strlen(nodeinst->getword()) - 1] != curword[0]) {
cout << "Not chained" << endl;
return 0;
}
else
return 0;
}
else { //node 두 개 이상일 때
while (nodeinst->getnext() != NULL) {
int comp = 0;
comp = strcasecmp(curword, nodeinst->getword());

if (comp == 0) {
cout << "Already Exists" << endl;
if (nodeinst->getword()[strlen(nodeinst->getword()) - 1] != curword[0])
cout << "Not chained" << endl;
return 0;
}
nodeinst = nodeinst->getnext();
}
if (List::transword(nodeinst->getword(), curword))
return 1;
else {
cout << "Not chained" << endl;
return 0;
}
}
}

List::~List(){
Node* temp1 = head;
if(temp1 == NULL)
return;
Node* temp2 = temp1->getnext();
while(temp1 != NULL){
cout<<temp1->getword()<<"삭제"<<endl;
delete[] temp1;
temp1 = temp2;
if(temp2 != NULL)
temp2 = temp2->getnext();
}
}

int main(void) {
List* list = new List;
char* instword = new char[20];
bool comp = 0;
cout << "CMD(Word/exit)>>";
cin >> instword;
comp = strcmp(instword, "exit");
if (comp == false){
delete[] instword;
delete list;
return 0;
}
list->InsertNode(instword);
list->DisplayList();
while (1) {
instword = new char[20];
cout << "CMD(Word/exit)>>";
cin >> instword;
comp = strcmp(instword, "exit");
if (comp == false)
break;
else if (comp == true)
{
int i = 0;
i = list->Compare(instword);
if (i == 0)
{
continue;
}
else if (i == 1)
{
list->InsertNode(instword);
list->DisplayList();
}
}
}
delete[] instword;
delete list;
}
109 changes: 109 additions & 0 deletions 과제제출/marahohc/04/02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>
#include <cstring>

using namespace std;

class Bullet {
private:
int bullet;
Bullet* next;
public:
Bullet() {
bullet = 0;
next = nullptr;
}
void setnext(Bullet* NEXT) { next = NEXT; }
Bullet* getnext() { return next; }
void setbullet(int num) { bullet = num; }
int getbullet() { return bullet; }
};

class List {
private:
Bullet* head;
Bullet* tail;
public:
List(void) {
head = NULL;
tail = NULL;
}
~List(void);
void MakeRevolver() {
head = new Bullet;
head->setbullet(1);
Bullet* currbullet = head;

for (int i = 0; i < 5; i++) {
Bullet* newbullet = new Bullet;

currbullet->setnext(newbullet);
currbullet = newbullet;
}
currbullet->setnext(head);
}

int Shoot() {
int num = head->getbullet();
if (num == 1) {
cout << "You Died..." << endl;
return 1;
}
else {
cout << "You Survived!" << endl;
head = head->getnext();
return 0;
}
}
void Rotate() {
srand((unsigned int)time(NULL));

int num = rand() % 6 + 1;
for (int i = 0; i < num; i++) {
head = head->getnext();
}
}
};

List::~List(){
Bullet *temp1 = head;
if(temp1 == NULL)
return;
Bullet* temp2 = temp1->getnext();
while(temp1 != NULL){
delete temp1;
temp1 = temp2;
temp2 = temp1->getnext();
}
}

int main(void) {
char* command = new char;
List* list = new List;
int num = 0;
cout << "Command list(shoot/rotate)" << endl;

list->MakeRevolver();
list->Rotate();
while (1) {
cout << "CMD>> ";
cin >> command;
if (!strcmp(command, "shoot")) {
if (list->Shoot())
return 0;

}
else if (!strcmp(command, "rotate")) {
list->Rotate();
system("clear");
}
else {
cout << "Wrong Command!" << endl;
}
}
delete[] command;
delete list;
return 0;
}