Chapter 12 Experimental Structures and Unions

Chapter 12 Experimental Structures and Unions

 

  • Question 9.3:

Suppose a university has the following registration form, and use the best way to define its type. Name Gender Date of Birth Occupation Status Year Month Day School Title Position

 

1. Problem solving ideas:

    Define the structure date, and define the integer variable year, month, and day. Define structure variable work, character variable university, position, title. Define the structure variable student, character variable name, gender, etc. The alias STUDENT stu1 is output similar to Wang Gang.

 

2. Source code:

#include <stdio.h>

#include <stdlib.h>

 

    typedef struct date

    {

        int year;

        int month;

        int day;

    }DATE;

    typedef struct carrer

    {

        char college [10];

        char rank[10];

        char post[10];

    }STREET;

    typedef struct student

     {

         char studentName[10];

         char studentSex[10];

         DATE birthday;

         CARRER state;

     }STUDENT;

int main ()

{

    STUDENT stu1={"王刚","M",{1991,5,19},{"Ningxia","montio","manage"}};

    STUDENT stu2;

    stu2=stu1;

    printf("stu2: %8s%3si%6d/ %02d/ %02d%10s%10s%10s",stu2.studentName,stu2.studentSex,stu2.birthday.year,stu2.birthday.month,stu2.birthday.day,stu2.state.college,stu2.state.rank,stu2.state.post);

 return 0;

}

3. Screenshot of program running effect:

 

 

 

 

 

  • Topic 2:

Please define a clock structure type, which contains three members of ``hours, minutes and seconds'', and then please change the clock simulation display program written with global variables in Chapter 7 Exercise 7.2 to use structure pointer variable parameters to rewrite the program .

 

  1. Problem-solving ideas:

Define the structure variable time, and define the integer variable hours, minutes, and seconds. The pointer pt points to the time, the d address is called with the alias TIME d, and the time is initialized to 0.

  1. Source code:

#include <stdio.h>

#include <stdlib.h>

typedef struct time

{

    int hour;

    int minute;

    int second;

}TIME;

void func(TIME *pt)

{

      pt->second++;

      if(pt->second==60)

      {

          pt->second=0;

          pt->minute++;

      }

      if(pt->minute==60)

      {

          pt->minute=0;

          pt->hour++;

      }

      if(pt->hour==24)

      {

         pt->hour=0;

      }

}

void display(TIME *pt)

{

  printf("%2d:%2d:%2d \n",pt->hour,pt->minute,pt->second);

}

void deplay()

{

    int t;

    for(t=0;t<100000000;t++);

}

int main ()

{

   int i;

   TIME d;

   d.hour=0;

   d.minute = 0;

   d.second=0;

   for(i=0;i<100000000;i++)

   {

       func(&d);

       display(&d);

       deplay();

   }

    return 0;

}

 

3. Screenshot of program running effect:

 

 

 

 

 

  • Question 3:

Please rewrite the program exercise 12.2, update the function

() Use integer and remainder operations to update the clock.

 

  1. Problem-solving ideas:

The conditional time =====24, expressed in the form of remainder and divisible,

second % 60==0 ,minute / 60==1,hour % 24 ==0。

 

2. Source code:

#include <stdio.h>

#include <stdlib.h>

typedef struct time

{

    int hour;

    int minute;

    int second;

}TIME;

void func(TIME *pt)

{

      pt->second++;

      if(pt->second % 60==0)

      {

          pt->second=0;

          pt->minute++;

      }

      if(pt->minute / 60==1)

      {

          pt->minute=0;

          pt->hour++;

      }

      if(pt->hour % 24 ==0)

      {

         pt->hour=0;

      }

}

void display(TIME *pt)

{

  printf("%2d:%2d:%2d \n",pt->hour,pt->minute,pt->second);

}

void deplay()

{

    int t;

    for(t=0;t<100000000;t++);

}

int main ()

{

   int i;

   TIME d;

   d.hour=0;

   d.minute = 0;

   d.second=0;

   for(i=0;i<100000000;i++)

   {

       func(&d);

       display(&d);

       deplay();

   }

    return 0;

}

3. Screenshot of program running effect:

 

 

 

 

 

 

 

 

 

  • MOOC web course test results:

1. Test 1:

 

Guess you like

Origin blog.csdn.net/weixin_48450741/article/details/112464922