458 - The Decoder & C语言gets函数,字符输出输出 & toascii()
2014-12-15 15:20
344 查看
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.
your program should print the message:
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.
getchar()与scanf的作用是相同的,但就是比较简练。同理putchar().
谈下fgets(),这个函数是用来获取一行的。
fgets(char* buf,int number,FILE *fin)从输入流里读入(number - 1)个字符转存到buf指向的地址中,遇到换行符'\n'或者文件结束符'\0'就会停止.
注意fgets的有效字符为(number - 1 )个,最后会加上文件结束符'\0'。fgets能读取有效的一行,因为它遇到回车符'\n'就会停止,而这个'\n'也会是buf中最后一个有效字符(再往后就是文件结束符'\0').只有一种情况下,buf不会以'\n'为最后一个有效字符,那就是在遇到换行符之前遇到文件结束符'\0',即输入文件本身不是以回车结束的。(引用于http://blog.sina.com.cn/s/blog_608e238e0100kvvi.html)。
使用fgets与题目不符,题目中输入没有说以换行符决定一行,所以还是用getchar比较合适。
2. 同时在提交时使用toascii来得到数字时,提示为WA。
#include <ctype.h>
定义函数:int toascii(int c);
函数说明:toascii()会将参数c 转换成7 位的unsigned char 值,第八位则会被清除,此字符即会被转成ASCII码字符。
返回值:将转换成功的ASCII 码字符值返回。
范例:将int 型a 转换成ASSII 码字符。
使用这个的话,编译不能通过。若输入全是ascii码的话,由于ascii最大数字为127,第八位就是为0,不受影响。题目中说明输入为ascii码中的字符,因此,我认为这是平台的原因导致WA。
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.
----------------------------------------------------------------------------------------------------------------------------
题目解答:
#include<stdio.h> int main(){ char c; int j; while((c = getchar())!= EOF){ if(c != '\n'){ putchar(c-7); } else putchar(c); } return 0; }
getchar()与scanf的作用是相同的,但就是比较简练。同理putchar().
谈下fgets(),这个函数是用来获取一行的。
#define LOCAL #include<stdio.h> #include<ctype.h> #include<string.h> #define MAXN 1000 char buf[MAXN]; int main(){ #ifdef LOCAL freopen("data.txt","r",stdin); freopen("out.txt","w",stdout); #endif int i,j; int tmp; while(fgets(buf,MAXN,stdin) != NULL){ tmp = strlen(buf); for(i = 0; i < tmp;i++){ j = toascii(buf[i])-7; printf("%c",j); } printf("\n"); } return 0; }
fgets(char* buf,int number,FILE *fin)从输入流里读入(number - 1)个字符转存到buf指向的地址中,遇到换行符'\n'或者文件结束符'\0'就会停止.
注意fgets的有效字符为(number - 1 )个,最后会加上文件结束符'\0'。fgets能读取有效的一行,因为它遇到回车符'\n'就会停止,而这个'\n'也会是buf中最后一个有效字符(再往后就是文件结束符'\0').只有一种情况下,buf不会以'\n'为最后一个有效字符,那就是在遇到换行符之前遇到文件结束符'\0',即输入文件本身不是以回车结束的。(引用于http://blog.sina.com.cn/s/blog_608e238e0100kvvi.html)。
使用fgets与题目不符,题目中输入没有说以换行符决定一行,所以还是用getchar比较合适。
2. 同时在提交时使用toascii来得到数字时,提示为WA。
#include <ctype.h>
定义函数:int toascii(int c);
函数说明:toascii()会将参数c 转换成7 位的unsigned char 值,第八位则会被清除,此字符即会被转成ASCII码字符。
返回值:将转换成功的ASCII 码字符值返回。
范例:将int 型a 转换成ASSII 码字符。
j = toascii(buf[i])-7;
使用这个的话,编译不能通过。若输入全是ascii码的话,由于ascii最大数字为127,第八位就是为0,不受影响。题目中说明输入为ascii码中的字符,因此,我认为这是平台的原因导致WA。
相关文章推荐
- oracle的结果输出单引号 ' 的字符 的sql语句
- UVA 458(The Decoder)
- (使用STL自带的排序函数进行编程7.3.14)UVA 10062 Tell me the frequencies!(求一个字符出现的次数(包括字母和数字)&&按出现的次数降序)
- POJ 2125 --Destroying The Graph【最小割解决 "最小点权覆盖问题" && 输出解(割边集) && 各种不懂】
- 输入一串字符串,其中有普通的字符与括号组成(包括‘(’、‘)’、‘[’,']'),要求验证括号是否匹配,如果匹配则输出0、否则输出1.
- 请输出开如"1A 2B 3C ........26Z"的字符序列,用集合实现
- hdoj 1026 Ignatius and the Princess I【bfs&&路径输出】
- 编写函数,对标准输入进行扫描,并对“the”出现的次数进行计数。 进行比较时应区分大小写。认为一个单词有一个或多个空格字符分隔, 并且输入行在长度上不会超过100个字符,计数结果输出。
- 有关数组 & 动态数组,输出数组中最长字符以及最短字符,若最长最短不止一个,都输出
- (字符串的处理4.7.14)UVA 10252 Common Permutation(寻找两个字符串中的共同字符&&按字典序输出)
- 输出整数类型边界值及其大小(K&R <The C programming language> 习题 2-1)
- 输出字符图形·3
- 输出字符图形·2
- 【python】 关于去掉转义字符\,输出中文字符(decode("string_escape"))
- HDU 3729 I'm Telling the Truth -- 二分图最大匹配 输出方案
- hdu_1048_The Hardest Problem Ever_字符输入输出问题水题一道
- 输出字符图形·1
- 写一函数,输入一个四位数字,要求输出这四个数字字符,但每两个数字间空格。如输入1990,应输出"1 9 9 0"。
- hdoj 3294 Girls' research 【Manacher算法】【输出最长回文子串 + 字符转化】
- HDU I'm Telling the Truth (二分图最大匹配+字典序最大路径输出(好题))