RmakeでC言語の構造体
投稿者:
ラジアン
投稿日:2013/02/14 23:27
C言語で使う構造体を無理やり配列で処理してみようと思います。
上がC言語での構造体の宣言の例です。これをRmakeで使えるようにしてみます。
使い方の例はこちらをクリック→→→ジャンプ
以下を開始スクリプトに貼り付ける。
使い方の例(上の構造体の例に合わせています。)
構造体の配列化も取り入れたかったけど、さすがにややこしくなったのでやめました。
(文字列を別の配列に入れることで、構造体の配列化を擬似的に取り入れることが出来ます。)
ツイート
struct sankaku{
int teihen;
int takasa;
int menseki;
};
struct bun{
char *word;
struct bun *next;
};
上がC言語での構造体の宣言の例です。これをRmakeで使えるようにしてみます。
使い方の例はこちらをクリック→→→ジャンプ
以下を開始スクリプトに貼り付ける。
setVariable("struct", createArray())
getVariable("struct")[0] = createArray()
getVariable("struct")[0][0] = ""; getVariable("struct")[0][1] = 0
getVariable("struct")[0][2] = createArray(); getVariable("struct")[0][2][0] = 0
getVariable("struct")[0][3] = 0
def getStructPoint2(point, w1)
arr_w1 = createArray(); arr_w1 = splitString(w1, "->")
if point == 0
i = 0
else
i = 1
end
ii = point; flg1 = true
while (i < getArrayLength(arr_w1)) && (flg1)
n = point; m = 0; flg = false
while (m < getVariable("struct")[ii][1]) && (!flg)
if arr_w1[i] == getVariable("struct")[getVariable("struct")[ii][2][m]][0]
n = getVariable("struct")[ii][2][m]
flg = true
end
m = m + 1
end
if n == point
flg1 = false
end
i = i + 1; ii = n
end
if ii == point
speak("ERROR : 変数がありません。")
else
return ii
end
end
def getStructPoint(w1)
return getStructPoint2(0, w1)
end
def getStruct2(point, w1)
ii = getStructPoint2(point, w1)
if getVariable("struct")[ii][1] != 0
speak("ERROR : 「->」での指定の数が足りていません。")
else
return getVariable("struct")[ii][3]
end
end
def getStruct(w1)
return getStruct2(0, w1)
end
def setStruct2(point, w1, w2)
arr_w1 = createArray(); arr_w1 = splitString(w1, "->")
if point == 0
i = 0
else
i = 1
end
ii = point
while i < getArrayLength(arr_w1)
n = 0; m = 0; flg = false
while (m < getVariable("struct")[ii][1]) && (!flg)
if arr_w1[i] == getVariable("struct")[getVariable("struct")[ii][2][m]][0]
n = getVariable("struct")[ii][2][m]
flg = true
end
m = m + 1
end
if n == 0
s_len = getArrayLength(getVariable("struct"))
getVariable("struct")[ii][2][getVariable("struct")[ii][1]] = s_len
getVariable("struct")[ii][1] = getVariable("struct")[ii][1] + 1
getVariable("struct")[s_len] = createArray()
getVariable("struct")[s_len][0] = arr_w1[i]; getVariable("struct")[s_len][1] = 0
getVariable("struct")[s_len][2] = createArray(); getVariable("struct")[s_len][2][0] = 0
getVariable("struct")[s_len][3] = w2
ii = s_len
else
getVariable("struct")[n][3] = w2
ii = n
end
i = i + 1
end
end
def setStruct(w1, w2)
setStruct2(0, w1, w2)
end
使い方の例(上の構造体の例に合わせています。)
setStruct("三角形A->底辺", 1); setStruct("三角形A->高さ", 6)
setStruct("三角形A->面積", getStruct("三角形A->底辺") * getStruct("三角形A->高さ") / 2)
speak("三角形A->面積 = " + getStruct("三角形A->面積")) #出力「三角形A->面積 = 3」
setStruct("a->word", "this"); setStruct("b->word", "is")
setStruct("c->word", "a"); setStruct("d->word", "pen")
setStruct("a->next", getStructPoint("b")); setStruct("b->next", getStructPoint("c"))
setStruct("c->next", getStructPoint("d")); setStruct("d->next", 0)
w = ""; point = getStructPoint("a")
while point != 0
w = w + getStruct2(point, "->word") + " "
point = getStruct2(point, "->next")
end
speak(w) #出力「this is a pen 」
構造体の配列化も取り入れたかったけど、さすがにややこしくなったのでやめました。
(文字列を別の配列に入れることで、構造体の配列化を擬似的に取り入れることが出来ます。)
コメントする
コメントするには、ログインする必要があります。
コメント一覧
qhqh123(投稿日:2013/02/15 06:54,
履歴)
なるほど 理解不可能