Eighth test report

c language lab report

Experimental data type structure (IX)

Name: Lai thousand times experiment Location: 514 Things laboratory experiments Time: June 26, 2019

1, experimental purposes, and requirements

(1) Type Description master structure variables and structures, arrays, and the use of a pointer defined method.
(2) learn reference structure members.
(3) using the master list of points constituting the basic algorithm structure pointer member.
(4) for enumerated types and union types of instructions, definitions and methods assign initial values of its variables.
(5) for storing the structure type variable union members and learn the reference data members.
(6) to learn the correct enumeration type constant references to learn how to operate the enumeration type variables.

Second, the experimental content

(1) Application of the structure variable 9.3.1 Experiment

1. Problem Description:
Test by using a structure type describes the year, month, date, enter a date, that date statistics are the number of days during the year.
2 flowchart:

3, the program code:

#include<stdio.h>
main()
{
    struct date
    {
        int year;
        int month;
        int days;
    }a;
    int i,days=0;
    printf("输入年、月、日:");
    scanf("%d,%d,%d",&a.year,&a.month,&a.days);
    for(i=1;i<a.month;i++)
    {
        if(i==1||i==3||i==5||i==7||i==8||i==10)
        days+=31;
        else if(i==4||i==6||i==9||i==11)
        days+=30;
        else if(a.year%4==0&&a.year%100!=0||a.year%400==0)
        days+=29;
        else days+=28;
    }
    days+=a.days;
    printf("%d年%d月%d日是该年的第%d天",a.year,a.month,a.days,days);
}

4, the results:

5. Analysis:

Application (2) an array of structures 9.3.2 Experiment

1. Description of the problem:
in the election, assuming that there are six candidates, there are 10 people to vote (can only vote for one candidate), the statistical number of votes each candidate with the array of structures.
2 flowchart:

3, program code;

#include"stdio.h"
#include<string.h>
struct person
{
    char name[20];
    int count;
}a[6]={"zhang",0,"li",0,"wang",0,"zhao",0,"liu",0,"zhu",0,};
main()
{
    int i,j;
    char abc[20];
    for(i=1;i<=10;i++)
    {
        printf("输入候选人名字:");
        scanf("%s",abc);
        for(j=0;j<6;j++)
        if(strcmp(abc,a[j].name)==0) 
        a[j].count++;
    }
    for(j=0;j<6;j++)
    printf("%s:%d\n",a[j].name,a[j].count);
}

4, the results:

5. Analysis:

Applications (3) of the union 9.3.3 Experiment

1. Problem Description:
Write a program fill out the form. Keyboard input information from students and teachers, if the student, the class / function column to fill in the class; if the teacher, the class / job title column to fill.
2, flow chart:

3, program code:
I:

#include<stdio.h>
#include<stdlib.h>
struct
{
    int number;
    char name[30];
    char job;
    union
    {
        int classes;
        char position[10];
    }category;
 }person[2];
main()
 {
    int i;
    for(i=0;i<2;i++)
    {
        printf("请输入姓名、编号、职业:\n");
        scanf("%s%d%s",person[i].name,person[i].number,person[i].job);
        if(person[i].job=='s')
        {
          scanf("%d",&person[i].category.classes);
        }
        else if(person[i].job=='t')
        {
             scanf("%s",&person[i].category.position);
        }
        else
        {
            printf("input error!");
            abort();
        } 
     }
     printf("\n");
     printf("编号\t\t姓名\t\t职业\t\t班级/职务\n");
      for(i=0;i<2;i++)
        if(person[i].job=='s')
        {
          printf("%d\t\t%s\t\t%c\t\t%d\t\t\n",person[i].number,person[i].name,
           person[i].job,person[i].category.classes);
        }
        else
        {
          printf("%d\t\t%s\t\t%c\t\t%s\t\t\n",person[i].number,person[i].name,
           person[i].job,person[i].category.position);
        }
 }

Students:

#include<stdio.h>
#include<stdlib.h>
struct
      {
          int number;
          char name[30];
          char job;
          union
          {
              int classes;
              char position[10];
          }category;
       }person[2];
main()
{
    int i;
    for(i=0;i<2;i++)
    {
        scanf("%s%d%s",&person[i].name,&person[i].number,&person[i].job);
        if(person[i].job=='s')
        {
            scanf("%d",&person[i].category.classes);
        }
        else if(person[i].job=='t')
        {
            scanf("%s",&person[i].category.position);
        }
        else{
            printf("input error!");
            abort();
        }
    }
    printf("\n");
    printf("编号\t\t姓名\t\t职位\t\t班级\职务\n");
    for(i=0;i<2;i++)
    {
        if(person[i].job=='s')
        printf("%d\t\t%s\t\t%c\t\t%d\n",person[i].number,person[i].name,person[i].job,person[i].category.classes);
        else
        printf("%d\t\t%s\t\t%c\t\t%s\n",person[i].number,person[i].name,person[i].job,person[i].category.position);
    }
    
        }

4, the results:

5. Analysis:

(4) Experimental 9.3.4 Application structure pointer

1、问题描述:
n个人围成一个圈,从第s个人开始按顺时钟1,2,3,...,m的顺序报数,数到m的人出圈,然后从出圈的下一个人开始重复此过程,输出所有出圈人的顺序,n,s,m从键盘输入。
2、流程图

3、程序代码:

#include<stdio.h>
#define N 10
struct child
{
    int no;
    int next;
};
struct child link[N];
main()
{
    int i,n,m,s,count,h;
    printf("输入围圈人数,出圈报数,开始报数位置:");
    scanf("%d,%d,%d",&n,&m,&s); 
    for (i=1;i<=n;i++)
    {
        if(i==n)
           link[i].next=1;
        else
           link[i].next=i+1;
        link[i].no=i;
    }
    count=0;
    if(s==1)h=n;
    else h=s-1;
    printf("出圈顺序为:");
    while(count<n-1)
    {
        i=0;
        while(i!=m)
        {
            h=link[h].next;
            if(link[h].no) 
            i++;
        }
        printf("%d,",link[h].no);
        link[h].no=0;
        count++;
     } 
     for(i=1;i<=n;i++)
     if(link[i].no!=0)
     printf("%d",link[i].no);
}

4、运行结果:

5、问题分析:

三、实验小结

Guess you like

Origin www.cnblogs.com/lai-/p/11110491.html