数据结构(C语言版)第一版 习题P37~P38
2015-05-29 08:24
519 查看
T1:
编写一个结构表示太阳系中的行星。
T2.修改humanbeing结构体
T3.编写结构表示下列各个形状:三角形,矩形,圆形。
编写一个结构表示太阳系中的行星。
#include<stdio.h> #include<string.h> typedef struct{ char name[10]; double distance; int satellite; }planet; void print(planet *); void main() { planet p3,p2,*pt3,*pt2; strcpy(p3.name,"Earth"); p3.distance = 1.52e8; p3.satellite = 1; pt3 = &p3; print(pt3); strcpy(p2.name,"Venus"); p2.distance = 1.07e8; p2.satellite = 0; pt2 = &p2; print(pt2); } void print(planet *p) { printf("Name: %s\n",p->name); printf("distance: %g\n",p->distance); printf("Satellite: %d\n",p->satellite); }
T2.修改humanbeing结构体
#include<stdio.h> #include<string.h> typedef struct{ int month; int day; int year; }date; typedef struct {;}nomarriage; typedef struct{date dom,dodivorce;}divorce; typedef struct{date dom,dadeath;}widow; typedef struct{ enum tagfiled{single,married,widowed,dovorced} status; union{ nomarriage nom; date dom; divorce divorcestuff; widow widowstuff; }u; } maritalStatus; typedef struct { char name[10]; int age; float salary; date dob; maritalStatus ms; }humanBeing; void printPerson(humanBeing *); int main() { humanBeing p1,*pt1; p1.dob.month = 5; p1.dob.day = 16; p1.dob.year = 1978; strcpy(p1.name, "Fiona"); p1.salary = 1.00; p1.ms.status = married; p1.ms.u.dom.month = 10; p1.ms.u.dom.day = 31; p1.ms.u.dom.year = 1999; pt1 = &p1; printPerson(pt1); } void printPerson(humanBeing *p)//使用指针能够节省空间 {/*print out the information on a person */ printf("Name: %s\n", p->name); printf("DOB: %d/%d/%d\n", p->dob.month, p->dob.day, p->dob.year); printf("Salary: %5.2f\n", p->salary);//指向结构体的指针只能用->访问成员 switch(p->ms.status){ case married: printf("Marriage Date: %d/%d/%d\n", p->ms.u.dom.month, p->ms.u.dom.day, p->ms.u.dom.year); } }
T3.编写结构表示下列各个形状:三角形,矩形,圆形。
#include <stdio.h> #include <string.h> typedef struct {double length, width;} rectStuff; typedef struct {double base, height;} triStuff; typedef struct { char name[10]; enum tagField {rectangle, triangle, circle} shapeType; union { rectStuff rect; triStuff tri; double radius; } stats; }shape; void printShape(shape); int main() { shape s1, s2, s3; strcpy(s1.name, "rectangle"); s1.shapeType = rectangle; s1.stats.rect.length = 10; s1.stats.rect.width = 20; strcpy(s2.name, "triangle"); s2.shapeType = triangle; s2.stats.tri.base = 102; s2.stats.tri.height = 450; strcpy(s3.name ,"circle"); s3.stats.radius = 2.5; /* printf("%f\n",s3.stats.radius);*/ printShape(s1); printShape(s2); printShape(s3); } void printShape(shape s) {/*print out the information on a shape */ printf("Name: %s\n", s.name); switch(s.shapeType){ case rectangle: printf("\tLength:%f\n", s.stats.rect.length); printf("\tWidth:%f\n\n", s.stats.rect.width); break; case triangle: printf("\tBase:%f\n", s.stats.tri.base); printf("\tHeight:%f\n\n", s.stats.tri.height); break; case circle: printf("Radius:%f\n",s.stats.radius); break; } }
相关文章推荐
- 大顶堆排序C实现--【严蔚敏数据结构实现】
- 数据结构之单链表(对单链表的所有操作)
- 数据结构面试题总结2——数组:求出现次数超过一半的元素
- 【数据结构】栈的应用--括号的匹配(c++)
- 数据结构面试题总结1——数组:求最大、次大值
- 数据结构之---C语言实现数组的顺序存储表示(可运行)
- 【数据结构】循环队列的实现(c++)
- 【数据结构】顺序队列的实现(c++)
- 【数据结构】顺序栈的实现(c++)
- 【数据结构】双循环链表(c++)
- 数据结构算法代码实现——线性表的顺序表示与实现(二)
- 【数据结构】双链表(c++)
- 数据结构之数组(对数组的所有操作!)
- 郝斌老师数据结构笔记
- 第一章 数据结构绪论
- c++基本数据结构的类的用法--栈,队列,链表
- nginx基本数据结构ngx_module_t,ngx_module_s
- 数据结构面试题总结
- linux下数据结构之STL的使用
- 面试经验分享之数据结构、算法题