C Primer Plus--- Chapter 14---Structures and Other Data Forms ---9.复习题
- 1. 1. What’s wrong with this template?
- 2. 2. Here is a portion of a program. What will it print?
- 3. Devise a structure template that will hold the name of a month, a three-letter abbreviation for the month, the number of days in the month, and the month number.
- 4. Define an array of 12 structures of the sort in question 3 and initialize it for a non-leap year.
- 5. Write a function that, when given the month number, returns the total days in the year up to and including that month. Assume that the structure template of question 3 and an appropriate array of such structures are declared externally.
- 6.
- 7. Consider the following programming fragment:
- 8. Consider the following declarations:
- 9. Define a structure template suitable for holding the following items: the name of an automobile, its horsepower, its EPA city-driving MPG rating, its wheelbase, and its year. Use car as the template tag.
- 10. Suppose you have this structure:
- 11. Declare an enumeration with the tag choices that sets the enumeration constants no, yes, and maybe to 0, 1, and 2, respectively.
- 12. Declare a pointer to a function that returns a pointer-to-char and that takes a pointer-to char and a char as arguments.
- 13. Declare four functions and initialize an array of pointers to point to them. Each function should take two double arguments and return a double. Also, show two ways using the array to invoke the second function with arguments of 10.0 and 2.5.
1. 1. What’s wrong with this template?
structure {
char itable;
int num[20];
char * togs
}
错误:
- 结构体关键字应为 struct
- 结构体至少应有标签 tag 或结构体变量中一个
- 最后一个结构体成员声明后要有分号 ;
2. 2. Here is a portion of a program. What will it print?
#include <stdio.h>
struct house {
float sqft;
int rooms;
int stories;
char address[40];
};
int main(void)
{
struct house fruzt = {1560.0, 6, 1, "22 Spiffo Road"};
struct house *sign;
sign = &fruzt;
printf("%d %d\n", fruzt.rooms, sign->stories);
printf("%s \n", fruzt.address);
printf("%c %c\n", sign->address[3], fruzt.address[4]);
return 0;
}
答案:
6 1
22 Spiffo Road
S p
3. Devise a structure template that will hold the name of a month, a three-letter abbreviation for the month, the number of days in the month, and the month number.
struct month
{
char name[10];
char abb[4];
int days;
int month;
}
4. Define an array of 12 structures of the sort in question 3 and initialize it for a non-leap year.
struct month months[12] =
{
{"January", "jan", 31, 1},
{"February", "feb", 28, 2},
{"March", "mar", 31, 3},
{"April", "apr", 30, 4},
{"May", "may", 31, 5},
{"June", "jun", 30, 6},
{"July", "jul", 31, 7},
{"August", "aug", 31, 8},
{"September", "sep", 30, 9},
{"October", "oct", 31, 10},
{"November", "nov", 30, 11},
{"December", "dec", 31, 12}
}
5. Write a function that, when given the month number, returns the total days in the year up to and including that month. Assume that the structure template of question 3 and an appropriate array of such structures are declared externally.
extern struct month months[];
int days(int month)
{
int total, index;
if(month < 1 || month > 12)
return(-1);
for(index = 1, total = 0; index <= month; index++)
total += months[index-1].days;
return (total);
}
6.
a. Given the following typedef, declare a 10-element array of the indicated structure. Then, using individual member assignment (or the string equivalent), let the third element describe a Remarkatar lens with a focal length of 500 mm and an aperture of f/2.0.
typedef struct lens { /* lens descriptor */
float foclen; /* focal length,mm */
float fstop; /* aperture */
char brand[30]; /* brand name */
} LENS;
b. Repeat part a., but use an initialization list with a designated initializer in the declaration rather than using separate assignment statements for each member.
答案:
a :
LENS bigEye[10];
bigEye[2].foclen = 500;
bigEye[2].fstop = 2.0;
strcpy(bigEye[2].brand, "Remarkatar");
b :
LENS bigEye[10] = {[2] = {500, 2, "Remarkatar"}};
7. Consider the following programming fragment:
struct name {
char first[20];
char last[20];
};
struct bem {
int limbs;
struct name title;
char type[30];
};
struct bem * pb;
struct bem deb = {
6,
{"Berbnazel", "Gwolkapwolk"},
"Arcturan"
};
pb = &deb;
a. What would each of the following statements print?
printf("%d\n", deb.limbs);
printf("%s\n", pb->type);
printf("%s\n", pb->type + 2);
b. How could you represent “Gwolkapwolk” in structure notation (two ways)?
c. Write a function that takes the address of a bem structure as its argument and prints the contents of that structure in the form shown here (assume that the structure template is in a file called starfolk.h):
Berbnazel Gwolkapwolk is a 6-limbed Arcturan.
答案:
a :
6
Arcturan
cturan
b :
deb.title.last;
pb -> title.last;
c :
void prbem(const struct bem *pbem)
{
printf("%s %s is a %d-limbed %s.\n", pbem -> title.first, pbem -> title.last, pbem -> limbs, pbem -> type);
}
8. Consider the following declarations:
struct fullname {
char fname[20];
char lname[20];
};
struct bard {
struct fullname name;
int born;
int died;
};
struct bard willie;
struct bard *pt = &willie;
a. Identify the born member of the willie structure using the willie identifier.
b. Identify the born member of the willie structure using the pt identifier.
c. Use a scanf() call to read in a value for the born member using the willie identifier.
d. Use a scanf() call to read in a value for the born member using the pt identifier.
e. Use a scanf() call to read in a value for the lname member of the name member using the willie identifier.
f. Use a scanf() call to read in a value for the lname member of the name member using the pt identifier.
g. Construct an identifier for the third letter of the first name of someone described by the willie variable.
h. Construct an expression representing the total number of letters in the first and last names of someone described by the willie variable.
答案:
a :
willie.born;
b :
pt -> born;
(*pt).born;
c :
scanf("%d", &willie.born);
d :
scanf("%d", &pt -> born);
e :
scanf("%s", willie.name.lname);
f :
scanf("%s", pt -> name.lname);
g :
willie.name.fname[2];
h :
strlen(willie.name.fname) + strlen(willie.name.lname);
9. Define a structure template suitable for holding the following items: the name of an automobile, its horsepower, its EPA city-driving MPG rating, its wheelbase, and its year. Use car as the template tag.
struct car
{
char name[40];
float hp;
float eparate;
float wbase;
int year;
};
10. Suppose you have this structure:
struct gas {
float distance;
float gals;
float mpg;
};
a. Devise a function that takes a struct gas argument. Assume that the passed structure contains the distance and gals information. Have the function calculate the correct value for the mpg member and return the now completed structure.
b. Devise a function that takes the address of a struct gas argument. Assume that the passed structure contains the distance and gals information. Have the function calculate the correct value for the mpg member and assign it to the appropriate member.
答案:
a :
struct gas mpg(struct gas trip)
{
if (trip.gals > 0)
trip.mpg = trip.distance / trip.gals;
else
trip.mpg = -1.0;
return trip;
}
b :
void mpg(struct gas *trip)
{
if (trip->gals > 0)
trip->mpg = trip->distance / trip->gals;
else
trip->mpg = -1.0;
}
11. Declare an enumeration with the tag choices that sets the enumeration constants no, yes, and maybe to 0, 1, and 2, respectively.
enum choice{no, yes, maybe};
12. Declare a pointer to a function that returns a pointer-to-char and that takes a pointer-to char and a char as arguments.
char * (*pt)(char *, char);
13. Declare four functions and initialize an array of pointers to point to them. Each function should take two double arguments and return a double. Also, show two ways using the array to invoke the second function with arguments of 10.0 and 2.5.
double f1(double, double);
double f2(double, double);
double f3(double, double);
double f4(double, double);
double (*pt[4])(double, double) = {f1, f2, f3, f4};
(*pt[1])(10.0, 2.5);
double f1(double, double);
double f2(double, double);
double f3(double, double);
double f4(double, double);
typedef double (*ptype) (double, double);
ptype pf[4] = {f1, f2, f3, f4};