0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[paiza]C言語のストリーム出力をする

Posted at

原則、paizaでfopen関数は使えない。

ストリーム出力だけなら、次のコードでも出力だけはできる。

<コード例>

#include
#include
#define NAME_LEN 50
#define NINZU_MAX 100

int main( void ){
FILE *fp;
int i, j;
int ninzu = 0;
char name[ NAME_LEN ][ NINZU_MAX ];
double height[ NINZU_MAX ];
double weight[ NINZU_MAX ];
double hsum = 0;
double wsum = 0;

if ( ( fp = fopen ( "abc.txt", "r" ) ) == NULL )
    printf ( "そのファイルをオープンできません。\n" );
else {
    for ( i = 0; i < NINZU_MAX; i++ ) {
        if ( fscanf ( fp, "%s%lf%lf",
            /* 名前・身長・体重の3項目の読み込みができれば3が返却される */
            name[ ninzu ], &height[ ninzu ], &weight[ ninzu ] ) != 3 )
            break;
        hsum += height[ ninzu ];
        wsum += weight[ ninzu ];
        ninzu++;
    }
    /* 身長の昇順にソート */
    for ( i = 0; i < ninzu - 1; i++ ){
        for ( j = ninzu - 1; j < i ; j-- ){
            if ( height[ j - 1 ] > height[ j ] ){
                char tn[ NAME_LEN ];
                double td;
                strcpy ( tn, name[ j ] );
                strcpy ( name[ j ], name[ j - 1 ] );
                strcpy ( name[ j - 1 ], tn );
                td = height[ i ]; height[ j ] = height[ j - 1 ]; height[ j - 1 ] = td;
                td = weight[ i ]; weight[ j ] = weight[ j - 1 ]; weight[ j - 1 ] = td;
            }
        }
        for ( i = 0; i < ninzu; i++ )
            printf ( "%-10s %5.1f %5.1f \n", name[ i ], height[ i ], weight[ i ] );
        printf ( "--------------------\n" );
        printf ( "平均      %5.1f %5.1f\n", hsum / ninzu, wsum / ninzu );

        fclose ( fp );
    }
}
return 0;

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?