当前位置: 首页 > 工具软件 > Julius > 使用案例 >

Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was ke

宗政财
2023-12-01

Julius Caesar lived in a time of danger and intrigue. The hardest situation
Caesar ever faced was keeping himself alive. In order for him to survive, he
decided to create one of the first ciphers. This cipher was so incredibly
sound, that no one could figure it out without knowing how it worked.
You are a sub captain of Caesar’s army. It is your job to decipher the
messages sent by Caesar and provide to your general. The code is simple.
For each letter in a plaintext message, you shift it five places to the
right to create the secure message (i.e., if the letter is ‘A’, the cipher
text would be ‘F’). Since you are creating plain text out of Caesar’s
messages, you will do the opposite:

Cipher text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character
should remain the same, and all alphabetical characters will be upper case
Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. All characters will be uppercase.

A single data set has 3 components:

Start line - A single line, “START”

Cipher message - A single line containing from one to two hundred characters, inclusive, comprising a single message from Caesar

End line - A single line, “END”

Following the final data set will be a single line, “ENDOFINPUT”.

Output

For each data set, there will be exactly one line of output. This is the original message by Caesar.

Sample Input

START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT

Sample Output

IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

/*Julius Caesar lived in a time of danger and intrigue. The hardest situation
Caesar ever faced was keeping himself alive. In order for him to survive, he
decided to create one of the first ciphers. This cipher was so incredibly 
sound, that no one could figure it out without knowing how it worked.
You are a sub captain of Caesar's army. It is your job to decipher the 
messages sent by Caesar and provide to your general. The code is simple. 
For each letter in a plaintext message, you shift it five places to the 
right to create the secure message (i.e., if the letter is 'A', the cipher 
text would be 'F'). Since you are creating plain text out of Caesar's 
messages, you will do the opposite:

Cipher text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character 
should remain the same, and all alphabetical characters will be upper case*/

#include <stdio.h>
#include <string.h>
#define N 100

int main()
{
	char c[N];
	int i;
	char *p,*q;
	q = c;
	
	for(i = 0;i < N;i++)
	{
		c[i] = getchar();
		if(strstr(c,"ENDOFINPUT"))
		break;
	 } 
    
    while(1)
    {
    	for(p = strstr(q,"START") + 6;p < strstr(q,"END");p++)
	    {
		    if(*p >= 'A' && *p <= 'E')
		    *p = *p + 21;
	     	else if(*p >= 'F' && *p <= 'Z')
		    *p = *p - 5;
	    } 
	    q = p + 4;
	    if(q == strstr(c,"ENDOFINPUT"))
	    break;
	}
    
	q = c;
	while(1)
    {
    	for(p = strstr(q,"START") + 6;p < strstr(q,"END");p++)
	    {
		   printf("%c",*p); 
	    } 
	    q = p + 4;
	    if(q == strstr(c,"ENDOFINPUT"))
	    break;
	}
	
}

我遇到很多问题:
1.对于接收回车符只能使用getchar()函数。scanf只能接收非空格字符,也就是说有空就不能接收。gets()函数只能接收一行的数据,遇到回车键直接就执行下一个语句,表明已经接收完毕,并不接收回车键。
2.在使用if语句时,尤其要注意变量的值,谨慎选择,特别是在多个if语句重叠的时候。
在做这个题目的时候,我是用的一个字符数组,直接在这个字符数组里面更改,输出时也是跳跃性的输出,可以根据自己的需要拓宽字符数组的大小。

 类似资料:

相关阅读

相关文章

相关问答