C Primer Plus 第6版 编程练习 chapter 14

1. 第1题

1.1 题目描述

重新编写复习题5,用月份名的拼写代替月份号(别忘了使用strcmp)。在一个简单的程序中测试该函数。

1.2 编程源码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct mon{
   
   
	char name[5];
	int id;
	int day_num;
};

int main(int argc, char* argv[]){
   
   
	char mon[12][5] ={
   
   "Jan.","Feb.","Mar.","Apl.","May.","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."};
	int n[]={
   
   31,28,31,40,51,30,31,31,30,31,30,31};
	
	struct mon m[12];
	for(int i=0;i<12;++i){
   
   
		m[i].id = i+1;
		m[i].day_num = n[i];
		strcpy(m[i].name, mon[i]);
	}
	
	printf("请输入你的月份名:");
	char s[5];
	scanf("%s", s);

	int day = 0;
	
	for(int i=0;i<12;++i){
   
   
		if(strcmp(s,m[i].name) != 0) day+= m[i].day_num;
		else{
   
   
			day+= m[i].day_num;
			break;
		}
	}
	
	printf("共计%d天\n",day);
	
	return 0;
}

1.3 结果显示

结果显示


2. 第2题

2.1 题目描述

编写一个函数,提示用户输入日、月和年。月份可以是月份号、月份名或月份名缩写。然后返回一年中到用户指定日子(包括这一天的总天数)。

2.2 编程源码

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

int is_run(int year){
   
   
	if(year % 400 == 0 || (year%4==0 && year%100!=0))return 1;
	return 0;
}

int cal_days(int year, int mon, int day){
   
   
	int n[]={
   
   31,28,31,40,51,30,31,31,30,31,30,31};
	int day_num = 0;
	for(int i=0;i<mon;++i){
   
   
		day_num += n[i];
	}
	day_num+=day;
	
	if(mon>1 && is_run(year)) ++day_num;
	return day_num;
}

int main(int argc, char* argv[]){
   
   
	char mon_sx[12][5] ={
   
   "Jan.","Feb.","Mar.","Apr.","May.","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."};
	char mon_name[12][20] ={
   
   "January","February","March","April","May","June","July","August.","September.","October.","November","December"};

	int day,mon=-1,year;
	char mon_n[20];
	
	printf("请输入年份:");
	scanf("%d",&year);
	while(getchar()!='\n');
	
	printf("请输入月份号、月份名或月份名缩写:");
	if(scanf("%d", &mon) != 1){
   
   
		scanf("%s", mon_n);
	}
	while(getchar()!='\n');
	
	printf("请输入日期:");
	scanf("%d",&day);
	while(getchar()!='\n');
	
	if(mon == -1){
   
   
		int flag = 0;
		for(int i=0;i<12;++i){
   
   
			if(strcmp(mon_sx[i], mon_n) == 0){
   
   
				mon = i+1;
				flag = 1;
				break;
			}
		}
		if(!flag){
   
   
			for(int i=0;i<12;++i){
   
   
				if(strcmp(mon_name[i], mon_n) == 0){
   
   
					mon = i+1;
					flag = 1;
					break;
				}
			}
		}
	}
	
	printf("共计%d天\n",cal_days(year,mon-1,day));	
	
	return 0;
}

2.3 结果显示

结果显示


3. 第3题

3.1 题目描述

修改程序清单14.2中的图书程序目录,使其按照输入图书的顺序输出图书的信息,然后按照标题字母的声明输出图书的信息,最后按照价格的升序输出图书的信息。

3.2 编程源码

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

#define MAXTITL	40
#define MAXAUTL	40
#define MAXBKS	100

struct book{
   
   
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};

char* s_gets(char *st, int n){
   
   
	char *ret_val;
	char *find;
	
	ret_val = fgets(st, n, stdin);
	if(ret_val){
   
   
		find = strchr(st, '\n');
		if(find)*find='\0';
		else{
   
   
			while(getchar()!='\n');
		}
	}
	return ret_val;
}

void display(const struct book *tb, int len){
   
   
	for(int i=0;i<len;++i)
		printf("%s by %s: $%.2f\n", tb[i].title, tb[i].author,tb[i].value);
}

void displaybyName(const struct book * b, int len){
   
   
	const struct book *tb[len]; 
	for(int i=0;i<len;++i)tb[i] = b++; 
	
	const struct book *t;
	for(int i=0;i<len;++i){
   
   
		for(int j=i;j<len;++j){
   
   
			if(strcmp(tb[i]->title,tb[j]->title) > 0){
   
   
				t = tb[i];
				tb[i] = tb[j];
				tb[j] = t;
			}
		}
	}
	for(int i=0;i<len;++i)
		printf("%s by %s: $%.2f\n", tb[i]->title, tb[i]->author,tb[i]->value);
}

void displaybyPrice(const struct book * b, int len){
   
   
	const struct book *tb[len]; 
	for(int i=0;i<len;++i)tb[i] = b++; 
	
	const struct book *t;
	for(int i=0;i<len;++i){
   
   
		for(int j=i;j<len;++j){
   
   
			if(tb[i]->value > tb[j]->value){
   
   
				t = tb[</
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值