C Primer Plus第六版 第十四章 编程练习

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

#include <stdio.h>
#include <string.h>
struct month {
	char monthname[10];
	char monthn[4];
	int days;
	int monthnumber;
};
int days_count(char * str,int year)
{
	int i,j;
	int total=0;
	int feb=(year%4==0)?29:28;  // 闰年2月29天
	struct month year_m[12]={
		{"Junrary","Jun",31,1},
		{"Febrary","Feb",feb,2},
        {"March","Mar",31,3},
		{"Aprial","Apr",30,4},
		{"May","May",31,5},
		{"June","Jue",30,6},
		{"July","Jul",31,7},
		{"Augest","Aug",31,8},
		{"September","Sep",30,9},
		{"Octable","Oct",31,10},
		{"Novemble","Nov",30,11},
		{"Decemble","Dec",31,12}
	};
	for(i=0;i<12;i++)
	{
		if(strcmp(year_m[i].monthname,str)==0)
			for(j=i;j>=0;j--)
				total+=year_m[j].days;
	}
	return total;
}

// 测试函数
#include <stdio.h>
#include <string.h>
char * s_gets(char * st,int n);
#define SLEN 10
int main(void)
{
	int year;
	char MONTH[SLEN];

	puts("Enter the year: ");
	scanf("%d",&year);
	getchar();
	puts("Enter the month(empty line to quit): ");
	while(s_gets(MONTH,SLEN)!=NULL&&MONTH[0!='\0'])
	{
		if(days_count(MONTH,year)!=0)
			printf("The days at %s month of %d year is %d.\n",MONTH,year,days_count(MONTH,year));
		else
			printf("Month input error.\n");
		puts("Enter the month(empty line to quit): ");
	}
	puts("Bye.");

	return 0;
}

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')
				continue;
	}

	return ret_val;
}

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

#include <stdio.h>
#include <string.h>
struct MONTH {
	char monthname[4];
	int days;
};

int days_count(unsigned int year,char * Month,unsigned int days)
{
	int i,j;
	int total=0;
	struct MONTH months[12]={
		{"Jun",31},
		{"Feb",year%4==0?29:28},     // 闰年2月份29天
		{"Mar",31},
		{"Apr",30},
		{"May",31},
		{"Jue",30},
		{"Jul",31},
		{"Aug",31},
		{"Sep",30},
		{"Oct",31},
		{"Nov",30},
		{"Dec",31}
	};
	for(i=0;i<12;i++)
	{
		if(strcmp(months[i].monthname,Month)==0)      // 计算以及判定日期所在月份之前的月份
		{
			for(j=i-1;j>=0;j--)
				total+=months[j].days;
			break;
		}
	}
	if(i==12)
	{
		printf("Month input error.\n");
		return -1;
	}
	if(days<1||days>months[i].days)    // 判定日期是否正确
	{
		printf("Date input error.\n");
		return -1;
	}
	return total+days;
}

int main(void)
{
	int i,j;
	unsigned int year,days;
	char month[4];
	int total=0;

	puts("Enter the year: ");
	while(scanf("%u",&year)==1)
	{
	    getchar();
	    puts("Enter the month: ");
	    fgets(month,4,stdin);
		puts("Enter the date: ");
		scanf("%u",&days);
		getchar();
		if((total=days_count(year,month,days))!=-1)
			printf("The days of year %u,month %s,date %u is %d.\n",year,month,days,total);
		else
		{
			puts("Enter the year: ");
			continue;
		}
		puts("Enter the year: ");
	}
	printf("Done.\n");

	return 0;
}

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

#include <stdio.h>
#include <string.h>
char * s_gets(char * st,int n);
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100
struct book{
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};

void sort_input(struct book * books[],int count)  // 按输入图书的顺序输出
{
	int index;
	if(count>0)
	{
		printf("Here is the list of your books:\n");
		for(index=0;index<count;index++)
			printf("%s by %s:$%.2f\n",books[index]->title,books[index]->author,books[index]->value);
	}
	else
		printf("No books?Too bad.\n");

}

void sort_title(struct book * books[],int count)  // 按标题字符的声明输出
{
	int i,j,index;
	struct book * temp;
	if(count>0)
	{
		for(i=0;i<count-1;i++)
			for(j=i+1;j<count;j++)
				if(strcmp(books[i]->title,books[j]->title)>0)
				{
					temp=books[i];
					books[i]=books[j];
					books[j]=temp;
				}
		printf("Here is the list of your book:\n");
		for(index=0;index<count;index++)
			printf("%s by %s:$%.2f.\n",books[index]->title,books[index]->author,books[index]->value);
	}
	else
		printf("No books?Too bad.\n");

}

void sort_value(struct book * books[],int count)   // 按价格的升序输出
{
	int i,j,index;
	struct book * temp;
	if(count>0)
	{
		for(i=0;i<count-1;i++)
			for(j=i+1;j<count;j++)
				if(books[j]->value>books[j]->value)
				{
					temp=books[i];
					books[i]=books[j];
					books[j]=temp;
				}
		printf("Here is the list of your book:\n");
		for(index=0;index<count;index++)
			printf("%s by %s:%.2f.\n",books[index]->title,books[index]->author,books[index]->value);
	}
	else
		printf("No books?Too bad.\n");

}

// 测试函数
int main(void)
{
	struct book library[MAXBKS];
	int count=0;
	int index;
	struct book * pts[MAXBKS];

	for(index=0;index<MAXBKS;index++)
		pts[index]=&library[index];
	printf("Please enter the books,empyt line to quit:\n");
	while(s_gets(pts[count]->title,MAXTITL)!=NULL&&pts[count]->title[0]!='\0')
	{
		puts("Enter the author: ");
		s_gets(pts[count]->author,MAXAUTL);
		puts("Enter the price: ");
		scanf("%f",&pts[count++]->value);  // bug (未读取成功时,为0)
		while(getchar()!='\n')
			continue;
		printf("Enter next book,empty line to quit:\n");
	}
	puts("Sort by input:");
	sort_input(pts,count);
	puts("Sort by title:");
	sort_title(pts,count);
	puts("Sort by price");
	sort_value(pts,count);
	puts("Done");

	return 0;
}

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')
			continue;
	}

	return ret_val;
}

4.编写一个程序,创建一个有两个成员的结构模板:

    a.第1个成员是社会保险号,第2个成员是一个有3个成员的结构,第1个成员代表名,第2 个成员代表中间名,第3个成员表示姓。创建并初始化一个内含5个该类型结构的数组。该程序以下 面的格式打印数据:

    Dribble, Flossie M. –– 302039823

    如果有中间名,只打印它的第1个字母,后面加一个点(.);如果没有中间名,则不用打印 点。编写一个程序进行打印,把结构数组传递给这个函数。

    b.修改a部分,传递结构的值而不是结构的地址。

#include <stdio.h>
#include <string.h>
#define NLEN 20
struct name {
	char fname[NLEN];
	char mname[NLEN];
	char lname[NLEN];
};
struct record {
	char SInumber[20];
	struct name names;
};

void show_record_ptr(struct record * ptr[],int size)         // 参数为指针
{
	int index;

	for(index=0;index<size;index++)
	{
		if(ptr[index]->names.mname[0]=='\0')
			printf("%s,%s -- %s\n",ptr[index]->names.fname,ptr[index]->names.lname,
				                   ptr[index]->SInumber);
		else
			pri
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值