zoukankan      html  css  js  c++  java
  • 第九章 构造数据类型实验

    C程序设计实验报告

    实验项目:

    1、结构体变量的应用

    2、结构体数组的应用

    3、共用体的应用

    4、结构体指针的应用

    姓名:曹时仙 实验地点:教学楼  实验时间2019.6.26

    一、本章要点

    1、对结构体和共用体的概念进行区分,学会定义变量和使用变量。

    2、结构类型的定义可以嵌套,结构类型与联合类型的定义也可以相互嵌套,只是要注意被嵌套的类型必须先有定义。

    3、将指针、数组与结构体或共用体相结合来实现数据的输入输出。

    4、要了解各种函数的作用,例如:abort(),strmcmp()等。

    二、实验内容

    1、结构体变量的应用

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

    实验代码

    #include<stdio.h>
     main()
    {
    	struct date
    	{
    		int years;
    		int month;
    		int day;
    	}a;
    	
    	int i, days;
    	printf("输入年,月,日:");
    	scanf("%d,%d,%d",&a.years,&a.month,&a.day); 
    		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.years%400==0) days+=29;
    			else days+=28;
    		}
    	
    	days+=a.day;
    	printf("%d年%d月%d日是该年的第%d天",a.years,a.month,a.day,days);
    	 
    }
    

     问题分析:这题主要问题在如在if语句内表达i是几月份,然后就是最后的输出结果的表达。

    运行结果:

    2、结构体数组的应用

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

    实验代码:

    #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[i]);
    		for(j=0;j<6;j++)
    		if(strcmp(a[j].name,&abc[i])==0)a[j].count+=1;
    	}
    	for(j=0;j<6;j++)
    	printf("%s:%d
    ",a[j].name,a[j].count);
    }
    

     问题分析:该题主要难点在判断if语句中的“若第j个候选人名字与输入的名字相同”,这里需要用上strcmp函数进行比较。

    运行结果:

    3、共用体的应用

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

    实验代码:

    #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("
    ");
        printf("编号		姓名		职业		班级/职务
    ");
         for(i=0;i<2;i++)
        {
            if(person[i].job=='s')
                {
                    printf("%d		",person[i].number);
                    printf("%s		",person[i].name);
                    printf("%c		",person[i].job);
                    printf("%d
    ",person[i].category.classes);
                }
            else
                {
                    printf("%d		",person[i].number);
                    printf("%s		",person[i].name);
                    printf("%c		",person[i].job);
                    printf("%s
    ",person[i].category.position);     
                }
        }
    }
    

     问题分析:这题问题较大,问题出在教科书上,name是指针,把name改成数组就好了,后来老师在课堂上解决了

    运行结果:

    4、结构体指针的应用

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

    实验代码:

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

     问题分析:主要问题是变量繁多,容易搞错,其他没什么。

    运行结果:

    三、实验小结

    1、有的时候真的不能太相信书本,书本上的也有错,如果一直按照书上敲出来的代码有错的话,不妨试试自己的代码。

    2、遇到不懂的函数需要翻书自己找到并学习

    3、函数和算法在C语言中依旧是很重要的

  • 相关阅读:
    二、Blender/Python API总览
    一、Blender/Python 快速入门
    【翻译】View Frustum Culling --3 Clip Space Approach – Extracting the Planes
    【翻译】View Frustum Culling --2 Geometric Approach – Extracting the Planes
    【翻译】 View Frustum Culling --1 View Frustum’s Shape
    列表切片
    numpy--深度学习中的线代基础
    基于正则表达式用requests下载网页中的图片
    Python基础知识整理
    C++ | boost库 类的序列化
  • 原文地址:https://www.cnblogs.com/shixian/p/11109233.html
Copyright © 2011-2022 走看看