UVa OJ 458

本文介绍了一个简单的程序,该程序能够读取特定格式的文件,并通过调整ASCII字符集中的字符值来解码信息。示例展示了如何正确解码并打印出原本的信息。

 

 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来实现换行。

 

问题:无

 

解决:无

 

转载于:https://www.cnblogs.com/X-Spider/p/3535964.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值