当前位置:首页 > C语言程序设计习题参考答案!(第二版 杜友福)
head1 连接前 1 head1 连接后 1 2 5 图11.13 链表的连接
head2 2 5 \\0 7 9 \\0 7 9 \\0
提示:函数原型为:struct Node *link(struct Node *head1, struct Node *head2);
struct Node *link(struct Node *head1, struct Node *head2) {
struct Node *p ,*head0; p=head1; head0=head1; while(p->next!=NULL) p=p->next; p->next=head2; return head0; }
5.有两个链表a 和 b。设结点中包括学号、姓名。从a链表中删除去与b链表中有相同学号的那些结点。
#include
char name[8];
struct student *next; }a[LA],b[LB];
void main()
{struct student a[LA]={{101,\ struct student b[LB]={{103,\ {108,\ int i;
struct student *p,*p1,*p2,*head1,*head2; head1=a; head2=b; printf(\ \\n\
for(p1=head1,i=1;i<=LA;i++) {if(i
printf(\ if(i
printf(\
for(p2=head2,i=1;i<=LB;i++) { if(i
printf(\ if(i
37
p1=head1;
while(p1!=NULL) {p2=head2;
while((p1->num!=p2->num)&&(p2->next!=NULL)) p2=p2->next;
if(p1->num==p2->num) if(p1==head1)
head1=p1->next; else
{p->next=p1->next; p1=p1->next;} else
{p=p1;p1=p1->next;} }
printf(\ p1=head1;
while(p1!=NULL)
{printf(\ \\n\ p1=p1->next; } }
习题十二 位运算
一、选择题
1—8: CBDBC AAB 二、填空题
1. 5+3<<2的值为 32 。
2. a为任意整数,能将变量a清零的表达式是__a&0__。
3. 能将两字节变量x的高字节置全1,低字节保持不变的表达式是_x|0xFF00_。 4. 下列程序的输出结果是 64 。 #include
{char x=040;printf(“%d\\n”,x=x<<1);} 三、编程题
1. 取一个整数a从右端开始的4~7位。
#include
int num, mask;
printf(\ scanf(\ printf(\
num >>= 4; /*右移4位,将4~7位移到低4位上*/
mask = ~ ( ~0 << 4); /*间接构造1个低4位为1、其余各位为0的整数*/ printf(\ :0x%x\\n\ }
38
2. 编一个将十六进制数转换成二进制形式显示的程序。
#include \void main() {
int num, mask, i;
printf(\scanf(\
mask = 1<<15; /*构造1个最高位为1、其余各位为0的整数(屏蔽字)*/ printf(\for(i=1; i<=16; i++)
{ putchar(num&mask ? ’1’ : ‘0’); /*输出最高位的值(1/0)*/
num <<= 1; /*将次高位移到最高位上*/ if( i%4==0 ) putchar(‘,’); /*四位一组,用逗号分开*/ }
printf(\}
习题十三 文件
一、选择题 1—7: BDABB BC 二、填空题
1. 2 !feof(f1) f2
2. fopen(filename,”w”) ch 3. 0 “r” !feof(fp) 4. BBBCCCDDD
5. “w” str[I]-32 “r” 6. “bi.dat” fp 7. fopen ftell 三、编程题
编程题1: 调用fputs函数,把10个字符串输出到文件中;再从此文件中读入这10个字符串放在一个数组中;最后把字符串数组中的字符串输出到终端屏幕,以检查所有操作的正确性。
#include
FILE *fp=fopen(\
char *str[10]={ \
\
char str2[10][20]; if(fp==NULL)
{ printf(\ return; }
for(i=0;i<10;i++) { fputs(str[i],fp);
fputs(\
}
fclose(fp);
fp=fopen(\ if(fp==NULL)
39
{ printf(\ return; } i=0;
while(i<10&&!feof(fp))
{ printf(\ i++; }
}
编程题2:从键盘读入10个浮点数,以二进制形式存入文件中。再从文件中读出数据显示在屏幕上。修改文件中第四个数据。再从文件中读出数据显示在屏幕上,以验证修改的正确性。
#include \void ctfb(FILE *fp) { int i; float x; for(i=0;i<10;i++) { scanf(\ fwrite(&x,sizeof(float),1,fp); } }
void fbtc(FILE *fp) { float x; rewind (fp); fread(&x,sizeof(float),1,fp); while(!feof(fp)) { printf(\ fread(&x,sizeof(float),1,fp); } }
void updata(FILE *fp,int n,float x)
{ fseek(fp,(long)(n-1)*sizeof(float),0); fwrite(&x,sizeof(float),1,fp); } main()
{ FILE *fp; int n=4; float x; if((fp=fopen(\ { printf(\ exit(0); } ctfb(fp); fbtc(fp); scanf(\ updata(fp,n,x); fbtc(fp); fclose(fp); }
40
共分享92篇相关文档