C language and algorithm design course experiment 2: data types, operators and simple input and output

C language and algorithm design course experiment 2: data types, operators and simple input and output

insert image description here

1. Purpose of the experiment

insert image description here

  • (1) Master the data types of C language, and understand the internal relationship between character data and integer data.
  • (2) Master the correct input methods for various numerical data.
  • (3) Learn to use the relevant arithmetic operators of C, as well as expressions containing these operators, especially the use of self-increment ( + + ) and self-decrement (- -) operators.
  • (4) Learn to write and run simple applications.
  • (5) Be further familiar with the process of editing, compiling, linking and running C programs.

2. Experimental content

2.1. Input and run the program given in Chapter 3, Question 4 of the textbook:

(1) Input and run the program given in Chapter 3, Question 4 of the textbook as follows:

#include <stdio.h>

int main()
{
    
    
	char cl, c2;
	cl = 97;
	c2 = 98;
	printf("%c %c\n"cl, c2);
	printf("%d %d\n", cl, c2);
	return 0;
}
  • ① Run the above program and analyze why the information is output.

  • ②If you change lines 4 and 5 of the program to

cl = 197;
c2 = 198;

What information will be output when running? Why?

  • ③ If the third line of the program is changed to
int cl,c2;

What information will be output when running? Why?

2.2. Enter the program for Chapter 3, Question 5

(2) Enter the program in Chapter 3, Question 5. That is:
use the following scanf function to input data to make a = 3, b = 7, x = 8.5, y = 71.82, cl = 'A' , c2 = 'a'. Ask how to type on the keyboard?

#include <stdio.h>
int main()
{
    
    
	int a, b;
	float x, y;
	char c1, c2;
	scanf("a=%d b=%d", &a, &b);
	scanf("%f %e", &x, &y);
	scanf("%c%c", &c1, &c2);
	return 0;
}

When running, input data in the following ways, observe the output results, and analyze the reasons.
insert image description here

Through this question, summarize the rules of the input data and the places that are prone to errors.

2.3. Enter the following program:

Enter the following program:

#include <stdio.h>	
int main()
{
    
    
	int i, j, m, n;
	i = 8;
	j = 10;
	m = ++i; n = j ++;
	printf("%d, %d, %d, %d\n", i, j, m, n);
	return 0;
}

① Compile and run the program, pay attention to the values ​​of i, j, m, n variables.
② Change lines 6 and 7 to

m = i++;
n = ++j;

Then compile and run, and analyze the results.
③ Program changed to

#include <stdio.h>
int main()
{
    
    
	int i, j;
	i = 8;
	j = 10;
	printf("%d,%d\n", i++, j ++);
}

Then compile and run, and analyze the results.
④ On the basis of ③, change the printf statement to

printf("%d, %d\n", ++i, ++j);

Compile and run again.
⑤ Change the printf statement to

printf("%d, %d, %d, %d\n", i, j, i++, j++);

Then compile and run, and analyze the results.
⑥ The program is changed to:

#include <stdio.h>
int main()
{
    
    
	int i, j, m = 0, n = 0;
	i = 8;
	j = 10;
	m += i ++; n -= --j;
	printf("i= %d,j= %d,m= %d,n= %d\n", i, j, m, n);
	return 0;
}

Then compile and run, and analyze the results.

2.4. Program design question: If the annual growth rate of my country's GNP is 9%, calculate the percentage increase of my country's GNP 10 years from now. Programming.

(4) If the annual growth rate of my country's GNP is 9%, calculate the percentage increase of my country's GNP 10 years from now. Write a program. (Chapter 3, Question 1)
The calculation formula is:insert image description here

r is the annual growth rate, n is the number of years, and p is the multiple compared with the present.

  • ① Input the program compiled by yourself, compile and run, and analyze the running results.
  • ② The annual growth rate is not specified in the program, instead use the scanf function statement to input, and input 7%, 8%, and 10% respectively. Observation results.
  • ③ Add the printf function statement in the program to prompt what data to input and explain what data to output.

3. Experimental steps

insert image description here

3.1. Data types, operators and simple input and output Experiment Topic 1: Input and run the program given in Chapter 3, Question 4 of the textbook:

(1) Input and run the program given in Chapter 3, Question 4 of the textbook as follows:

#include <stdio.h>

int main()
{
    
    
	char cl, c2;
	cl = 97;
	c2 = 98;
	printf("%c %c\n"cl, c2);
	printf("%d %d\n", cl, c2);
	return 0;
}
  • ① Run the above program and analyze why the information is output.

Run the above program, the output of these information is as follows

insert image description here

 error C2146: 语法错误: 缺少“)(在标识符“cl”的前面)
 warning C4473: “printf”: 没有为格式字符串传递足够的参数
 message : 占位符和其参数预计 2 可变参数,但提供的却是 0 参数
 message : 缺失的可变参数 1 为格式字符串“%c”所需
 error C2059: 语法错误:)2>已完成生成项目“2-数据类型、运算符和简单的输入输出.vcxproj”的操作 - 失败。
========== 全部重新生成: 成功 1 个,失败 1 个,跳过 0==========

The reason for the error is that printf("%c %c\n"cl, c2);there is a missing comma in front of c1.
has been changed to look like this

printf("%c %c\n", cl, c2);

The compilation operation can pass, as shown below

insert image description here

  • ②If you change lines 4 and 5 of the program to
cl = 197;
c2 = 198;

What information will be output when running? Why?

If you change lines 4 and 5 of the program to
cl = 197;
c2 = 198;
the running results are as follows

insert image description here

You can see that the output is
??
-59 -58

  • ③ If the third line of the program is changed to
int cl,c2;

What information will be output when running? Why?

If the third line of the program is changed to
int cl, c2;
the output result is as follows

insert image description here

Change line 3 to
int cl, c2;
the output result is
ab
97 98

3.2. Data types, operators and simple input and output Experiment Topic 2: Input the program of Chapter 3, Question 5

(2) Enter the program in Chapter 3, Question 5. That is:
use the following scanf function to input data to make a = 3, b = 7, x = 8.5, y = 71.82, cl = 'A' , c2 = 'a'. Ask how to type on the keyboard?

#include <stdio.h>
int main()
{
    
    
	int a, b;
	float x, y;
	char c1, c2;
	scanf("a=%d b=%d", &a, &b);
	scanf("%f %e", &x, &y);
	scanf("%c%c", &c1, &c2);
	printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\n", a, b, x, y, c1, c2);
	return 0;
}

When running, input data in the following ways, observe the output results, and analyze the reasons.
insert image description here

How to enter data

  1. a=3,b=7,x=8.5,y=71.82,A,a turns

The input and output results are as follows

a=3,b=7,x=8.5,y=71.82,A,a
a=3,b=-858993460,x=-107374176.000000,y=-107374176.000000,c1=,,c2=b

insert image description here

  1. a=3 b=7 x=8.5 y=71.82 A

The input and output results are as follows

a=3 b=7 x=8.5 y=71.82 A a
a=3,b=-858993460,x=-107374176.000000,y=-107374176.000000,c1=b,c2==

insert image description here

  1. a=3 b=7 8.5 71.82 A

The input and output results are as follows

a=3 b=7 8.5 71.82 A a
a=3,b=7,x=8.500000,y=71.820000,c1= ,c2=A

insert image description here

  1. a=3 b=7 8.5 71.82AaEnter

The input and output results are as follows

a=3 b=7 8.5 71.82Aa
a=3,b=7,x=8.500000,y=71.820000,c1=A,c2=a

insert image description here

  1. 3 7 8.5 71.82AaEnter

The input and output results are as follows

3 7 8.5 71.82Aa
a=-858993460,b=-858993460,x=3.000000,y=7.000000,c1= ,c2=8

insert image description here

  1. a=3 b=7 enter
    8.5 71.82 enter
    A enter
    a enter

The input and output results are as follows

a=3 b=7
8.5 71.82
A
a=3,b=7,x=8.500000,y=71.820000,c1=
,c2=A

insert image description here

  1. a=3 b=7 Enter
    8.5 71.82 Enter
    Aa Enter

The input and output results are as follows

a=3 b=7
8.5 71.82
Aa
a=3,b=7,x=8.500000,y=71.820000,c1=
,c2=A

insert image description here
8. a=3 b=7 Enter
8.5 71.82Aa Enter

The input and output results are as follows

a=3 b=7
8.5 71.82Aa
a=3,b=7,x=8.500000,y=71.820000,c1=A,c2=a

insert image description here

Through this question, summarize the rules of the input data and the places that are prone to errors.

  • scanf("a=%d b=%d", &a, &b);When entering, it should a=%d b=%dbe consistent with:a=3 b=7
  • scanf("%f %e", &x, &y);When entering, it should %f %ebe consistent with:8.5 71.82

3.3. Data types, operators and simple input and output Experiment topic 3: Enter the following program:

Enter the following program:

#include <stdio.h>	
int main()
{
    
    
	int i, j, m, n;
	i = 8;
	j = 10;
	m = ++i; n = j ++;
	printf("%d, %d, %d, %d\n", i, j, m, n);
	return 0;
}

① Compile and run the program, pay attention to the values ​​of i, j, m, n variables.

Run the above program, the output is as follows

insert image description here

++i is to add one to the variable itself first, and then assign it to the variable m.
j ++ means that the variable is first assigned to the variable n, and then one is added to itself.
So m=9, n=10

② Change lines 6 and 7 to

m = i++;
n = ++j;

Then compile and run the above program, the output is as follows

insert image description here

i ++ is to assign the variable to the variable m first, and then add one to itself.
++j is to add one to the variable itself first, and then assign it to the variable m.
So m=8, n=11

  • The analysis of 3-6 is similar to the analysis of 1-2.

③ Program changed to

#include <stdio.h>
int main()
{
    
    
	int i, j;
	i = 8;
	j = 10;
	printf("%d,%d\n", i++, j ++);
}

Then compile and run the above program, the output is as follows

insert image description here

④ On the basis of ③, printfchange the statement to

printf("%d, %d\n", ++i, ++j);

Then compile and run the above program, the output is as follows

insert image description here

⑤ Change the printf statement to

printf("%d, %d, %d, %d\n", i, j, i++, j++);

Then compile and run the above program, the output is as follows

insert image description here

⑥ The program is changed to:

#include <stdio.h>
int main()
{
    
    
	int i, j, m = 0, n = 0;
	i = 8;
	j = 10;
	m += i ++; n -= --j;
	printf("i= %d,j= %d,m= %d,n= %d\n", i, j, m, n);
	return 0;
}

Then compile and run the above program, the output is as follows

insert image description here

3.4. Data types, operators and simple input and output experiments Topic 4: Program design question: If the annual growth rate of my country's GNP is 9%, calculate the percentage increase of my country's GNP 10 years from now . Programming.

(4) If the annual growth rate of my country's GNP is 9%, calculate the percentage increase of my country's GNP 10 years from now. Write a program. (Chapter 3, Question 1)
The calculation formula is:insert image description here

r is the annual growth rate, n is the number of years, and p is the multiple compared with the present.

  • ① Input the program compiled by yourself, compile and run, and analyze the running results.

3.4.1. Writing programs

Write the program as follows

3.4.1.1, the calculation percentage of the for loop method is as follows

	//for 循环方式计算
	for (int i = 0; i < n; i++)
	{
    
    
		p = p * (1 + r);
	}

	printf("%.3lf\n", p);

3.4.1.2. The method of calling the math function to calculate the percentage is as follows

	// 调用math函数方式
	p = 1.0;
	r = 0.09;
	n = 10;

	p = pow(1 + r, n);
	printf("%.3lf\n", p);

3.4.1.3, the for loop method and the method of calling the math function to calculate the percentage to form a comprehensive program is as follows

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    
    
	system("color 3E");

	double p, r;
	int n;

	p = 1.0;
	r = 0.09;
	n = 10;

	// for 循环方式
	for (int i = 0; i < n; i++)
	{
    
    
		p = p * (1 + r);
	}

	printf("%.3lf\n", p);
	
	// 调用math函数方式
	p = 1.0;
	r = 0.09;
	n = 10;

	p = pow(1 + r, n);
	printf("%.3lf\n", p);

	system("pause");
	return 0;
}

The program compilation result is as follows

insert image description here

  • ② The annual growth rate is not specified in the program, instead use the scanf function statement to input, and input 7%, 8%, and 10% respectively. Observation results.

3.4.2. Change the annual interest rate to the input method

The procedure is as follows

	p = 1.0;
	//r = 0.09;
	n = 10;

	scanf("%lf", &r);

	// for 循环方式
	for (int i = 0; i < n; i++)
	{
    
    
		p = p * (1 + r);
	}

	printf("%.3lf\n\n", p);
	
	// 调用math函数方式
	p = 1.0;
	//r = 0.09;
	n = 10;
	
	scanf("%lf", &r);

	p = pow(1 + r, n);
	printf("%.3lf\n", p);

Enter 7%,8%,10%the running results respectively as follows

  1. 7%

insert image description here
2. 8%

insert image description here

  1. 10%

insert image description here

3.4.3. Add printf function statement in the program

③ Add the printf function statement in the program to prompt what data to input and explain what data to output.
Add printfthe following statement

	printf("输入年利率: ");
	printf("\n国民生产总值与现在相比增长%.3lf百分比\n\n", p);

The result of running the program is as follows

insert image description here

4. Experimental summary

insert image description here

This time, I passed the C language and algorithm design course experiment 2: data types, operators and simple input and output. Mastered the following points.

  • (1) Master the data types of C language, and understand the internal relationship between character data and integer data.
  • (2) Master the correct input methods for various numerical data.
  • (3) Learn to use the relevant arithmetic operators of C, as well as expressions containing these operators, especially the use of self-increment ( + + ) and self-decrement (- -) operators.
  • (4) Learn to write and run simple applications.
  • (5) Be further familiar with the process of editing, compiling, linking and running C programs.

5. The complete procedure of the experiment

insert image description here

5.1. Data types, operators and simple input and output Experiment Topic 1: Input and run the complete program of the program given in Chapter 3, Question 4 of the textbook

The complete program is as follows

#include <stdio.h>

int main()
{
    
    
	/*char cl, c2;*/
	int cl, c2;
	cl = 97;
	c2 = 98;
	printf("%c %c\n", cl, c2);
	printf("%d %d\n", cl, c2);
	return 0;
}

5.2. Data types, operators and simple input and output Experiment Topic 2: Input the complete program of the program in Chapter 3, Question 5

The complete program is as follows

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
int main()
{
    
    
	int a, b;
	float x, y;
	char c1, c2;
	scanf("a=%d b=%d", &a, &b);
	scanf("%f %e", &x, &y);
	scanf("%c%c", &c1, &c2);
	printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\n", a, b, x, y, c1, c2);
	return 0;
}


5.3. Data types, operators, and simple input and output experiments Topic 3: Input the complete program of the following program

The complete program is as follows

#include <stdio.h>	
int main()
{
    
    
	//int i, j, m, n;
	//i = 8;
	//j = 10;
	///*m = ++i; n = j++;*/
	//m = i++;
	//n = ++j;
	//printf("%d, %d, %d, %d\n", i, j, m, n);

	/*int i, j;
	i = 8;
	j = 10;*/
	/*printf("%d,%d\n", i++, j++);*/
	/*printf("%d, %d\n", ++i, ++j);*/
	/*printf("%d, %d, %d, %d\n", i, j, i++, j++);*/

	int i, j, m = 0, n = 0;
	i = 8;
	j = 10;
	m += i++; n -= --j;
	printf("i= %d,j= %d,m= %d,n= %d\n", i, j, m, n);
	return 0;
}

5.4. Data types, operators and simple input and output experiments Topic 4: Program design question: If the annual growth rate of my country's GNP is 9%, calculate the percentage increase of my country's GNP 10 years from now . Complete program for writing programs

The complete program is as follows

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    
    
	system("color 3E");

	double p, r;
	int n;

	p = 1.0;
	//r = 0.09;
	n = 10;

	printf("输入年利率: ");
	scanf("%lf", &r);

	// for 循环方式
	for (int i = 0; i < n; i++)
	{
    
    
		p = p * (1 + r);
	}

	printf("\n国民生产总值与现在相比增长%.3lf百分比\n\n", p);


	// 调用math函数方式
	p = 1.0;
	//r = 0.09;
	n = 10;
	
	printf("输入年利率: ");
	scanf("%lf", &r);

	p = pow(1 + r, n);
	printf("\n国民生产总值与现在相比增长%.3lf百分比\n\n", p);

	system("pause");
	return 0;
}


insert image description here

Guess you like

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