Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created March 21, 2019 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maehrm/bc11a92e7e4305e0bcccb3233e42a19c to your computer and use it in GitHub Desktop.
Save maehrm/bc11a92e7e4305e0bcccb3233e42a19c to your computer and use it in GitHub Desktop.
平成20年度秋期試験_基本情報午後問10
#include <stdio.h>
#define ROWS 4 /* 換字表の行数 */
#define COLS 23 /* 換字表の列数 */
void encrypt_text(const char *, const char *,
const char[ROWS][COLS]);
void decrypt_text(const char *, const char *,
const char[ROWS][COLS]);
void make_ctbl(char [ROWS][COLS]);
void encrypt_text(const char *in_filename,
const char *out_filename,
const char ctbl[ROWS][COLS]) {
FILE *ifp, *ofp;
char ch[2];
int col[2], row[2], flg, i, sts;
int cnt2;
ifp = fopen(in_filename, "r");
ofp = fopen(out_filename, "w");
do {
sts = fgetc(ifp);
while ((char)sts == '\n') {
fputc('\n', ofp);
sts = fgetc(ifp);
}
if (sts != EOF) {
ch[0] = sts;
cnt2 = 0;
sts = fgetc(ifp);
while ((char)sts == '\n') {
cnt2++;
sts = fgetc(ifp);
}
if (sts == EOF) { /* 文字列が奇数の場合 */
ch[1] = ' ';
} else {
ch[1] = sts;
}
for (i = 0; i < 2; i++) {
flg = 0;
for (row[i] = 0; row[i] < ROWS; row[i]++) {
for (col[i] = 0; col[i] < COLS; col[i]++) {
if (ch[i] == ctbl[row[i]][col[i]]) {
flg = 1;
break;
}
}
if (flg != 0) break;
}
}
if (row[0] == row[1]) {
if (col[0] == col[1]) { /* 2文字が同一の場合 */
ch[0] = ch[1] = ctbl[(row[0] + 1) % ROWS][(col[0] + 1) % COLS];
} else { /* 2文字が同じ行にある場合 */
ch[0] = ctbl[row[0]][(col[0] + 1) % COLS];
ch[1] = ctbl[row[1]][(col[1] + 1) % COLS];
}
} else if (col[0] == col[1]) { /* 2文字が同じ列にある場合 */
ch[0] = ctbl[(row[0] + 1) % ROWS][col[0]];
ch[1] = ctbl[(row[1] + 1) % ROWS][col[1]];
} else {
ch[0] = ctbl[row[0]][col[1]];
ch[1] = ctbl[row[1]][col[0]];
}
fputc(ch[0], ofp);
for (; cnt2 > 0; cnt2--) {
fputc('\n', ofp);
}
fputc(ch[1], ofp);
}
} while (sts != EOF);
fclose(ifp);
fclose(ofp);
}
void decrypt_text(const char *in_filename,
const char *out_filename,
const char ctbl[ROWS][COLS]) {
FILE *ifp, *ofp;
char ch[2];
int col[2], row[2], flg, i, sts;
int cnt2;
ifp = fopen(in_filename, "r");
ofp = fopen(out_filename, "w");
do {
sts = fgetc(ifp);
while ((char)sts == '\n') {
fputc('\n', ofp);
sts = fgetc(ifp);
}
if (sts != EOF) {
ch[0] = sts;
cnt2 = 0;
sts = fgetc(ifp);
while ((char)sts == '\n') {
cnt2++;
sts = fgetc(ifp);
}
ch[1] = sts;
for (i = 0; i < 2; i++) {
flg = 0;
for (row[i] = 0; row[i] < ROWS; row[i]++) {
for (col[i] = 0; col[i] < COLS; col[i]++) {
if (ch[i] == ctbl[row[i]][col[i]]) {
flg = 1;
break;
}
}
if (flg != 0) break;
}
}
if (row[0] == row[1]) {
if (col[0] == col[1]) { /* 2文字が同一の場合 */
ch[0] = ch[1] =
ctbl[(row[0] + ROWS - 1) % ROWS][(col[0] + COLS - 1) % COLS];
} else { /* 2文字が同じ行にある場合 */
ch[0] = ctbl[row[0]][(col[0] + COLS - 1) % COLS];
ch[1] = ctbl[row[1]][(col[1] + COLS - 1) % COLS];
}
} else if (col[0] == col[1]) { /* 2文字が同じ列にある場合 */
ch[0] = ctbl[(row[0] + ROWS - 1) % ROWS][col[0]];
ch[1] = ctbl[(row[1] + ROWS - 1) % ROWS][col[1]];
} else {
ch[0] = ctbl[row[0]][col[1]];
ch[1] = ctbl[row[1]][col[0]];
}
fputc(ch[0], ofp);
for (; cnt2 > 0; cnt2--) {
fputc('\n', ofp);
}
fputc(ch[1], ofp);
}
} while (sts != EOF);
fclose(ifp);
fclose(ofp);
}
void make_ctbl(char ctbl[ROWS][COLS]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
ctbl[i][j] = i * COLS + j + 0x20;
printf("%c", ctbl[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
char ctbl[ROWS][COLS];
if (argc != 4) {
printf("Usage: %s e|d file1 file2\n", argv[0]);
return 1;
}
make_ctbl(ctbl); /* 換字表の作成 */
switch (*argv[1]) {
case 'e':
encrypt_text(argv[2], argv[3], ctbl);
break;
case 'd':
decrypt_text(argv[2], argv[3], ctbl);
break;
}
return 0;
}
@maehrm
Copy link
Author

maehrm commented Mar 21, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment