Learn C programming in 11 days

Day 8 Structure

Simple structure

 

/*********************************************************/

/*  struct1.cpp - VC6.0                                  */

/*  Simple structure                                     */

/*  J Luo, 18-Mar-2001                                   */

/*********************************************************/

 

#include<stdio.h>

#include<string.h>

 

struct Student

{

     int no;

     char name[10];

     float score;

};

 

void main()

{

     Student st1;

 

     st1.no = 101;

     strcpy(st1.name,"Ding Yi");

     st1.score = 100.0;

 

     printf("%d\t%s\t%f\n", st1.no, st1.name, st1.score);

}

 

Structure of student

 

/*********************************************************/

/*  struc2.cpp - VC6.0                                   */

/*  Structure of student                                 */

/*  J Luo, 24-May-2001                                   */

/*********************************************************/

 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<time.h>

 

#define N 10

 

struct Student

{

     int id;

     char name[10];

     int score[4];

};

 

void main()

{

     int i, j, n, sum;

 

     Student st[N];

 

     srand( (unsigned)time( NULL ) );

    

     i=0;

 

     do {

         printf("ID: ");

         scanf("%d", &st[i].id);

         if ( st[i].id <= 0 ) break;

         printf("Name: ");

         scanf("%s", &st[i].name);

//       if ( strcmp(st[i].name, "NONE") == 0 ) break;

         sum = 0;

         for (j=1; j<4; j++) {

              st[i].score[j] = 40 * rand() / RAND_MAX + 60 ;

              sum = sum + st[i].score[j];     

         }

         st[i].score[0] = sum / 3;

         i++;

         printf("%d\n", i);

     } while ( i<N );

     n=i;

 

     for (i=0; i<n; i++) {

         printf("ID: %d\t", st[i].id);

         printf("Name: %10s\t", st[i].name);

         for (j=0; j<4; j++) printf("%4d  ", st[i].score[j]);

         printf("\n");

     }

 

}

 

List creation

 

/*********************************************************/

/*  list1.cpp - VC6.0                                    */

/*  Simple list creation                                 */

/*  J Luo, 18-Mar-2001                                   */

/*  C style memory allocation malloc()                   */

/*  J Luo, 24-May-2001                                   */

/*********************************************************/

 

#include<stdio.h>

#include<stdlib.h>

 

struct List

{

     int id;

     char name[10];

     List * next;

};

 

void main()

{

     List * head;

     List * node;

 

     head = NULL;

 

     while ( 1 )

     {

         node = (struct List *) malloc(sizeof(struct List)) ;

//       node = new List;

         printf("ID: ");

         scanf("%d", &node->id);

         if ( node->id < 0 ) break;

         printf("Name: ");

         scanf("%s", &node->name);

 

         if ( head == NULL )    node->next = NULL;

         else node->next = head;

         head = node;

     }

    

     while ( head )

     {

         printf("ID: %d \n", head->id);

         printf("Name: %s \n", head->name);

         head =head->next;

     }

}

 

List creation with student records

 

/*********************************************************/

/*  list1.cpp - VC6.0                                    */

/*  Simple list creation                                 */

/*  J Luo, 18-Mar-2001                                   */

/*  C style memory allocation malloc()                   */

/*  J Luo, 24-May-2001                                   */

/*********************************************************/

 

#include<stdio.h>

#include<stdlib.h>

 

struct List

{

     int no;

     List * next;

};

 

void main()

{

     List * head;

     List * node;

 

     head = NULL;

 

     while ( 1 )

     {

         node = (struct List *) malloc(sizeof(struct List)) ;

//       node = new List;      

         printf("No = ");

         scanf("%d", &node->no);

         if ( node->no < 0 ) break;

 

         if ( head == NULL )    node->next = NULL;

         else node->next = head;

         head = node;

     }

    

     while ( head )

     {

         printf("No = %d \n", head->no);

         head =head->next;

     }

}