C language lab report (c)

c program test report

Name: Lin Shilong experiment Location: home test time 2020.3.25
pilot projects:
   1, if the statement of application 2, switch / case sentence applied
   3, switch / case statement nested if statements use
   4 nested switch / case structure application
   5, 6 analysis program, a calculator implementation

1, experimental purposes and requirements

1. master C language representation of the logical 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 in the switch statement usage and

Second, the experimental content

1. exercises: 4.3.1

Problem Description: three inputs respectively represent sub-length, width and height of an integer value, and determines the output of the box is a cube or a rectangular parallelepiped.

Experiment Code

#include <stdio.h> 
main () 
{ 
 int L, W, H; 
 the printf ( " Please enter the box length, width, height: \ n- " ); 
 Scanf ( " % D% D% D " , & L, W & , & H); 
  IF (L == == L W && H) 
     the printf ( " the box is cube \ n-. " ); 
  the else 
     the printf ( " of the box is a rectangular parallelepiped \ n-. " ); 
}

Analysis: to distinguish between the equal sign "==" and the assignment "=."

2. exercises: 4.3.2

Problem Description:

Write a program to achieve the following functions, stores print paper (18 yuan / present), the ink cartridge (132 / month), an optical disk (4.5 yuan / tablet) sale, discount stores carry bargain. Specifically, the following: total customers to buy commodities reached $ 100, 5% discount; more than 200 yuan, discount of 6%; reached more than $ 300 discount 7%; reached more than $ 400, discount of 8%; reached 500 more than yuan, 10% discount, depending on the amount of purchase, print payables.

Experiment code:

#include <stdio.h>
 int main () 
{ 
    a float A, B, C, x; / * A, B, C are the number of printing paper, ink cartridges, optical disk, x is the customer should pay * /  
    int SUM; / * sUM is the customer who bought the sum of merchandise * /  
    the printf ( " enter the number of printing paper, ink cartridges, optical disk: \ n- " ); 
    Scanf ( " % F,% F,% F " , & a, & B, & C); 
    sUM = 18 is * A + 132 * B + 4.5 of 5 * C;
     Switch (SUM / 100 ) 
    { 
        Case  0 : X = SUM;
         BREAK ;
         Case 1:x=0.95*sum;
        break;
        case 2:x=0.94*sum;
        break;
        case 3:x=0.93*sum;
        break;
        case 4:x=0.92*sum;
        break;
        default:x=0.90*sum;
        break;
    }
    printf("应付款数:%.2f\n",x);
    return 0;
    
    
}

Analysis: When a switch, define the sum can not be defined with a floating-point data, use the integer, character or an enumerated type, or an error occurs.

3. exercises: 4.3.3

Problem Description:

Enter a date, output the number of days in the month.

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)
                   printf("day=29");
                else
                   printf("day=28");
                break;
        case 1:printf("day=31");break;
        case 3:printf("day=31");break;
        case 5:printf("day=31");break;
        case 7:printf("day=31");break;
        case 8:printf("day=31");break;
        case 10:printf("day=31");break;
        case 12:printf("day=31");break;
        case 4:printf("day=30");break;
        case 6:printf("day=30");break;
        case 9:printf("day=30");break;
        case 11:printf("day=30");break;
                break;
    return 0;
    }
}

Analysis: Note leap year terms

4. exercises: 4.3.4

Problem Description:

Preparation of a vending machine program, change procedures following function: two menus, one menu is selected commodity type; two specific menu item is selected (commodity price and varieties can self). Customers first select the type of goods, and then select the specific product, enter the quantity purchased. Vending machine and the number of goods according to the selection of the input, and displays the calculated total amount of the selected goods.

Experiment Code

#include <stdio.h> 
main () 
{ 
    int X, n-, Y;
     a float SUM = 0.0 ; 
    the printf ( " Please choose: Commodity 1. 2. 3. Food Supplies \ n- " ); 
    Scanf ( " % D " , & X);
     Switch (X) 
    { 
        Case  . 1 : the printf ( " Please choose: 1. toothbrush (3.5 yuan /) 2. toothpaste (6.2 yuan /) \ n- " ); 
                the printf ( "        3. soap (2 million / piece) 4. towel (8.6 yuan /) \ 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 : the printf ( " Please select: a pen (3 / branched) 2. Notebook (1.2 yuan /) \ n- " ); 
                the printf ( "         3. folder (12 million /) 4. Cases ( 8.6 yuan / month) \ n- " ); 
                Scanf ( " % D " , & Y); 
                the printf ( " number? " ); 
                Scanf ( " % D " , & n-);
                 Switch (Y) 
                { 
                  Case  . 1 : SUM = . 3 * n-; BREAK ;
                  case  2 : SUM = 1.2n-*; BREAK ;
                   Case  . 3 : SUM = 12 is * n-; BREAK ;
                   Case  . 4 : SUM = 8.6 * n-; BREAK ; 
                } 
                BREAK ;
         Case  . 3 : the printf ( " Please choose: 1. sugar (3.6 yuan / packets) 2. salt (1 / bag) \ n- " ); 
                the printf ( "         .. 3 cake (2 /) 4. noodles (3.6 yuan /) \ n- " ); 
                Scanf ( " % D " , & Y); 
                the printf ( " number? ");
                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("Total:% 2f element \ n- " , SUM);
     return  0 ; 
}

Analysis: Note English switching, error-prone.

5. exercises: 4.3.5

Problem Description:

Run the following program, the output of the analysis results, and written test report

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=%1f\n",x);
    printf("y=%1f\n",y);
    printf("z=%1f\n",z);
    if(z==0)
        printf("z=0.\n");
    else
        printf("z is equal to of 0. The \ n- " );
     return  0 ;    
 }

Analysis: z calculated value should be an infinite number close to zero, its value is displayed in the operation -0.000000 results. This is because the number of digits after the decimal% lf for the restrictions caused by incomplete display, in fact, the program is correct.

6. exercises: 4.3.5 (2)

Problem Description:

Run the following program, the output of the analysis results, and written test report

Experiment Code

#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);
}

Description of the problem: the different mathematical operators, can not be used directly 5 <sum <10, logical operators need.

Third, the project training: Achieving calculator

1, design ideas: a simple calculation to realize the arithmetic calculator

2, a flowchart of:

3. Problems encountered during the design and improvement of methods:

(1) improved the case where the dividend is 0;

(2) Before the remainder of the sign as a division sign, now changed back.

4, the code:

#include<stdio.h>
main()
{
    int a,b;
    char m;
    printf("请输入要计算式子:\n");
    scanf("%d %c %d",&a,&m,&b);
    if(m=='+')
    printf("%d+%d=%d\n",a,b,a+b);
    else if(m=='-')
    printf("%d-%d=%d\n",a,b,a-b);
    else if(m=='*')
    printf("%d*%d=%d\n",a,b,a*b);
    else if(m=='/')
    {
        if(b==0)
        printf("输入错误,分母不为零\n");
        else
        printf("%d/%d=%d\n",a,b,a/b);
    }
    else if(m=='%')
    printf("%d Mod %d= %d\n",a,b,a%b);
    else
    printf(" Input error " );
     return  0 ; 
     
    
}

Fourth, test summary

The experiment mainly on the structure if the structure and familiarity with the master switch, found wanting by writing code, I found myself lost the code speed is too slow, indicating that he is still not enough skilled, requires a lot of practice after school.

Guess you like

Origin www.cnblogs.com/zql1228687135/p/12593779.html