[ create a new paste ] login | about

Link: http://codepad.org/4gKoDGev    [ raw code | output | fork ]

C, pasted on Jan 29:
/*
学生のテストの点数は以下のような形式でテキチュトファイルに記録されているものとする

 48, 29, 38, 52, 92, 78, 37, 65, 77, 34, 78, 85, 20, 67,  8, 43,  6, 72, 51, 83
 77, 90, 81, 63, 90, 68, 66, 30, 53, 38, 33, 66, 25, 66, 92, 36, 27, 72, 44, 89
 62, 13, 87, 75, 81, 57, 91, 28, 68, 66, 81, 50, 51, 42, 23, 82, 79, 88, 48, 25
  4, 21, 19, 54, 82, 10,  2, 44, 29, 16, 17, 58, 98, 11, 73, 13,  0, 45, 26, 99
 86, 48, 69, 61,  3, 21, 74, 79, 36, 37, 48, 47, 50, 48, 91, 22,  3, 50, 20, 18
 -1

データの終わりは「特別な値で知らせる」方法をとり、その値は -1 とする
*/

#include <stdlib.h>
#include <stdio.h>

#define MAXLINE 10

int custom_read( char *s, int lim, FILE *pfile ) {
	int c, i;
	for ( i = 0; i < lim - 1 && ( c = fgetc( pfile ) ) != EOF && c != '\n' && c != ','; ++i ) {
		s[ i ] = c;
	}
	s[i] = '\0';

	return i;
}

void printbar( int l ) {
	printf( "[%2d]:", l );
	for ( ; l != 0; --l ) {
		printf( "+" );
	}
}

int main( int argc, char **argv ) {
	FILE *fp;
	char s[ MAXLINE ];
	int i;
	int hg[ 11 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

	if ( argc < 2 ) {
		printf( "usage a filename" );
		return -1;
	}
	if ( ! ( fp = fopen( argv[1], "r" ) ) ) {
		fprintf( stderr, "%s can't open.\n", argv[1] );
		return -2;
	}

	/* データリード部 */
	while( custom_read( s, MAXLINE, fp ) ) {
		int n = atoi( s );
		if ( n == -1 ) {
			break;
		}
		++hg[ n / 10 ];
	}

	/* データ出力部 */
	printf( "  階級  度数:ヒストグラム\n" );
	for ( i = 0; i != 11; ++i ) {
		if ( i != 10 ) {
			printf( "%3d-%3d ", i * 10, i * 10 + 9 );
		} else {
			printf( "100     " );
		}
		printbar( hg[ i ] );
		printf( "\n" );
	}

	return 0;
}


Output:
1
2
usage a filename
Exited: ExitFailure 255


Create a new paste based on this one


Comments: