结构体引出的意义
设计程序时,最重要的一个步骤就是选择一个表示数据的好方法。在多数情况下,使用简单的变量甚至数组是不够的。
例如,假设需要开发一个图书管理软件。需要显示出每本书的各种信息:书名、作者、出版商、版权日期、页数、册数及价格。其中的一些信息(如书名)可以用字符数组存储,另外一些信息可以用一个int数组或float数组存储。 如果使用7个不同的数组来保存所有的信息是不合适的,尤其是需要按书名排序、按作者排序、按价格排序时。一个好的解决方法是使用一个数组,该数组的每个成员(元素)包含了一本书的所有信息。 因此,需要一种数据形式,既可以包括字符串又可以包括数字。C语言的结构体满足了这种需求。
创建结构体变量的语句如下: struct book library; 看到这条指令(语句),编译器会创建一个变量library。编译器使用book模板为该变量分配空间:一个具有MAXTITL个元素的char数组,一个具有MAXAUTL个元素的char数组和一个float变量。这些存储空间以名字library结合在一起。 struct book所起的作用就像int或float在较简单的声明中的作用一样。 struct book doyle, panshin, * ptbook;
结构体成员的访问
结构体就像一个超级数组(superarray),在这个超级数组内,一个元素可以是char类型,下一个元素可以是float类型,再下一个元素可以是int数组。使用下标可以访问一个数组的各个元素,使用结构成员运算符(.)可以访问结构中的各个成员。 例如,library.value就是指library的value部分。可以像使用任何其他float变量那样使用library.value。同样,可以像使用一个char数组那样使用library.title。在本质上,.title、.author和.value在book结构体中扮演了下标的角色。 gets(library.title); scanf("%f", &library.value); // 结构成员运算符(.)的优先级高于&
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXNAMELENGTH 20 #define STNUMBER 5 // 最大学生数目 typedef enum { WENKE, // 文科生 Liberal art LIKE // 理科生 Science }studentType; typedef struct wenkeScore { int histroyScore; // 历史成绩 int politicScore; // 政治成绩 }wenkeScore; typedef struct likeScore { int physicsScore; // 物理成绩 int chemicalScore; // 化学成绩 }likeScore; typedef union score { wenkeScore wScore; likeScore lScore; }score; // 学生成绩 typedef struct studentScore { studentType stType; // 学生类型:理科/文科 score stRealScore; }studentScore; // 学生信息 typedef struct studentInfo { int studentID; // 学号 char studentName[MAXNAMELENGTH]; // 姓名 // 理科生:物理、化学;文科生:历史、政治 studentScore stScore; }studentInfo; void getEveryStudentInfo(studentInfo *pstInfo) { int stType; if (NULL == pstInfo) { printf("getStudentInfo:Invalid Argument\n"); return; } printf("Please input ID:"); scanf("%d",&pstInfo->studentID); // ->的优先级高于& getchar(); printf("Please input Name:"); fgets(pstInfo->studentName,MAXNAMELENGTH,stdin); pstInfo->studentName[strlen(pstInfo->studentName)-1] = '\0'; printf("Is this student WENKE or LIKE?<0-WENKE,1-LIKE>:"); scanf("%d",&stType); if (0 == stType) { pstInfo->stScore.stType = WENKE; } else { pstInfo->stScore.stType = LIKE; } if (WENKE == pstInfo->stScore.stType) { printf("Please input histroy score:"); scanf("%d",&pstInfo->stScore.stRealScore.wScore.histroyScore); printf("Please input politic score:"); scanf("%d",&pstInfo->stScore.stRealScore.wScore.politicScore); } else { printf("Please input physics score:"); scanf("%d",&pstInfo->stScore.stRealScore.lScore.physicsScore); printf("Please input chemical score:"); scanf("%d",&pstInfo->stScore.stRealScore.lScore.chemicalScore); } return; } int getStudentInfo(studentInfo *pstInfo) { int stType; int stNum; int i; if (NULL == pstInfo) { printf("getStudentInfo:Invalid Argument\n"); return; } printf("How many students?\n"); scanf("%d",&stNum); for (i = 0; i < stNum; i++) { getEveryStudentInfo(pstInfo+i); } return stNum; } void displayEveryStudentInfo(const studentInfo *pstInfo) { if (NULL == pstInfo) { printf("getStudentInfo:Invalid Argument\n"); return; } printf("Student ID:%d\n",pstInfo->studentID); printf("Student Name:%s\n",pstInfo->studentName); if (WENKE == pstInfo->stScore.stType) { printf("Histroy score:%d ",pstInfo->stScore.stRealScore.wScore.histroyScore); printf("Politic score:%d\n",pstInfo->stScore.stRealScore.wScore.politicScore); } else { printf("Physics score:%d ",pstInfo->stScore.stRealScore.lScore.physicsScore); printf("Chemical score:%d\n",pstInfo->stScore.stRealScore.lScore.chemicalScore); } return; } void displayStudentInfo(const studentInfo *pstInfo,int num) { int i; if (NULL == pstInfo || num <= 0) { printf("getStudentInfo:Invalid Argument\n"); return; } printf("*******************************\n"); printf("Current Student Infomation\n"); printf("*******************************\n"); for (i = 0; i < num; i++) { displayEveryStudentInfo(pstInfo+i); } return; } void wFindhigh(studentInfo*pstInfo) { int high=pstInfo->stScore.stRealScore.wScore.histroyScore; if (high<pstInfo->stScore.stRealScore.wScore.politicScore) { high=pstInfo->stScore.stRealScore.wScore.politicScore; printf("the high score is politic= %d\n",high); } else printf("the high score is histroy= %d\n",high); } int main() { int studentNum; studentInfo st[STNUMBER]; studentNum = getStudentInfo(st); // 获取学生信息 displayStudentInfo(st,studentNum); wFindhigh(st); return 0; }
