第9章习题答案

  • 格式:pdf
  • 大小:95.79 KB
  • 文档页数:4

第 9 章 结构体和共用体
26
{ int m;
char *pn;
} tab[2]={{1, "ab"},{2, "cd"}},*p=tab;
则表达式*p­>pn 的值为

表达式*(++p)­>pn 的值为

解:
a
c
5.分析下列程序的输出结果。 #include <stdio.h> struct student {
【思考和习题】第 9 章答案:
1.试定义一结构,用来描述日期,具体的说,该结构共有 3 个成员变量,分别描述年、 月、日信息。
解: struct date { int year;
int month; int day; }; 2.写出下列程序的输出结果: struct info { int k; char *s; }t;
6.以下程序运行后的输出结果是

#include <stdio.h>
struct NODE
{
int num;
struct NODE *next;

void main( ) { struct NODE s[3]={{1, '\0'},{2, '\0'},{3, '\0'}},*p,*q,*r;
int sum=0;
{if (strcmp(bok[j].name,bok[j+1].name)>0)
第 9 章 结构体和共用体
28
{strcpy(,bok[j].name);
strcpy(bok[j].name,bok[j+1].name);
strcpy(bok[j+1].name,);
temp.price=bok[j].price;
bok[j].price=bok[j+1].price;
bok[j+1].price=temp.price;
strcpy(temp.author,bok[j].author);
strcpy(bothor);
第 9 章 结构体和共用体
27
s[0].next=s+1;s[1].next=s+2; s[2].next=s; p=s;q=p­>next;r=q­>next; sum+= q­>next­>num;sum+= r­>next­>next­>num; printf(" %d\n",sum); } 解:输出结果: 5
void main( ) {
int i,j; /*以下为输入*/ for(i=0;i<N;i++) {
printf("\nInput information of book %d:\n",i+1); printf("name: " ); scanf("%s",bok[i].name); printf("\nprice: "); scanf("%f",&bok[i].price); printf("\nauthor: " ); scanf("%s",bok[i].author); printf("\npress: " ); scanf("%s",bok[i].press); } /*以下为排序*/ for(i=0;i<N;i++) for(j=0;j<N­i;j++)
void f(struct info t) { t.k=1997;
t.s="Borland"; }
void main() { t.k=2000;
t.s="Inprise"; f(t); printf("%d,%s\n",t.k,t.s); } 解:输出结果: 2000,Inprise 3.若有定义 struct {int m,n;}s[2]={{1,2},{3,4}},*p=s; 则表达式++p­>m 的值为 表达式(++p)­>n 的值为 解: 2 4 4.若有定义 struct
strcpy(bok[j+1].author,temp.author);
strcpy(temp.press,bok[j].press);
strcpy(bok[j].press,bok[j+1].press);
strcpy(bok[j+1].press,temp.press);
}
}
/*以下为输出*/
printf("NO.
7.输入 20 本书的名称、单价、作者、出版社,按书名进行排版和输出。 #include <stdio.h> #include <string.h> #define N 20 struct Book {
char name[36]; float price; char author[8]; char press[20]; }bok[N],temp;
name
price
author
press \n");
for(i=0;i<N;i++)
{
printf("\n%3d%20s%12.2f%10s%20s",i+1,bok[i].name, bok[i].price, bok[i].author,
bok[i].press);
}
}
int num; char name[20]; char sex; char addr[30]; }a={1001, "Zhang san",'M', "123 Wuyi Road"};
void main( ) {
printf("No: %ld\nName: %s\nSex:%c\nAddress: %s\n",a.num,,a.sex,a.addr); } 解:输出结果: No:1001 Name:Zhang san Sex:M Address:123 Wuyi Road