Created
July 28, 2013 16:18
-
-
Save shun159/6099149 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'inline' | |
class Test | |
def run_main() | |
run | |
end | |
inline do |builder| builder.c <<-EOF | |
/* header files */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
/* macros */ | |
#define N 8 | |
/* functions */ | |
int ArrayPush(int *stack, int data, int *sp, size_t n); | |
int ArrayPop(int *stack, int *sp); | |
/* main */ | |
int run(void) { | |
int data; | |
int stack[N]; /* 配列 (スタック) */ | |
int sp = 0; /* スタックポインタ */ | |
/* 配列にプッシュする */ | |
ArrayPush(stack, 10, &sp, N); | |
printf("Push: %d\n", 10); | |
ArrayPush(stack, 20, &sp, N); | |
printf("Push: %d\n", 20); | |
ArrayPush(stack, 30, &sp, N); | |
printf("Push: %d\n", 30); | |
ArrayPush(stack, 40, &sp, N); | |
printf("Push: %d\n", 40); | |
/* 配列からポップする */ | |
while ( sp ) { | |
data = ArrayPop(stack, &sp); | |
printf("Pop: %d\n", data); | |
} | |
return EXIT_SUCCESS; | |
} | |
/** | |
* 配列にプッシュする | |
* @param[in,out] stack 配列 | |
* @param[in] data 配列にプッシュする値 | |
* @param[in,out] sp スタックポインタ | |
* @param[in] n 配列の最大要素数 | |
* @retval >=1 配列に格納されている要素の数 | |
* @retval 0 配列にプッシュに失敗 | |
*/ | |
int ArrayPush(int *stack, int data, int *sp, size_t n) { | |
if ( *sp < n ) { | |
stack[(*sp)++] = data; | |
return *sp; | |
} else { | |
return 0; | |
} | |
} | |
/** | |
* 配列からポップする | |
* @param[in] stack 配列 | |
* @param[in,out] sp スタックポインタ | |
* @return ポップした値 | |
*/ | |
int ArrayPop(int *stack, int *sp) { | |
if ( *sp > 0 ) { | |
return stack[--(*sp)]; | |
} else { | |
return 0; | |
} | |
} | |
EOF | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment