结构.docx
《结构.docx》由会员分享,可在线阅读,更多相关《结构.docx(15页珍藏版)》请在冰豆网上搜索。
结构
习题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;imonth;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;iif(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");
for(i=1;p1{p=p1;
p1->next=a+i;
printf("%s,%s\n",p->num,p->name);
p1=p1->next;
}
p->next=NULL;
p2=b;
printf("\nlistb:
\n");
for(i=1;p2
{p=p2;
p2->next=b+i;
printf("%s,%s\n",p2->num,p2->name);
p2=p2->next;
}
p->next=NULL;
del(head1,head2);
p=a;
printf("\nresult:
\n");
while(p!
=NULL)
{printf("%s,%s\n",p->num,p->name);
p=p->next;
}
}
10.12读入一串字符,将其中不重复的字母以递减的顺序存入一个链表中,结点由字母、该字母出现的次数以及指向下一个结点的指针组成,程序最后对链表遍历,输出各个字母机器出现的次数。
例如,设输入的字符串为goodmorning,则程序的输出结果为:
d1
g2
i1
m1
n2
o3
r1
所建立的链表为:
head
d
1
g
2
…
r
1
NULL
解答:
#defineNULL0
#include"stdlib.h"
#include"string.h"
structlist
{
charch;
intcount;
structlist*next;
};
main()
{
structlist*u,*w,*p,*head=NULL;
char*cc;
inti=0;
gets(cc);
while(*(cc+i)!
='\0')
{
p=(structlist*)malloc(sizeof(structlist));
p->ch=*(cc+i);
p->next=NULL;
p->count=1;
u=head;
while(u!
=NULL&&*(cc+i)ch)
{
w=u;
u=u->next;
}
if(u==NULL)
head=p;
else
if(*(cc+i)==u->ch)
u->count=u->count+1;
else
{w->next=p;
p->next=u;
}
i++;
}
p=head;
while(p!
=NULL)
{printf("%c",p->ch);
printf("%8d\n",p->count);
p=p->next;
}
}