C Primer Plus 第九章 課後答案
阿新 • • 發佈:2018-12-12
目錄
複習題
1.實際引數和形式引數的區別是什麼
形式引數:
被定義在被呼叫函式中的變數
實際引數:
出現在函式呼叫中的值,該值被附給形式引數
2.根據下面各函式的描述,分別編寫它們的ANSI C函式頭。注意,只需寫出函式頭,不用寫函式體
a.donut()接受一個int型別的引數,列印若干(引數指定數目)個0
b.gear()接受兩個int型別的引數,返回int型別的值
c.guess()不接受引數,返回一個int型別的值
d.stuff_it()接受一個double型別的值和double型別變數的地址,把第1個值儲存在指定位置
a. void donut(int);
b. int gear(int, int);
c. int guess();
d. void stuff_it(double, double*)
3.根據下面各函式的描述,分別編寫它們的ANSI C函式頭。注意,只需寫出函式頭,不用寫函式體
a.n_to_char()接受一個int型別的引數,返回一個char型別的值
b.digit()接受一個double型別的引數和一個int型別的引數,返回一個int型別的值
c.which()接受兩個可儲存double型別變數的地址,返回一個double型別的地址
d.random()不接受引數,返回一個int型別的值
a. char n_to_char(int);
b. int digit(double, int);
c. double* which(double*, double*);
d. int random();
4.設計一個函式,返回兩整數之和
int add(int a, int b)
{
return a + b;
}
5.如果把複習題4改成返回兩個double型別的值之和,應如何修改函式
double add(double a, double b)
{
return a + b;
}
6.設計一個名為alter()的函式,接受兩個int型別的變數x和y,把它們的值分別改成兩個變數之和以及兩變數之差
void alter(int *a, int *b)
{
int temp = *a + *b;
*b = *a - *b;
*a = temp;
return;
}
7.下面的函式定義是否正確
void salami(num)
{
int num, count;
for (count = 1; count <= num; num++)
printf(" O salami mio!\n");
}
void salami(int num)
{
int count;
for (count = 1; count <= num; count++)
printf(" O salami mio!\n");
}
8.編寫一個函式,返回3個整數引數中的最大值
int max(int a, int b, int c)
{
return a > b ? (a > c ? a : c) : (b > c ? b : c);
}
9.給定下面的輸出:
Please choose one of the following:
1) copy files 2) move files
3) remove files 4) quit
Enter the number of your choice:
a.編寫一個函式,顯示一份有4個選項的選單,提示使用者進行選擇(輸出如上所示)。
b.編寫一個函式,接受兩個int型別的引數分別表示上限和下限。該函式從使用者的輸入中讀取整數。如果整數超出規定上下限,函式再次列印選單(使用a部分的函式)提示使用者輸入,然後獲取一個新值。如果使用者輸入的整數在規定範圍內,該函式則把該整數返回主調函式。如果使用者輸入一個非整數字符,該函式應返回4。
c.使用本題a和b部分的函式編寫一個最小型的程式。最小型的意思是,該程式不需要實現選單中各選項的功能,只需顯示這些選項並獲取有效的響應即可。
#include <stdio.h>
#include <stdlib.h>
void menu()
{
printf("Please choose one of the following: \n");
printf("1) copy files 2) move files \n");
printf("3) remove files 4) quit\n");
printf("Enter the number of your choice:");
}
int ul(int u, int l)
{
int num;
while(scanf("%d", &num) == 1)
{
if(num >= l && num <= u)
{
return num;
}
else
{
menu();
}
}
return 4;
}
int main(void)
{
int u, l;
scanf("%d %d", &l, &u);
int x = ul(u, l);
printf("\nXXXX%d\n", x);
return 0;
}
程式設計練習
1.設計一個函式min(x, y),返回兩個double型別值的較小值。在一個簡單的驅動程式中測試該函式
#include <stdio.h>
#include <stdlib.h>
double mix(double a, double b)
{
return a > b ? b : a;
}
int main(void)
{
double a, b;
scanf("%lf %lf", &a, &b);
printf("%lf\n", mix(a, b));
return 0;
}
2.設計一個函式chline(ch, i, j),列印指定的字元j行i列。在一個簡單的驅動程式中測試該函式
#include <stdio.h>
#include <stdlib.h>
void chline(char ch, int i, int j)
{
for(int a = 0; a < j; a++)
{
for(int b = 0; b < i; b++)
{
putchar(ch);
}
putchar('\n');
}
}
int main(void)
{
char ch;
int i, j;
scanf("%c %d %d", &ch, &i, &j);
chline(ch, i, j);
return 0;
}
3.編寫一個函式,接受3個引數:一個字元和兩個整數。字元引數是待列印的字元,第1個整數指定一行中列印字元的次數,第2個整數指定列印指定字元的行數。編寫一個呼叫該函式的程式
同上?????
4.兩數的調和平均數這樣計算:先得到兩數的倒數,然後計算兩個倒數的平均值,最後取計算結果的倒數。編寫一個函式,接受兩個double型別的引數,返回這兩個引數的調和平均數
#include <stdio.h>
#include <stdlib.h>
double aver(double a, double b)
{
return 1 / ((1 / a + 1 / b) / 2.0);
}
int main(void)
{
double a, b;
scanf("%lf %lf", &a, &b);
printf("%lf\n", aver(a, b));
return 0;
}
5.編寫並測試一個函式larger_of(),該函式把兩個double型別變數的值替換為較大的值。例如, larger_of(x, y)會把x和y中較大的值重新賦給兩個變數
#include <stdio.h>
#include <stdlib.h>
void large_of(double *a, double *b)
{
*a > *b ? *b = *a : *a = *b;
}
int main(void)
{
double a, b;
scanf("%lf %lf", &a, &b);//注意引數是指標
large_of(&a, &b);
printf("%lf %lf\n", a, b);
return 0;
}
6.編寫並測試一個函式,該函式以3個double變數的地址作為引數,把最小值放入第1個變數,中間值放入第2個變數,最大值放入第3個變數
#include <stdio.h>
#include <stdlib.h>
void large_of(double *a, double *b, double *c)
{
int m;
*a < *b ? (*a < *c ? (*b < *c ? : m = *b, *b = *c, *c = m) : m = *b, *b = *a, *a = *c, *c = m) : (*b < *c ? : (*a < *c ? : m = *b, *b = *a, *a = *c, *c = m));
}
int main(void)
{
double a, b, c;
scanf("%lf %lf %lf", &a, &b, &c);
large_of(&a, &b, &c);
printf("%lf %lf %lf\n", a, b, c);
return 0;
}
7.編寫一個函式,從標準輸入中讀取字元,直到遇到檔案結尾。程式要報告每個字元是否是字母。如果是,還要報告該字母在字母表中的數值位置。例如,c和C在字母表中的位置都是3。合併一個函式,以一個字元作為引數,如果該字元是一個字母則返回一個數值位置,否則返回-1
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int checkch(char c)
{
if(isupper(c))
{
return c - 'A' + 1;
}
else if(islower(c))
{
return c - 'a' + 1;
}
return -1;
}
void inputstream(FILE* in)
{
char ch;
int x;
while((ch = getc(in)) != EOF)
{
x = checkch(ch);
if(x > 0)
{
printf("Is a alpha %c, it's index is %d\n", ch, x);
}
}
}
int main(void)
{
FILE* in = fopen("123.txt", "r");
inputstream(in);
fclose(in);
return 0;
}
8.第6章的程式清單6.20中,power()函式返回一個double型別數的正整數次冪。改進該函式,使其能正確計算負冪。另外,函式要處理0的任何次冪都為0,任何數的0次冪都為1(函式應報告0的0次冪未定義,因此把該值處理為1)。要使用一個迴圈,並在程式中測試該函式
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
double power(double n, int p) // 函式定義
{
double pow = 1;
int i, f = 0;
if(p == 0)
{
if(n == 0)
{
printf("0^0 is undefined\n");
}
return 1;
}
if(n == 0)
{
return 0;
}
if (p < 0)
{
p = -p;
f = 1;
}
for (i = 1; i <= p; i++)
pow *= n;
if(f)
{
return 1 / pow;
}
else
{
return pow;
}
// 返回pow的值
}
int main(void)
{
double n;
int p;
while(scanf("%lf %d", &n, &p) == 2)
{
printf("%lf\n", power(n, p));
}
return 0;
}
9.使用遞迴函式重寫程式設計練習8
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
double power(double n, int p) // 函式定義
{
if(n == 0)
{
if(p == 0)
{
printf("0^0 has't been defined\n");
return 1;
}
return 0;
}
if(p == 0)
{
return 1;
}
if(p > 0)
{
return n * power(n, p - 1);
}
else
{
return 1 /n * power(n, p + 1);
}
}
int main(void)
{
double n;
int p;
while(scanf("%lf %d", &n, &p) == 2)
{
printf("%lf\n", power(n, p));
}
return 0;
}
10.為了讓程式清單9.8中的to_binary()函式更通用,編寫一個to_base_n()函式接受兩個在2~10範圍內的引數,然後以第2個引數中指定的進位制列印第1個引數的數值。例如,to_base_n(129, 8)顯示的結果為201,也就是129的八進位制數。在一個完整的程式中測試該函式
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void to_base_n(unsigned long n, unsigned short x) /* 遞迴函式 */
{
int r;
r = n % x;
if (n >= x)
to_base_n(n / x, x);
printf("%d", r);
return;
}
int main(void)
{
unsigned long n;
unsigned short x;
scanf("%lu %hu", &n, &x);
to_base_n(n, x);
return 0;
}
11.編寫並測試Fibonacci()函式,該函式用迴圈代替遞迴計算斐波那契數
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void Fibonacci(int n)
{
if(n == 1)
{
printf("1\n");
}
if(n == 2)
{
printf("1\n");
}
unsigned long long a = 1, b = 1, sum;
for(int i = 3; i <= n; i++)
{
sum = a + b;
a = b;
b = sum;
}
printf("%llu\n", sum);
return;
}
int main(void)
{
int n;
scanf("%d", &n);
Fibonacci(n);
return 0;
}