More Related Content Similar to 第8章结构体与共用体 (20) 第8章结构体与共用体4. 基本概念 一组信息 作为 一个逻辑整体,共同描述了一 个 完整 个体。 C 语言 引入一种能集中不同数据类型于一体的构造类型 -- 结构体类型 。结构体类型的变量可以拥有不同数据类型的成员,是不同数据类型成员的集合。 9. 8.1.2 结构体变量的声明 定义结构体类型同时定义变量 struct student { char name[20]; char sex; long num; float score[3]; } stu1,stu2; /* 定义结构体类型变量 */ 将变量直接定义在结构体类型的定义后,比较紧凑。 但 ,当程序较为复杂时,结构体数据类型通常封装为单独的文件, 此 种定义变量的方式,并不可取。 10. 8.1.2 结构体变量的声明 通过定义无名结构体数据类型定义变量 struct{ char name[20]; char sex; long num; float score[3]; } stu1,stu2; /* 定义该结构体类型变量 */ 无结构体类型 名称 , 导致仅能 直接定义变量外,不能在程序的其他位置定义构体类型变量 27. 例 8.3( 续 ) /* 指针变量指向一个 Student 类型结构体的存储空间 */ ptr = (struct Student *)malloc(sizeof(struct Student)); scanf("%s %c", ptr->name,&ptr->sex); scanf("%ld",&ptr->num); for(i = 0; i < 3; i ++) scanf("%f",&ptr->score[i]); fflush(stdin); /* 清空输入缓冲区 , 以回车作为上一行数据的输入结束符 */ printf("*********************************************"); printf("Name Sex Num Score1 Score2 Score3 "); printf("%-8s %-2c %8ld", ptr->name,ptr->sex,ptr->num); for(i = 0; i < 3; i ++) printf("%8.2f",ptr->score[i]); printf(""); free(ptr); /* 释放指针变量所指向的存储空间 */ return 0; } 30. 8.2.4 结构体数组和指针的应用 int i; /* p 指向结构体变量 */ p=&student1; /* 访问结构体变量 */ printf("%s,%c,%5.1f",student1.name,(*p).sex,p->score); /* q 指向结构体数组的元素 */ q=stu; for(i=0; i<3; i++,q++) /* 循环访问结构体数组的元素 ( 下标变量 )*/ printf("%s,%c,%5.1f",q->name,q->sex,q->score); return 0; } 运行程序结果: 输出: Zhang,F, 86.0 Wang,F, 98.0 Liu,M, 90.0 Song,M, 95.0 52. /* 删除点恰在 head 指针处,须修改 head 指针 */ if ( head == p ) head = p->next; else if ( p->next != NULL) prep->next = p->next; else /* 删除点恰在最后一结点 , 直接将前一个结点的 next 域置空 */ prep->next = NULL; if ( p == NULL ) printf(“Not exist!”); else free(p); return head; } Editor's Notes #include <stdio.h> #include <stdlib.h> #define N 5 struct Student { char name[20]; /* 学生姓名 */ long num; /* 学号 */ float score[3];/* 三科考试成绩 */ float aver; /* 成绩均分 */ }; struct Student stu[N]; /* 定义了一个班级同学的信息 */ static float scoreaver[N]; void InputData() { int i,j; for(i = 0; i < N; i++) { scanf(&quot;%s %ld %f %f %f&quot;,stu[i].name,&stu[i].num, &stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2])/3; /* 计算平均成绩 */ for(j = 0; j < 3; j++) scoreaver[j] += stu[i].score[j]; /* 计算每门课程的总成绩 */ } } void SortData() { int i,j; struct Student temp; /* 也可以写成 struc Student temp; */ for(i = 0; i < N -1; i++) for(j = i; j < N; j++) if ( stu[i].aver < stu[j].aver ){ temp = stu[i]; /* 使用结构体变量的赋值操作实现元素的交换 */ stu[i] = stu[j]; stu[j] = temp; } } void PrintData() { int i; printf(&quot;***********************************************\\n&quot;); printf(&quot; Name Num Score1 Score2 Score3\\n&quot;); for(i = 0; i < N; i++) printf(&quot;%-10s%8ld%8.2f%8.2f%8.2f\\n&quot;,stu[i].name,stu[i].num, stu[i].score[0],stu[i].score[1],stu[i].score[2]); } void PrintAverage() { int i; printf(&quot;************************************************\\n&quot;); for(i=0; i < 3; i++) printf(&quot;Score%d Average:%8.2f\\n&quot;,i+1,scoreaver[i]/N); } int main() { InputData(); SortData(); PrintData(); system(&quot;Pause&quot;); return 0; } #include <stdio.h> #include <math.h> struct complex_t{ double modular; double real, imag; }; int scan_complex(struct complex_t *c); void print_complex(struct complex_t c); struct complex_t add_complex(struct complex_t c1, struct complex_t c2); struct complex_t multiply_complex(struct complex_t c1, struct complex_t c2); double modular(struct complex_t c); int main(){ struct complex_t com1, com2; printf(&quot; 输入第一个复数的实部和虚部 ( 空格隔开 )\\n&quot;); while (!scan_complex(&com1)) printf(“ 数据读入出错!请重试 \\n”); printf(&quot; 输入第二个复数的实部和虚部 ( 空格隔开 )\\n&quot;); while (!scan_complex(&com2)) printf(“ 数据读入出错!请重试 \\n”); /* 实现两个复数的加 */ printf(&quot;\\n&quot;); print_complex(com1); printf(&quot; + &quot;); print_complex(com2); printf(&quot; = &quot;); print_complex(add_complex(com1, com2)); /* 实现两个复数的积 */ printf(&quot;\\n&quot;); print_complex(com1); printf(&quot; × &quot;); print_complex(com2); printf(&quot; = &quot;); print_complex(multiply_complex(com1, com2)); /* 取复数的模 */ printf(&quot;\\n|&quot;); print_complex(com1); printf(&quot;| = &quot;); printf(&quot;%.2f&quot;,modular(com1)); printf(&quot;\\n&quot;); return 0; } /* 读入复数 返回 1 : 输入正常 , 0 : 输入错误 , EOF :读入结束标志位 (Ctrl+Z) */ int scan_complex(struct complex_t *c){ int status; fflush(stdin); /* 清空输入缓冲区 */ status = scanf(&quot;%lf%lf&quot;, &c->real, &c->imag); if (status == 2) status = 1; else if (status != EOF) status = 0; return (status); } void print_complex(struct complex_t c){ char sign; if (c.imag < 0) sign = '-'; else sign = '+'; printf(&quot;(%.2f %c %.2fi)&quot;, c.real, sign, fabs(c.imag)); } /* c1 + c2 */ struct complex_t add_complex(struct complex_t c1, struct complex_t c2){ struct complex_t csum; csum.real = c1.real + c2.real; csum.imag = c1.imag + c2.imag; return (csum); } /* c1 * c2 */ struct complex_t multiply_complex(struct complex_t c1, struct complex_t c2){ struct complex_t temp; temp.real = c1.real * c2.real - c1.imag * c2.imag; temp.imag = c1.real * c2.imag + c1.imag * c2.real; return (temp); } /* |c| */ double modular(struct complex_t c){ return sqrt(c.real * c.real + c.imag * c.imag); } #include <stdio.h> struct Node{ char name[20]; long num; Node *next; }; struct Node * create(List *); int visit(struct Node *); int main(){ struct Node *head=NULL; struct Node temp; head = create(head); long num; if ( head == NULL ){ printf(&quot;Create List Error\\n&quot;); exit(0); } visit(head); while (1){ if ( getnewnode(&temp) == 0 ) printf(&quot;Can not generate a new Node\\n&quot;); else break; } head = insert_node(head,&temp); visit(head); printf(&quot;Input a num to delete:\\n&quot;); scanf(&quot;%ld&quot;,&num); head = delete_node(head,num); visit(head); return 0; } struct Node * create(struct Node * head){ Node *newnode; struct Node *p; if ( head != NULL ){ printf(&quot;List already exisit\\n&quot;); return head; } newnode = (Node*)malloc(sizeof(Node)); if ( getnewnode(newnode)== 0 ){ printf(&quot;Can not generate a new Node\\n&quot;); return NULL; } head = p = newnode; while ( newnode->num != -1 ){ newnode = (Node*)malloc(sizeof(Node)); getnewnode(newnode); p->next = newnode; p = newnode; } return head; } int visit(struct Node *head){ struct Node * p = head; if ( p == NULL ) return 0; while( p->next != NULL ){ printf(&quot;%10s %10ld\\n&quot;,p->name,p->num); p = p->next; } return 1; } int getnewnode(struct Node *node){ if ( scanf(&quot;%s%ld&quot;,node->name,&node->num) == 0 ) return 0; node->next = NULL; return 1; } /* 程序名称: ex8_9.c 建立日期: 2010-8-10 程序功能:链表中插入一个结点 */ List* insert_node(List* head,Node *node){ List *p = head; List *prep = p; if( head == NULL || node == NULL ) return head; while( p->next != NULL ){ if ( p->num < node->num ){ prep = p; p = p->next; } else if ( p->num == node->num ){ printf(&quot;%s already exist!\\n&quot;,node->name); return head; } else break; } if ( head == p ){ /* 如果插入点在链表的头部,需要改动 head 指针 */ node->next = p; head = node; } else{ node->next = prep->next; prep->next = node; } return head; } #include <stdlib.h> #include <stdio.h> #include <string.h> #define MAX 100 struct addr{ char name[30]; char street[40]; char city[20]; char state[10]; unsigned long int zip; } addr_list[MAX]; void init_list(); void enter(); void deleteone(); void print(); int menu_select(); int find_free(); int main(){ char choice; init_list(); for(;;){ choice = menu_select(); switch(choice){ case 1: enter(); break; case 2: deleteone(); break; case 3: print(); break; case 4: exit(0); } } printf(&quot;\\n&quot;); return 0; } void init_list() { /* 初始化地址簿 */ int t; for(t=0; t<MAX; ++t) addr_list[t].name[0] = '\\0'; } int menu_select(){ /* 选择一个菜单项 */ char s[80]; int c; printf(&quot;1. Enter a name\\n&quot;); printf(&quot;2. Delete a name\\n&quot;); printf(&quot;3. print the file\\n&quot;); printf(&quot;4. Quit\\n&quot;); do { printf(&quot;\\nEnter your choice: &quot;); gets(s); c = atoi(s); /* 系统函数,将数字串转成数值 */ } while(c<0 || c>4); return c; } void enter(){ /* 在空位处输入联系人详细信息 */ int position; char s[80]; position = find_free(); if(position==-1) { printf(&quot;\\nList Full&quot;); return; } printf(&quot;Enter name: &quot;); gets(addr_list[position].name); printf(&quot;Enter street: &quot;); gets(addr_list[position].street); printf(&quot;Enter city: &quot;); gets(addr_list[position].city); printf(&quot;Enter state: &quot;); gets(addr_list[position].state); printf(&quot;Enter zip: &quot;); gets(s); addr_list[position].zip = strtoul(s, '\\0', 10); } int find_free() { /* 地址簿中查找空位 */ int t; for(t=0; addr_list[t].name[0] && t<MAX; ++t) ; if(t==MAX) return -1; /* 地址簿已满 */ return t; } void deleteone(){ /* 在地址簿中删除一个联系人 */ int position; char s[80]; printf(&quot;enter record #: &quot;); gets(s); position = atoi(s); if(position>=0 && position < MAX) addr_list[position].name[0] = '\\0'; } void print(){ /* 显示详细地址 */ int t; for(t=0; t<MAX; ++t) { if(addr_list[t].name[0]) { printf(&quot;%s\\n&quot;, addr_list[t].name); printf(&quot;%s\\n&quot;, addr_list[t].street); printf(&quot;%s\\n&quot;, addr_list[t].city); printf(&quot;%s\\n&quot;, addr_list[t].state); printf(&quot;%lu\\n\\n&quot;, addr_list[t].zip); } } }