C language and algorithm design course experiment three: the simplest C programming - sequential programming (4)

insert image description here

1. Purpose of the experiment

(1) Master the usage method of one of the most used statements in C language - assignment statement.
(2) Master the input and output methods of various types of data, and be able to use various format conversion symbols correctly.
(3) Further master the methods of writing and debugging programs.

2. Experimental content

insert image description here

2.4. Translate "China" into password

(4) The program will "China"be translated into a password, and the password rule is: replace the original letter with the fourth letter after the original letter. For example, the fourth letter after the letter 'A' is 'E', replace 'A' with 'E'. Therefore, "China"it should be translated as "Glmre". Please compile a program to make the values ​​of the five variables cl, c2, c3, c4, and c5 respectively by assigning initial values. After calculation, make 'C','h','i','n','a'cl, c2, c3, c4, and c5 become respectively 'G','l','m','r','e'. Use the putchar function and printf function to output these 5 characters respectively.
① Input the pre-edited program and run the program. Analyze for compliance.
② Change the initial values ​​of cl, c2, c3, c4, and c5 to: 'T'、'o'、'd'、'a'、'y', and make the following supplements to the decoding rules: 'W'replace 'A'with, 'X'replace 'B'with, 'Y'replace 'C'with, 'Z'replace 'D'with. Modify the program and run it.
③ Modify the decoding rule as follows: a letter is replaced by the 4th letter in front of it, such as 'E'substituted with 'A', 'Z'substituted with 'U', 'D'substituted with 'Z', substituted 'C'with , substituted with 'Y', substituted with substituted. Modify the program and run it.'B''X''A''V'

3. Experimental steps

insert image description here

3.4. Sequential programming experiment topic 4: Experimental steps of translating "China" into a password

(4) The program will "China"be translated into a password, and the password rule is: replace the original letter with the fourth letter after the original letter. For example, the fourth letter after the letter 'A' is 'E', replace 'A' with 'E'. Therefore, "China"it should be translated as "Glmre". Please compile a program to make the values ​​of the five variables cl, c2, c3, c4, and c5 respectively by assigning initial values. After calculation, make 'C','h','i','n','a'cl, c2, c3, c4, and c5 become respectively 'G','l','m','r','e'. Use the putchar function and printf function to output these 5 characters respectively.
① Input the pre-edited program and run the program. Analyze for compliance.

3.4.1. Variable definition and initial value assignment

// 变量的定义与赋初值
	char c1 = 'C', c2 = 'h', c3 = 'i', c4 = 'n', c5 = 'a';

3.4.2. Translate characters into passwords

	// 将字符译成密码
	c1 = c1 + 4;
	c2 = c2 + 4;
	c3 = c3 + 4;
	c4 = c4 + 4;
	c5 = c5 + 4;

3.4.3, putchar and printf output password

	// putchar与printf方式输出密码
	printf("putchar方式输出China的密码是:");
	putchar(c1); 
	putchar(c2);
	putchar(c3);
	putchar(c4);
	putchar(c5);
	
	printf("\nprintf方式输出China的密码是: %c%c%c%c%c\n", c1, c2, c3, c4, c5);

3.4.4. Program running results

The results of the experiment of translating "China" into passwords are as follows

insert image description here

② Change the initial values ​​of cl, c2, c3, c4, and c5 to: 'T'、'o'、'd'、'a'、'y', and make the following supplements to the decoding rules: 'W'replace 'A'with, 'X'replace 'B'with, 'Y'replace 'C'with, 'Z'replace 'D'with. Modify the program and run it.

  • The program is modified as follows
	// 将字符译成密码
	c1 = c1 + 4;
	c2 = c2 + 4;
	c3 = c3 + 4;
	c4 = c4 + 4;
	c5 = c5 + 4 - 26;

operation result

insert image description here

③ Modify the decoding rule as follows: a letter is replaced by the 4th letter in front of it, such as 'E'substituted with 'A', 'Z'substituted with 'U', 'D'substituted with 'Z', substituted 'C'with , substituted with 'Y', substituted with substituted. Modify the program and run it.'B''X''A''V'

  • The program is modified as follows
	// 将字符译成密码
	c1 = c1 - 4;
	c2 = c2 - 4;
	c3 = c3 - 4 + 26;
	c4 = c4 - 4 + 25;
	c5 = c5 - 4;

operation result

insert image description here

4. Experimental summary

insert image description here

Through this experiment: C language and algorithm design course experiment three: the simplest C programming - sequential programming (4), the following points have been mastered.

-(1) Master the usage method of one of the most used statement-assignment statement in C language.
-(2) Master the methods of input and output of various types of data, and be able to use various format conversion symbols correctly.
-(3) Further master the methods of writing and debugging programs.

5. The complete procedure of the experiment

insert image description here

5.4. Sequential programming experiment topic 4: The complete program of the experimental steps of translating "China" into a password

The complete program is as follows

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

int main()
{
    
    
	// 变量的定义与赋初值
	/*char c1 = 'C', c2 = 'h', c3 = 'i', c4 = 'n', c5 = 'a';*/

	// 变量的定义与赋初值
	char c1 = 'T', c2 = 'o', c3 = 'd', c4 = 'a', c5 = 'y';

	// 将字符译成密码
	//c1 = c1 + 4;
	//c2 = c2 + 4;
	//c3 = c3 + 4;
	//c4 = c4 + 4;
	//c5 = c5 + 4;

	// 将字符译成密码
	//c1 = c1 + 4;
	//c2 = c2 + 4;
	//c3 = c3 + 4;
	//c4 = c4 + 4;
	//c5 = c5 + 4 - 26;

	// 将字符译成密码
	c1 = c1 - 4;
	c2 = c2 - 4;
	c3 = c3 - 4 + 26;
	c4 = c4 - 4 + 25;
	c5 = c5 - 4;

	// putchar与printf方式输出密码
	printf("putchar方式输出Today的密码是:");
	putchar(c1); 
	putchar(c2);
	putchar(c3);
	putchar(c4);
	putchar(c5);

	printf("\nprintf方式输出Today的密码是: %c%c%c%c%c\n", c1, c2, c3, c4, c5);

	return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/m0_47419053/article/details/128616048