Chapter IX construct experimental data type

C program test report

experimental project:

1, using the structure variable

2, the use of an array of structures

Using 3, a union

Using 4, the structure of the pointer

Name: Chen Fuzhou experiment Location: classroom teaching building 514 experiment Time: 2019.6.26

1, experimental purposes and requirements

(1) the type of control structure described types and structures, variables, array, and the use of a pointer defined method.
(2) learn to refer to members of the structure.
(3) using the master list of points constituting the basic algorithm structure pointer member.
(4) for enumerated types and union types of instructions which define the variables and methods of the initial value.
(5) understand the type of variable storage structure union members, and the members learn in the reference data.
(6) to learn the correct enumeration type constant references to learn how to operate the enumeration type variables.

Second, the experimental content

9.3.1 using the structure variable

Brief description of (1) the problem: a brief description of the problem: Test by using a structure type describes the year, month, date, enter a date is the first date statistics on how many days of the year.

FIG Algorithm Description:

(2) Experiment Code:

#include<stdio.h>
 main()
{
    struct date
    {
        int year;
        int month;
        int day;
    };
    struct date a;
    int i,days=0;
    printf("输入年,月,日:");
    scanf("%d%d%d",&a.year,&a.month,&a.day);
    for(i=1;i<a.month;i++)
    {
        if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
        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.day . 1 ; 
    the printf ( " % d% d% d month on the first day of the day of the% d " , a.year, a.month, a.day, Days); 
}

operation result:

(3) Analysis: None.

9.3.2 The use of an array of structures

Brief description of (1) the problem: a simple 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 statistics of votes each candidate with an array of structures number.

FIG Algorithm Description:

(2) Experiment 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);
}

运行结果:

(3)问题分析:百度了函数strcmp的作用:比较两个字符串,若str1==str2,则返回0;若str1<str2,则返回负数;若str1>str2,则返回正数;程序中,若候选人名字与输入名字相同,则票数加1.

9.3.3 共用体的运用

(1)问题的简单描述:问题的简单描述:编写程序填写表格。从键盘输入学生和教师的信息,若是学生,则班级/职务栏填入班级;若是教师,则班级/职务栏填入职称。

算法描述如图:

(2)实验代码:

#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("请输入编号、姓名、职业:");
        scanf("%d%s%s",&person[i].number,&person[i].name,&person[i].job);
        if(person[i].job=='s')
            {
                printf("请输入班级:"); 
                scanf("%d",&person[i].category.classes);
            } 
        else if(person[i].job=='t')
            {
                printf("请输入职务:"); 
                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",person[i].number);
                printf("%s\t\t",person[i].name);
                printf("%c\t\t",person[i].job);
                printf("%d\n",person[i].category.classes);
            }
        else
            {
                printf("%d\t\t",person[i].number);
                printf("%s\t\t",person[i].name);
                printf("%c\t\t",person[i].job);
                printf("%s\n",person[i].category.position);     
            }
    }
}

运行结果:

(3)问题分析:无

9.3.4 结构体指针的运用

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

算法描述如下:

(2)实验代码:

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

运行结果:

(3)问题分析:无.

三、实验小结

学了各种各样的结构体:结构体数组,结构体变量与函数,结构体指针,觉得程序有些难,特别是最后一个,我自己笔算的结果和运行结果不同,课前没有预习,两节实验课只做了前两个实验,后两个实验参考了其他同学的代码.总之,对课文内容还要强化.

 

 

实验项目:

1、结构体变量的运用

2、结构体数组的运用

3、共用体的运用

4、结构体指针的运用

姓名:陈福洲  实验地点:教学楼514教室  实验时间:2019.6.26

一、实验目的与要求

(1)掌握结构体类型说明和结构体类型变量、数组、指针的定义方法及使用。
(2)学会引用结构体中的成员。
(3)掌握利用指向结构体的指针成员构成链表的基本算法。
(4)了解联合体类型和枚举类型的说明、其变量的定义及赋初值的方法。
(5)了解联合类型变量中各成员的存储结构,学会引用各成员中的数据。
(6)学习正确引用枚举类型常量,了解如何对枚举类型变量进行操作。

二、实验内容

9.3.1 结构体变量的运用

(1)问题的简单描述:问题的简单描述:试利用结构体类型描述年、月、日,输入一个日期,统计日期是本年度第多少天。

算法描述如图:

(2)实验代码:

#include<stdio.h>
 main()
{
    struct date
    {
        int year;
        int month;
        int day;
    };
    struct date a;
    int i,days=0;
    printf("输入年,月,日:");
    scanf("%d%d%d",&a.year,&a.month,&a.day);
    for(i=1;i<a.month;i++)
    {
        if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
        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.day+1;
    printf("%d年%d月%d日是该年的第%d天",a.year,a.month,a.day,days);
}

运行结果:

(3)问题分析:无。

9.3.2 结构体数组的运用

(1)问题的简单描述:问题的简单描述:在选举中,假设有6位候选人,有10个人参加投票(只能对一位候选人投票),用结构体数组统计各候选人的得票数。

算法描述如图:

(2)实验代码:

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

运行结果:

(3)问题分析:百度了函数strcmp的作用:比较两个字符串,若str1==str2,则返回0;若str1<str2,则返回负数;若str1>str2,则返回正数;程序中,若候选人名字与输入名字相同,则票数加1.

9.3.3 共用体的运用

(1)问题的简单描述:问题的简单描述:编写程序填写表格。从键盘输入学生和教师的信息,若是学生,则班级/职务栏填入班级;若是教师,则班级/职务栏填入职称。

算法描述如图:

(2)实验代码:

#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("请输入编号、姓名、职业:");
        scanf("%d%s%s",&person[i].number,&person[i].name,&person[i].job);
        if(person[i].job=='s')
            {
                printf("请输入班级:"); 
                scanf("%d",&person[i].category.classes);
            } 
        else if(person[i].job=='t')
            {
                printf("请输入职务:"); 
                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",person[i].number);
                printf("%s\t\t",person[i].name);
                printf("%c\t\t",person[i].job);
                printf("%d\n",person[i].category.classes);
            }
        else
            {
                printf("%d\t\t",person[i].number);
                printf("%s\t\t",person[i].name);
                printf("%c\t\t",person[i].job);
                printf("%s\n",person[i].category.position);     
            }
    }
}

运行结果:

(3)问题分析:无

9.3.4 结构体指针的运用

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

算法描述如下:

(2)实验代码:

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

运行结果:

(3)问题分析:无.

三、实验小结

学了各种各样的结构体:结构体数组,结构体变量与函数,结构体指针,觉得程序有些难,特别是最后一个,我自己笔算的结果和运行结果不同,课前没有预习,两节实验课只做了前两个实验,后两个实验参考了其他同学的代码.总之,对课文内容还要强化.

 

 

实验项目:

1、结构体变量的运用

2、结构体数组的运用

3、共用体的运用

4、结构体指针的运用

姓名:陈福洲  实验地点:教学楼514教室  实验时间:2019.6.26

一、实验目的与要求

(1)掌握结构体类型说明和结构体类型变量、数组、指针的定义方法及使用。
(2)学会引用结构体中的成员。
(3)掌握利用指向结构体的指针成员构成链表的基本算法。
(4)了解联合体类型和枚举类型的说明、其变量的定义及赋初值的方法。
(5)了解联合类型变量中各成员的存储结构,学会引用各成员中的数据。
(6)学习正确引用枚举类型常量,了解如何对枚举类型变量进行操作。

二、实验内容

9.3.1 结构体变量的运用

(1)问题的简单描述:问题的简单描述:试利用结构体类型描述年、月、日,输入一个日期,统计日期是本年度第多少天。

算法描述如图:

(2)实验代码:

#include<stdio.h>
 main()
{
    struct date
    {
        int year;
        int month;
        int day;
    };
    struct date a;
    int i,days=0;
    printf("输入年,月,日:");
    scanf("%d%d%d",&a.year,&a.month,&a.day);
    for(i=1;i<a.month;i++)
    {
        if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
        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.day+1;
    printf("%d年%d月%d日是该年的第%d天",a.year,a.month,a.day,days);
}

运行结果:

(3)问题分析:无。

9.3.2 结构体数组的运用

(1)问题的简单描述:问题的简单描述:在选举中,假设有6位候选人,有10个人参加投票(只能对一位候选人投票),用结构体数组统计各候选人的得票数。

算法描述如图:

(2)实验代码:

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

运行结果:

(3)问题分析:百度了函数strcmp的作用:比较两个字符串,若str1==str2,则返回0;若str1<str2,则返回负数;若str1>str2,则返回正数;程序中,若候选人名字与输入名字相同,则票数加1.

9.3.3 共用体的运用

(1)问题的简单描述:问题的简单描述:编写程序填写表格。从键盘输入学生和教师的信息,若是学生,则班级/职务栏填入班级;若是教师,则班级/职务栏填入职称。

算法描述如图:

(2)实验代码:

#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("请输入编号、姓名、职业:");
        scanf("%d%s%s",&person[i].number,&person[i].name,&person[i].job);
        if(person[i].job=='s')
            {
                printf("请输入班级:"); 
                scanf("%d",&person[i].category.classes);
            } 
        else if(person[i].job=='t')
            {
                printf("请输入职务:"); 
                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",person[i].number);
                printf("%s\t\t",person[i].name);
                printf("%c\t\t",person[i].job);
                printf("%d\n",person[i].category.classes);
            }
        else
            {
                printf("%d\t\t",person[i].number);
                printf("%s\t\t",person[i].name);
                printf("%c\t\t",person[i].job);
                printf("%s\n",person[i].category.position);     
            }
    }
}

运行结果:

(3)问题分析:无

9.3.4 结构体指针的运用

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

算法描述如下:

(2)实验代码:

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

运行结果:

(3)问题分析:无.

三、实验小结

学了各种各样的结构体:结构体数组,结构体变量与函数,结构体指针,觉得程序有些难,特别是最后一个,我自己笔算的结果和运行结果不同,课前没有预习,两节实验课只做了前两个实验,后两个实验参考了其他同学的代码.总之,对课文内容还要强化.

 

 

Guess you like

Origin www.cnblogs.com/0219cfz/p/11106752.html