Created
April 22, 2018 20:11
-
-
Save maehrm/bcac7abf0c8d791a4b968981e6867bfb to your computer and use it in GitHub Desktop.
平成30年度春期基本情報技術者試験 午後 問9 プログラム1
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define recSize 1002 /* 入力レコードの最大文字数 + 2('\n', '\0') */ | |
void outList(char *dataFile, char *listFile, | |
int keyPos, int keyLen, int valuePos, int valueLen) { | |
FILE *inFile, *outFile; | |
char inBuf[recSize], inKey[10], key[10], temp[10]; | |
long count, inValue, value; | |
char format[] = " %9s %9ld %9ld\n"; | |
key[keyLen] = '\0'; | |
inKey[keyLen] = '\0'; | |
temp[valueLen] = '\0'; | |
outFile = fopen(listFile, "w"); | |
inFile = fopen(dataFile, "r"); | |
if (fgets(inBuf, recSize,inFile) != NULL) { | |
strncpy(key, inBuf + keyPos, keyLen); | |
count = 1; | |
value = atol(strncpy(temp, inBuf + valuePos, valueLen)); | |
while (fgets(inBuf, recSize,inFile) != NULL) { | |
strncpy(inKey, inBuf + keyPos, keyLen); | |
inValue = atol(strncpy(temp, inBuf + valuePos, valueLen)); | |
if (strcmp(key, inKey) != 0) { | |
fprintf(outFile, format, key, count, value); | |
count = 1; | |
strcpy(key, inKey); | |
value = inValue; | |
} | |
else { | |
count++; | |
value += inValue; | |
} | |
} | |
fprintf(outFile, format, key, count, value); | |
} | |
fclose(inFile); | |
fclose(outFile); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
#ifdef Q1 | |
/* 設問1: 図1を利用 */ | |
outList(argv[1], argv[2], 16, 6, 28, 6); | |
#endif | |
#ifdef Q2 | |
/* 設問2: 図3を利用 */ | |
outList(argv[1], argv[2], 9, 2, 29, 6); | |
#endif | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://maehrm.hatenablog.com/entry/2018/04/23/051536