Experimental eleven structure

                                                                                                                          Sort contacts

1. Experimental requirements: the establishment of a communication record, the structure of the address book records include: name, date of birth, telephone number; birthday which also includes three: year, month, day. Programming, define a nested structure type, the input n (n <10) contacts the information, then the order of their age sequentially outputs the information.

2. Experience * pay attention to moderation. See the comparison is graded to determine the date, do not confuse the subject.

                * When using the structure, to be noted that there are different types of re-use

               * When the structure is to have a reference to the original structure plus a name (or more) the name of the new structure to facilitate call it.

               * Damascene structure can be used as additive p [i] .bd.year.

3, program

#include<stdio.h>
#include<string.h>
struct birthday{
       double year;
    int month,day;
};
struct phone{
 int num;
 char name[10];
 struct birthday bd;
}
;
int main()
{
 int i,j,n ,max;
 struct phone p[100],t;
 printf("Entern n:");
 scanf("%d",&n);
 printf("请输入通讯人姓名,电话号码,生日\n");
 for(i=0;i<n;i++)
  scanf("%s%d%lf%d%d",p[i].name,&p[i].num,&p[i].bd.year,&p[i].bd.month,&p[i].bd.day);
 for(i=0;i<n-1;i++){
  max=i;
  for(j=i+1;j<n;j++){
   if(p[j].bd.year<p[max].bd.year){
      t=p[max];
         p[max]=p[j];
      p[j]=t;
   }
   else if(p[j].bd.month<p[max].bd.month){
         t=p[max];
            p[max]=p[j];
          p[j]=t;
   }
    else if(p[j].bd.day<p[max].bd.day){
           t=p[max];
              p[max]=p[j];
           p[j]=t;  
   }
  }
 }
 printf("Birthday order is\n:");
   for(i=0;i<n;i++)
    printf("name %s \n telephone number %d\n  %1.1f%d%d\n",p[i].name,p[i].num,p[i].bd.year,p[i].bd.month,p[i].bd.day);
return 0;
}

                                                                                                               By grade student achievement statistics

1. Experimental requirements: 10 students entered school number, name and achievements, the output level of student achievement and the number of failing. Record of each student, including student number, name and grade scores, requirements definition and call functions set_grade (), set its level based on student achievement, and the statistics do not pass the number, grade settings: 85 to 100 A, 70 ~ 84 is B , is 60 to 69 C, 0 ~ 59 is D.

2, experience

            * When assigning statistical pointer, pay attention to move the pointer (p ++), and the pointer can point directly to the substructure without additional structures in the body structure name.

             * When the assignment of grades, pay attention to the type of assignment in advance, pay attention to the input and output formats,

             * In the conventional structure subtypes in vivo, the assignment can not, calculates assignment cycle.

3, the source

The first (array assignment)

#include <stdio.h>
#include <string.h>
struct Student {/ * record student information * /
 int NUM;
 char name [10];
 Double RES;
 char Rank;
};
int set_grade (struct Student TS [], int n-);
int main ()
{
 struct student TS [10];
 int n-, I, COUNT;
 the printf ( "enter number of students:"); / * student input information * /
 Scanf ( "% D", & n-) ;
 for (I = 0; I <n-; I ++) {
  the printf ( "student number");
  Scanf ( "% D", & TS [I] .num);
  the printf ( "name:");
  Scanf ( "% S ", & TS [I] .name);
  the printf (" results: ");
  Scanf ("% LF ", & TS [I] .res);
  COUNT = set_grade (TS, n-); / * fail number * /
 }
 printf ( "The number of failing is:% d \ n",count);
 for(i=0;i<n;i++){
  printf ( "Student ID:% d Name:% s level:% c \ n", ts [i] .num, ts [i] .name, ts [i] .rank); / * student information output stage grades * /
}
 
return 0;
}
int set_grade (struct student TS [], int n-)
{
 int I, COUNT = 0;
 for (I = 0; I <n-; I ++) / * Analyzing student grades * /
{
  IF ( TS [I] .res <60) {
   TS [I] .rank = 'D';
   count ++;
  } / * * fail count plus. 1 /
  the else IF (TS [I] .res <70 && TS [I] .res> 60 =) {
   TS [I] .rank = 'C';
}
  the else IF (TS [I] .res <85 && TS [I] .res> = 70) {
   TS [I] .rank = 'B';
}
  the else {
   TS [I] .rank = 'A';
}
}
return COUNT;/ * Returns the number of failing * /
}

第二种 指针赋值

#include <stdio.h>
struct student{
 int num;
    char name[10];
    int res;
    char rank;
};
int set_grade( struct student t[], int n );
int main()
{
 struct student t[10];
    int n, i, count;
 printf("请输入学生个数:");
    scanf("%d\n", &n);
 printf("请输入学生 学号 姓名成绩");
   for(i = 0; i < n; i++){
    scanf("%d%s%d", &t[i].num, t[i].name, &t[i].res);
 }
    count = set_grade(t, n);
    printf("The count for failed (<60): %d\n", count);
    printf("The grades:\n");
    for(i = 0; i < n; i++)
        printf("%d %s %c\n", t[i].num, t[i].name, t[i].rank);
return 0;
}
int set_grade( struct student t[], int n )
{
 int count = 0, i;
     for(i = 0;i<n;i++){
   if(t[i].res<60){
           t[i].rank = 'D';
           count++;
   }
         else if((t[i].res<70)&&(t[i].res>=60)){
           t[i].rank = 'C';
   }
         else if((t[i].res<85)&&(t[i].res>=70)){
           t[i].rank = 'B';
  }
         else{
           t[i].rank = 'A';
   }
}
return count;
}

 

 

Guess you like

Origin www.cnblogs.com/28183311141-/p/avasdvc.html