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);
相关推荐
饮水桶_T2 小时前
JAVA IO流笔记
开发语言·数据结构·算法
Felven4 小时前
C. Brr Brrr Patapim
c语言·开发语言
路溪非溪5 小时前
python标准库使用总结
开发语言·python
Zwawa6 小时前
嵌入式OLED表驱动菜单:用一张表、一个栈和一个窗口重写表驱动菜单
c语言·单片机·嵌入式
SomeOtherTime7 小时前
Rust 面向对象介绍(AI回答)
开发语言·python·rust
wo_jack7 小时前
C++ 入门笔记(十七):list 入门 —— 为什么 list 不能 sort?
数据结构·c++·面试·stl·list
zh_xuan7 小时前
kotlin常见面试题
开发语言·kotlin·面试题
草莓熊Lotso9 小时前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
zaizai100710 小时前
算法(左程云)1
数据结构
huaqianzkh10 小时前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#