E D R , A S I H C RSS

서민관

서민관

소개

  • 09학번
  • 컴퓨터공학부
  • ZeroPager
  • 공부잘함
  • 취향을 존중해드려야..
  • 정보통신부

내용

"Test String"

OMS

  • 못 한 것들
인공신경망(ANN, Artificial Neural Network) - 2
Neuro-Fuzzy System - 3
유전 알고리즘(GA, Genetic Algorithm)
데이터 마이닝 - 연관 규칙 분류기(Associative Rule based Classifier) : CPAR
XML-RPC을 이용한 네이버 블로그 글 올리기

코드

  • Map in C
    Map.h

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int BOOL;
#define TRUE 1
#define FALSE 0

typedef struct Map {
	void (*put)(char *key, int value);
	int (*get)(char *key);
	BOOL (*contains)(char *key);
	void (*remove)(char *key);
	void (*clear)();
} Map;

Map *createMap();

/*
// Commented for private using in Map.
// To watch details, watch Map.cpp
void put(char *key, int value);
int get(char *key);
BOOL contains(char *key);
void removePairFromMap(char *key);
void clear();
//*/

Map.c
#include "Map.h"

typedef struct KeyValuePair {
	char *key;
	int value;
	struct KeyValuePair *next;
} KeyValuePair;

// functions for KeyValuePair List

KeyValuePair *createPair(char *key, int value) {
	KeyValuePair *ret = (KeyValuePair *)malloc(sizeof(KeyValuePair));
	memset(ret, 0, sizeof(KeyValuePair));
	
	ret->key = (char *)malloc(sizeof(char) * (strlen(key) + 1));
	strcpy(ret->key, key);
	ret->value = value;
	
	return ret;
}

void removePair(KeyValuePair **pair) {
	free((*pair)->key);

	free(*pair);

	*pair = NULL;
}

void addPair(KeyValuePair **head, KeyValuePair *keyValuePair) {
	if ( *head == NULL ) {
		*head = keyValuePair;
	} else {
		KeyValuePair *temp = *head;

		while ( temp->next != NULL ) {
			if ( strcmp(temp->key, keyValuePair->key) == 0 ) {
				return;
			}
			temp = temp->next;
		}

		temp->next = keyValuePair;
	}
}

KeyValuePair *getPair(KeyValuePair **head, char *key) {
	if ( *head == NULL ) {
		return NULL;
	} else if ( strcmp((*head)->key, key) == 0 ) {
		KeyValuePair *ret = *head;

		*head = (*head)->next;

		return ret;
	} else {
		KeyValuePair *successor = *head;
		KeyValuePair *ret = (*head)->next;

		while ( ret != NULL ) {
			if ( strcmp(ret->key, key) == 0 ) {
				successor->next = ret->next;
				ret->next = NULL;

				return ret;
			}

			successor = ret;
			ret = ret->next;
		}

		return NULL;
	}
}

BOOL hasKey(KeyValuePair **head, char *key) {
	if ( *head != NULL ) {
		KeyValuePair *temp = *head;

		while ( temp != NULL ) {
			if ( strcmp(temp->key, key) == 0 ) {
				return TRUE;
			}

			temp = temp->next;
		}
	}

	return FALSE;
}

void clearList(KeyValuePair **head) {
	if ( *head == NULL ) {
	} else if ( *head != NULL && (*head)->next == NULL ) {
		removePair(head);
	} else {
		KeyValuePair *successor = *head;
		KeyValuePair *temp = (*head)->next;

		while ( temp != NULL ) {
			removePair(&successor);
			successor = temp;
			temp = temp->next;
		}

		removePair(&successor);
	}

	*head = NULL;
}

KeyValuePair *head = NULL; // Real KeyValuePair List Head.

// functions for Map

void put(char *key, int value) {
	addPair(&head, createPair(key, value));
}

int get(char *key) {
	KeyValuePair *temp = getPair(&head, key);
	
	if ( temp != NULL ) {
		return temp->value;
	}

	return 0;
}

BOOL contains(char *key) {
	return hasKey(&head, key);
}

void removePairFromMap(char *key) {
	getPair(&head, key);
}

void clear() {
	clearList(&head);
}

Map *createMap() {
	Map *ret = (Map *)malloc(sizeof(Map));
	memset(ret, 0, sizeof(Map));

	ret->put = put;
	ret->get = get;
	ret->contains = contains;
	ret->remove = removePairFromMap;
	ret->clear = clear;

	return ret;
}

  • main.c

#include <assert.h>

#include "Map.h"

int main() {
	Map *map = createMap();

	map->put("a", 10);
	map->put("b", 20);
	map->put("c", 30);
	map->put("d", 40);
	
	assert(map->contains("c") == TRUE);
	assert(map->contains("d") == TRUE);
	assert(map->get("b") == 20);

	map->remove("c");

	assert(map->contains("c") == FALSE);

	map->clear();

	assert(map->contains("a") == FALSE);
	assert(map->contains("b") == FALSE);
	assert(map->contains("c") == FALSE);
	assert(map->contains("d") == FALSE);

	map->put("b", 20);

	map->put("b", 30);

	assert(map->get("b") == 20);

	map->clear();

	return 0;
}

  • 형한텐 항상 배울게 많네요. 최근 자바로 짜는 프로그램은 개발방식이 형이 하던거랑 꽤나 비슷해진거 같구요. 다음에 간단한 프로젝트 코드레이스라도 해보면 좋을거 같아요. ㅎㅎ 형, 겸손한것도 좋지만 자랑하는 것도 능력이에요. 형의 능력을 뽐내봐요! ㅎㅎㅎ -김태진

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2015-02-13 02:48:54
Processing time 0.0870 sec