C Primer Plus Test 5
試題來(lái)源:C Primer Plus (第6版)中文版
本章主要為運(yùn)算符、表達(dá)式和語(yǔ)句的測(cè)試題。

5.1 編寫一個(gè)程序,把分鐘表示的時(shí)間轉(zhuǎn)換成用小時(shí)和分鐘表示的時(shí)間。使用#define或const創(chuàng)建一個(gè)表示60的符號(hào)常量或const變量。通過(guò)while循環(huán)讓用戶重復(fù)輸入值,直到用戶輸入小于或等于0的值才停止循環(huán)。
#include<stdio.h>
#include<stdlib.h>
#define TIME 60
int main(){
????int min, h, m;
????printf("Enter the minute:\n");
????scanf("%d", &min);
????while(min > 0){
????????h = min / TIME;
????????m = min % TIME;
????????printf("The time is %d hour %d minute\n", h, m);
????????printf("Enter the minute:\n");
????????printf("If you want to stop it, enter 0 or less 0\n");
????????scanf("%d", &min);
????}
????system("pause");
????return 0;
}


5.2編寫一個(gè)程序,提示用戶輸入一個(gè)整數(shù),然后打印從該數(shù)到比該數(shù)大10的所有整數(shù)(例如,用戶輸入5,則打印5~15的所有整數(shù),包括5和15)。要求打印的各值之間用一個(gè)空格、制表符或換行符分開。
#include<stdio.h>
#include<stdlib.h>
int main(){
int num, max;
printf("Enter one number:\n");
scanf("%d",&num);
max = num + 10;
printf("空格:");
while( num <= max){?
printf("%d ",num);
num ++;
}
printf("\n");
num = max - 10;
printf("制表符:");
while( num <= max){?
printf("%d\t",num);
num ++;
}
printf("\n");
num = max - 10;
printf("換行符:");
while( num <= max){?
printf("%d\n",num);
num ++;
}
system("pause");
return 0;
}?


5.3編寫一個(gè)程序,提示用戶輸入天數(shù),然后將其轉(zhuǎn)換成周數(shù)和天數(shù)。例如用戶輸入18,則轉(zhuǎn)換成2周4天。以下面的格式顯示結(jié)果:
????18 days are 2 weeks, 4 days.
通過(guò)while循環(huán)讓用戶重復(fù)輸入天數(shù),當(dāng)用戶輸入一個(gè)非正值時(shí)(如0或-20),循環(huán)結(jié)束。
#include<stdio.h>
#include<stdlib.h>
#define W_D 7
int main(){
????int day, weeks, days;
????printf("Enter the number of day:\n");
????scanf("%d", &day);
????while(day > 0){
????????weeks = day / W_D;
????????days = day % W_D;
????????printf("%d days are %d weeks, %d days.\n", day, weeks, days);
????????printf("Enter the number of day:(<=0 to quit)\n");
????????scanf("%d", &day);
????}
????system("pause");
????return 0;
}


5.4編寫一個(gè)程序,提示用戶輸入一個(gè)身高(單位:厘米),并分別以厘米和英寸為單位顯示該值,允許有小數(shù)部分。程序應(yīng)該能讓用戶重復(fù)輸入身高,直到用戶輸入一個(gè)非正值。其輸出示例如下:
????Enter a height in centimeters: 182
????182.0 cm = 5 feet, 11.7 inches
????Enter a height in centimeters (<=0 to quit): 168.7
????168.7?cm = 5 feet, 6.4 inches
????Enter a height in centimeters (<=0 to quit): 0
????bye
#include<stdio.h>
#include<stdlib.h>
#define C_F 0.0328
#define F_I 12
int main(){
????float height, inches, c_f, feet;
????int feets;
????printf("Enter a height in centimeters:");
????scanf("%f", &height);
?????while(height > 0){
????????????feet = height * C_F;
????????????feets = (int)feet;
????????????inches = (feet - feets) * F_I;
????????????printf("%.1f cm = %d feet, %.1f inchces\n", height, feets, inches);
????????????printf("Enter a height in centimeters(<=0 to quit):\n");
????????????scanf("%f", &height);
????}
????printf("bye\n");
????system("pause");
????return 0;
}

5.5修改程序addemup.c,你可以認(rèn)為addmup.c是計(jì)算20天里賺多少錢的程序(假設(shè)第1天賺$1、第2天賺$2,以此類推)。修改程序,使其可以與用戶交互,根據(jù)用戶輸入的數(shù)進(jìn)行計(jì)算(即,用讀入的一個(gè)變量來(lái)代替20)。
addemup.c
#include<stdio.h>
int main(){
????int count, sum;
????count = 0;
????sum = 0;
????whlle(count ++ < 20)
????????sum = sum + count;
????printf("sum=%d\n",sum);
????return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(){
????int sum, num, count;
????sum = 0;
????printf("Enter a number:\n");
????scanf("%d", &num);
????count = 0;
????while(count <= num){
????????sum = sum + count;
????????count = count + 1;
????}
????printf("The end is %d\n", sum);
????system("pause");
????return 0;
}


5.6修改編程練習(xí)5.5的程序,使其能計(jì)算整數(shù)的平方和(可以認(rèn)為第1天賺$1、第2天賺$4),以此類推。C沒(méi)有平方函數(shù),但是可以用n*n來(lái)表示n的平方。
#include<stdio.h>
#include<stdlib.h>
int main(){
????int sum, count;
????sum = 0;
????printf("Enter a number:\n");
????scanf("%d", &num);
????count = 0;
????while(count ++ < num){
????????sum += count * count;
????}
????printf("%d\n", sum);
????system("pause");
????return 0;
}


5.7編寫一個(gè)程序,提示用戶輸入一個(gè)double類型的數(shù),并打印該數(shù)的立方值。自己設(shè)計(jì)一個(gè)函數(shù)計(jì)算并打印立方值。main()函數(shù)要把用戶輸入的值傳遞給該函數(shù)。
#include<stdio.h>
#include<stdlib.h>
double Cbue(double num){
????return (num * num * num);
}
int main(){
????double num;
????printf("Enter a num of double:\n");
????scanf("%lf", &num);
????printf("cbue is %lf\n", Cbue(num));
????return 0;
}


5.8編寫一個(gè)方程,顯示求模運(yùn)算的結(jié)果。把用戶輸入的第一個(gè)整數(shù)作為
求模運(yùn)算符的第二個(gè)運(yùn)算對(duì)象,該數(shù)在運(yùn)算過(guò)程中保持不變。用戶輸入
的書是第一個(gè)運(yùn)算對(duì)象。當(dāng)用戶輸入一個(gè)非正值時(shí),程序結(jié)束。
輸出示例如下:
This program computes moduli.
Enter an integer to serve as the second operand:256
Now enter the first operand:438
438 % 256 is 182
Enter next number for first operand (<=0 to quit):1234567
1234567 % 256 is 135
Enter next number for first operand (<=0 to quit):0
Done?
#include<stdio.h>
#include<stdlib.h>
int main(){
int num,f_oper,mod;
printf("This program computes moduli.\n");
printf("Enter an integer to serve as the second operand:");
scanf("%d",&num);
printf("\nNow enter the first operand:");
scanf("%d",&f_oper);
while(f_oper > 0){
mod = f_oper % num;
printf("\n%d %% %d is %d",f_oper,num,mod);
printf("\nEnter next number for first operand (<=0 to quit):");
scanf("%d",&f_oper);
}
printf("Done\n");
return 0;
}

5.9編寫一個(gè)程序,要求用戶輸入一個(gè)華氏溫度。程序應(yīng)讀取double
類型的值作為溫度值,并把該值作為參數(shù)傳遞給一個(gè)用戶自定義的
函數(shù)Temperatures()。該函數(shù)計(jì)算攝氏溫度和開氏溫度,并以小數(shù)
點(diǎn)后面兩位數(shù)的精度顯示3種溫度。要使用不同的溫標(biāo)來(lái)表示這3個(gè)
溫度。下面是華氏溫度轉(zhuǎn)攝氏溫度的公式:
攝氏溫度 = 5.0/9.0*(華氏溫度 - 32.0)
開氏溫度常用于科學(xué)研究,0表示絕對(duì)零,代表最低的溫度。下面
是攝氏溫度轉(zhuǎn)開氏溫度的公式:
開氏溫度 = 攝氏溫度 + 273.16
Temperatures()函數(shù)中用const創(chuàng)建溫度轉(zhuǎn)換中使用的變量。在main
函數(shù)中使用一個(gè)循環(huán)讓用戶重復(fù)輸入溫度,當(dāng)用戶輸入q或其他非數(shù)字
時(shí),循環(huán)結(jié)束。scanf函數(shù)返回讀取數(shù)據(jù)的數(shù)量,所以如果讀取數(shù)字則
返回1,如果讀取q則不返回1.可以使用==運(yùn)算符將scanf的返回值和1
作比較,測(cè)試兩值是否相等。?
#include<stdio.h>
#include<stdlib.h>
double Temperatures(double tem){
const double h_s = 5.0/9.0;?
const double s_k = 273.16;
double s_tem, k_tem;
s_tem = (tem - 32.0) * h_s;
k_tem = s_tem + s_k;
printf("%.2lf du\t %.2lf kai\n",s_tem,k_tem);
}
int main(){
double tem;
int status;
printf("Enter a number of huashi:");
status = scanf("%lf",&tem);
while(status == 1){
printf("\n");
Temperatures(tem);
printf("Enter a number of huashi('q' to quit):");
status = scanf("%lf",&tem);
}
printf("Bye\n");
system("pause");
return 0;
}
