定义一个名字为worker的结构类型精.docx
- 文档编号:25932531
- 上传时间:2023-06-16
- 格式:DOCX
- 页数:15
- 大小:20.38KB
定义一个名字为worker的结构类型精.docx
《定义一个名字为worker的结构类型精.docx》由会员分享,可在线阅读,更多相关《定义一个名字为worker的结构类型精.docx(15页珍藏版)》请在冰豆网上搜索。
定义一个名字为worker的结构类型精
习题10
10.1定义一个名字为worker的结构类型,用于存储职工信息,数据项包括:
num(工号)、name(姓名)、sex(性别)、title(职称)、pay(工资),再定义两个该类型的结构变量,名字为qiao和song。
解答
structworker
{
intnum;
charname[20];
charsex;
chartitle[16];
floatpay;
}qiao,song;
10.2利用第1题定义的worker类型,直接定义一个结构数组person(体积假设为5),并对其初始化,然后按行输出每一个结构元素的值。
解答:
structworker
{
intnum;
charname[8];
charsex;
chartitle[8];
floatpay;
}
main()
{
structworkerperson[5]={{001,"张三",'F',"讲师",920},
{002,"李四",'M',"副教授",1200},
{003,"王五",'F',"讲师",920},
{004,"赵六",'F',"教授",1500},
{005,"陈七",'M',"讲师",920}};
inti;
for(i=0;i<5;i++)
{printf("%-3d",person[i].num);
printf("%-8s",person[i].name);
printf("%-3c",person[i].sex);
printf("%-8s",person[i].title);
printf("%.2f",person[i].pay);
printf("\n");
}
}
10.3定义结构类型structcard,代表一张扑克牌的结构,结构中包含两个成员:
牌的花色(clubs,hearts,spades,diamonds)和面值(2,3,4,5…J,Q,K,A);再用该类型定义一个结构数组deck,代表一副扑克的52张牌。
解答:
enumcolor(clubs,hearts,spades,diamonds);
enumvalue(2,3,4,5,6,7,8,9,10,J,D,K,A);
structcard
{
enumcolorcolor1;
enumvaluevalue1;
}deck[52];
10.4以第2题为基础,编写程序,对person数组按姓名从小到大排序。
解答:
#include"string.h"
structworker
{
intnum;
charname[8];
charsex;
chartitle[8];
floatpay;
}
main()
{
structworkerperson[5]={{001,"张三",'F',"讲师",920},{002,"李四",'M',"副教授",1200},
{003,"王五",'F',"讲师",920},{004,"赵六",'F',"教授",1500},
{005,"陈七",'M',"讲师",920}};
inti,j;
structworkertemp;
for(j=0;j<4;j++)
for(i=0;i<4-j;i++)
if(strcmp(person[i].name,person[i+1].name)>0)
{
temp=person[i];
person[i]=person[i+1];
person[i+1]=temp;
}
for(i=0;i<5;i++)
{printf("%-3d",person[i].num);
printf("%-8s",person[i].name);
printf("%-3c",person[i].sex);
printf("%-8s",person[i].title);
printf("%.2f",person[i].pay);
printf("\n");
}
}
10.5试写出下列类型定义和宏定义的区别。
typedefintINTEGER;
#defineINTEGERint
解答:
typedefintINTEGER;指的是定义一种数据类型,名字为INTEGER,与系统中的整型数据相同。
而#defineINTEGERint是宏定义,它只是字符串的代换。
10.6预测下面程序的运行结果,并上机验证之。
(上机试)
#include
structs_tag
{charsc[2];
intsi;
floatsf;
}sx={"A",12,34.56};
unionu_tag
{charuc[2];
intui;
floatuf;
}ux={"A"};
main()
{inti,j;
i=sizeof(structs_tag);
j=sizeof(unionu_tag);
printf("structsize:
%d,unionsize:
%d",i,j);
printf("ux=%s\n",ux.uc);
ux.uf=56.78;
printf("ux=%f\n",ux.uf);
}
解答:
运行结果是:
structsize:
8,unionsize:
4
ux=A
ux=56.779999
10.7定义一个存储日期信息的结构类型structdate,再编写函数findday(structdate*p),将p指向的日期值转换成这一年中的天数。
例如,main函数若以2000年5月3日作为实参数去调用findday函数,将返回126。
解答:
structdate
{
intyear;
intmonth;
intday;
}
findday(structdate*p)
{
inti,leap,c=0;
intx[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
leap=(p->year%4==0&&p->year%100!
=0)||(p->year%400==0);
for(i=1;i
c=c+x[leap][i];
c=c+p->day;
printf("%d\n",c);
}
main()
{
structdate*p,date1;
printf("Inputdate\n");
printf("year=");
scanf("%d",&date1.year);
printf("month=");
scanf("%d",&date1.month);
printf("day=");
scanf("%d",&date1.day);
p=&date1;
findday(p);
}
10.8已知某结构数组存储了若干种药品信息,结构包含:
id(药品代号)、name(名称)、price(价格)和validay(有效期),试编写程序,求所有药品的平均价,并对有效期在某规定日期(自行假设)之前的药品价格一律下调30%。
解答:
#defineN5
structdate
{
intyear;
intmonth;
intday;
};
structmedicine
{
intid;
charname[20];
intprice;
structdatevaliday;
};
main()
{
inti;
floatave=0;
structmedicinemedicine1[N];
structdatedate1;
printf("Inputinformations:
\n");
for(i=0;i { printf("id="); scanf("%d",&medicine1[i].id); printf("name="); scanf("%s",medicine1[i].name); printf("price="); scanf("%d",&medicine1[i].price); ave=ave+medicine1[i].price; printf("validay\n"); printf("year="); scanf("%d",&medicine1[i].validay.year); printf("month="); scanf("%d",&medicine1[i].validay.month); printf("day="); scanf("%d",&medicine1[i].validay.day); } for(i=0;i {printf("%-3d",medicine1[i].id); printf("%-10s",medicine1[i].name); printf("%-5d",medicine1[i].price); printf("%d."medicine1[i].validay.year); printf("%d.",medicine1[i].validay.month); printf("%d.",medicine1[i].validay.day); printf("\n"); } ave=ave/N; printf("ave=%f\n",ave); printf("输入一个截止日期"); printf("year="); scanf("%d",&date1.year); printf("month="); scanf("%d",&date1.month); printf("day="); scanf("%d",&date1.day); for(i=0;i if(strcmp(&medicine1[i].validay,&date1)<0) medsion1[i].price=0.7*medicine1[i].price; for(i=0;i {printf("%-2d",medicine1[i].id); printf("%-10s",medicine1[i].name); printf("%-5d",medicine1[i].price); printf("%d.",medicine1[i].validay.year); printf("%d.",medicine1[i].validay.month); printf("%d.",medicine1[i].validay.day); printf("\n"); } } 10.9编写递归函数integerlist(head,d)建立一个与正整数d同序的整数链表,链表建成以后,头指针由head返回。 例如,设d=3186,则所创建的链表如下图所示。 此题不会。 head 3 1 8 6 NULL 10.10为第9题创建的链表编写排序函数sort(head),head为已知链表的头指针,排序方式为升序。 此题不会。 10.11编写函数del(head1,head2),实现从链表1中删去在链表2中有相同学号的结点。 假设head1、head2分别为链表1的头指针和链表2的头指针,两个链表的结构都只包含学号和成绩两项数据。 解答: #defineL14 #defineL25 #defineNULL0 #include"string.h" structstudent { charnum[10]; charname[8]; structstudent*next; }; del(structstudent*head1,structstudent*head2) { structstudent*p1,*p2,*p; intflag=0; p1=p=head1; while(p1! =NULL) { p2=head2;flag=0; while(p2! =NULL&&strcmp(p1->num,p2->num)! =0) p2=p2->next; if(strcmp(p1->num,p2->num)==0) if(p1==head1) head1=p1->next; else {p->next=p1->next;flag=1;} if(! flag)p=p1; p1=p1->next; } } main() { structstudenta[L1]={{"34101","aaa"},{"34102","bbb"},{"34103","ccc"}, {"34104","ddd"}}; structstudentb[L2]={{"34105","eee"},{"34106","fff"},{"34103","ccc"}, {"34104","ddd"},{"34107","ggg"}}; structstudent*head1,*head2,*p,*p1,*p2; inti; head1=a; head2=b; p1=a; printf("\nlista: \n");
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 定义 一个 名字 worker 结构 类型