C Programming Experiment Report III

C language programming experiment report

Name: Zhou Manjie

Experimental Location: home

Experimental Time: 2020.3.26

experimental project:

  • 4.3.1: The if statement
  • 4.3.2: switch-case application
  • 4.3.3: The switch-case nested if statements
  • 4.3.4: switch-case application of the nested structure
  • 4.3.5: Analysis Program
  • Training Project: Achieving calculator

1, experimental purposes and requirements

1. Master C language representation logic value (0 for "false", 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. Nested master switch statement syntax and usage, attention break in a switch statement, the usage and the switch statement.

Content Experiments

1, lab exercises: The if statement

A brief description of the problem: reading three boxes represent the length, width and height of an integer value, and determines the output of the box is a cube or a rectangular parallelepiped.
2 experiment code:

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

3 Analysis: when you enter, you need to use &&

2, exercises: switch-case application

A brief description of the problem: write a program to achieve the following functions: print shop paper (18 yuan / this), ink cartridges (132 yuan / month), CD (4.5 yuan / Zhang) sold stores carry discount bargain activities. Specific rules are as follows: Total customers to buy goods over 100 yuan, 5% discount; more than 200 yuan, discount of 6%; more than 300 yuan, 7% discount; more than 400 yuan discount of 8%; over 500 yuan, 10% discount. Depending on the amount of purchase, loans payable is calculated.
2 experiment code:

#include<stdio.h>
main()
{
	int x,y,a,b,c,temp;    /*x,y分别为打印纸、墨盒的单价,a,b,c分别为购买的数量*/ 
	double z,sum,t;          /*z为光盘的单价,sum为实际付款数,t为应付款数*/ 
	printf("请输入打印纸,墨盒,光盘的数量:\n");
	scanf("%d,%d,%d",&a,&b,&c);
	x=18;y=132;z=4.5;
	sum=a*x+b*y+c*z;
	if(sum<=100)
	temp=0;
	else if(100<sum&&sum<=200)
	temp=1;
	else if(200<sum&&sum<=300)
	temp=2;
	else if(300<sum&&sum<=400)
	temp=3;
	else if(400<sum&&sum<=500)
	temp=4;
	else if(sum>500)
	temp=5; 
	switch(temp)
	{
		case 0:t=sum;
		break;
		case 1:t=sum*0.95;
		break;
		case 2:t=sum*0.94;
		break;
		case 3:t=sum*0.93;
		break;
		case 4:t=sum*0.92;
		break;
		case 5:t=sum*0.9;
		break;
	}
	printf("应付款数为:%lf\n",sum);
}

3 Analysis: there is no time to switch the input method when entering text over, resulting in a comma-input errors. A few percent should be expressed as a decimal.

3, lab exercises: The switch-case nested if statements

A brief description of the problem: the input values in a given year, the month, the output of the number of days in the month.
2 experiment code:

#include<stdio.h>
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=31;break;
		case 4:days=30;break;
		case 6:days=30;break;
		case 9:days=30;break;
		case 11:days=30;break;
	}
	printf("%d",days);
}

3 Analysis: None

4, exercises: switch-case application of the nested structure

A brief description of the problem: write a vending machine program. This program has the following features: two two menus, one menu is selected commodity type; two specific menu item is selected. Customers first select the type of goods, and select merchandise, enter the number of purchase. Vending machines based on the number of goods and input selection, calculates and displays the total amount of the selected goods.
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);
    return 0;
}

3 Analysis: The code contains the text, pay attention to the half-width English

5, lab exercises: Analysis Program

A brief description of the problem: Modify the error code
2 experiment code:
source code:

#include<stdio.h>
int main()
{
	int num=20;
	if(5<sum<10) 
	    printf("%d  in  rangd  (5,10)!\n",num);
	else
	    printf("%d  out  of  range (5,10)!\n",num);
}

After modifying the code:

#include<stdio.h>
int main()
{
	int num=20;
	if(5<num&&num<10)
	    printf("%d  in  rangd  (5,10)!\n",num);
	else
	    printf("%d  out  of  range (5,10)!\n",num);
}

3 Analysis: conditions of the original code input errors, in the case of the original code, only judgment is true or false. The result thus output is 0 or 1, so that error. Logical operators should be used to connect them.

Training Project: Achieving calculator

flow chart:

Code:

#include<stdio.h> 
main()
{
	int e;
	double a,b,c,d;
	char op;
	for(;c>0;c!=1)
	{
		printf("请输入一个算式:\n");
		scanf("%lf%c%lf",&a,&op,&b);
		getchar();
		switch(op)
		{
			case'+':
				printf("%lf+%lf=%lf\n",a,b,d=a+b);
				break;
				case'-':
					printf("%lf-%lf=%lf\n",a,b,d=a-b);
					break;
					case'*':
						printf("%lf*%lf=%lf\n",a,b,d=a*b);
						break;
						case'/':
							if(b!=0)
							printf("%lf/%lf=%lf\n",a,b,d=a/b);
							else
							printf("除数不能为0!\n");
							break;
		}
		printf("结果为:\n",d);
		printf("继续计算请输入不为1的正数,退出请输入1:\n");
		scanf("%d",&e);
		if(e==1) 
		break;
	}
}

Analysis: When using a for loop problems encountered infinite loop

Experimental Summary:

In this experiment, mainly familiar with the if statement and the switch statement. The main problem encountered in the code section simple calculator, especially the loop using only solved with the help of students, or more operating it!

Guess you like

Origin www.cnblogs.com/absolutely-123/p/12573020.html