c language program design lab report (c)

C Programming lab report

Pilot projects: Experimental realization of the experiment 4.3.1 4.3.2 4.3.3 Experimental Experimental Experimental 4.3.4 4.3.5 calculator
Name: LONG Cheng experiment Location: home experiment Time: 2020.3.25

1, experimental purposes and requirements

1, grasp the logic value of c language representation (0 for "false" and 1 for "true")
2, learn the correct use of the relationship and logical expressions
3, to master all forms of if statement syntax and usage. matching relationship if and else if statements, as well as nested if statements
nested 4, master switch statement syntax and usage, pay attention to the switch statement and break statement Usage switch statement

Second, the experimental content

1, lab exercises: Experimental 4.3.1 if statement Application

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>
main()
{
    int l,w,h;
    printf("请输出箱子的长、宽、高;\n");
    scanf("%d%d%d",&l,&w,&h);
    if((l==w)&&(w==h))
      printf("该箱子为正方形。\n");
    else
      printf("该箱子为长方形。\n");
    return 0;
}

Analysis 3: l When the initial writing code = w = h, can not be found after the switch (lw)&&(wh) logical expression

2, exercises: Experimental 4.3.2 switch ..... case statement 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,z,a;   //x为打印纸的数量,y为墨盒的数量,z为光盘的数量
    float sum;
    sum=0.0;
    printf("请输入打印纸、墨盒、光盘的数量:\n");
    scanf("%d%d%d",&x,&y,&z);
    sum=18*x+132*y+4.5*z;
    a=sum/100;
    switch(a)
    {
	    case 1:
		    sum=0.95*sum;break;
	    case 2:
		    sum=0.96*sum;break;
	    case 3: 
	            sum=0.93*sum;break;
	    case 4:
	        sum=0.92*sum;break;
	    default:
	        sum=0.9*sum;break;
    }
    printf("应付款=%.2f\n",sum);
    return 0;
}

3 Analysis: How to determine the beginning for different discounts and some do not know how to do.

3, lab exercises: Experimental 4.3.3 switch applications .... case statement nested if statements

1, 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>
int main()
{
    int year,month,days;
    printf("plaese 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:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12: days=31;break;
        case 4:
        case 6:
        case 9:
        case 11: days=30;break;
    }
    printf("%d年%d月有%d天",year,month,days);
    return 0; 
}

3. Analysis: None

4, exercises: Experimental 4.3.4 switch .... case structure nested applications

1, 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: In the process of writing the code of the handover problem attention half-width English

5, lab exercises: 4.3.5 Analysis of experimental procedures

1, run the following program, the output of the analysis results, and reports written test
procedures:

#include<stdio.h>
int main()
{
    double x=1000/3.0;
    double y=x-333.0;
    double z=3*y-1.0;
    printf("x=%1f\n",x);
    printf("y=%1f\n",y);
    printf("z=%1f\n",z);
    if(z==0) printf("z==0.\n");
         else printf("z不等于0.\n");
    return 0;
 } 

operation result:

2, run the following program, analyze the causes of the error and modify the program so that it outputs the correct result, and then write lab reports
were revised procedures:

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

Analysis: Problems if conditional statement, c language does not recognize 5 <num such statements <10, but should be 5> = num && num> = 100)

Third, the project training: Achieving calculator

1, design ideas: to achieve a simple four arithmetic operations of addition, subtraction, while for the case where the determination divisor
2 flowchart:

3. Problems encountered during the design and improvement of methods: a method for processing the divisor is zero question, use the if .... else statement
4, the code

#include<stdio.h>
main()
{
    int a,b,d;
    char c;    //定义变量的数据为字符型
    printf("***简**易**计**算**器***:\n");
    printf("请输入如1+1,1*1,1-1,1/1的四则运算式:\n");
    scanf("%d%c%d",&a,&c,&b);   //输入四则运算式
    switch(c)
    {
     case'+':d=a+b;break;   //进行加法运算
     case'-':d=a-b;break;   //进行减法运算
     case'*':d=a*b;break;   //进行乘法运算 
     case'/':
        if(b==0)
	      printf("输入错误!:\n"); //进行除法运算
        else
	      d=a/b; 
    break;
        default:d=0;break;
    } 
    printf("%d%c%d=%d\n",a,c,b,d);
}

Fourth, the experiment Summary (analysis of personal gains and losses)

Through this five experiments to understand the switch ... case statement deepen some, while for some places need to use logical expressions with an understanding will look through textbooks encounter some do not know where. For simple calculator, the divisor is zero
case is still not resolved feel the need to continue to learn after school, as well as how to achieve the cycle they still need more hands to learn after school.

Guess you like

Origin www.cnblogs.com/ljc-0819/p/12573694.html