C Primer Plus第六版第九章源码

//9.1
#include<stdio.h>
#define NAME "GIGATHINK, INC."
#define ADDRESS "101 Megabuck Plaza"
#define PLACE "Megapolis, CA 94904"
#define WIDTH 40

void starbar(void);

int main(void)
{
	starbar();
	printf("%s\n",NAME);
	printf("%s\n",ADDRESS);
	printf("%s\n",PLACE);
	starbar();
	
	return 0;
}

void starbar(void)
{
	int count;
	
	for (count = 1;count <= WIDTH; count++)
		putchar('*');
		putchar('\n');
}
//9.2
#include<stdio.h>
#include<string.h>
#define NAME "GIGATHINK"
#define ADDRESS "101 Megabuck Plaza"
#define PLACE "Megapolis, CA 94904"
#define WIDTH 40
#define SPACE ' '

void show_n_char(char ch, int num);

int main(void)
{
	int spaces;
	
	show_n_char('*',WIDTH);
	putchar('\n');
	show_n_char(SPACE, 12);
	printf("%s\n",NAME);
	spaces = (WIDTH - strlen(ADDRESS)) / 2;
	
	show_n_char(SPACE, spaces);
	printf("%s\n",ADDRESS);
	show_n_char(SPACE,(WIDTH - strlen(PLACE)) / 2);
	
	printf("%s\n",PLACE);
	show_n_char('*',WIDTH);
	putchar('\n');
	
	return 0;
}

void show_n_char(char ch, int num)
{
	int count;
	
	for (count = 1; count <= num; count++)
		putchar(ch);
}
//9.3
#include<stdio.h>
int imin(int, int);

int main(void)
{
	int evil1, evil2;
	
	printf("Enter a pair of integers (q to quit):\n");
	while (scanf("%d %d",&evil1, &evil2) == 2)
	{
		printf("The lesser of %d and %d is %d.\n",
				evil1,evil2,imin(evil1,evil2));
		printf("Enter a pair of integers (q to quit):\n");
	}
	printf("Bye.\n");
	
	return 0;
}
int imin(int n,int m)
{
	int min;
	if (n < m)
		min = n;
	else
		min = m;
		
	return min;
}
//9.4
#include<stdio.h>
int imax();

int main(void)
{
	printf("The maximum of %d and %d is %d.\n",3, 5, imax(3));
	printf("The maximum of %d and %d is %d.\n",3, 5, imax(3.0,5.0));
	return 0;
}

int imax(n, m)
int n, m;
{
	return (n > m ? n : m);
}
//9.5
#include<stdio.h>
int imax(int, int);
int main(void)
{
	printf("The maximum of %d and %d is %d.\n",
			3, 5, imax(3));
	printf("The maximum of %d and %d is %d.\n",
			3, 5, imax(3.0,5.0));
	return 0;
}

int imax(int n,int m)
{
	return (n > m ? n : m);
}
//9.6
#include<stdio.h>
void up_and_down(int);;

int main(void)
{
	up_and_down(1);
	return 0;
}

void up_and_down(int n)
{
	printf("Level %d: n location %p\n", n, &n);
	if (n < 4)
		up_and_down(n + 1);
	printf("LEVEL %d: n location %p\n", n, &n);
}
//9.7
#include<stdio.h>
long fact(int n);
long rfact(int n);
int main(void)
{
	int num;
	
	printf("This program calculates factorials.\n");
	printf("Enter a value in the range 0-12 (q to quit):\n");
	while (scanf("%d", &num) == 1)
	{
		if (num < 0)
			printf("No negative numbers, please.\n");
		else if (num > 12)
			printf("Keep input under 13.\n");
		else
		{
			printf("loop: %d factorial = %ld\n",
					num, fact(num));
			printf("recursion: %d factorial = %ld\n",
					num, rfact(num));
		}
		printf("Enter a value in the range 0-12 (q to quit):\n");
	}
	printf("Bye.\n");
	
	return 0;
}

long fact(int n)
{
	long ans;
	
	for (ans = 1;n > 1;n--)
		ans *= n;
		
	return ans;
}
long rfact(int n)
{
	long ans;
	
	if (n > 0)
		ans = n * rfact(n - 1);
	else
		ans = 1;
		
	return ans;
}
//9.8
#include<stdio.h>
void to_binary(unsigned long n);

int main(void)
{
	unsigned long number;
	printf("Enter an integer (q to quit):\n");
	while(scanf("%lu",&number) == 1)
	{
		printf("Binary equivalent: ");
		to_binary(number);
		putchar('\n');
		printf("Enter an integer (q to quit):\n");
	}
	printf("Done.\n");
	
	return 0;
}
void to_binary(unsigned long n)
{
	int r;
	
	r = n % 2;
	if (n >= 2)
		to_binary(n / 2);
	putchar(r == 0? '0':'1');
	
	return;
}
//9.9 9.10
#include<stdio.h>
#include"hotel.h" //自定义文件
 
int main(void)
{
	int nights;
	double hotel_rates;
	int code;
	
	while ((code = menu()) != QUIT)
	{
		switch (code)
		{
			case 1:hotel_rate = HOTEL1;
					break;
			case 2:hotel_rate = HOTEL2;
					break;
			case 3:hotel_rate = HOTEL3;
					break;
			case 4:hotel_rate = HOTEL4;
					break;
			default:hotel_rate = 0.0;
					printf("Oops!\n");
					break;
		}
		nights = getnights();
		showprice(hotel_rate, nights);
	}
	printf("Thank you and goodbye");
	
	return 0;
}
//9.10
#include<stdio.h>
#include"hotel.h"
int menu(void)
{
	int code, status;
	
	printf("\n%s%s\n",STARS, STARS);
	printf("Enter the number of the desired hotel:\n");
	printf("1) Fairfield Arms          2) Hotel Olympic\n");
	printf("3) Chertworthy Plaza       4) The Stockton\n");
	printf("5) quit\n");
	printf("%s%s\n", STARS, STARS);
	while ((status = scanf("%d",&code)) != 1 ||
			(code < 1 || code >5))
	{
		if (status != 1)
			scanf("%*s");
		printf("Enter an integer from 1 to 5, please.\n");
	}
	
	return code;
}

int getnights(void)
{
	int nights;
	
	printf("How many nights are needed? ");
	while (scanf("%d",&nights) != 1)
	{
		scanf("%*s");
		printf("Please enter an integer, such as 2.\n");
	}
	
	return nights;
}

void showprice(double rate, int nights)
{
	int n;
	double total = 0.0;
	double factor = 1.0;
	
	for (n = 1; n <= nights;n++, factor *= DISCOUNT)
		total += rate * factor;
	printf("The total cost will be $%0.2f.\n",total);
}
//9.11
#define QUIT     5
#define HOTEL1   180.00
#define HOTEL2   225.00
#define HOTEL3   255.00
#define HOTEL4   355.00
#define DISCOUNT   0.95
#define STAR "***********************"
//显示列表
int menu(void);
//返回预定天数
int getnights(void);

//根据费率、入住天数计算费用
//并显示结果
void showprice(double rate, int nights); 
//9.12
#include<stdio.h> 
void mikado(int);
int main(void)
{
	int pooh = 2, bah = 5;/*main()的局部变量*/
	
	printf("In main(), pooh = %d and &pooh = %p\n",pooh, &pooh);
	printf("In main(), bah = %d and &bah = %p\n",bah, &bah);
	mikado(pooh);
	
	return 0; 
}

void mikado(int bah)
{
	int pooh = 10;
	
	printf("In mikado(), pooh = %d and &pooh = %p\n",pooh, &pooh);
	printf("In mikado(), bah = %d and &bah = %p\n",bah, &bah);
}
//9.13
#include<stdio.h>
void interchange(int u, int v);

int main(void)
{
	int x = 5, y = 10;
	
	printf("Originally x = %d and y = %d.\n", x, y);
	interchange(x, y);
	printf("Now x = %d and y = %d.\n", x, y);
	
	return 0;
 } 
 
 void interchange(int u, int v)
 {
 	int temp;
 	
 	temp = u;
 	u = v;
 	v = temp;
}
//9.14
#include<stdio.h>
void interchange(int u, int v);

int main(void)
{
	int x = 5, y = 10;
	
	printf("Originally x = %d and y = %d.\n", x, y);
	interchange(x, y);
	printf("Now x = %d and y = %d.\n", x, y);
	
 	return 0;
} 

void interchange(int u, int v)
{
	int temp;
 	
 	printf("Originally u = %d and v = %d.\n", u, v);
 	temp = u;
 	u = v;
 	v = temp;
 	printf("Now u = %d and v = %d.\n", u, v);
}
//9.15
#include<stdio.h>
void interchange(int * u, int * v);

int main(void)
{
	int x = 5, y = 10;
	printf("Originally x = %d and y = %d.\n", x, y);
	interchange(&x, &y);
	printf("Now x = %d and y = %d.\n", x, y);
	
 	return 0;	
} 

void interchange(int * u, int * v)
 {
 	int temp;
 	
 	temp =  *u;
 	*u = *v;
 	*v = temp;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值