神秘博客

C Primer Plus [第六版]第14章复习题答案

3

struct month {
  char month_a[20];
  char month_b[3];
  int days;
  int month_c;
};

 

4

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

#include <stdio.h>

int days_s(int n);

struct month {
  char month_a[20];
  char month_b[3];
  int days;
  int month_c;
};

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 }

};



int main()
{
  int num;

  puts("请输入月份号");
  scanf_s("%d", &num);

  printf("1 - %d 月总计 %d 天.\n", num, days_s(num));

  system("pause");
  return 0;
}
int days_s(int n)
{
  int i;
  int count = 0;

  
  for (i = 0; i < n; i++)
  {
    count += months[i].days;
    if (months[i].month_c == n)
      break;
  }

  return count;
}

6

a

#include <stdio.h>
#include <string.h>

typedef struct lens {
  float foclen;  //焦距长度
  float fstop;  //孔径
  char brand[30];  //品牌名称
} LENS;

int main()
{
  LENS lens_a[10];
  lens_a[2].foclen = 500;
  lens_a[2].fstop = 2.0;
  strcpy_s(lens_a[2].brand, strlen("Remarkata") + 1, "Remarkata");

  printf("%s 镜头: 焦距长度 %g, 孔径为 f/%.1f\n", lens_a[2].brand, lens_a[2].foclen, lens_a[2].fstop);


  system("pause");
  return 0;
}

b

#include <stdio.h>
#include <string.h>

typedef struct lens {
  float foclen;  //焦距长度
  float fstop;  //孔径
  char brand[30];  //品牌名称
} LENS;

int main()
{
  LENS lens_a[10] = { [2] = {500,2.0,"Remarkata" } };
  

  printf("%s 镜头: 焦距长度 %g, 孔径为 f/%.1f\n", lens_a[2].brand, lens_a[2].foclen, lens_a[2].fstop);


  system("pause");
  return 0;
}

7

c

#include <stdio.h>
void output(struct bem *pst);
struct name {
  char first[20];
  char last[20];
};

struct bem {
  int limbs;
  struct name title;
  char type[30];
};

#include "starfolk.h"

int main()
{
  struct bem *pb;
  struct bem deb = 
  {
    6,
      { "Berbnazel","Gwolkapwolk" },
      "Arcturan"
  };
  pb = &deb;

  output(pb);


  system("pause");
  return 0;
}


void output(struct bem *pst)
{
  printf("%s %s is a %d-limbed %s.\n", (*pst).title.first, (*pst).title.last, pst->limbs, pst->type);
}

9

struct car {
  char name[20];
  float horsepower;
  float Grade;
  float Wheelbase;
  int years;
};

10

a

struct gas {
  float distance;
  float gals;
  float mpg;
};

struct gas mpgs_a(struct gas pst)
{
  if (pst.gals > 0)
    pst.mpg = pst.distance / pst.gals;
  else
    pst.mpg = -1;

  return pst;
}

b

struct gas {
  float distance;
  float gals;
  float mpg;
};


void mpgs_b(struct gas *pst)
{
  if (pst->gals > 0)
    pst->mpg = pst->distance / pst->gals;
  else
    pst->mpg = -1;
}

11

enum choices
{
  no,yes,maybe
};

12

char *(*pf)(char *st, char ch);

13

double num_1(double n, double m);
double num_2(double n, double m);
double num_3(double n, double m);
double num_4(double n, double m);

int main()
{
  double(*fp[4])(double, double) = { num_1,num_2,num_3,num_4 };

  fp[1](10.0, 2.5);
  (*fp[1])(10.0, 2.5);
}

 

版权说明:
点赞
  1. 神秘说道:

    :haha: 签到成功!签到时间:上午11:47:22,每日打卡,生活更精彩哦~

发表评论

电子邮件地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏