C primer plus(第六版)第九章源代码
程序员文章站
2022-06-21 10:46:53
...
第九章
/* 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 */
/* usehotel.c 房间费率程序 */
/* 与 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 */
/* hotel.c 酒店管理程序 */
#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 */
/* hotel.h 符号常量和 hotel.c 中所有函数的原型 */
#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 */
/* loccheck.c -- 查看变量被储存在何处 */
#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 */
/* swapl.c -- 第一个版本的交换函数 */
#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 */
/* swap2.c -- 查找swapl.c的问题 */
#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 */
/* swap3.c -- 使用指针解决交换函数的问题 */
#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;
}
下一篇: 移动端页面