Ninth test report

Chapter IX construct experimental data type

Name: Zhang Shifeng experiment Location: 514 classroom experiment time: 6.28

 experimental project:

9.3.1 Application structure variables.

9.3.2 Application of an array of structures.

9.3.3 Application of the common body.

9.3.4 Application of a pointer mechanism

1, experimental purposes, and requirements

9.3.1. 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.

9.3.2. In the election vote, contains the names of candidates, votes, to assume a number of candidates, the statistical number of votes each candidate with the array of structures.

9.3.3. If the teacher and student data in the same table. Teachers include number, name, occupation and position data include student number, name, occupation and class. Try to write programs fill out the form.

9.3.4.n personal circle, clockwise beginning 1,2,3, ..., m the order number of packets, the number of people out of the m s from individual ring, then the ring from a repeat this process began, people enter the circle of all-out order.

 

Second, the experimental content

9.3.1 application, the structure variables

1, 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.

2, a flowchart

3, experiment code:

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

4、运行结果

 

5、问题分析

没有什么很大的问题

9.3.2、结构体数组的应用

1、问题的简单描述:

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

2、流程图

 

3、实验代码

#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、运行结果

 

5、问题分析

在if(strcmp(abc,a[j].name)==0)a[j].count++;一直出错,有一点搞不懂;

9.3.3、共用体的应用

1、问题的简单描述:

编写程序填写表格。从键盘输入学生和教师的信息,若是学生,则班级/职务栏填入班级;若是教师,则班级/职务栏填入职称。

2、流程图

 

3、实验代码

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

4、运行结果

 

5、问题分析

根据流程图一步一步来,在输入中有一些问题。

9.3.4、结构体指针的应用

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[30];
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、运行结果

 

三、实验小结

1、一些结构型变量的定义有些不能很好的完成

2、在结构体函数嵌套中,结构体名称是逐级输入

 

Guess you like

Origin www.cnblogs.com/zsflhm/p/11111235.html