Chapter IX structures and unions

C Programming lab report

: Lab exercise
applications 9.3.1, structure variables

9.3.2 Application mentioned array structure

9.3.3 Application of the common body

9.3.4, provide structural applications pointer

Name: Zou Huiying experiment Location: experimental classroom teaching building 514 Time: June 26

First, this chapter points

9.3.1 application, the structure variables

  • Definition of the structure type struct date, it contains three members of the year, month, day;
  • Define a variable of type struct date, and the initial value from the keyboard
  • Using the loop to calculate the number of days
  • Using branch statements determines a leap year

9.3.2 Application mentioned array structure

  • Candidates struct person defined structure, containing the name, two members of the votes;
  • Struct person defined array of structures, custom number, 0 is the initial votes
  • The cyclic input the names of candidates, the person plus 0 votes
  • When the candidate entered is not specified candidates, the equivalent of invalid ballots, not cumulative

9.3.3 Application of the common body

  • Definition of the structure type, other members number, name, occupation and class (or office)
  • The definition of the type of array structure can have a number of array elements
  • Loop using the input data for each record, the record if occupation is "s", the record of the student, the student input class; if the recording career of "t", then the record is the teacher, enter the duties of teachers
  • Judging professional records, which use the printf () statement output record

9.3.4, provide structural applications pointer

  • Definition of the structure type which staff members number (address)
  • The definition of the type of array structure can have array elements n
  • n personal circle, consider using a structure array elements in the "next member number" to achieve, but the last element of "next personnel number" refers to the first

Second, the experimental content

9.3.1 application, the structure variables

  • Brief description of the problem: Test by using a structure type describes the year, month, date, enter a date, that date statistics are the days of the year. Algorithm is described as shown:
  • 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)
        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;
             printf("%d年%d月%d日是该年的第%d天",a.year,a.month,a.day,days);
 } 
  • The results:
  • 问题分析:
    1.结构体类型的定义可放在函数体内或函数体外
    2.结构体变量在被用时,要用结构体名.变量名的形式
    3.当i=1,3,5,7,8,9时,函数输出结果为

    试了一月二月份都对的上,到了三月以后就会多两天,说明并没有把二月当做28,或29来算,而是直接当做31天,所以不能作为整体输出,应该分别输入。

9.3.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);
    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);
}
  • 实验结果:
  • 问题分析:
    1.scanf中引用变量,引用数组的时候不要寻址符&
    2.比较函数strcmp(str1,str2),若str1=str2,则返回零;若str1<str2,则返回负数;若str1>str2,则返回正数。

9.3.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("\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);
 } 
  • 实验结果:
  • 问题分析:
    1.调用abort()函数退出程序
    2.制作表格用\t\t来输出,否者就会出现不对齐

9.3.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);
}
  • 实验结果:
  • 问题分析:定义数组link[N],N可以定义稍微大些,但n比N小

三、实验小结

1.数组引用的时候不用寻址符,因为表示首地址
2.不能将记录作为一个 整体输出,而应分别输出
3.进行比较的时候用strcmp函数,退出程序用abort函数

Guess you like

Origin www.cnblogs.com/zzzdka/p/11109150.html