Write a complete program, that would 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 are a one for one character substitution based upon a single arithmetic manipulatio n 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{pvu51pit ' pz ' h ' {yhklthyr ' vm ' {ol ' pu{lyuh{pvuhs ' i|zpulzz ' Thjopul ' Jvywvyh{pvu51klj ' 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 are a trademark of the International Business Machine Corporatio N.*dec is the trademark of the Digital equipment Corporation.
Your program should accept all sets of characters so 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{pvu51pit ' pz ' h ' {yhklthyr ' vm ' {ol ' pu{lyuh{pvuhs ' i|zpulzz ' Thjopul ' Jvywvyh{pvu51klj ' pz ' {ol ' {yhklthyr ' vm ' {ol ' kpnp{hs ' lx|pwtlu{' Jvywvyh{pvu5
Sample Output
*CDC is the trademark of the Control Data CORPORATION.*IBM are a trademark of the International Business Machine Corporatio N.*dec is the trademark of the Digital equipment Corporation.
--------------------------------------------------------------------------------------------------------------- -------------
Topic Answer:
#include <stdio.h>int main () { char C; int J; while ((c = GetChar ())!= EOF) {
if (c! =
"
\n
'
' {Putchar (c -
7
);
else
Putchar (c);
return
0
;}
The role of GetChar () is the same as that of scanf, but it is relatively concise. similarly Putchar ().
Talk about Fgets (), which is used to get a row.
#defineLOCAL#include<stdio.h>#include<ctype.h>#include<string.h>#defineMAXN 1000CharBUF[MAXN];intMain () {#ifdef LOCAL freopen ("Data.txt","R", stdin); Freopen ("OUT.txt","W", stdout);#endif inti,j; inttmp; 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) reads (number-1) characters from the input stream to the address where the buf points, and encounters a newline character ' \ n ' or a file Terminator ' \ s ' stops.
Note the effective word identifier (number-1) of fgets, and end with the file Terminator ' \ '. Fgets can read a valid line because it will stop when it encounters a carriage return ' \ n ', and the ' \ n ' will be the last valid character in the BUF (and then the end of the file). In only one case, BUF does not use ' \ n ' as the last valid character, That is, before encountering a newline character, the file Terminator ' + ' is encountered, that is, the input file itself is not ended with a carriage return. (referred to as http://blog.sina.com.cn/s/blog_608e238e0100kvvi.html).
Use fgets and the topic does not match, the question input does not say with the newline character to decide the line, therefore still uses the GetChar to be suitable.
2. When using TOASCII to obtain a number at the time of submission, the hint is WA.
#include <ctype.h>
define function: int toascii (int c);
Function Description: Toascii () converts the parameter C to a 7-bit unsigned char value, the eighth bit is cleared, and the character is converted to ASCII characters.
Return value: Returns the converted ASCII character value for success.
Example: Convert int A to assii code character.
j = toascii (Buf[i])-7;
If you use this, the compilation cannot pass. If the input is all ASCII code, because the ASCII maximum number is 127, the eighth bit is 0, not affected. The title describes the input as ASCII characters in the code, so I think this is the cause of the platform WA.
458-the Decoder & C language gets function, character output output & Toascii ()