编程珠玑第三章课后习题

3、编写标语函数,输入一个大写字母,输出一个字符数组,该字符数组用字符图形方式描绘该字母

这题不大懂,百度了下答案,发现答案的重点在于将26个字母用特定的表示方式表示出来,可以考虑为字母的外形设计一个定制模板,自己规定一套模板编写的格式,然后写一个解析程序,每次打印字母时,只需解析字母对应的模板即可,这样主要的工作量就花在每个字母模板的编写上,当然模板的编写是相当简单的,将字母图形转化为相应的模板格式即可。例如

x x x x x x x x x
x x x x x x x x x
x x x x x x x x x
      x x x      
      x x x      
      x x x      
      x x x      
      x x x      
      x x x      
x x x x x x x x x
x x x x x x x x x
x x x x x x x x x
这个I可以用39x,63b3x3b,39x 表示。

这个方法倒是提醒了我一些当遇到复杂的表示方法时,可以找出这个复杂方法的共性,然后用简单的表现形式代替复杂的表现形式,最后只要通过解析简单的表现形式即可。

4、编写日期处理函数

#include <stdio.h>
#include <stdlib.h>
#include <iostream> 
int monthdays[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
typedef struct 
{
	int year;
	int month;
	int day;
}Date;
//判断是否是闰年
bool isrunyear(Date *date)
{
	if(((date->year%4==0) && (date->year%100!=0))
		|| (date->year % 400) == 0)
		return true;
	return false;
}
//计算某个日期是一年中的第几天
int yearday(Date *date)
{
	int total = 0;
	for(int i = 1; i < date->month; i++)
	{
		total+=monthdays[i];
	}
	if(date->month > 2 && isrunyear(date))
	{
		total+=1;
	}
	return total+date->day;
}
//计算两个日期之间的天数间隔(date2 晚于 date1)
int betweenday(Date *date1, Date *date2)
{
	if(date1->year == date2->year)
		return yearday(date2) - yearday(date1);
	else
	{
		int totaldays = 0;
		for(int i = date1->year; i < date2->year; i++)
		{
			Date d;
			Date *date;
			date = &d;
			d.year = i;
			totaldays+=isrunyear(date)?366:365;
		}

		return totaldays - yearday(date1) + yearday(date2);	
	}
	
}
//判断某日子属于一周中第几天
int weekday(Date *date)
{
	Date d;
	Date *d1;
	d1 = &d;
	d.year = 1900;
	d.month = 1;
	d.day = 1;
	return betweenday(d1,date)%7+1;
}
//输出某年某月日历
void printyearmonth(Date *date)
{
	printf("%d年%d月\n",date->year,date->month);
	printf("日 一 二 三 四 五 六\n");
	date->day = 1;
	int first = weekday(date);
	int i;
	if(first != 7)
	{	
		//先输出1号前面的空白
		for(i = 0; i < first; i++)
		{
			printf("   ");
		}
		//输出1号后非空白部分
		for(i = 1; i <= 7 - first; i++)
		{
			printf("%-3d",i);
		}
		printf("\n");
	}
	//计算该月总天数
	int monthday = (date->month==2)?(isrunyear(date)?29:28):monthdays[date->month];
	
	for(i = 8 - first; i <= monthday; i++)
	{
		printf("%-3d",i);
		if(i % 7 == 7 - first)
			printf("\n");
	}
	printf("\n");
}
int getlength(char *a)
{
	return 0;
}


void main()
{
	Date d1;
	Date *date1;
	date1 = &d1;
	d1.year = 2011;
	d1.month = 2;
	d1.day = 1;
	Date d2;
	Date *date2;
	date2 = &d2;
	date2->year = 2013;
	date2->month = 4;
	date2->day = 5;
	printf("%d\n", betweenday(date1,date2));
	printf("%d\n", weekday(date2));
	printyearmonth(date1);

}

5、查找后缀连字符的连接

#include <stdio.h>
#include <stdlib.h>
#include <iostream> 
char *p[] = {"et-ic", "al-is-tic", "s-tic", "p-tic", "-lyt-ic", "ot-ic", "an-tic",
"n-tic", "c-tic", "at-ic", "h-nic", "n-ic", "m-ic", "l-lic", "b-lic", "-clic", "l-ic",
"h-ic", "f-ic", "d-ic", "-bic", "a-ic", "-mac", "i-ac"};
//返回后缀连字符连接
int getlastsamechar(char* a)
{
	int result = -1;
	//先将后缀字符数组每项反转
	int i = 0;
	int j = 0;
	int alen = strlen(a);
	char* aswap = new char[alen + 1];
	strcpy(aswap, a);
	for(i = 0; i < alen/2; i++)
	{
		char c = aswap[i];
		aswap[i] = aswap[alen - 1 - i];
		aswap[alen - 1 - i] = c;
	}
	printf("%s\n",aswap);
	int n = sizeof(p)/sizeof(char*);
	for(i = 0; i < n; i++)
	{
		int len = strlen(p[i]);
		char* b = new char[len+1];
		strcpy(b,p[i]);
		//去掉字符串里的-
		for(j = 0; j < len; j++)
		{
			if(b[j] == '-')
			{
				for(int q = j; q < len; q++)
				{
					b[q] = b[q+1];
				}
				len--;
			}
		}
		for(j = 0; j < len/2; j++)
		{
			char c = b[j];
			b[j] = b[len - 1 - j];
			b[len - 1- j] = c;
		}
		if(alen >= len)
		{
			for(j = 0; j < len; j++)
			{
				if(aswap[j] != b[j])
				{
					break;
				}				
			}
			delete b;
			if(j == len)
			{
				result = i;
				break;
			}
		}

	}
	delete aswap;
	return result;
}
void main()
{
	char *a = "clinic";
	int result = getlastsamechar(a);
	printf("%d\n",result);
	if(-1 != result)
	{
		char* res = new char[strlen(p[result]) + 1];
		strcpy(res, p[result]);
		printf("%s\n", res);
	}


}

8、获取数码管显示方式

#include <stdio.h>
#include <stdlib.h>
#include <iostream> 
//数字0-9的数码管显示方式
char *number[] = {"1011111","0000101","1110110","1110101","0101101",
	"1111001","1111011","0010101","1111111","1111101"};
//获取16bit的正整数
char** getNumber(int bit)
{
	char** result= new char*[5];
	for(int i = 4; i >= 0; i--)
	{
		result[i] = number[bit%10]; 
		bit/=10;
	}
	return result;
}
void main()
{
char** num = getNumber(54673);
	for(int i = 0; i < 5; i++)
	{	
		printf("%s\n",*num);
		num++;
	}
}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值