C Primer Plus 14.17 复习题

1、

复制代码
struct qwe {
	
	char itable;
	
	int num[20];
	
	char *togs;
	
}; 

2、

3、

复制代码
struct year {
	
	char month[10];
	
	char MONTH[4];
	
	int days;
	
	int numbers;
	
}; 

4、

复制代码
struct year 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、

复制代码
int days(const struct year months[], int month)
{
	
	int i, total = 0;
	
	if(month < 1 || month > 12)
	
		return 1;
		
	else
	
	{
	
		for(i = 0; i < month; i++)
			
			total += months[i].days;
	
		return total;
				
	}
}

6、

a.

复制代码
#include <string.h>

typedef struct lens {
	
	float foclen;
	
	float fstop;
	
	char brand[30];	

} LENS;

LENS arry[10];

arry[2].foclen = 500;

arry[2].fstop = 2.0;

strcpy(arry[2].brand ,"Remarkata");

/*
	
	因为数组名在C语言中是个常量指针,表示数组首元素的地址。而字符串也是个常量指针,
	直接赋值数组的话会改变地址,故可以选择给数组一个字符一个字符赋值,
	或者用 strcpy() 函数把字符串拷贝到数组中 

*/

b.

复制代码
#include <string.h>

typedef struct lens {
	
	float foclen;
	
	float fstop;
	
	char brand[30];	

} LENS;

LENS arry[10];

LENS arry[10] = 
{
	
    [2] = {  

        .foclen = 500.0,

        .fstop = 2.0,

        .brand = "Remarkata"  // 注意:这里可以写字符串常量
    }


};

/*

①与其它初始化方式混合: 

LENS arry[10] = {

    [2].foclen = 500.0,  // 只为特定元素的特定成员赋值

    [2].fstop = 2.0,

    [2].brand = "Remarkata",

    [5].brand = "Nikon"  // 可以只初始化部分成员

};

*/ 


/*

 ②全部使用指定初始化器:
 
 LENS arry[10] = {
   
    [0] = {.foclen = 50.0, .fstop = 1.8, .brand = "Sigma"},
   
    [1] = {.foclen = 85.0, .fstop = 1.4, .brand = "Sony"},
  
    [2] = {.foclen = 500.0, .fstop = 2.0, .brand = "Remarkata"},
  
    [3] = {.foclen = 35.0, .fstop = 1.4, .brand = "Zeiss"}
  


};

*/ 


/*

③部分初始化 + 传统初始化:

LENS arry[10] = {
   
    [2] = {500.0, 2.0, "Remarkata"},  // 指定索引,传统顺序
   
    {100.0, 4.0, "Tamron"},  // 没有指定索引,就是[0]
    
	[4] = {200.0, 2.8, "Tokina"}

};

*/ 

7、

a.

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 *ptr);

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.\n",fp->title.first, 
			fp->title.last, fp->limbs, fp->type);
				
}

8、

复制代码
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]; 

h.

strlen(willie.name.fname) + strlen(willie.name.lname);

9、

复制代码
struct car {

	char name[20];
	
	float horsepower;
	
	float EAPMPG;
	
	float wheelbase;

	int year;

};

10、

a.

复制代码
struct gas mpgs(struct gas *fp)
{
 
    if(fp.gals > 0)
 
        fp.mpg = fp.distance / fp.gals;
 
    else
 
        fp.mpg = -1.0;
 
    return fp;

}

b.

复制代码
void set_mpgs(struct gas *fp)
{
   
    if(fp->gals > 0)
   
        fp->mpg = fp->distance / fp->gals;
   
    else
   
        fp->mpg = -1.0;

}

11、

12、

13、

复制代码
double function1(double , double );

double function2(double , double );

double function3(double , double );

double function4(double , double );

double (*fp[4])(double , double)= {

	function1, 
	
	function2, 
	
	function3, 
	
	function4

};


function1(10.0, 2.5);

(*fp[2])(10.0, 2.5);
相关推荐
小龙报3 小时前
【嵌入式51单片机】51 单片机中断入门到精通:中断类型、寄存器操作、优先级配置与实战代码
c语言·驱动开发·单片机·嵌入式硬件·物联网·mongodb·51单片机
拾忆,想起3 小时前
Dubbo序列化异常终结指南:从精准诊断到根治与防御
开发语言·前端·微服务·架构·php·dubbo·safari
小武~3 小时前
Leetcode 每日一题C 语言版 -- 274 H-index
c语言·算法·leetcode
waves浪游3 小时前
进程控制(中)
linux·运维·服务器·开发语言·c++
0 0 03 小时前
CCF-CSP 36-3 缓存模拟(cache)【C++】
开发语言·c++·算法
wjs20243 小时前
C# 环境:深入解析与优化实践
开发语言
吃着火锅x唱着歌4 小时前
LeetCode 2909.元素和最小的山形三元组II
数据结构·算法·leetcode
满天星83035774 小时前
【Linux】信号(上)
linux·运维·服务器·开发语言·c++
霸王大陆4 小时前
《零基础学 PHP:从入门到实战》模块十:从应用到精通——掌握PHP进阶技术与现代化开发实战-5
android·开发语言·php