Skip to content

Commit

Permalink
Second commit
Browse files Browse the repository at this point in the history
final term assignment 3rd submit

implemented features
- print member list
- add new member
- remove member
- edit member data
- search member
- save member data to file
- read member data from file
  • Loading branch information
Byeol committed Jun 8, 2014
1 parent 4a91f57 commit b269c93
Show file tree
Hide file tree
Showing 16 changed files with 622 additions and 149 deletions.
69 changes: 69 additions & 0 deletions binarySearchTree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "binarySearchTree.h"

NODE * findNode(NODE * rootNode, int key)
{
if (rootNode == NULL)
return NULL;

if (key == rootNode->key)
return rootNode;
else if (key < rootNode->key)
return findNode(rootNode->left, key);
else if (key > rootNode->key)
return findNode(rootNode->right, key);
}

NODE * createNode(int key, NODEDATA * value)
{
NODE * node = malloc(sizeof(NODE));
node->key = key;
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
}

NODE ** findPosition(NODE * rootNode, int key)
{
NODE ** targetPos = NULL;

if (!rootNode)
return NULL;

if (key < rootNode->key)
targetPos = &(rootNode->left);
else if (key > rootNode->key)
targetPos = &(rootNode->right);
else if (key == rootNode->key)
return NULL;

if (*targetPos)
return findPosition(*targetPos, key);
else
return targetPos;
}

void insertNode(NODE * rootNode, int key, NODEDATA * value)
{
NODE * newNode = createNode(key, value);
NODE ** newNodePos = findPosition(rootNode, key);

if (newNodePos != NULL)
*newNodePos = newNode;
else
throwException(2);
}

void deleteNode(NODE * rootNode, int key)
{
}

void traverseNode(NODE * rootNode, void(*callbackFunc)(NODEDATA *))
{
if (rootNode == NULL)
return;

traverseNode(rootNode->left, callbackFunc);
callbackFunc(rootNode->value);
traverseNode(rootNode->right, callbackFunc);
}
24 changes: 24 additions & 0 deletions binarySearchTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef __BINARYSEARCHTREE_H__
#define __BINARYSEARCHTREE_H__

#include "common.h"

#include "person.h"
typedef struct _NODEDATA {
PERSON * member;
} NODEDATA;

typedef struct _NODE {
int key;
NODEDATA * value;
struct _NODE * left;
struct _NODE * right;
} NODE;

NODE * createNode(int key, NODEDATA * value);
NODE * findNode(NODE * rootNode, int key);
void insertNode(NODE * rootNode, int key, NODEDATA * value);
void deleteNode(NODE * rootNode, int key);
void traverseNode(NODE * rootNode, void(*callbackFunc)(NODEDATA *));

#endif
110 changes: 97 additions & 13 deletions common.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
#include "common.h"

void throwException(int exception)
{
assert(exception);
}

int getChoice(char * string_list[], int listCount)
{
int i, op;

for (i = 0; i < listCount; i++)
printf("%d. %s\n", i + 1, string_list[i]);

scanf(" %d", &op);

if (!(1 <= op && op <= listCount))
return -1;

else return op;
}

int getString(char * str, int size, int (checkFunc)(char *))
{
int res;
Expand Down Expand Up @@ -29,43 +49,43 @@ int choiceFromList(char * choiceList[], int choiceCount, int startPosition)
switch (keyInput)
{
case KEY_UP:
if (nowPosition <= 2)
if (nowPosition <= startPosition)
{
playShortBeep();
break;
}

setCursorPosition(1, nowPosition);
setTextColor(DEFAULT_TEXT_ATTRIBUTE);
printf("%s\n", choiceList[nowPosition - 2]);
printf("%s\n", choiceList[nowPosition - startPosition]);

nowPosition--;

setCursorPosition(1, nowPosition);
setTextColor(BACKGROUND_INTENSITY);
printf("%s\n", choiceList[nowPosition - 2]);
printf("%s\n", choiceList[nowPosition - startPosition]);
setTextColor(DEFAULT_TEXT_ATTRIBUTE);
break;
case KEY_DOWN:
if (nowPosition >= 3)
if (nowPosition > startPosition)
{
playShortBeep();
break;
}

setCursorPosition(1, nowPosition);
setTextColor(DEFAULT_TEXT_ATTRIBUTE);
printf("%s\n", choiceList[nowPosition - 2]);
printf("%s\n", choiceList[nowPosition - startPosition]);

nowPosition++;

setCursorPosition(1, nowPosition);
setTextColor(BACKGROUND_INTENSITY);
printf("%s\n", choiceList[nowPosition - 2]);
printf("%s\n", choiceList[nowPosition - startPosition]);
setTextColor(DEFAULT_TEXT_ATTRIBUTE);
break;
case KEY_RETURN:
return nowPosition - 2;
return nowPosition - startPosition;
break;
}
}
Expand Down Expand Up @@ -100,7 +120,7 @@ void readMemberDataFromFile()
fclose(dataFile);
}

int nameCheck(char * str)
int checkNameValid(char * str)
{
// rule: korean character

Expand All @@ -114,11 +134,10 @@ int nameCheck(char * str)
return 0;
}

int phoneCheck(char * str)
int checkPhoneValid(char * str)
{
// rule: [0-9]{3}-[0-9]{4}-[0-9]{4}

int memberIdx;
char * token, *destPtr;

int cnt;
Expand Down Expand Up @@ -155,11 +174,76 @@ int phoneCheck(char * str)

// check duplicated

for (memberIdx = 1; memberIdx < getMemberCount(); memberIdx++)
if (findMemberByPhone(str) != NULL)
return PHONE_DUPLICATED;

return 0;
}

int changeMemberName(PERSON * member, STRING str)
{
// check name is valid
int res;
res = checkNameValid(str);

switch (res)
{
if (!strcmp(getMember(memberIdx)->phone, str))
return PHONE_DUPLICATED;
case 0:
strcpy(member->name, str);
return 0;
case NAME_NOT_KOREAN:
printf("%s\n", nameNotKorean);
printf("%s\n", inputNameHelp);
break;
}

return -1;
}

int changeMemberAddress(PERSON * member, STRING str)
{
strcpy(member->address, str);
return 0;
}

int changeMemberPhone(PERSON * member, STRING str)
{
// check phone is valid
int res;
res = checkPhoneValid(str);

switch (res)
{
case 0:
strcpy(member->phone, str);
return 0;
case WRONG_INPUT:
printf("%s\n", wrongInput);
printf("%s\n", inputPhoneHelp);
break;
case PHONE_NOT_DIGIT:
printf("%s\n", phoneNotDigit);
printf("%s\n", inputPhoneHelp);
break;
case PHONE_DUPLICATED:
printf("%s\n", phoneDuplicated);
break;
}

return -1;
}

PERSON * choiceDuplicatedOne(PERSONLIST personList)
{
// choice one in personList
int choice = 0;
int i;

printf("%s\n", message_matchedManyMembers);

for (i = 0; i < personList.memberCnt; i++)
printf("%d. %d\n", i + 1, personList.member[i]->memberId);
scanf(" %d", &choice);

return personList.member[choice - 1];
}
17 changes: 13 additions & 4 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,27 @@
#include "constants.h"
#include "messages_ko-KR.h"

#include "dataStructure.h"
#include "dataStructure_BST.h"
#include "mainFunction.h"
#include "consoleFunction.h"

#pragma warning (disable:4996)

typedef void(*funcPointer)();
typedef char * STRING;

void throwException(int exception);
int getChoice(char * string_list[], int listCount);
int getString(char * str, int size, int (checkFunc)(char *));
int choiceFromList(char * choiceList[], int choiceCount, int startPosition);
FILE * openDataFile(char *opt);
void readMemberDataFromFile();
int nameCheck(char * str);
int addressCheck(char * str);
int phoneCheck(char * str);
int checkNameValid(char * str);
int checkAddressValid(char * str);
int checkPhoneValid(char * str);
int changeMemberName(PERSON * member, STRING str);
int changeMemberAddress(PERSON * member, STRING str);
int changeMemberPhone(PERSON * member, STRING str);
PERSON * choiceDuplicatedOne(PERSONLIST personList);

#endif
4 changes: 3 additions & 1 deletion consoleFunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ void setTextColor(int Color)
int getKeyInput()
{
short isEscPress = 0;
short isReturnPress;

while (!isEscPress)
{
isEscPress = GetAsyncKeyState(VK_ESCAPE);
isReturnPress = abs(GetKeyState(VK_RETURN)) % 2;

if (GetAsyncKeyState(VK_UP) & SHRT_MAX)
return KEY_UP;
Expand All @@ -61,7 +63,7 @@ int getKeyInput()
return KEY_LEFT;
else if (GetAsyncKeyState(VK_RIGHT) & SHRT_MAX)
return KEY_RIGHT;
else if ((GetKeyState(VK_RETURN) & SHRT_MAX) == 1)
else if (isReturnPress != abs(GetKeyState(VK_RETURN)) % 2)
return KEY_RETURN;
}

Expand Down
3 changes: 3 additions & 0 deletions constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
#define PHONE_NOT_DIGIT 2
#define PHONE_DUPLICATED 3

// exception code
#define KEY_IS_DUPLICATED 1

#endif
30 changes: 0 additions & 30 deletions dataStructure.c

This file was deleted.

11 changes: 0 additions & 11 deletions dataStructure.h

This file was deleted.

Loading

0 comments on commit b269c93

Please sign in to comment.