struct complex float real float image struct value
- Slides: 76
【例】若有以下定义,则正确的赋值语句为 struct complex {float real; float image; }; struct value { int no; struct complex com; }val 1; A) com. real=1; C) val 1. com. real=1; 答案:C)val 1. com. real=1(参见下图) B) val 1. complex. real=1; D) val 1. real=1; com no real Val 1 。 image
– 对外部结构体变量进行初始化 例 struct date 说明: { int year, month, day } ; 全体赋值:一一对应。 struct student 部分赋值: 未赋值的 { long int num; 成员,对于数值型和字 char name[20]; 符型系统自动赋 0。 char sex; struct date birthday; } stu 1={89031, “Li Lin”, ‘M’, 1980, 8, 18} main( ) { printf(“No: %ldn. Name: %sn. Sex: %cn Birthday: %d%d%dn”, stu 1. num, stu 1. name, stu 1, sex, stu 1. birthday. year, stu 1. birthday. month, stu 1. birthday. day); }
2、同时进行结构体类型和结构体数组的定义 struct student { long int num; char name[20]; char sex; int age; float score; char addr[30]; } stu[3]; 3、直接定义结构体数组 struct { … … } st[3];
例11. 2 (p 266) struct person /*定义外部存储结构数组*/ { char name[20]; int count; } leader[3]={"Li", 0, "Zhang", 0, "Fun", 0}; main() { int i, j; char leader_name[20]; for (i=1; i<=3; i++) 输入候选人名 { scanf("%s", leader_name); for (j=0; j<3; j++) 比较 if (strcmp(leader_name, leader[j]. name)==0) leader[j]. count++; 累加 } …… }
例10. 3 (二) 赋初值 stu_1. num=89101; strcpy(stu_1. name, “Li Lin”); stu_1. sex='M'; stu_1. score=89. 5; p stu_1 p 89101 p Li Lin printf(“No: %ldn. Name: %sn p Sex: %cn. Score: %7. 2 fnn”, stu_1. num, stu_1. name, stu_1. sex, stu_1. score); printf(“No: %ldnname: %snsex: %cn score: %7. 2 fnn”, p->num, p->name, p->sex, p->score); } M 89. 5
例 11. 4 (p 269)(一) struct student /*定义结构体*/ { int num; char name[20]; char sex; int age; }; struct student stu[3]={{10101, “Li Lin”, ‘M’, 18}, {10102, “Zhang Fun”, ‘M’, 19}, {10103, “Wang Min”, ‘F’, 20}}; 结构体数组
例 11. 4 (p 269)(二) main() { struct student *p; printf (“ No Name Sex Agen”); for (p=stu; p<stu+3; p++) printf(“%5 d %-10 s %2 c %4 dn”, p->num, p->name, p->sex, p->age); } p p p 10101 Li Lin M 18 10102 Zhang Fun M 19 10103 Wang Min F 20
【例】求程序运行� 果。 P=x a=10 b a=30 x=[0] “abcd” main() { struct wc b “ABCD” ++p x=[1] {int a; char *b; }*p; static struct wc x[2]={10, "abcd", 30, "ABCD"}; p=x; printf("%d, ", p->a); printf("%cn", *(++p)->b); } 答案: 10,A
例 11. 5 以指向结构体变量的指针作实参 #include “string. h” #define format “%dn%sn%fn%fn” 宏定义 struct student { int num; 结构体类型 char name[20]; (全局变量) float score[3]; }; main() { void print(); 数组名为数 结构体变量 struct student stu; 组的首地址 scanf(“%d%s%f%f%f”, &stu. num, stu. name, &stu. score[0], &stu. score[1], &stu. score[2]); print(&stu); 结构体的首地址为实参 }
例 115 void print(struct student * p ) 指向结构体的指针 { printf ( format, p->num, p->name, p->score[0], p->score[1], p->score[2]); printf(“n”); }
“值传递” 结构体变量的成员为函数参数。 #include "string. h" #define format "%dn%sn%fn%fn" struct student { …… }; main() { void print(); struct student stu; …… print(stu); } void print(struct student stt) { …… 实参与形参为同一类型的结构体 }
举例 11. 6 main() { struct student { int num; char name[20]; int score; }; 结构体数组 struct student stu[4]; struct student *p; int i, temp; int max; 结构体指针
举例 11. 6 for (i=0; i<4; i++) scanf("%d%s%d", &stu[i]. num, stu[i]. name, &stu[i]. score); &num for (max=stu[0]. score, i=1; i<4; i++) name if (stu[i]. score>max) &score { max=stu[i]. score; &num nmae temp=i; &score } &num p=stu+temp; name …… } &score &num name &score
定义结构体类型 struct 结构体名 { 数据类型 成员名 1; 数据类型 成员名 2; : 数据类型 成员名 n; }; num 123 score 89 next struct student { int num; float score; sturct student *next; }
用指针处理链表 q 链表——动态地进行存储分配的数据结构。 struct node { int num; struct node *next }; h 1788 n 1 n 2 n 3 n 4 1232 1452 1049 0
用指针处理链表 q 单链表 h #define LEN sizeof(struct node) struct node //自定类型 20 30 { int num; NULL p struct node *next q }; struct node *h, *p*q; //变量声明 …… h 头指针 p=(struct node *)malloc(LEN); 指向第一个元素 q=(struct node *)malloc(LEN); p->num=20; q->num=30; q 尾指针 h=p; p->next=q; 地址部分存放NULL, q->next=NULL; 不再指向其它元素 ……
建立链表 新结点于链表尾部 struct node *creat( )//建链表 { int n; struct node *h, *q, *p; p=(struct node *)malloc(LEN); p p p h p scanf("%d", &n); h p->num=n; n 1 n 2 n 3 n 4 n 1 n 2 n 3 h=p; q=p; 00 scanf("%d", &n); q q while (n!=99) { p=(struct node *)malloc(LEN); q->next=0; p->num=n; return h; q->next=p; } q=p; scanf("%d", &n); 先进先出——队列建表 }
输出链表 遍历 void print(struct node *h) { struct node *p; p=h; while (p!=0) { printf("%dn", p->num); p=p->next; } } h p p n 1 p n 2 n 3 p n 4 0
查找 结点 有条件的遍历 struct node *search(struct node *h) { struct node *p; p=h; while (p->num!=3) p=p->next; return (p); } h p p n 1 3 n 4 0
删除结点 (p之后的结点) h void delnode(struct node *p) { struct node *m; m=p->next; p->next=m->next; free (m); } m=p->next p n 1 3 n 3 delete (m); n 4 0 p->next=m->next 删除之后: h n 1 3 n 4 0
插 入结点 void insert(nodetype *p) { nodetype *g; g= (struct node *)malloc(LEN); g->a=80; g->next=p->next; p->next=g; } g h h n 1 p 3 3 80 80 n 4 0 0
完整程序 #define LEN sizeof(struct node) #include<stdio. h> struct node //数据结构 { int num; struct node *next; }; struct node *creat(); //函数声明 void print(struct node *); struct node *search(struct node *); void delnode(struct node *);
main() { struct node *h=0, *q; h=creat(); print(h); q=search(h); delnode(q); print(h); }
以下函数merge() 的输入参数h指向要合并链表的首部 void merge(struct *h) { struct node *p 1, *p 2 l if ( 1 ) return h; p 1=h; p 2=h->next; while (p 2!=NULL) { p 1 ->data+=p 2 ->data; p 1 ->next=p 2 ->next; delete(p 2); p 1=( 2 ); if ( 3 ) p 2=( 4 else p 2=NULL; } } )
p 1 ->data+=p 2 ->data p 2 h n 1+n 2 delete p 2 n 4 n 3 n 2 p 1 ->next=p 2 ->next p 1=p 1 ->next h n 1+n 2 n 1 p 1 n 3 p 2=p 1 ->next n 4
答案 1 2 3 4 h=NULL p 1 ->next p 1!=NULL p 1 ->next
共用体声明的表示方法 Ø 例 union data { int i, char ch; float f; }a, b, c; union data { int i, char ch; float f; }; union data a, b, c; 引用形式 共同体变量名. 成员名 例 a. i a. ch a. f union { int i, char ch; float f; } a, b, c;
例 11. 12(p 289) struct { char name[20]; 要建立如下的表格 name num sex job class position Li 1011 F S Wang 2085 M T 501 prof int num; char sex; char job; union { int class; char position[10]; 数据结构 }category; 结构体数组 结构体类型定义中包括共同体 }person[2]; 程序设计框图(p 245 图 10. 27)
main() { int i, j; /*输入数据*/ for (i=0; i<2; i++) { scanf("%s%d%c%c", person[i]. name, &person[i]. num, &person[i]. sex, &person[i]. job); if (person[i]. job=='s') scanf("%d", &person[i]. category. class); else if (person[i]. job=='t') scanf("%s", &person[i]. category. position); else printf("Input error"); }
for (i=0; i<2; i++) { if (person[i]. job=='s') printf("%-6 d%-10 s%-6 c%/*输出数据*/ 8 dn", person[i]. num, person[i]. name, person[i]. sex, person[i]. job, person[i]. category. class); else if (person[i]. job=='t') printf("%-6 d%-10 s%-6 c%8 sn", person[i]. num, person[i]. name, person[i]. sex, person[i]. job, person[i]. category. position); else printf("n"); }
例 :利用枚举类型输出春、夏、秋、冬。 定义一外部枚举类型 enum season { spring, summer, autumn, winter, end}; enum season select( ) { int s; printf(“n. Pleaser input a number(0 -4): ”); scanf(“%d”, &s); return ((enum season) s); 类型的强制转换 }
例 应用举例 main() { enum season times; 枚举变量 while(1) { times=select(); 调用枚举函数 if (times==end) break; switch (times) { case spring: printf(“n. It is spring. ”); break; case summer: printf(“n. It is summer. ”); break; case autumn: printf(“n. It is autumn. ”); break; case winter: printf(“n. It is winter. ”); break; } } }
例 类型说明 typedef int INTEGER; 变量说明 INTEGER i, j; int i, j; 类型说明 typedef struct { int yeaa; int month; int day; } DATE; 变量说明 DATE dirthday; DATE *p;
3. 在下列程序段中枚举变量c 1和 c 2的值分别是 (1) 和 (2) 。 Main() { enum color {red, yellow, blue=4, green, white}c 1, c 2; c 1=yellow; c 2=white; printf(“%d, %dn”, c 1, c 2); }
4. 以下程序的执行结果是 。 typedeg struct { long x[2]; int y[4]; char z[8]; } MYTYPE; MYTYPE a ; main() { printf(“%dn”, sizeof(a)); }
5. 以下程序的执行结果是 main() { union { char *name; int age; int income; }s; s. name=“Wang Ling”; s. age=28; s. income=1000; printf(“%dn”, s. age); } 。
6. 以下程序用于在结构体数组中查找分数最高和最地的同学姓 名和成绩。请在程序的空白出填写一条语句或一个表达式。 #include “stdio. h” main() { int max, min, i , j; static struct { char name[8]; int score; } stud[4]={“Li”, 92, ”Wang”, 72, ”Zhong”, 83, ”Sun”, 60}; max=min=0; for (i=1; i< = (1) ; i++) if (stud[i]. score > stud[max]. score) (2) ; else if (stud[i]. score <stud[min]. score) (3) ; printf (“ 最高分,%s, %dn”, (4) ); printf (“最低分, %s, %dn: , (5) ); }
答案 1. D 2. B 3. (1) 1 (2) 6 4. 24 5. 1000 6. (1) 3 (2) max=i; (3) min=i; (4) stud[max]. name, stud[max]. score (5) stud[min]. name, stud[min]. score
- Eft revision task
- Negative float in project management
- Est eft lst lft
- Struct node int i float j
- Struct node int i float j
- Typedef struct node
- Typedef struct tree int info struct *left
- Typedef struct tree int info
- Struct node int data struct node* next
- Copying structs in c
- Complex float python
- N/ python
- Real vs virtual
- Real image vs virtual image
- Contoh value creation
- Typedef struct node
- Bruno dislikes sitting on the beach
- Droplet infection
- Quiz on compound sentences
- The electra complex
- Psychodynamic vs psychoanalytic
- Rorschach test answers
- Complex machine
- Magnitude of complex number
- Image transform in digital image processing
- Linear position invariant degradation
- Image compression model in digital image processing
- Key stage in digital image processing
- Analog image and digital image
- Fidelity criteria in digital image processing
- Image sharpening in digital image processing
- Static image vs dynamic
- Image geometry in digital image processing
- Area of convergence
- Isopreference curve
- Appuyez sur l’image qui est dans le bon sens
- Physical image vs logical image
- Dot resultado
- Cameraman.tif
- Walsh transform in digital image processing
- Image geometry in digital image processing
- Noise
- Xuite blog
- Unsupervised image to image translation
- Singular value decomposition image compression
- Real images vs virtual images
- Similarities of real self and ideal self
- Diverging lens ray tracing
- Phy 1214
- In the previous lesson i learned that accounting
- Volcano plot interpretation
- D value and z value
- Saponification number
- Present value vs future value
- Relative value vs absolute value
- What is the value of what is the value of
- Expected value
- Anthropocentrism examples
- Value creation vs value capture
- Lesson 4-1 absolute value equations
- Income approach appraisal
- Rent real estate principles a value approach
- Rent real estate principles a value approach
- Grafica vertical
- The real real fashion copywriter
- Sensor and (tiempo real or real time)
- Real life polynomials
- Ch 9 ream people real choice case study
- The real thing short story
- Marketing real people real choices
- Southpoint village apartments fishers indiana
- Slidetodoc.com
- Marketing real people real decisions
- Marketing real people real decisions
- Zurn z1090
- Rokko float
- Present tense verbs with future meaning