The Decoder |
Write a complete program that will correctly decode a set of characters into a valid message. Your program should read a given file of a simple coded set of characters and print the exact message that the characters contain. The code key for this simple coding is a one for one character substitution based upon a single arithmetic manipulation of the printable portion of the ASCII character set.
Input and Output
For example: with the input file that contains:
1JKJ‘pz‘{ol‘{yhklthyr‘vm‘{ol‘Jvu{yvs‘Kh{h‘Jvywvyh{pvu5 1PIT‘pz‘h‘{yhklthyr‘vm‘{ol‘Pu{lyuh{pvuhs‘I|zpulzz‘Thjopul‘Jvywvyh{pvu5 1KLJ‘pz‘{ol‘{yhklthyr‘vm‘{ol‘Kpnp{hs‘Lx|pwtlu{‘Jvywvyh{pvu5
your program should print the message:
*CDC is the trademark of the Control Data Corporation. *IBM is a trademark of the International Business Machine Corporation. *DEC is the trademark of the Digital Equipment Corporation.
Your program should accept all sets of characters that use the same encoding scheme and should print the actual message of each set of characters.
Sample Input
1JKJ‘pz‘{ol‘{yhklthyr‘vm‘{ol‘Jvu{yvs‘Kh{h‘Jvywvyh{pvu5 1PIT‘pz‘h‘{yhklthyr‘vm‘{ol‘Pu{lyuh{pvuhs‘I|zpulzz‘Thjopul‘Jvywvyh{pvu5 1KLJ‘pz‘{ol‘{yhklthyr‘vm‘{ol‘Kpnp{hs‘Lx|pwtlu{‘Jvywvyh{pvu5
Sample Output
*CDC is the trademark of the Control Data Corporation. *IBM is a trademark of the International Business Machine Corporation. *DEC is the trademark of the Digital Equipment Corporation.
1 #include <stdio.h> 2 #include <string.h> 3 #define maxn 10000 4 int main() 5 { 6 #ifdef CLOCK 7 freopen("data.in", "r", stdin); 8 freopen("data.out", "w", stdout); 9 #endif 10 int i; 11 #ifdef LOCAL 12 FILE * fin,* fout; 13 fin = fopen("data.in", "rb"); 14 fout = fopen("data.out", "wb"); 15 #endif 16 char buf[maxn]; 17 memset(buf, 0, sizeof(buf)); 18 while (fgets(buf, sizeof(buf), stdin)) 19 { 20 for (i = 0; i < strlen(buf); i++) 21 buf[i] -= 7; 22 buf[i - 1] = ‘\0‘; 23 printf("%s\n", buf); 24 } 25 #ifdef LOCAL 26 fclose(fin); 27 fclose(fout); 28 #endif 29 return 0; 30 }
报告:还是比较顺的解决了,只是其中因为\r\n的问题纠结一下,strlen的长度实际是指到‘\0‘,且包括\n,即题中buf[i]为‘\0‘,buf[i-1]为‘\n‘。而当用到fprintf时,printf("%s\n", buf);改为fprintf("%s\r\n", buf);即后面需要加上\r\n来实现换行。
问题:无
解决:无