1 / 34

第十章 共用体

第十章 共用体. 实际问题中,如:在学校的教师和学生中填写以下表格:. 姓名  字符型; 年龄  整型; 职业  字符型; 单位  ? ;. union {. 班级  整型; 部门  字符型;. };. 本章结构. 10.1 共用体类型的基本操作. 一、共用体的定义. 二、共用体变量的定义. 三、共用体变量的赋值与引用. 一、共用体的定义. 格式 :. union 共用体名 { 成员 表列 };. union perdata { int class; char office[10] ;

xerxes
Télécharger la présentation

第十章 共用体

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第十章 共用体 实际问题中,如:在学校的教师和学生中填写以下表格: 姓名字符型; 年龄整型; 职业字符型; 单位?; union { 班级整型; 部门字符型; };

  2. 本章结构 10.1 共用体类型的基本操作 一、共用体的定义 二、共用体变量的定义 三、共用体变量的赋值与引用

  3. 一、共用体的定义 格式: union 共用体名 { 成员 表列 }; union perdata {int class; char office[10]; };

  4. 二、共用体变量的定义 共用体变量的定义和结构体变量相同。 也有三种形式。即先定义,再说明;定义同时说明和直接说明。以perdata类型为例:union perdata{int class;char officae[10];};union perdata a,b; /*说明a,b为perdata类型*/或者可同时说明为:union perdata{ int class;char office[10]; }a,b; 或直接说明为: union{ int class;char office[10]; }a,b “共用体”中,各成员共享一段内存空间, 一个共用体变量的长度等于各成员中最长的长度。

  5. 三、共用体变量的赋值与引用 1、共用体变量的赋值 只能在程序中进行。一个联合变量, 每次只能赋予一个成员值即,一个联合变量的值就是联合变员的某一个成员值。 2、共用体变量的引用 格式同结构体

  6. 共用体应用举例: 1、设有一个教师与学生通用的表格: 教师数据有姓名,年龄,职业,教研室四项。 学生有姓名,年龄,职业,班级四项。 要求: 编程输入人员数据, 再以表格输出。

  7. 小结1. 都可用三种方式作变量说明。 2. 一个结构体变量的总长度等于所有成员长度之和。共用体变量的长度等于最长的成员的长度。

  8. 3. “.”是成员运算符,可用它表示成员项,成员还可用“->”运算符来表示。4.结构体变量可以作为函数参数,函数也可返回指向结构体的指针变量。而共用体变量不能作为函数参数,函数也不能返回指向共用体的指针变量。但可以使用指向共用体变量的指针,也可使用共用体数组。5.结构体定义允许嵌套,结构体中也可用共用体作为成员,形成结构体和共用体的嵌套。

  9. 第十一章 文件 一、fopen()、fclose() 二、fgetc()、 fputc() fgets()、 fputs() fread()、 fwrite() fscanf()、fprintf() 三、rewind()、fseek()、ftell()、feof 四、ferror()、clearerr()

  10. 例1、建立一个文本文件并将指定的内容写入文件例1、建立一个文本文件并将指定的内容写入文件 #include <string.h> #include <stdio.h> int main(void) {    FILE *fp;    char buf[11] = "0123456789";    /* create a file containing 10 bytes */    fp = fopen(“I:\\turboc2\exer\1.txt", "w");    fwrite(&buf, strlen(buf), 1, fp);    /* close the file */    fclose(fp);    return 0; }

  11. 例2、逐个字符读取已存在的一个文本文件内容,并显示例2、逐个字符读取已存在的一个文本文件内容,并显示 #include <string.h> #include <stdio.h> int main(void) {    FILE *fp;    char ch; fp = fopen(“I:\\turboc2\exer\1.txt", “r"); while(fp!=EOF)   {fgetc(ch, fp); printf(“\n%c”,ch); }    /* close the file */    fclose(fp);    return 0; }

  12. 例3、文件输入输出错误的检测 #include <string.h> #include <stdio.h> int main(void) {    FILE *fp;    char buf[11] = "0123456789";    /* create a file containing 10 bytes */    fp = fopen(“I:\\turboc2\exer\1.txt", “r");    fwrite(&buf, strlen(buf), 1, fp); /*error capture*/ if(ferror(fp))   { printf(“Something is Wrong!”); clearerr(fp);} /* close the file */    fclose(fp);    return 0; }

  13. 例4、文件的定位 #include <string.h> #include <stdio.h> int main(void) {    FILE *fp;    char buf[11] = "0123456789";    char ch=‘’; /* create a file containing 10 bytes */    fp = fopen(“I:\\turboc2\exer\1.txt", “r");    fseek(fp,4,0); printf(“%c”,ch=fgetc(fp)); /*error capture*/ if(ferror(fp))   { printf(“Something is Wrong!”); clearerr(fp);} /* close the file */    fclose(fp);    return 0; }

  14. 文件操作总结 理解文件的类型 掌握文件打开的方式的含义 知道文件打开与关闭配对出现 明确文件“读”“写”的含义 熟练文件操作函数的功能、返回值与形参

  15. 第十二章 位运算 ━━━━━━━━━━━━━━━━━━━━操作符                       作用 ──────────────────      &                       按位与 |                        按位或 ^                       按位异或 ~                        按位反 >>                         右移 <<                         左移 ━━━━━━━━━━━━━━━━━━━━

  16. 例子 main() { int left=8; int right=7; int res; res=left&right; printf("\n%d&%d=%d",left,right,res); res=left<<1; printf("\n%d<<1=%d",left,res); }

  17. 位运算总结 按位运算与逻辑运算不等同 例如:若x=7, 则 x&&8 的值为 真, x&8的值为0 只适用于字符型和整数型变量。 是C语言具有低级语言能力的体现。

  18. 第十三章 扩充内容 1枚举 2位段 3类型定义符 typedef 4 链表及动态存储分配 5 链表 6 宏定义——自学

  19. 1枚举 ——举出变量的所有可能的取值 变量取值有时被限定在一个有限的范围内。 例如: 一个星期七天 一年十二个月 一个班每周六门课

  20. 枚举的定义 格式: enum 枚举名 { 枚举值表 }; enum weekday { sun,mon,tus,wen,thr,fri,sat };

  21. 枚举变量的定义 enum weekday { ...... }; enum weekday a,b,c; 或者为: enum weekday { ...... }a,b,c; 或者为: enum { ...... }a,b,c;

  22. 例子 main(){enum weekday{ sun,mon,tue,wed,thu,fri,sat } a,b,c;a=sun;b=mon;c=tue;printf("%d,%d,%d",a,b,c);

  23. 2位段 --把一个字节中的二进位划分为几个不同的区域 main(){bit.a=1;bit.b=7;bit.c=15;printf("%d,%d,%d\n",bit.a,bit.b,bit.c);pbit=&bit;pbit->a=0;pbit->b&=3;pbit->c|=1;printf("%d,%d,%d\n",pbit->a,pbit->b,pbit->c);} structbs{unsigned a:1;unsigned b:3;unsigned c:4;} bit,*pbit;

  24. 3 类型定义符 typedef 格式: typedef 原类型名新类型名 typedef floatREAL float score; REAL score;

  25. typedefstruct { int num; char name[20]; char sex; float score; }STU ; STU class[30],boy;

  26. 4 动态存储分配 --适合变长数据的处理 1.分配内存空间函数malloc (类型说明符*) malloc (size) 2.分配内存空间函数 calloc (类型说明符*) calloc (n,size) 3.释放内存空间函数free free (void * ptr)

  27. 举例 struct stu{int num;char *name;char sex;float score;} *ps; main(){ps=(struct stu*)malloc(sizeof(struct stu));ps->num=102;ps->name="Zhang ping";ps->sex='M';ps->score=62.5;printf("Number=%d\nName=%s\n",ps->num,ps->name);printf("Sex=%c\nScore=%f\n",ps->sex,ps->score);free(ps);}

  28. 1249 5 链表 链表是一种常见的重要的数据结构。 利用它可以实现动态地进行存储分配。 链表是指将若干个数据项按一定的原则连接起来的表。 链表中每一个数据称为节点。 连接的原则是: 前一个节点指向下一个节点; head 1249 1094 1021 头指针 空地址 ----- 单向链表结构

  29. 例子 动态链表的建立、插入、删除及查找 #define NULL 0#define TYPE struct stu#define LEN sizeof(struct stu)struct stu{int num;int age;struct stu *next;};

  30. TYPE * creat(int n){struct stu *head,*pf,*pb;int i;for(i=0;i<n;i++){pb=(TYPE *)malloc(LEN);printf("input Number and Age\n");scanf("%d%d",&pb->num,&pb->age);if(i==0)pf=head=pb;else pf->next=pb;pb->next=NULL;pf=pb;}return(head);}

  31. TYPE * delete(TYPE * head,int num){TYPE *pf,*pb;if(head==NULL){ printf("\nempty list!\n");goto end;}pb=head;while (pb->num!=num && pb->next!=NULL){pf=pb;pb=pb->next;}if(pb->num==num){ if(pb==head) head=pb->next;else pf->next=pb->next;printf("The node is deleted\n"); }elsefree(pb);printf("The node not been found!\n");end:return head;}

  32. TYPE * insert(TYPE * head,TYPE * pi){TYPE *pb ,*pf;pb=head;if(head==NULL){ head=pi;pi->next=NULL; }else{while((pi->num>pb->num)&&(pb->next!=NULL)){ pf=pb;pb=pb->next; }if(pi->num<=pb->num){ if(head==pb) head=pi;else pf->next=pi;pi->next=pb; }else{ pb->next=pi;pi->next=NULL; }}return head;}

  33. void print(TYPE * head){printf("Number\t\tAge\n");while(head!=NULL){printf("%d\t\t%d\n",head->num,head->age);head=head->next;}

  34. Thanks!

More Related