Top >C言語 練習問題集

10. 構造体

練習問題 10 - 1

int 型のメンバー x と y を持つ構造体 POINT を定義し、以下のプログラムを実行しなさい


struct POINT
{
    int     x;
    int     y;
};

void main( void )
{
    struct POINT aBigDipper[] = { {8,5}, {11,1}, {25,2},
                                {26,6}, {36,9}, {46,12}, {58,12} };
    char    space[15][61];
    int     i, j;

    for( i = 0 ; i < 15 ; i++ )
    {
        for( j = 0 ; j < 60 ; j++ )
            space[i][j] = ' ';

        space[i][j] = 0;
    }

    for( i = 0 ; i < 7 ; i++ )
        space[ aBigDipper[i].y ][ aBigDipper[i].x ] = '*';

    for( i = 0 ; i < 15 ; i++ )
        printf( "%s\n", space[i] );
}

練習問題 10 - 2

何月であるかを表す数値(1~12)と降水量(整数:mm)をメンバーとする構造体を定義し、4ヵ月分の月と降水量を入力して表示するプログラムを作成しなさい


struct PRECIPITATION
{
    int    month;
    int     milli;
};

void main( void )
{
    struct PRECIPITATION aPrec[4];
    int    i;

    for( i = 0 ; i < 4 ; i++ )
    {
        printf( "何月ですか?\n" );
        scanf( "%d", &aPrec[i].month );
        printf( "降水量は?\n" );
        scanf( "%d", &aPrec[i].milli );
    }

    for( i = 0 ; i < 4 ; i++ )
        printf( " %d月 %4dmm\n", aPrec[i].month, aPrec[i].milli );
}

練習問題 10 - 3

以下の内容のプログラムを作成しなさい

  • int型のメンバー x と y を持つ構造体 POINT を定義する
  • 構造体 POINT を引数とし、各メンバーの値を2倍にした POINT を戻り値とする関数を定義する
  • 構造体 POINT 型の変数の各メンバーに値を設定し、作成した関数によって値を2倍にする
  • 構造体 POINT 型の変数の各メンバーの値を表示する

struct POINT
{
    int    x;
    int     y;
};

struct POINT mul2( struct POINT pnt )
{
    pnt.x *= 2;
    pnt.y *= 2;

    return pnt;
}

void main( void )
{
    struct POINT p;

    p.x = 12;
    p.y = 7;

    p = mul2( p );

    printf( "x=%d, y=%d\n", p.x, p.y );
}

練習問題 10 - 4

以下の内容のプログラムを作成しなさい

  • 学生の試験結果を表す、次のメンバーを持った構造体を定義する
    ・名前(char型配列)
    ・試験の得点(int型)
    ・成績評価(char型)
  • 成績評価を設定する関数を定義する
    ・作成した構造体のポインタを引数とする
    ・試験の得点によって成績評価に文字を設定する
        80点以上→'A'、70点以上→'B'、60点以上→'C'、60点未満→'D'
  • 作成した構造体の変数に、名前と試験の得点を入力する
  • 作成した関数によって、成績評価を設定する
  • 構造体の内容(名前、試験の得点、成績評価)を表示する

struct EXAMRESULTS
{
    char    name[50];    // 名前
    int     score;       // 得点
    char    result;      // 成績
};

void SetResult( struct EXAMRESULTS* pExRes )
{
    if( pExRes->score >= 80 )
        pExRes->result = 'A';
    else if( pExRes->score >= 70 )
        pExRes->result = 'B';
    else if( pExRes->score >= 60 )
        pExRes->result = 'C';
    else
        pExRes->result = 'D';
}

void main( void )
{
    struct EXAMRESULTS exres;

    printf( "名前は?\n" );
    scanf( "%s", exres.name );
    printf( "得点は?\n" );
    scanf( "%d", &exres.score );

    SetResult( &exres );

    printf( "%s、%d点、成績 %c\n", exres.name, exres.score, exres.result );
}

練習問題 10 - 5

前問で作成した構造体と関数を使用して、4人分の名前と得点を入力し、評価を設定して表示するプログラムを作成しなさい


void main( void )
{
    struct EXAMRESULTS    aExRes[4];
    int     i;

    for( i = 0 ; i < 4 ; i++ )
    {
        printf( "名前は?\n" );
        scanf( "%s", aExRes[i].name );
        printf( "得点は?\n" );
        scanf( "%d", &aExRes[i].score );

        SetResult( aExRes + i );
    }

    for( i = 0 ; i < 4 ; i++ )
        printf( "%s、%d点、成績 %c\n",
                aExRes[i].name, aExRes[i].score, aExRes[i].result );
}

練習問題 10 - 6

以下は、都市の名前と位置を表示するプログラムである
名前と位置を一覧表示する関数 ShowCityInfo を完成させなさい

引数は、CITYINFO構造体の配列と配列の要素数である


struct POSITION
{
    double  latitude;     // 緯度
    double  longitude;    // 経度
};

struct CITYINFO
{
    char    name[50];
    struct POSITION    pos;
};

void ShowCityInfo( struct CITYINFO* aCity, int count )
{
    int i;

    for( i = 0 ; i < count ; i++ )
        printf( "\n%s、緯度:%f、経度:%f", aCity[i].name,
                aCity[i].pos.latitude, aCity[i].pos.longitude );
}

void main( void )
{
    struct CITYINFO aCity[] = { { "東京", 35.41, 139.45 },
                                { "大阪", 34.41, 135.29 },
                                { "名古屋", 35.11, 136.54 } };

    ShowCityInfo( aCity, 3 );
}
PAPER BOWL
NEZEN