解答例 6-5-1

【問1】

#include <stdio.h>

int main( void )
{
        char houhou;
        int data1, data2, kekka = 0;

        printf( "計算方法の入力( + - * / )" );
        scanf( "%c", &houhou );
        printf( "整数値1の入力 " );
        scanf( "%d", &data1 );
        printf( "整数値2の入力 " );
        scanf( "%d", &data2 );

        switch ( houhou ) {
                case    '+':
                        kekka = data1 + data2;
                        break;
                case    '-':
                        kekka = data1 - data2;
                        break;
                case    '*':
                        kekka = data1 * data2;
                        break;
                case    '/':
                        if (data2 != 0)
                                kekka = data1 / data2;
                        else
                                printf("0で割ることはできません\n");
                        break;
                default:
                        printf( "計算方法の入力エラーです\n" );
                        break;
        }
        printf( "kekka = %d\n", kekka );

        return 0;
}

【問2】

#include <stdio.h>

int main( void )
{
	int no;

	printf( "input number( 1-10 ) " );
	scanf( "%d", &no );

	switch ( no ) {
		case    1:
		case    2:
		case    3:
			printf( "1:one   2:two   3:three\n" );
			break;
		case    4:
		case    5:
		case    6:
			printf( "4:four  5:five  6:six\n" );
			break;
		case    7:
		case    8:
		case    9:
		case    10:
			printf( "7:seven 8:eight 9:nine  10:ten\n" );
			break;
		default:
			printf( "number input error\n" );
			break;
	}
	return 0;
}

戻る


banner初心者のためのポイント学習C言語」
Copyright(c) 2000-2004 TOMOJI All Rights Reserved