458 - The Decoder & C语言gets函数,字符输出输出 & toascii()
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.
----------------------------------------------------------------------------------------------------------------------------
题目解答:
#include<stdio.h>
int main(){
char c;
int j;
while((c = getchar())!= EOF){
if(c != '\n'){
putchar(c-);
}
else
putchar(c);
}
return ;
}
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 = ; i < tmp;i++){
j = toascii(buf[i])-;
printf("%c",j);
}
printf("\n");
}
return ; }
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。
458 - The Decoder & C语言gets函数,字符输出输出 & toascii()的更多相关文章
- 【转载】C语言itoa()函数和atoi()函数详解(整数转字符C实现)
本文转自: C语言itoa()函数和atoi()函数详解(整数转字符C实现) 介绍 C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. int/float to ...
- C语言printf()函数:格式化输出函数
C语言printf()函数:格式化输出函数 头文件:#include <stdio.h> printf()函数是最常用的格式化输出函数,其原型为: int printf( char ...
- Swift3.0语言教程查找字符集和子字符串
Swift3.0语言教程查找字符集和子字符串 Swift3.0语言教程查找字符集和子字符串,在字符串中当字符内容很多时,我们就需要使用到查找字符集或者子字符串的方法.以下我们将讲解3种查找字符集和子字 ...
- C语言的本质(15)——C语言的函数接口入门
C语言的本质(15)--C语言的函数接口 函数的调用者和其实现者之间存在一个协议,在调用函数之前,调用者要为实现者提供某些条件,在函数返回时,实现者完成调用者需要的功能. 函数接口通过函数名,参数和返 ...
- 13-C语言字符串函数库
目录: 一.C语言字符串函数库 二.用命令行输入参数 回到顶部 一.C语言字符串函数库 1 #include <string.h> 2 字符串复制 strcpy(参数1,参数2); 参数1 ...
- C语言字符串函数大全
C语言字符串函数大全 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include ...
- 【R】R语言常用函数
R语言常用函数 基本 一.数据管理vector:向量 numeric:数值型向量 logical:逻辑型向量character:字符型向量 list:列表 data.frame:数据框c:连接为向量或 ...
- C语言——指向函数的指针
转载自:http://www.cnblogs.com/liangyan19910818/archive/2011/08/19/2145270.html C语言——指向函数的指针 函数类型 (* 函数指 ...
- R语言中的字符处理
R语言中的字符处理 (2011-07-10 22:29:48) 转载▼ 标签: r语言 字符处理 字符串 连接 分割 分类: R R的字符串处理能力还是很强大的,具体有base包的几个函数和strin ...
随机推荐
- FCKeditor配置与使用
fckeditor - (1)资料介绍与安装 fckeditor介绍 FCKeditor是一个专门使用在网页上属于开放源代码的所见即所得文字编辑器. 1.fckeditor官网:http://ww ...
- Linux基础入门(实验楼实验)
实验一 Linux系统简介 Linux和windows.Mac OS一样是一种操作系统.最早流行起来的操作系统是UNIX,但由于其过度商业化,价格昂贵,因此在校园里人们大多选择MINIX.1991年, ...
- 日志自定义Tag
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentHashMap; /** * Crea ...
- kylin-cube存储结构
前言 本篇文章通过图文的方式分析不同维度组合下的cube在hbase中的存储结构 需要声明的是,kylin不存原始数据,存储cube 全维度构建 假设一张表有3个字段name,age,sex,那么当通 ...
- node scripts/install.js 停顿解决办法
参考:node-sass 安装卡在 node scripts/install.js 解决办法 在安装hexo的时候,运行: npm install hexo-cli -g 卡死在了 node scri ...
- SQL 触发器的缺点 坏处 弊端 哼╭(╯^╰)╮
(自己总结,有误请不吝赐教) 1.如果触发频率高,占用内存,降低数据访问速度 2.相对不灵活,一旦触发马上执行,不能排除特殊情况 3.一定程度上打乱代码结构,相关的代码都需要特别注释,否则造成阅读和维 ...
- springboot p6spy 打印完整sql
调试时打印出sql的需求,太正常不过了,mybatis也提供了这样的功能: mybatis: configuration: log-impl: org.apache.ibatis.logging.st ...
- 2016"百度之星" - 初赛(Astar Round2B)1003 瞬间移动 组合数学+逆元
瞬间移动 Accepts: 1018 Submissions: 3620 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/ ...
- H5 canvas建造敌人坦克
接着上一篇(http://www.cnblogs.com/zhouhuan/p/H5_tankgame3.html),这一篇建造敌人的坦克. 思路是,基于可扩展性和性能等方面的考虑,用构造函数改造 ...
- 这些HTML、CSS知识点,面试和平时开发都需要 No1-No4(知识点:HTML、CSS、盒子模型、内容布局)
这些HTML.CSS知识点,面试和平时开发都需要 No1-No4 系列知识点汇总 这些HTML.CSS知识点,面试和平时开发都需要 No1-No4(知识点:HTML.CSS.盒子模型.内容布局) ...