C programming lab report (third)

C Programming lab report

Pilot project: 4.3.1,4.3.2,4.3.3,4.3.4,4.3.5
Name: Guang-Jun Li experiment Location: home experiment time 2020.3.28
1, experimental purposes and requirements
1, C language to master the representation of a logical value (0 represents "false" and 1 for "true")
2, learn the proper use of the relationship and logical expressions
3. mastered various forms of if statement syntax and usage. matching relationship if and else if statements, as well as nested if statements.
4. The master switch statement syntax and use of attention to break in a switch statement in usage and switch statements nested
Second, the experiment content
1, lab exercises: 4.3.1
brief description of a problem: Apply the if statement
2 experiment code:

#include<stdio.h>
int main()
{
	int l,w,h;
	printf("请输入箱子的长宽高:\n");
	scanf("%d%d%d",&l,&w,&h);
	if(l>0&w>0&h>0)
	    if(l==w&&w==h)
	        printf("该箱子是正方体。\n");
	    else
	        printf("该箱子是长方体。\n");
	return 0; 
 } 

Analysis 3: Note that the use of ifelse
2, exercises: 4.3.2
brief description of the problem 1: Application of the switch-case
2 experiment code:

#include<stdio.h>
int main()
{
	int a,b,c,x,sum;//a代表打印纸,b代表墨盒,c代表光盘,sum表示应付款数 
	printf("请输入各种商品的购买数量:\n"); 
	scanf("%f,%f,%f",&a,&b,&c);
	sum=(18*a+132*b+4.5*c);
    x=sum/100;
    	switch(x)
    	{
    		case 0:sum=sum;
    		printf("应付款=%f",sum);
    		break;
    		case 1:sum=sum*0.95;
    		printf("应付款=%f",sum);
    		break;
    		case 2:sum=sum*0.94;
    		printf("应付款=%f",sum);
    		break;
			case 3:sum=sum*0.93; 
    		printf("应付款=%f",sum);
    		break;
    		case 4:sum=sum*0.92;
    		printf("应付款=%f",sum);
    		break;
    		case 5:sum=sum*0.9;
    		printf("应付款=%f",sum);
    		break;
    		default:printf("应付款=%f",sum); 
		}
		printf("应付款=%.2f",sum);
}

3 Analysis: Using switch structure, the definition of things to be clear
3. exercises: 4.3.3
a brief description of the problem 1: switch-case application of nested if statements
2 experiment code:

#include<stdio.h>
int main()
{
	int year,month,days;
	printf("please enter year and month:\n");
	scanf("%d,%d",&year,&month);
	switch (month)
	{
		case 2:
			if((year%4==0&&year%100!=0)||year%400==0)
			   days=29;
			else
			   days=28;
			break;
		case 1:days=31;break;
		case 3:days=31;break;
		case 5:days=31;break;
		case 7:days=31;break;
		case 8:days=31;break;
		case 10:days=31;break;
		case 12:days=30;break;
		case 4:days=30;break;
		case 9:days=30;break;
		case 11:days=30;break;
	}
	printf("%d年%d月%d天\n",year,month,days); 
	return 0; 
}

Analysis 3: step by step, mainly for determining February
4 exercises: 4.3.4
brief description of the problem 1: switch-case use nested structure
2 experiment code:

#include<stdio.h>
int main()
{
	int x,n,y;
	float sum=0.0;
	printf("请选择:1.日用品 2.文具 3.食品\n");
	scanf("%d",&x);
	switch(x)
	{
		case 1:printf("请选择:1.牙刷(3.5元/支) 2.牙膏(6.2元/支)\n");
		       printf("请选择:3.肥皂(2/块) 4.毛巾(8.6元/条)\n"); 
		       scanf("%d",&y);
		       printf("数量?");
			   scanf("%d",&n);
			   switch (y)
			   {
			    case 1:sum=3.5*n;break;
			    case 2:sum=6.2*n;break;
			    case 3:sum=2*n;break;
			    case 4:sum=8.6*n;break;
			   }
			   break;
		case 2:printf("请选择:1.笔(3元/支) 2.笔记本(1.2元/本)\n");
		       printf("请选择:3.文件夹(12元/个) 4.文具盒(8.6元/个)\n"); 
		       scanf("%d",&y);
		       printf("数量?");
			   scanf("%d",&n);
			   switch (y)
			   {
			    case 1:sum=3*n;break;
			    case 2:sum=1.2*n;break;
			    case 3:sum=12*n;break;
			    case 4:sum=8.6*n;break;
			   }
			   break;
		case 3:printf("请选择:1.糖(3.6元/包) 2.盐(1元/包)\n");
		       printf("请选择:3.饼(2元/个) 4.方便面(3.6元/条)\n"); 
		       scanf("%d",&y);
		       printf("数量?");
			   scanf("%d",&n);
			   switch (y)
			   {
			    case 1:sum=3.6*n;break;
			    case 2:sum=1*n;break;
			    case 3:sum=2*n;break;
			    case 4:sum=3.6*n;break;
			   }
			   break; 
	 } 
	 printf("总计;%.2f元\n",sum);
}

3 Analysis: process many, but not complicated, to be more careful
5. exercises: 4.3.5
a brief description of the problem 1: Analysis Program
2 experiment code:

#include<stdio.h>
int main()
{
	double x=1000/3.0;
	double y=x-333.0;
	double z=3*y-1.0;
	printf("x=%lf\n",x);
	printf("y=%lf\n",y);
	printf("z=%lf\n",z);
	if(z==0) printf("z==0.\n");
	    else printf("z不等于0.\n");
	return 0; 
}
#include<stdio.h>
int main()
{
	int num=20;
	if(5<num&&num<10)
	    printf("%d in range (5,10)!\n",num);
	else
	    printf("%d out of range (5,10)!\n",num);
}

3 Analysis: are some common mistakes possible, to remember these errors point.
Third, the project training: Achieving calculator
1, design ideas:
2, flow chart:

3. Problems encountered during the design and improvement of methods: the beginning of the divisor is not 0 can not be taken into account
4, Code:

#include<stdio.h>
int main()
{
	float x,y,z;
	char a; 
	printf("请输入两个数字:");
	scanf("%f%a%f",&x,&y,&z);
	switch(a)
	{
		case '+':z=x+y; break;
		case '-':z=x-y; break;
		case '*':z=x*y; break;
		case '/':z=(y==0)?(0):(x/y); break;
	    default:z=0; break;
	    
	}
	printf("%f%a%f=%f\n",x,y,a,z);
	return 0;
 } 

Fourth, the experiment summary
this week down still learned a lot, and every day there are in progress, although very slow, but also inferior to other people, but I will try to overtake them. Accelerate their speed and improve their capabilities.

Guess you like

Origin www.cnblogs.com/aki963624155/p/12587432.html