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 ...
随机推荐
- la3523 白书例题 圆桌骑士 双联通分量+二分图
具体题解看大白书P316 #include <iostream> #include <algorithm> #include <vector> #include & ...
- Python: 字符串格式化format()函数的使用
python从2.6开始支持format,新的更加容易读懂的字符串格式化方法,从原来的% 模式变成新的可读性更强的 花括号声明{}.用于渲染前的参数引用声明, 花括号里可以用数字代表引用参数的序号, ...
- presto-cli通过hive查询hdfs
1. 启动hive metastore 2. 启动hive thrift接口 参考:http://www.cnblogs.com/kisf/p/7497261.html 3. 下载presto se ...
- MS08_067漏洞渗透攻击
MS08_067漏洞渗透攻击实践 前期准备 kali和winxp要ping通 kali开启msfconsole: 同时在这里可以看到目前可攻击载荷个数一共是471个,也可以看到其他攻击的数量如图. 用 ...
- Android实践项目汇报
Android实践项目:推箱子 推箱子是一款来自日本的古老游戏,其设计目的是训练人的逻辑思维能力.游戏场景一般是设定在空间狭小的仓库中,要求把箱子摆放到指定位置.这就要求玩家巧妙的运用有限的空间和通道 ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The Eighth Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Eighth Week ...
- about SpringBoot学习后记
<SpringBoot实战>第一章节入门的名称为Spring风云再起 看起来Spring的功能确实受Java开发者喜爱 在SpringBoot中,继续将Spring框架做了另一次的封装使框 ...
- ssh connection refused
执行sudo apt-get install openssh-server命令安装SSH服务
- 【测试设计】基于正交法的测试用例设计工具--PICT
前言 我们都知道成对组合覆盖是一种非常有效的测试用例设计方法,但是实际工作过程中当成对组合量太大,我们往往很难做到有效的用例覆盖. PICT是微软公司出品的一款成对组合命令行生成工具,它很好的解决了上 ...
- 语义化标签和media媒体查询可以放心使用
现在的高级浏览器都支持html5,只有IE6-IE8不支持.(下面说的IE均值IE6-IE8) 有两个特性在IE是可以使用的: 1.语义化标签: header(头部) section(区块) foot ...