Last active
March 5, 2021 15:43
-
-
Save KeitetsuWorks/e154fb496ec3b4f651790639c4b8e55f to your computer and use it in GitHub Desktop.
1から10000までの整数が格納されているファイルからN個の整数を読込み、配列に格納する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file q10175187735.c | |
* @brief ファイルからN個の整数を読込み、配列に格納する | |
* @author Keitetsu | |
* @date 2017/06/11 | |
* @copyright Copyright (c) 2017 Keitetsu | |
* @par License | |
* This software is released under the MIT License. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define DATA_MIN 1 /**< 最小読込みデータ数 */ | |
#define DATA_MAX 16 /**< 最大読込みデータ数 */ | |
#define DATA_VALUE_MIN 1 /**< データ最小値 */ | |
#define DATA_VALUE_MAX 10000 /**< データ最大値 */ | |
/** | |
* @var input_fname | |
* @brief 読込むファイル名 | |
*/ | |
const char *input_fname = "rnd.txt"; | |
/** | |
* @brief データが最小値と最大値の要件を満たしているか検証する | |
* @param[in] data データ | |
* @retval 0 要件に一致 | |
* @retval 1 要件に不一致 | |
*/ | |
static int validateData(int data); | |
int main(void) | |
{ | |
int d[DATA_MAX], d_tmp; | |
int i, j, data_num; | |
FILE *input_fp; | |
/* 配列の初期化 */ | |
for (i = 0; i < DATA_MAX; i++) { | |
d[i] = 0; | |
} | |
/* ファイルのオープン */ | |
input_fp = fopen(input_fname, "r"); | |
if (input_fp == NULL) { | |
printf("%s のオープンに失敗しました。\n", input_fname); | |
exit(EXIT_FAILURE); | |
} | |
/* 読込みデータ数の指定 */ | |
printf("%s から読込むデータ数: ", input_fname); | |
scanf("%d", &data_num); | |
if (data_num < DATA_MIN || data_num > DATA_MAX) { | |
printf("読込むデータ数は %d から %d の間で指定します。\n", | |
DATA_MIN, DATA_MAX); | |
exit(EXIT_FAILURE); | |
} | |
/* ファイルからのデータ読込み */ | |
i = 0; | |
while ((fscanf(input_fp, "%d", &d_tmp) != EOF) && (i < data_num)) { | |
if (validateData(d_tmp) == 0) { | |
d[i] = d_tmp; | |
i++; | |
} | |
else { | |
printf("%d は読込みデータの要件を満たしていません。\n", d_tmp); | |
printf("読込みをスキップします。\n"); | |
} | |
} | |
if (i != data_num) { | |
printf("%s から読込んだデータ数は %d です。\n", | |
input_fname, i); | |
printf("データ数が不足しています。\n"); | |
exit(EXIT_FAILURE); | |
} | |
fclose(input_fp); | |
/* 昇順バブルソート */ | |
for (i = 0; i < (data_num - 1); i++) { | |
for (j = (data_num - 1); j > i; j--) { | |
if (d[j - 1] > d[j]) { | |
d_tmp = d[j - 1]; | |
d[j - 1] = d[j]; | |
d[j] = d_tmp; | |
} | |
} | |
} | |
/* 読込んだデータを昇順表示 */ | |
printf("%s から読込んだデータ(昇順): \n", input_fname); | |
for (i = 0; i < data_num; i++) { | |
printf("%d\n", d[i]); | |
} | |
exit(EXIT_SUCCESS); | |
} | |
static int validateData(int data) | |
{ | |
int retval; | |
if (data >= DATA_VALUE_MIN && data <= DATA_VALUE_MAX) { | |
retval = 0; | |
} | |
else { | |
retval = 1; | |
} | |
return retval; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 | |
1032 | |
9873 | |
-1 | |
432 | |
5060 | |
345 | |
3 | |
10001 | |
87 | |
301 | |
7890 | |
6541 | |
33 | |
214 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment