C言語ファイルI/O問題


質問です。


辞書の結合


辞書テキストファイル8-1-dict 1が2つあると仮定する.txtと8-1-dict 2.txtは、それぞれいくつかの英語の単語を格納します.格納形式は行ごとに格納され、各行に1つの単語が英字の順序で並べられています.
2つの辞書ファイルの内容を統合し、新しい辞書ファイル8-1-dict 3を生成するプログラムを作成してください.txt .
要求:(1)新しい辞書ファイルでは,各単語が秩序正しく配列されている.
(2)1つの単語が8-1-dict 1に既に現れる場合.txtでは、8-1-dict 2にも現れる.txtでは、新しいファイルに一度しか表示されません.
#define _CRT_SECURE_NO_WARNINGS   
#include<stdio.h>
#include<string.h>
int main() {
	char words[100][25]={0, 0};
	char words2[100][25] = { 0, 0 };
	char word[25] = { 0 };
	FILE* fp1 = fopen("8-1-dict1.txt", "r");
	FILE* fp2 = fopen("8-1-dict2.txt", "r");
	FILE* fp = fopen("8-1-dict.txt", "w");
	int i = 0;
	int len1,len2,n;
	while (fgets(word, sizeof(word), fp1)) {
		strcpy_s(words[i], 25, word);
		i++;
	}
	while (fgets(word, sizeof(word), fp2)) {
		for (int j = 0; j < i; j++) {
			if (!strcmp(words[j],word)) break;
			if (j == i - 1) {
				strcpy_s(words[i], 25, word);
				i++;
				break;
			}
		}
	}
	for (int j = 0; j < i; j++) {
		 len1 = strlen(words[j]) - 1;
		 int s = 0;
		for (int k = 0; k < i; k++) {
			if (!strcmp(words[j], words[k])) continue;
			len2 = strlen(words[k]) - 1;
			if (len1 > len2) n = len2;
			else n = len1;
			for (int t = 0; t < n; t++) {
				if ((int)words[j][t] > (int)words[k][t]) {
					s++;
					break;
				}
				else if ((int)words[j][t] < (int)words[k][t]) break;
			}

		}
		strcpy_s(words2[s], 25, words[j]);
	}
	for (int j = 0; j < i; j++) {
		printf("%s", words2[j]);
		fprintf(fp, "%s", words2[j]);
     }
	fclose(fp1);
	fclose(fp2);
	fclose(fp);
	return 0;

}
実行結果:

質問です。


株式収益統計


ある友人は証券業界で働いていて、お客様の株の収益状況を統計するプログラムを作成するのに役立つことを望んでいます.プログラムの入力はテキストファイル8-3-stockです.txt(データはアナログ数)では、株式の取引情報(最大1000件)が記録されています.各取引情報は、取引日、株式名、株式数、購入価格、販売価格の1行を占有し、各データ項目の間にスペースで区切られています.たとえば、次のようになります.
2009-04-29成長科技1000 17.8 18.3
2009-05-30長城化学工業2000 6.47 7.03
プログラムでこれらの取引情報を統計し、各取引の収益を計算し、結果を別のファイル8-3-stat.txtに保存します.新しいファイルの出力フォーマットは、取引日、株式名、資金占有率、収益率です.各コンテンツ項目の間にスペース間隔があります.
このうち、資金占有割合とは、その株式の購入金額とすべての株式の購入金額との比であり、
収益率とは、その株の収益金額と購入金額の比を指す.
#define _CRT_SECURE_NO_WARNINGS   
#include<stdio.h>
#include<string.h>
int main() {
	FILE* fp1 = fopen("8-3-stock.txt", "r");
	FILE* fp = fopen("8-3-stat.txt", "w");
	char name[1001][20] = { 0,0 };
	int year[1001] = { 0 };
	int money[1001] = { 0 };
	double buy[1001] = { 0 };
	double sell[1001] = { 0 };
	char monthDay[1001][15] = { 0,0 };
	int i = 0;
	int total=0;
	double distribution;
	double profit;
	while (1) {
		fscanf(fp1, "%d%s %s %d %lf %lf", &year[i], monthDay[i], name[i], &money[i], &buy[i], &sell[i]);
		if (!year[i])break;
		printf("%d%s %s %d %lf %lf \n", year[i], monthDay[i], name[i], money[i], buy[i], sell[i]);
		total += money[i];
		i++;
	}
	i = 0;
	while (year[i]) {
		profit = sell[i]/ buy[i];
		distribution = (double)money[i] / (double)total;
		fprintf(fp, "%d%s %s %2lf %lf\n", year[i], monthDay[i], name[i],distribution, profit);
		printf("%d%s %s %2lf %lf\n", year[i], monthDay[i],name[i], distribution, profit);
		i++;

	}

	fclose(fp1);
	fclose(fp);
	return 0;

}
実行画面