Nanjing University of Posts and Telecommunications C language experiment report 2v2

Experiment 2: Selection and programming of loop structure

1. The purpose and requirements of the experiment
(1) Able to correctly choose to use the if statement and switch statement to realize the program design of the selected structure.
(2) Correctly use while, for, do...while statements to realize the programming of loop structure.
(3) Use loop nesting to realize more complex program control, and understand the execution process and condition changes of loop nesting programs.
(4) Use the vs 2010 debugger to track and debug the program, set breakpoints, single-step tracking, view the value of the corresponding variable or expression, help understand the loop, select the structure of the program execution process, and find and modify logic errors.

2. Experimental environment (experimental equipment)
hardware: microcomputer
software: Windows operating system, Microsoft Visual Studio 2010

3. Experiment principle and content
Experiment topic (1) [See topic 6 of experiment 2 of the experiment textbook] : Program exp2_6.c, read in the values ​​of 3 double variables a, b, and c, and use the if statement to judge whether they are If a triangle can be formed, what kind of triangle (equosceles triangle, isosceles triangle, right triangle, isosceles right triangle, general triangle)? Output the judgment results in various situations. (It is recommended to refer to the flow chart 4.20 of the experimental guidance textbook. Sorting a, b, and c from small to large can simplify the code)
Experimental solution:
① The code of the source program exp2_6.c is:

#include<stdio.h>
#include<math.h>
int main()
{
    
    
	double a,b,c,t;
	printf("请输入三角形的三边长:");
	scanf("%lf,%lf,%lf",&a,&b,&c);    
	if(a>b)  
	{
    
    
		t=a;
		a=b;
		b=t;
	}
	if (a>c)
	{
    
    
		t=a;
		a=c;
		c=t;
	}
	if (b>c)
	{
    
    
		t=b;
		b=c;
		c=t;
	}
	if (a+b<=c)
		printf("不能构成三角形");
	else if (fabs(a*a+b*b-c*c)<1e-3)
		if (fabs(a-b)<1e-3)
				printf("这是个等腰直角三角形");
		else
				printf("这是个直角三角形");
	else if (fabs(a-b)<1e-3||fabs(b-c)<1e-3)
		if (fabs(a-c)<1e-3)
				printf("这是个等边三角形");
		else
			    printf("这是个等腰三角形");
	else
			printf("这是个一般三角形");
	printf("\n");
	return 0;
}

② Run the program multiple times, enter different data, and get different results. Please fill in the following table. The
three original data represent the situation. The three sides of your input. The output result of your program
can form an equilateral triangle 3,3,3. This is an equilateral triangle. Equilateral triangle
can form right triangle 4,3,5 This is a right triangle
can form isosceles right triangle 1.414,1,1 This is an isosceles right triangle
can form isosceles triangle 3,3,4 This is an isosceles triangle
can form General triangle 2,3,4 This is a general triangle, it
cannot form triangle 1,1,2, it cannot form triangle

Experiment topic (2) [see topic 1 of experiment 3 of the experiment textbook] : Use while, for, do...while loop statements to calculate the sum problem:

Experimental answers:
① The source code for the sum of the first 1000 items is as follows:

#include<stdio.h>
int main()
{
    
    
	double item,sum=0;
	int i,sign=1;
	double m=2,n=1;
	for(i=1;i<=1000;i++)//循环i从1到1000
	{
    
    
	    item=sign*m*1.0/n;//计算每一项的绝对值
	    sign=-sign;//改变每一项的正负
		m=m+n;//计算分子
		n=m-n;//计算分母
		sum+=item;//求和
	}
	printf("sum=%f\n",sum);//输出和
	return 0;
}

① If the number of items to be calculated is not the constant 1000 but the variable n (value obtained by input), please modify it on the basis of the program exp3_1.c, the modified source program name is: exp3_1A.c, run the modified program. Please write the statement you have changed (including addition, deletion, modification) in the following table (the first line of the table is an example). The
original operating instructions in exp3_1.c are correspondingly modified. There is
no increase in exp3_1A.c and there is no prompt to enter the number of calculation items. Sentence printf("Please enter the number of calculation items N:");
There is no sentence to increase the number of input calculation items scanf("%d",&N);
for(i=1;i<=1000;i++) Modify 1000 to N for (i=1;i<=N;i++)

② Run the above program, fill in the following table, and observe the results
. The sum result of the n value input by the keyboard and the screen output
2 sum=0.500000
8 sum=0.577683
13 sum=2.195954
14 sum=0.577921
15 sum=2.195956
16 sum=0.577922
19 sum= 2.195956
20 sum=0.577922
199 sum=2.195956
200 sum=0.577922
  Please enter any odd and even numbers exceeding 16 by yourself , observe the sum results, and combine the results in the above table. What rules do you find? Please observe the characteristics of each item in the summation, consult the information, and explain the laws you have discovered.
I found that the
sum of any odd numbers
over 16 tends to 2.195956 and the sum results of any even numbers over 16 tends to 0.577922.
Explanation: The
sum of any odd and even numbers over 16 exceeds the accuracy of the double type.

Experiment topic (3) [see topic 4 of experiment textbook experiment three] : Write the program exp3_4.c, first use a loop to determine whether the positive integer x read from the keyboard is a prime number, and output the judgment result. Rewrite on this basis and add the outer loop to find all prime numbers in the specified range (a, b) and output them in the format of 5 per line. (Here a, b variables are input from the keyboard and the range is: 10≤a≤b≤1000)
Experimental solution:
write program exp3_4.c to read a positive integer x from the keyboard, judge whether it is a prime number and output the judgment result, code as follows:

#include<stdio.h>
#include<math.h>
int main()
{
    
    
	int x,k,i;
	printf("请输入一个正整数x:");
	scanf("%d",&x);
	if(x<=0)
		printf("请输入一个正整数:");
	else if(x==1)
		printf("%d is not a prime.\n",x);
	else
	{
    
    
		k=(int)sqrt(x);
	    for(i=2;i<=k;i++)
			if(x%i==0)
				break;
		if(i>k)
			printf("%d is a prime,\n",x);
		else
			printf("%d is not a prime.\n",x);
	}
	return 0;
}

② Rewrite on the basis of the program exp3_4.c, add the outer loop, find all the prime numbers in the specified range (a, b), and output them in the format of 5 per line. The code of the rewritten program exp3_4A.c is as follows :
(Hint: The variable x used to judge whether a prime number is no longer read in, but as a loop control variable, take all the numbers in the specified range (a, b) in turn, and input the values ​​of variables a and b from the keyboard. Ensure that the read a, b are in the range of [10, 1000], and a is the lower limit and b is the upper limit, that is, the condition that should be met is: 10<=a<=b<=1000, otherwise the input data is invalid. Prompt the user to re-enter a, b until the requirements are met)

#include<stdio.h>
#include<math.h>
int main()
{
    
    
	int x,k,i,count=0,a,b;
	printf("请输入正整数a,b:");
	scanf("%d,%d",&a,&b);
	if(a<10||b>1000||a>b)
		printf("请输入a和b使得10<=a<=b<=1000!");
	else 
	     for(x=a+1;x<b;x++)
	     {
    
    
		   k=(int)sqrt(x);
	       for(i=2;i<=k;i++)
			   if(x%i==0)
				   break;
		   if(i>k)
		   {
    
    
			   count++;
			   printf("%5d",x);
			   if(count%5==0)
				   printf("\n");
		   }
	     }
		 printf("\n");
	return 0;
}

Run the program, enter the values ​​of a and b according to the first column of the table, and fill in the corresponding output results
. The values ​​of a and b entered by the keyboard will be output on the screen.
12 14 13
24 28
34 56 37 41 43 47 53
111 189 113 127 131 137 139
149 151 157 163 167
173 179 181

Experiment topic (4) [see topic 6 of experiment three in the experiment textbook] : program exp3_6.c, and use loop nesting to output a diamond.
*
* * *
* * * * *
* * * * * * *
* * *
* * *
*
Experimental solution:
①Please write out the source code of the program exp3_6.c and make appropriate comments:

#include<stdio.h>
int main()
{
    
    
	int i,j;
	for(i=1;i<=4;i++)//上三角控制行
	{
    
    
		for(j=1;j<=4-i;j++)//上三角控制每行空格
			printf(" ");
	    for(j=1;j<=1+2*(i-1);j++)//上三角控制每行*
			printf("*");
		printf("\n");
	}
    for(i=3;i>=1;i--)//下三角控制行
	{
    
    
		for(j=1;j<=4-i;j++)//下三角控制每行空格
			printf(" ");
	    for(j=1;j<=1+2*(i-1);j++)//下三角控制每行*
			printf("*");
		printf("\n");
	}
	return 0;
}

②Try to make certain modifications on the basis of the above program, and output respectively: upper triangle, lower triangle, left half triangle, right half triangle. What is the control statement that needs to be modified?
Output the upper triangle: delete the statement of the
lower triangle. Output the lower triangle: delete the statement of the upper triangle.
Output the left half triangle: modify the statement
that controls each line . Output the right general triangle: modify the statement that controls the space sum of each line .
③Try to modify the program to achieve Output a rhombus of any line, the number of lines is determined by the input, and check how many lines of graphics can you output?

#include<stdio.h>
int main()
{
    
    
	int i,j,n;
	printf("请输入菱形行数n:");
	scanf("%d",&n);
	for(i=1;i<=(n+1)/2;i++)//上三角控制行
	{
    
    
		for(j=1;j<=(n+1)/2-i;j++)//上三角控制每行空格
			printf(" ");
	    for(j=1;j<=1+2*(i-1);j++)//上三角控制每行*
			printf("*");
		printf("\n");
	}
    for(i=(n-1)/2;i>=1;i--)//下三角控制行
	{
    
    
		for(j=1;j<=(n+1)/2-i;j++)//下三角控制每行空格
			printf(" ");
	    for(j=1;j<=1+2*(i-1);j++)//下三角控制每行*
			printf("*");
		printf("\n");
	}
	return 0;
}

It can output 167 lines and only odd-numbered lines.

4. Experiment summary (including problems and solutions, experience, opinions and suggestions, experimental error messages and solutions, etc.)
(1) Main problems encountered in the experiment and solutions

main problem:

  1. In exp2_6.c, when describing a=b, fabs is not written before the statement, resulting in only the first case.
  2. Always forget to take the address of the variable in the input statement
  3. In Exp3_6.c, when writing a triangle, the output is an equilateral triangle.
    Solution:
  4. After checking the book tips, the problem was found and solved.
  5. Because I am familiar with this kind of error, I find and solve the problem quickly.
  6. After checking the book tips and thinking deeply, the problem was corrected.

(2) Experimental experience

To write code, you still have to add more machine exercises to find and correct problems in time, avoid small problems, ask more, search more, and think more. Don't be impetuous!

(3) Comments and suggestions (nothing can be omitted)

Fifth, support the graduation requirement index points
1.2-H master computer software and hardware related engineering basic knowledge, and be able to use it to analyze related engineering problems in computer and application fields.
3.1-M Master the professional knowledge and development tools needed to design/develop complex engineering problem solutions.

Guess you like

Origin blog.csdn.net/qq_46392282/article/details/109347347