1. 使用するプログラム言語はC言語とする。
2. プログラムはコンソールプログラムとする。
3. プログラムファイル(ntsSort.exe)の実行時にコマンドライン引数としてnレコードのデータ(商品コードと商 品名)が格納されたファイル(商品ファイル)のファイル名を与える。
4. プログラムは、商品ファイルを読み込み、各レコードの商品コード(先頭から3バイト)をキーとして昇順に ソートし、ソート結果を標準出力に出力する。
商品ファイルの内容のサンプルとプログラム起動及び実行結果イメージを以下に記す。
商品ファイル(test.dat)の内容のサンプル
106 消しゴム
019 シャープペンシル替え芯
047 ボールペン(黒)
085 万年筆
016 シャープペンシル
066 ステープラ
023 ボールペン(赤)
プログラム起動及び実行結果出力イメージ
016 シャープペンシル
019 シャープペンシル替え芯
023 ボールペン(赤)
047 ボールペン(黒)
066 ステープラ
085 万年筆
106 消しゴム
5. 商品ファイルのレコード数は、2以上の任意の数とする。
6. 商品ファイルの改行コードはCrLf(0x0d0a)とする。
7. 商品ファイルのレコードは最大1024Byteとする。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 構造体 */
typedef struct {
char name[1021]; //商品名
int code[3]; //商品コード
}strct;
/* 配列のメモリ確保 */
int main(int argc, char *argv[], strct* str)
{
// 動的メモリの確保。確保したメモリをstrct型ポインタにキャスト。
str = (strct*)malloc(sizeof(strct));
memset(str, 0, sizeof(strct));
return 0;
}
/* ファイルの読み込み */
int add(int argc, char* argv[], strct* str)
{
FILE *fp; // FILE型構造体
char fname[100];
char buf[256];
strcpy_s(fname, 100, argv[1]);
int Index = 1;
int *Input = 0;
if (argc != 2) {
printf_s("引数の個数が異常です");
return -1;
}
// ファイルのオープン
fopen_s(&fp, fname, "r");
if (fp == NULL) {
printf("%sファイルを開けません。\n", fname);
return 0;
}
else {
printf("%sファイルを開いた!\n", fname);
}
// ファイルの読み込み
while (fgets(buf, sizeof(buf), fp)) {
printf("%s", buf);
Index = Index + 1;
}
// ファイルの位置を最初に戻す
rewind(fp);
// 配列にデータ格納
while (fscanf_s(fp, "%3d %1020s", &str[*Input].code[3], &str[*Input].name[1021], 1024) != EOF) {
*Input = (int)malloc(sizeof(int*));
Input++;
}
// ファイルのクローズ
if (fclose(fp) != 0) {
printf("ファイルのクローズに失敗しました。\n");
}
else {
printf("ファイルをクローズしました\n");
return 0;
}
return 0;
}
/* バブルソート 昇順 */
void bubbleSort(int code[], int Index, strct* str)
{
int i, j, temp;
for (i = 0; i < (Index - 1); i++) {
for (j = (Index - 1); j > i; j--) {
if (code[j - 1] > code[j]) {
temp = code[j - 1];
code[j - 1] = code[j];
code[j] = temp;
}
}
}
}
/* 標準出力 */
void standard(FILE *fp, int Index, strct* str)
{
int output = 1;
while (output < Index) {
printf("%03d %s", &str[output].code[0], &str[output].name);
if (output < Index - 1) {
printf("\n");
}
output++;
}
}
また、ソースで間違えてる部分とか指摘してもらえると嬉しいです。
よろしくお願いします。