期末テストの解答(例)
ペーパーテスト
Ⅰ
(ⅰ) C  (ⅱ) B  (ⅲ) C  
Ⅱ
(ⅰ) D
(ⅱ) (1)  int *px   (2)  int *py  
(3)  &x   (4)  &y  
(ⅲ) (1)  struct point *pp   
          (2)  int t = pp -> x;
Ⅲ
(1) *str    (2)  len
オンラインテスト
feb15_1.cの解答例
#include <stdio.h>
int parenDepth(char str[]) {
    int n, d=0, max=0, pos=-1;
    for(n=0; str[n]; n++) {
        if (str[n]=='(') {
            d++;
            if (d>max) {
                max = d;
                pos = n;
            }
        } else if (str[n]=='(') {
            d--;
        }   
    }
    return pos;
}
int main(void) {
    char str1[] = "()";
    char str2[] = "((()())((())))";
    printf("%sの最深レベルのかっこの位置は %dです。\n", str1, parenDepth(str1));
    printf("%sの最深レベルのかっこの位置は %dです。\n", str2, parenDepth(str2));
    return 0;
}
feb15_2.cの解答例
#include <stdio.h>
struct mytime {
    int minute; /* 分を表す */
    int second; /* 秒を表す */
};
int countDown(struct mytime *p, int sec) {
    p->second -= sec;
    if(p->second < 0) {
        p->second += 60;
        p->minute -= 1;
    }
    if(p->minute < 0) {
        return 1;
    } else {
        return 0;
    }
}
int main(void) {
    struct mytime mt = {1, 10};
    while(!countDown(&mt, 10)) {
        printf("残り時間は %2d分 %02d秒です。\n", mt.minute, mt.second);
    }
    printf("残り時間がなくなりました。\n");
    return 0;
}
戻る
Koji Kagawa(kagawa@eng.〜)