Experiment 6: Structures, Commons and Enumerations
Experimental conclusions
Part1: Structural Types and Programming Applications
Program ex1_2.cpp, supplemented source code_
#include <stdio.h> const int N=5; // Define Structural Types struct student,And define STU For its alias typedef struct student { long no; char name[20]; int score; }STU; // Function declaration void input(STU s[], int n); int findMinlist(STU s[], STU t[], int n); void output(STU s[], int n); int main() { STU stu[N], minlist[N]; int count; printf("Input%d Student Information\n", N); input(stu, N); printf("\n Statistical Minimum Score Number and Student Information...\n"); count = findMinlist(stu, minlist, N); printf("\n Total%d The information is as follows.:\n", count); output(minlist, count); return 0; } // input n Students'information, stored in structured arrays s in void input(STU s[], int n) { int i; for(i=0; i<n; i++) scanf("%ld %s %d", &s[i].no, s[i].name, &s[i].score); } // Output structure s in n Element information void output(STU s[], int n) { int i; for(i=0; i<n; i++) printf("%ld %s %d\n", s[i].no, s[i].name, s[i].score); } // In Structural Array s Find the record of the lowest score student and store it in the structured array s in // Formal parameter n Is a structured array s Number of elements in // Function returns the number of students with the lowest score // In Structural Array s Find the record of the lowest score student and store it in the structured array s in // Formal parameter n Is a structured array s Number of elements in // Function returns the number of students with the lowest score int findMinlist(STU s[], STU t[], int n) { int i,j,min=s[0].score; j = 0; for(i=0;i<n;i++) { if(s[i].score < min) min = s[i].score; } for(i=0;j<n;j++) { if(s[j].score == min) t[i++] = s[j]; } return i; }
Input different test data and give screenshots of operation results_
Program ex1_3.cpp with supplemented source code_
#include <stdio.h> #include <string.h> const int N = 10; // Define Structural Types struct student,And define its alias STU typedef struct student { long int id; char name[20]; float objective; /*Score of objective questions*/ float subjective; /*Operational Question Score*/ float sum; char level[10]; }STU; // Function declaration void input(STU s[], int n); void output(STU s[], int n); void process(STU s[], int n); int main() { STU stu[N]; printf("Input%d Examinee Information: Admission number, name, objective score(<=40),Operational Question Score(<=60)\n", N); input(stu, N); printf("\n Processing candidates'information: Calculate the total score and determine the grade\n"); process(stu, N); printf("\n Print the complete information of candidates: Admission number, name, objective question score, operation question score, total score, grade\n"); output(stu, N); return 0; } // Enter candidates'information:Pass number, name, objective question score, operation question score void input(STU s[], int n) { int a; for(a=0;a<n;a++) scanf("%ld%s%f%f",&s[a].id,s[a].name,&s[a].objective,&s[a].subjective); } //Output candidates'complete information: Admission number, name, objective question score, operation question score, total score, grade void output(STU s[], int n) { int b; printf("Scores of objective questions on admission number Total Grading\n"); for(b=0;b<n;b++) printf(" %-10ld%-9s%-8.2f%-11.2f%-10.2f%-9s\n", s[b].id,s[b].name,s[b].objective,s[b].subjective,s[b].sum,s[b].level); } // Processing candidates'information: calculating total scores, ranking and determining grades void process(STU s[], int n) { int i,j,k,m1,m2; m1=N%10; m2=N%2; STU temp; for(i=0;i<n;i++) s[i].sum=s[i].objective+s[i].subjective; for(j=0;j<n-1;j++) for(k=0;k<n-j-1;k++) if(s[k].sum<s[k+1].sum) { temp=s[k]; s[k]=s[k+1]; s[k+1]=temp; } for(i=0;i<m1;i++) strcpy(s[i].level,"excellent"); for(i=m1;i<m2;i++) strcpy(s[i].level,"qualified"); for(i=m2;i<n;i++) strcpy(s[i].level,"Unqualified"); }
Input different test data and give screenshots of operation results_
Part2: Common Type and Programming Sample Running Courseware Source Samples
Summary: What are the differences between common and structural types?
(1) Common body is to construct data type, so that several different types of variables take up a section of memory (mutual coverage), each time only one member can use the common body to share a memory area, members of the structure have a separate memory area.
Structures are a type of structures, which are composed of several "members". Each member can be either a basic data type or a construction type.
And the data types of each member can be the same or different. The members of the structure have a separate memory area.
(2) Structures can store multiple variable types at the same time, while communities can only store and use one of multiple variable types at the same time.
(3) The biggest difference between the two is the memory occupied by the structure: the memory occupied by the structure is the sum of the component memory, and the memory occupied by the community is the memory equal to the largest component.
Part3: Enumeration Types and Programming Samples Running Courseware Source Samples
1. enum enumeration name {enumeration table}
Enumeration is a series of named integer constants. Variables of enumeration type should not be taken beyond the scope of definition. All available values (only one value in curly brackets) are listed in the enumeration table. Used to represent finite, discrete values.
Enumeration type is a basic data type (integer), not a construction type. The first corresponding integer 0 of the enumeration element and the second corresponding integer 1 of the enumeration element are in order and so on without special explanation.
2. Notes
The input and output of enumeration type need to be supported by "if..." Other","switch"statement.
(2) Enumeration type is essentially an integer, which is equivalent to an integer in use, but it can not directly assign an integer to the enumeration variable. If such an operation is carried out, the computer will report an error. So assigning a value to an enumerated variable requires a forced type conversion.
Summary and experience of experiments
In determining the critical conditions of circulation, it is always necessary to go back to the analysis from the operation to find the correct results slowly, but this is not a good thing for the encyclopedia practice and the upcoming final exam, which needs to be focused on. In the process of supplementing the code, such as the screenshot of the program ex1_3.cpp, the data input looks messy. In fact, the information of forgetting to type the name of that line results in that the information of the first line on the return bus can not be changed, but it can continue to run. This also makes me feel that it is very difficult and difficult to complete a program, but it is still a bad program to apply to practical work, because the lack of worry about input data let me find this problem that can not change the previous information, which is a wonderful sloppy, think carefully about it is in any case to take the initiative to think more after finding the problem. Next, the results will be very different. Fill in at the end of the term.