Write a program that does the inverse of the program you wrote in 04A1.
Keywords: nested if/switch, (circular) buffer Details If the input contains the strings "<", ">", "&", """, or "'", your program outputs this respectively as '<', '>', '&', '"', or '\''. Any other input should be output without changes.
Use single-character input. Do not rely on limited line lengths.
Hint: Use input/output redirection and the diff command for testing. Use 04-080in.txt as the input file and 04-080check.txt as the checking file. Try to get more and more lines of the test files to work correctly.
Hint: There are two main strategies to solve this problem: nested if (or switch) statements, or a small buffer.
続き。
int
main(void)
{
enum STATUS status;
status = NONE;
while (!feof(stdin)) {
int c = getchar();
push_buffer(c);
switch (c) {
case '&':
status = ENC_START;
break;
case ';':
switch (status) {
case LT_T:
printf("<");
break;
case GT_T:
printf(">");
break;
case AMP_P:
printf("&");
break;
case APOS_S:
printf("'");
break;
case QUOT_T:
printf("\"");
break;
default:
print_buffer();
}
clear_buffer();
break;
続き その2
case 'a':
if (status == ENC_START)
status = AMP_OR_APOS_A;
else
print_buffer();
break;
case 'g':
if (status == ENC_START)
status = GT_G;
else
print_buffer();
break;
case 'l':
if (status == ENC_START)
status = LT_L;
else
print_buffer();
break;
case 'm':
if (status == AMP_OR_APOS_A)
status = AMP_M;
else
print_buffer();
break;
case 'o':
if (status == APOS_P)
status = APOS_O;
else if (status == QUOT_U)
status = QUOT_O;
else
print_buffer();
break;
case 'p':
if (status == AMP_OR_APOS_A)
status = APOS_P;
else if (status == AMP_M)
status = AMP_P;
else
print_buffer();
break;
続き その3
case 'q':
if (status == ENC_START)
status = QUOT_Q;
else
print_buffer();
break;
case 's':
if (status == APOS_O)
status = APOS_S;
else
print_buffer();
break;
case 't':
if (status == LT_L)
status = LT_T;
else if (status == GT_G)
status = GT_T;
else if (status == QUOT_O)
status = QUOT_T;
else
print_buffer();
break;
case 'u':
if (status == QUOT_Q)
status = QUOT_U;
else
print_buffer();
break;
default:
printf("%c", c);
}
}