C语言第8章习题及答案.docx

上传人:b****2 文档编号:2933118 上传时间:2023-05-05 格式:DOCX 页数:18 大小:17.53KB
下载 相关 举报
C语言第8章习题及答案.docx_第1页
第1页 / 共18页
C语言第8章习题及答案.docx_第2页
第2页 / 共18页
C语言第8章习题及答案.docx_第3页
第3页 / 共18页
C语言第8章习题及答案.docx_第4页
第4页 / 共18页
C语言第8章习题及答案.docx_第5页
第5页 / 共18页
C语言第8章习题及答案.docx_第6页
第6页 / 共18页
C语言第8章习题及答案.docx_第7页
第7页 / 共18页
C语言第8章习题及答案.docx_第8页
第8页 / 共18页
C语言第8章习题及答案.docx_第9页
第9页 / 共18页
C语言第8章习题及答案.docx_第10页
第10页 / 共18页
C语言第8章习题及答案.docx_第11页
第11页 / 共18页
C语言第8章习题及答案.docx_第12页
第12页 / 共18页
C语言第8章习题及答案.docx_第13页
第13页 / 共18页
C语言第8章习题及答案.docx_第14页
第14页 / 共18页
C语言第8章习题及答案.docx_第15页
第15页 / 共18页
C语言第8章习题及答案.docx_第16页
第16页 / 共18页
C语言第8章习题及答案.docx_第17页
第17页 / 共18页
C语言第8章习题及答案.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C语言第8章习题及答案.docx

《C语言第8章习题及答案.docx》由会员分享,可在线阅读,更多相关《C语言第8章习题及答案.docx(18页珍藏版)》请在冰点文库上搜索。

C语言第8章习题及答案.docx

C语言第8章习题及答案

第八章

用一个数组存放图书信息,每本书是一个结构,包括下列几项信息:

书名、作者、出版年月、借出否,试写出描述这些信息的说明,并编写一个程序,读入若干本书的信息,然后打印出以上信息。

#include<>

typedefstruct

{

charName[20];

charAuthor[20];

intDate_Year;

intDate_Month;

intloaned;

}BOOK;

#defineN10

voidmain()

{

BOOKbooks[N];

inti;

for(i=0;i

{

printf("InputBook'sName:

");

gets(books[i].Name);

printf("InputBook'sAuthor:

");

gets(books[i].Author);

printf("InputBook'sYearofPublishing:

");

scanf("%d",&books[i].Date_Year);

printf("InputBook'sMonthofPublishing:

");

scanf("%d",&books[i].Date_Month);

printf("InputBook'sStatus,1-Loaned,2-Keepin:

");

scanf("%d",&books[i].loaned);

}

for(i=0;i

{

printf("Book:

%s,Author:

%s,Publishing:

%d-%d,Status:

%d\n",books[i].Name,books[i].Author,books[i].Date_Year,books[i].Date_Month,books[i].loaned);

}

 

}

编写一个函数,统计并打印所输入的正文中的各个英文单词出现的次数,并按次数的递减顺序输出。

#include<>

#include<>

typedefstruct

{

chars[20];

intcount;

}WORD;

#defineN100

WORDwords[N]={0};

intword_count=0;

intIsSeparator(charc)

{

if('A'<=c&&c<='Z'||

'a'<=c&&c<='z'||

'0'<=c&&c<='9'||

'_'==c||

'-'==c)

return0;

else

return1;

}

voidAddWord(char*s)

{

inti,found=0;

charss[20];

for(i=0;*s&&!

IsSeparator(*s);s++,i++)

ss[i]=*s;

ss[i]=0;

for(i=0;i

if(strcmp(ss,words[i].s)==0)

{

found=1;

break;

}

if(found==0)

{

words[word_count].count=1;

strcpy(words[word_count].s,ss);

word_count++;

}

else

{

words[i].count++;

}

}

 

voidfunc(char*s)

{

inti,j,t;

charc,lc;

WORDtemp;

lc=*s;

if(!

IsSeparator(lc))

AddWord(s);

for(i=1;c=*(s+i);i++)

{

if(!

IsSeparator(c)&&IsSeparator(lc))

AddWord(s+i);

lc=c;

}

for(i=0;i

{

t=i;

for(j=i+1;j

if(words[t].count

t=j;

if(i!

=t)

{

temp=words[t];

words[t]=words[i];

words[i]=temp;

}

}

 

for(i=0;i

{

printf("%s:

%d\n",words[i].s,words[i].count);

}

}

 

voidmain()

{

char*s="EveryCprogramhasaprimarymainfunctionthatmustbenamedmain";

func(s);

}

有10个学生,每个学生的数据包括学号、姓名和三门课的成绩。

用键盘输入10个学生的数据,要求打印出每个学生三门课的平均成绩,以及最高分的学生的数据(学号、姓名、三门课的成绩和平均分数)。

#include<>

#include<>

typedefstruct

{

charname[20];

charsno[20];

intscore_1;

intscore_2;

intscore_3;

doublescore_all;

}STUDENT;

#defineN10

STUDENTstudents[N]={0};

 

voidmain()

{

inti,max;

for(i=0;i

{

printf("Input#%dStudent'sName:

");

scanf("%s",students[i].name);

printf("Input#%dStudent'sSNO:

");

scanf("%s",students[i].sno);

printf("Input#%dStudent'sScore_1:

");

scanf("%d",&students[i].score_1);

printf("Input#%dStudent'sScore_2:

");

scanf("%d",&students[i].score_2);

printf("Input#%dStudent'sScore_3:

");

scanf("%d",&students[i].score_3);

students[i].score_all=students[i].score_1+students[i].score_2+students[i].score_3;

}

max=0;

for(i=0;i

{

printf("%s%s%d%d%d%lf\n",students[i].name,students[i].sno,students[i].score_1,students[i].score_2,students[i].score_3,students[i].score_all/3);

if(students[max].score_all

max=i;

}

printf("top=%s%s%d%d%d%lf\n",students[max].name,students[max].sno,students[max].score_1,students[max].score_2,students[max].score_3,students[max].score_all/3);

}

请编程序建立一个带有头结点的单向链表,链表结点中的数据通过键盘输入,当输入数据为-1时,表示输入结束。

#include<>

#include<>

typedefstruct__NODE

{

intdata;

__NODE*next;

}NODE,*LPNODE;

LPNODEhead=NULL;

voidAddNode(intdata)

{

LPNODEt;

t=(LPNODE)malloc(sizeof(NODE));

t->data=data;

t->next=NULL;

if(head==NULL)

head=t;

else

{

t->next=head;

head=t;

}

}

 

intCount(LPNODEhead)

{

inti=0;

while(head!

=NULL)

{

i++;

head=head->next;

}

returni;

}

 

voidmain()

{

intn;

do{

scanf("%d",&n);

if(n==-1)break;

AddNode(n);

}while(n!

=-1);

printf("Count:

%d",Count(head));

}

已知一个链表,链表中的结构为:

structobject

{charch;

structobject*next;

}

编写函数统计链表中的结点个数。

#include<>

#include<>

typedefstruct__NODE

{

intdata;

__NODE*next;

}NODE,*LPNODE;

LPNODEhead=NULL;

voidAddNode(intdata)

{

LPNODEt;

t=(LPNODE)malloc(sizeof(NODE));

t->data=data;

t->next=NULL;

if(head==NULL)

head=t;

else

{

t->next=head;

head=t;

}

}

 

intCount(LPNODEhead)

{

inti=0;

while(head!

=NULL)

{

i++;

head=head->next;

}

returni;

}

 

voidmain()

{

intn;

do{

scanf("%d",&n);

if(n==-1)break;

AddNode(n);

}while(n!

=-1);

printf("Count:

%d",Count(head));

}

说明一个枚举类型enummonth,它的枚举元素为:

Jan、Feb、…、Dec。

编写能显示上个月名称的函数last_month。

例如,输入Jan时能显示Dec。

再编写另一个函数printmon,用于打印枚举变量的值(枚举元素)。

最后编写主函数调用上述函数生成一张12个月份及其前一个月份的对照表。

#include<>

enummonth{Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};

char*ms[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

voidLast_Month(monthm)

{

inti;

if(m==0)

i=11;

else

i=m-1;

printf("%spre:

%s\n",ms[m],ms[i]);

}

voidprintmon(monthm)

{

printf("%sval:

%d\n",ms[m],m);

}

 

voidmain()

{

monthi;

for(i=Jan;i<=Dec;i=(enummonth)(i+1))

{

Last_Month(i);

printmon(i);

}

}

设有一包含职工编号、年龄和性别的单向链表,分别使用函数完成以下功能:

(1)建立链表;

(2)分别统计男女职工的人数;

(3)在链表尾部插入新职工;

(4)删除指定编号的职工;

(5)删除60岁以上的男职工和55岁以上的女职工,被删除的结点保存到另一个链表中。

在主函数中设计简单的菜单去调用上述函数。

#include<>

#include<>

#include<>

typedefstruct__EMPLOYEE

{

intsno;

intage;

intsex;

__EMPLOYEE*next;

}EMPLOYEE,*LPEMPLOYEE;

LPEMPLOYEEhead=NULL,retired=NULL;

voidAddNode(intsno,intage,intsex)

{

LPEMPLOYEEt,p;

t=(LPEMPLOYEE)malloc(sizeof(EMPLOYEE));

t->sno=sno;

t->age=age;

t->sex=sex;

t->next=NULL;

if(head==NULL)

head=t;

else

{

p=head;

while(p->next)

p=p->next;

p->next=t;

}

}

voidCountSex(int*pmale,int*pfemale)

{

inti=0,j=0;

LPEMPLOYEEp;

p=head;

while(p)

{

if(p->sex==1)

i++;

else

j++;

p=p->next;

}

*pmale=i;

*pfemale=j;

}

voidDeleteBySno(intsno)

{

LPEMPLOYEEp,t=NULL;

p=head;

while(p)

{

if(p->sno==sno)

{

if(p==head)

head=p->next;

else

t->next=p->next;

free(p);

}

t=p;

p=p->next;

}

}

voidDeleteByAge(intage1,intage2)

{

LPEMPLOYEEp,t=NULL;

p=head;

while(p)

{

if(p->sex==1&&p->age>age1||

p->sex==2&&p->age>age2)

{

if(p==head)

head=p->next;

else

t->next=p->next;

if(retired==NULL)

retired=p;

else

{

p->next=retired;

retired=p;

}

}

t=p;

p=p->next;

}

}

voidfunc1()

{

AddNode(1,30,1);

AddNode(2,65,1);

AddNode(3,40,2);

AddNode(4,30,2);

AddNode(5,56,2);

}

voidfunc2()

{

inti,j;

CountSex(&i,&j);

printf("Male:

%d,Female:

%d\n",i,j);

}

voidfunc3()

{

intsno,sex,age;

printf("InputSnoAgeSex(1:

male,2:

female):

");

scanf("%d%d%d",&sno,&age,&sex);

AddNode(sno,age,sex);

}

voidfunc4()

{

intsno;

printf("InputSnotoDelete:

");

scanf("%d",&sno);

DeleteBySno(sno);

}

voidfunc5()

{

DeleteByAge(60,55);

}

voidmain()

{

charc;

do{

//clrscr();

printf("1---func1\n");

printf("2---func2\n");

printf("3---func3\n");

printf("4---func4\n");

printf("5---func5\n");

c=getch();

switch(c)

{

case'1':

func1();break;

case'2':

func2();break;

case'3':

func3();break;

case'4':

func4();break;

case'5':

func5();break;

}

printf("Pressanykeytocontinue,Press'N'or'n'toExit\n");

c=getch();

}while(!

(c=='N'||c=='n'));

}

精心搜集整理,只为你的需要

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2