Skip to content

Instantly share code, notes, and snippets.

@hiroshiro
hiroshiro / towersofhanoi.c
Created March 18, 2015 10:43
再帰と後戻りアルゴリズム ハノイの塔 CポインタRichard Reeseオライリー・ジャパン
#include <stdio.h>
void TowersOfHanoi();
int main()
{
int n = 3;
char a = 'a';
char b = 'b';
char c = 'c';
TowersOfHanoi(n, a, b, c);
}
@hiroshiro
hiroshiro / reflective_print.c
Created March 18, 2015 10:42
再帰呼び出しテスト詳説 CポインタRichard Reeseオライリー・ジャパン
#include <stdio.h>
int Print();
int main()
{
int p = 4;
printf("Value of Print: %d\n", Print(p));
}
int Print(int n) { // 数を1からnまで後ろ向きに印刷する
if(n == 0) // これが終了ベースケース
@hiroshiro
hiroshiro / writefile01.c
Created March 18, 2015 10:34
'0'から'9'までの10文字をファイル(WriteData.txt)に書き込む。
/* WriteFile01.c */
/* '0'から'9'までの10文字をファイル(WriteData.txt)に書き込む。 */
#include <stdio.h>
int main(void)
{
FILE *pFile = NULL; // ファイルポインタを定義
char *pszInFileName = "WriteData.txt"; // 書き込みファイル名
char *writeData = "0123456789"; // 書き込むデータとしての文字列
@hiroshiro
hiroshiro / memoryalloc01.c
Last active August 29, 2015 14:17
malloc free関数のtest
/* MemoryAlloc01.c */
/* 動的に配列的なデータ域を確保する。 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *piData;
printf("int *piData; piDataのポインタ : %p\n", piData); //定義前のポインタの値を表示
piData = NULL; //確保したメモリエリアのポインタを格納する変数と定義
@hiroshiro
hiroshiro / stack.c
Last active August 29, 2015 14:17
複数のスタックを持てるようにする。
#include <stdbool.h>
#include "stack.h"
static bool isStackFull(const Stack *p) {
return p->top == p->size;
}
static bool isStackEmpty(const Stack *p) {
return p->top == 0;
}
@hiroshiro
hiroshiro / stack.c
Last active August 29, 2015 14:17
古典的なスタック実装
#include <stdbool.h>
#include "stack.h"
int buf[16];
int top = 0;
static bool isStackFull(void) {
return top == sizeof(buf) / sizeof(int);
}
@hiroshiro
hiroshiro / nogetinfoserver.c
Last active August 29, 2015 14:16
LinuxネットワーキングプログラミングバイブルTCP/IPプログラミング入門Chapter03 getaddinf()を利用しないserver_socket()IP4
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
@hiroshiro
hiroshiro / Makefile.client
Last active August 29, 2015 14:16
LinuxネットワーキングプログラミングバイブルTCP/IPプログラミング入門Chapter01 socket client Makefile
PROGRAM = client
OBJS = client.o
SRCS = $(OBJS:%.o=%.c)
CFLAGS = -g -Wall
LDFLAGS =
$(PROGRAM):$(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $(PROGRAM) $(OBJS) $(LDLIBS)
@hiroshiro
hiroshiro / pointer01.c
Created March 7, 2015 15:31
Cポインタを銀行口座として例える
/*
C言語のポインタのツボとコツがゼッタイにわかる本
著者 石黒 尚久
発行所 秀和システム
p47
*/
#include <stdio.h>
int main(void)
@hiroshiro
hiroshiro / dijkstra-s-algorithm.c
Created February 16, 2015 10:30
Dijkstra's algorithm C Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define BIG_EXAMPLE
typedef struct node_t node_t, *heap_t;
typedef struct edge_t edge_t;
struct edge_t {
node_t *nd; /* target of this edge */