C language experiment report of Nanjing University of Posts and Telecommunications

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

Experiment 1 sequence structure programming exercise

1. Experimental purpose and requirements
(1) Get to know the computer environment of Microsoft Visual Studio 2010 for the first time, learn to build projects and files, learn to compile, link and run programs, and find and modify simple syntax errors.
(2) Call the input and output processing functions scanf( ), printf( ), getchar( ), putchar() correctly to perform data I/O, observe the output format, and master the keyboard input method.
(3) Preliminarily use the debugger to track the program and observe the changes in the value of the variable.

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

3. Experiment principle and content
Experiment topics (1) [See topic 1 of experiment textbook experiment 1] : Create a folder named after your student ID on Disk D (for example: B07020518) (all the files for future experiments are stored in this file Folder); correctly enter the VS2010 integrated development environment, create a single-file project, the project name is: exp1_1, and then add a new source file to the project, named: exp1_1.c, complete: enter two from the keyboard Integers a and b, calculate and output their sum sum, difference difference, product product, quotient and average ave (note the definition type of ave and quotient).
Specific requirements:
① Edit, compile, connect and run the program, and observe the results.
② Observe the folders where exp1_1.obj, exp1_1.exe, and exp1_1.c are located.
③ Define the variable quotient as int and double respectively, and use the format string correctly in the printf function. When the two integers read are 4 and 5, observe the running results and analyze the reasons, and think about how to make the variable quotient more accurate.
④ Define the variable ave as int and double respectively, and use the format string correctly in the printf function. When the two integers read are 4 and 5, observe the different running results and analyze the reasons.
⑤ Track the program step by step (using the F10 key), and observe the changes of each variable during each step of the program.
Experimental answer:

① The source code is as follows:

#include<stdio.h>
int main()
{
    
    
	int a,b,sum,difference,product,quotient,ave;
    printf("请输入两个整数a和b: ");
	scanf("%d,%d",&a,&b);
    sum=a+b;
	difference=a-b;
	product=a*b;
	quotient=a/b;
	ave=(a+b)/2;
	printf("sum=%d,difference=%d,product=%d,quotient=%d,ave=%d\n",sum,difference,product,quotient,ave);
	return 0;
}

#include<stdio.h>
int main()
{
    
    
	int a,b,sum,difference,product;
	double quotient,ave;
    printf("请输入两个整数a和b:");
	scanf("%d,%d",&a,&b);
    sum=a+b;
	difference=a-b;
	product=a*b;
	quotient=a*1.0/b;
	ave=(a+b)*1.0/2;
	printf("sum=%d,difference=%d,product=%d,quotient=%f,ave=%f\n",sum,difference,product,quotient,ave);
	return 0;
}


full path name of the file where the file (for example: d: \ TTT \ t1.c)
exp1_1.cd:\ttt\exp1_1.c
exp1_1.obj D: \ TTT \ Debug \ exp1_1.obj
exp1_1.exe D: \ TTT \Debug\exp1_1.exe


The type of quotient solves the quotient statement output quotient format string the quotient value output on the screen
int quotient=a/b; %d 0
double quotient=a*1.0/b; %f 0.800000

Differences in running results, reasons, and measures to ensure correct results:
Difference: When the quotient type is int, the result has no decimal; when the quotient type is double, the result has a decimal, and the value is more accurate when a/1.0.
Reason: the difference of variable types, the difference between integer division and real number division.
Measures to ensure the correct result: try to use real number division when doing division.

The type of ave solves the sentence of ave and the format string of ave is the ave value output on the screen
int ave=(a+b)/2; %d 4
double ave=(a+b)*1.0/2; %f 4.500000

Differences in running results and reasons:
Differences: When the ave type is int, the result has no decimal; when the ave type is double, the result has a decimal, and the value is more accurate when (a+b)/1.0.
Reason: the difference of variable types, the difference between integer division and real number division.

Experiment topic (2) [see topic 3 of experiment textbook experiment 1] : Program exp1_3.c to realize the exchange of two integers a and b, reminding us to think about how we exchange two glasses of water in real life.
Experimental answer: The
code of the source program exp1_3.c is:

#include <stdio.h>
int main( )
{
    
    
	int a,b,temp;
	printf("请输入两个整数:");
	scanf("%d,%d",&a,&b);
	temp=a;
	a=b;
	b=temp;
	printf("a=%d,b=%d\n",a,b);
    return 0;
}

Experiment topic (3) [see topic 4 of experiment textbook experiment 1] : Program exp1_4.c, the factory coats a cylinder, the cost is 10 yuan per square centimeter, and now enter the radius (cm) of the bottom surface of the cylinder from the keyboard and The height of the cylinder (centimeters), find the surface area (square centimeters) of the cylinder and the required cost, and the output result is required to keep two decimal places.
Experimental answer:
① The code of the source program exp1_4.c is:

#include<stdio.h>
#define PI 3.14159
int main()
{
    
    
	double r,h,area,fy;
    printf("请输入圆柱底面的半径和圆柱的高: ");
	scanf("%lf,%lf",&r,&h);
    area=2*PI*r*r+2*PI*r*h;
	fy=10*area;
	printf("area=%.2f,fy=%.2f\n",area,fy);
	return 0;
}         

② Run the program several times and enter different data according to the requirements in the first column of the table below. Please fill in the table below
. The requirements of the input data. The data you input. The output result
radius and height of your program are all positive integers 1,2 area=18.85, fy=188.50
radius and height are positive real numbers 1.0, 2.0 area=18.85, fy=188.50
radius and height have a negative number 1, -2 area=-6.28, fy=-62.83
radius and height are both negative -1 , -2 area=18.85,fy=188.50
other test cases 2,3 area=62.83,fy=628.32

Experiment topic (4) [see topic 6 of experiment 1 of the experimental textbook] : Write a program exp1_6.c to input a 3-digit positive integer, calculate the reverse number of the number, and output it. For example: input 789, output 987;
experimental solution: the code of the source program exp1_6.c is:

#include<stdio.h>
int main()
{
    
    
	int x,a,b,c;
    printf("请输入一个三位整数:");
	scanf("%d",&x);
    a=x/100;
	b=x/10%10;
	c=x%10;
	printf("该数的逆序数=%d%d%d",c,b,a);
	return 0;
}    

4. Summary of the experiment (including problems and solutions, experience, opinions and suggestions, experimental error messages and solutions, etc.)
(1) The main problems encountered in the
experiment and the solutions The scanf function gives the address of the input variable, and when defining PI, add =) between PI and 3.14159, and then realize it and change it.
(2) Experimental experience
Learning C language needs to keep the basics in mind, practice and try more, to solve problems and explore better expressions.
(3) Opinions and suggestions (nothing can be omitted)
The computer in the school computer room always crashes during debugging

Experimental report
5. Supporting graduation requirement index points
1.2-H Master computer software and hardware related engineering basic knowledge, and can 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/109322480