首页 > 其他 > 详细

C Primer Plus 第14章 结构和其他数据形式 复习题与编程练习

时间:2015-12-11 02:01:44      阅读:621      评论:0      收藏:0      [点我收藏+]
复习题
1、以下模板有什么错误?
structure {
    char itable;
    int num[20];
    char * togs
}
答:
正确的关键字是struct而不是structure。模板需要在开始花括号前有一个标记或在结束花括号后有一个变量名。在*togs后面和在模板结尾处都应该有一个分号。
2、下面是某程序的一部分。输出会是什么?
#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、设计一个结构模板,保存一个月份名、一个3个字母的该月份的缩写、该月的天数,以及月份号。
答:
struct month {
    char name[30];
    char sup_name[4];
    int days;
    int month_day;
};
4、定义一个含有12个第3题中那种类型的结构的数组,并把它初始化为一个年份(非闰年)
答:
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、编写一个函数。当给出月份号后,程序返回一年中到该月为止(包括该月)总共的天数。假定在外部声明了第3题中的结构模板和一个该结构的数组。
答:
#include <stdio.h>

struct month {
    char name[30];
    char sup_name[4];
    int days;
    int month_day;
};
int days(const struct month months[], int month);
int main(void)
{
    int month;
    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}
    };
    printf("Please enter the month: \n");
    scanf("%d", &month);
    printf("the total days is: %d\n", days(months, month));
    return 0;
}
int days(const struct month months[], int month)
{
    int index, total;

    if(month < 1 || month > 12)
         return -1;
    else
    {
        for(index = 0; index < month; index++)
             total += months[index].days;
        return total;
    }
}
6、
a.给定下面的typedef,声明一个10个元素的指定结构的数组。然后通过各个成员赋值(或等价字符串),使第3个元素描述一个焦距长度为500mm,孔径为f/2.0的Remarkatar镜头。
typedef struct lens {     /* 镜头描述 */
    float foclen;         /* 焦距长度 */
    float fstop;          /* 孔径 */
    char brand[30];       /* 品牌名称 */
} LENS;
b.重复a,但在声明中使用一个指定初始化项目列表,而不是对每个成员使用单独的赋值语句。
答:
a.
    typedef struct lens {     /* 镜头描述 */
    float foclen;         /* 焦距长度 */
    float fstop;          /* 孔径 */
    char brand[30];       /* 品牌名称 */
    } LENS;
    LENS arr[10];
    arr[2].foclen = 500;
    arr[2].fstop = 2.0;
    strcpy(arr[2].brand, "Remarkatar");  // #include <string.h>
b.
    typedef struct lens {     /* 镜头描述 */
    float foclen;         /* 焦距长度 */
    float fstop;          /* 孔径 */
    char brand[30];       /* 品牌名称 */
    } LENS;
    LENS arr[10] = { [2] = {500, 2.0, "Remarkatar"} };
7、考虑下面的程序段:
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.下列每个语句会打印出什么?
printf("%d\n", deb.limbs);
printf("%s\n", pb->type);
printf("%s\n", pb->type + 2);
b.
怎样用结构符号表示"Gwolkapwolk"(使用两种方法)?
c.
编写一个函数,以一个bem结构的地址作为参数,并以下面所示的形式输出结构内容。假定结构模板在一个名为starfolk.h的文件中。
Berbnazel Gwolkapwolk is a 6-limbed Arcturan.
答:
a.
6
Arcturan
cturan
b.
deb.title.last
pb->title.last
c.
#include <stdio.h>
#include "starfolk.h"
struct name {
    char first[20];
    char last[20];
};
struct bem {
    int limbs;
    struct name title;
    char type[30];
};
void show(const struct bem *);
int main(void)
{
    struct bem * pb;
    struct bem deb = {
        6,
        {"Berbnazel", "Gwolkapwolk"},
        "Arcturan"
    };

    pb = &deb;
    show(pb);
    return 0;
}
void show(const struct bem * fp)
{
    printf("%s %s is a %d-limbed %s.", fp->title.first, fp->title.last, fp->limbs, fp->type);
}
8、考虑下列声明:
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.使用willie标识符表示willie结构的born成员。
b.使用pt标识符表示willie结构的born成员。
c.使用一个scanf()函数调用为通过willie标识符表示的born成员读入一个值。
d.使用一个scanf()函数调用为通过pt标识符表示的born成员读入一个值。
e.使用一个scanf()函数调用为通过willie标识符表示的name成员的lname成员读入一个值。
f.使用一个scanf()函数调用为通过pt标识符表示的name成员的lname成员读入一个值。
g.构造一个标识符,表示willie变量描述的人的名字的第3个字母。
h.构造一个表达式,表示willie变量描述的人的姓和名的所有字母数。
答:
a.willie.born
b.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];  (我觉得有欠考虑,万一姓不足3个字母怎么办?)
h.strlen(willie.name.fname) + strlen(willie.name.lname);
9、定义一个适合保存下列项目的结构模板:一辆汽车的名称、马力、市内行驶的EPA英里每加仑(mpg)等级、轴距和使用年数。用car作为模板标记。
答:(参考课后答案)
下面是一种可能性:
struct car {
    char name[20];
    float hp;
    float epampg;
    float wbase;
    int year;
};
10、假设有以下结构:
struct gas {
    float distance;
    float gals;
    float mpg;
};
a.设计一个函数,它接受一个struct gas参数。假定传递进来的结构包括distance和gals信息。函数为mpg成员正确计算出值并返回这个现在完整的结构。
b.设计一个函数,它接受一个struct gas参数的地址。假定传递进来的结构包括distance和gals信息。函数为mpg成员正确计算出值并把它赋给恰当的成员。
答:
a.
struct gas function1(struct gas fp)
{
    if(fp.gals > 0)
        fp.mpg = fp.distance / fp.gals;
    else
        fp.mpg = -1.0;
    return fp;
}
b.
void function1(struct gas * fp)
{
    if(fp->gals > 0)
        fp->mpg = fp->distance / fp->gals;
    else
        fp->mpg = -1.0;
}
11、声明一个枚举类型,使用choices作为标记,将枚举常量no、yes和maybe分别设置为0、1和2。
答:enum choices {no, yes, maybe};
12、声明一个指向函数的指针。该函数的返回值是一个char指针,参数为一个char指针和一个char值。
答:
char * (* fp)(char *, char);
13、声明4个函数,并把一个指针数组初始化为指向它们。每个函数接受两个double参数并返回一个double值。
答:
double f1(doubledouble);
double f2(doubledouble);
double f3(doubledouble);
double f4(doubledouble);
double (*fp[4])(doubledouble) = {f1, f2, f3, f4};
编程练习
1、
#include <stdio.h>
#include <string.h>
#define LEN 12
struct month {
    char name[30];
    char abbreviation[4];
    int days;
    int month_no;
};
const struct month months[LEN] =
{
    {"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}
};
int total_day(char * str);
int main(void)
{
    char month_name[10];

    printf("Please enter the month name: \n");
    while(gets(month_name) != NULL && month_name[0] != ‘\0‘)
    {
        if(total_day(month_name))
            printf("In a year the total number of days to %s is %d\n", month_name, total_day(month_name));
        else
            printf("You entered is not a month name\n");
        printf("Please enter the month name: \n");
    }

    return 0;
}
int total_day(char * str)
{
    int i, index;
    int total = 0;
    for(i = 0; i < LEN; i++)
    {
        if(strcmp(str, months[i].abbreviation) == 0)
        {
            index = i;
            for(i = 0, total = 0; i <= index; i++)
                total += months[i].days;
            break;
        }
    }
    return total;
}
2、(大家来来看看,写的怎么样啊!)
#include <stdio.h>
#include <string.h>
#define LEN 12
struct month {
    char name[30];
    char abbreviation[4];
    int days;
    int month_no;
};
const struct month months[LEN] =
{
    {"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}
};
int total_day(int month);
void eatline(void);
int main(void)
{
    int year;
    int day;
    int month;
    int total_mon, total;
    printf("Please enter a year(q to quit): \n");
    while(scanf("%d", &year) == 1 && year >= 1971 && year <= 9999)
    {
        printf("Please enter the month name: \n");
        while(scanf("%d", &month) != 1 || month < 1 || month > 12)
        {
            printf("The number of months you entered does not meet the requirements.\n");
            printf("Please again enter a month: \n");
            eatline();
        }
        total_mon = total_day(month);
        printf("Please enter a day: \n");
        while(scanf("%d", &day) != 1 || day < 1 || day > 31)
        {
            printf("The number of days you entered does not meet the requirements.\n");
            printf("Please again enter a day: \n");
            eatline();
        }
        total = day + total_mon;
        printf("The total number of days in a year to a given day is %d\n", total);
        printf("Please enter a year(q to quit): \n");
    }
    printf("Done.\n");
    return 0;
}
int total_day(int month)
{
    int i;
    int total = 0;
    if(month > 1)
        for(i = 0; i < month - 1; i++)
            total += months[i].days;
    return total;
}
void eatline(void)
{
    while(getchar() != ‘\n‘)
        continue;
}
3、
待续。。。。































C Primer Plus 第14章 结构和其他数据形式 复习题与编程练习

原文:http://www.blogjava.net/BeautifulMan/archive/2015/12/10/428593.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!