[02b2] 情報隠蔽されたバッファoddsum

次のような内容のヘッダファイルoddsum.hがある。

struct oddsum;
typedef struct oddsum *oddsump;
oddsump oddsum_new(void);
void oddsum_put(oddsump r, int x);
int oddsum_get(oddsump r);

このAPIは、まずoddsum_newで作成したバッファに対して1回以上 oddsum_putで整数を投入した後、oddsum_getで値を取得すると、最初に 投入されたものを1番目として、奇数番目のものの和が返される(getよ り前に必ず1回は_putが呼ばれるものとしてよい)。このAPIの実装を作 成せよ。下請け関数は使用しないこととする。

記号列 

コード 

選択肢

ア #include <stdbool.h>
イ #include <stdlib.h>
ウ #include "maxbuf.h"
エ #include "oddsum.h"
オ struct maxbuf { int flag, val; };
カ struct oddsum { int flag, val; };
キ maxbufp maxbuf_new(void) {
ク oddsump oddsum_new(void) {
ケ void maxbuf_put(maxbufp r, int x) {
コ void oddsum_put(oddsump r, int x) {
サ int maxbuf_get(maxbufp r) {
シ int oddsum_get(oddsump r) {
ス }
セ maxbufp r = (maxbufp)malloc(sizeof(struct maxbuf));
ソ oddsump r = (oddsump)malloc(sizeof(struct oddsum));
タ if(r->flag || r->val < x) {
チ if(r->flag) {
ツ } else if(r->val < x) {
テ r->flag = !(r->flag);
ト r->flag = false;
ナ r->flag = true;
ニ r->val = 0;
ヌ r->val = r->val + x;
ネ r->val = x;
ノ return r->val;
ハ return r;

選択肢の行をドラグして上のコード領域に配置してください。 コード領域の行はドラグにより位置が変更できます。 削除したい場合は選択肢の領域に戻してください。