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 ...
随机推荐
- python中的切片
python中提供了一种很方便的方法来完成取出指定范围内的元素,这种方法就是切片(Slice). 以下为切片的例子: In [1]: L = ['Michael', 'Sarah', 'Tracy', ...
- 深入理解jvm--Java中init和clinit区别完全解析(转)
转自:http://blog.csdn.net/u013309870/article/details/72975536 init和clinit区别 ①init和clinit方法执行时机不同 init是 ...
- OpenVAS应用实例
OpenVAS应用实例 首先打开Metasploitables Linux,输入ifconfig查看本机ip地址为192.168.200.125 使用kali linux ping一下Metasplo ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The Tenth Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Tenth Week ...
- Javaworkers团队最终项目总结
Javaworkers团队最终项目总结 小组成员 20145205武钰 20145222黄亚奇 20145235李涛 20145103冯文华 团队项目总结 案例提出及工程用时 本次项目由十一到十六周共 ...
- 【分页问题】elasticsearch 深分页问题以及解决方法
本文主要参考: 1.https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html ...
- android平台蓝牙编程
Android平台支持蓝牙网络协议栈,实现蓝牙设备之间数据的无线传输. 本文档描述了怎样利用android平台提供的蓝牙API去实现蓝牙设备之间的通信,蓝牙设备之间的通信主要包括了四个步骤:设置蓝牙设 ...
- Android 之低版本高版本实现沉浸式状态栏
沉浸式状态栏确切的说应该叫做透明状态栏.一般情况下,状态栏的底色都为黑色,而沉浸式状态栏则是把状态栏设置为透明或者半透明. 沉浸式状态栏是从android Kitkat(Android 4.4)开始出 ...
- html5 自带全屏API调用方法
function FullScreen(){ var el = $('html')[0];//要全屏的元素,如果要全页面全屏,建议使用html节点而不是body节点 var isFullscreen= ...
- cf812B 搜索
B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes input s ...