c语言入门经典课后作业,《C语言入门经典(第4版)》课后练习参考答案

第1章 C语言编程

练习1.1 编写一个程序,用两个printf()语句分别输出自己的名字和地址。

#include

int main(void) {

printf("姓名:小狮子\n");

printf("地址:月亮之上\n");

return 0;

}

练习1.2 将上一个练习修改成所有的输出只用一个printf()语句。

#include

int main(void) {

printf("姓名:小狮子\n地址:月亮之上\n");

return 0;

}

练习1.3 编写一个程序,输出下列文本,格式如下所示:"It's freezing in here," he said coldly.

#include

int main(void) {

printf("\"It's freezing in here,\" he said coldly.\n");

return 0;

}

第2章 编程初步

习题2.1 编写一个程序,提示用户用英寸输入一个距离,然后将该距离值输出为码、英尺和英寸的形式。

#include

/*

* 1码=3英尺=36英寸

*/

int main()

{

float len_inch = 0.0f; // 英寸

float len_foot = 0.0f; // 英尺

float len_yard = 0.0f; // 码

// 提示用户用英寸输入一个距离

printf("请用英寸输入一个距离:");

scanf("%f", &len_inch);

// 计算英尺

len_foot = len_inch / 12;

// 计算码

len_yard = len_foot / 3;

// 输出计算后的结果

printf("%.2f英寸 = %.2f英尺 = %.2f码\n", len_inch, len_foot, len_yard);

return 0;

}

习题2.2 编写一个程序,提示用户用英尺和英寸输入一个房间的长和宽,然后计算并输出面积,单位是平方码,精度为小数点后两位数。

#include

int main()

{

float length_in_foot = 0.0f; // 房间的宽度 单位英尺

float width_in_inch = 0.0f; // 房间的宽度 单位英寸

float area_in_yard = 0.0f; // 房间的面积 单位平方码

// 提示用户输入房间的长度

printf("请输入房间的长度(单位:foot):");

scanf("%f", &length_in_foot);

// 提示用户输入房间的宽度

printf("请输入房间的宽度(单位:inch):");

scanf("%f", &width_in_inch);

// 计算房间的面积 1码=3英尺=36英寸

area_in_yard = (length_in_foot / 3) * (width_in_inch / 36);

// 输出房间的面积

printf("房间的面积是:%.2f平方码。\n", area_in_yard);

return 0;

}

习题2.3 一个产品有两个版本:其一是标准版,价格是$3.5,其二是豪华版,价格是$5.5。编写一个程序,使用学到的知识提示用户输入产品的版本和数量,然后根据输入的产品数量,计算并输出价格。

#include

int main()

{

float price_of_normal = 3.5; // 普通版单价

float price_of_deluxe = 5.5f; // 豪华版单价

int number_of_normal = 0; // 普通版的数量

int number_of_deluxe = 0; // 豪华版的数量

float total_price = 0.0f; // 总价

// 提示用户输入产品的数量

printf("请输入普通版的数量:");

scanf("%d", &number_of_normal);

printf("请输入豪华版的数量:");

scanf("%d", &number_of_deluxe);

// 计算总价

total_price = number_of_normal * price_of_normal + number_of_deluxe + price_of_deluxe;

// 输出总价

printf("总价为:$%.2f\n", total_price);

return 0;

}

习题2.4 编写一个程序,提示用户从键盘输入一个星期的薪水(以美元为单位)和工作时数,它们均为浮点数,然后计算并输出每个小时的平均时薪,输出格式如下所示:

Your average hourly pay rate is 7 dollars and 54 cents.

#include

int main()

{

float salary = 0.0f; // 一个星期的薪水(以美元为单位)

float hours = 0.0f; // 工作时数

float salary_per_hour = 0.0f; // 每个小时的平均时薪

// 提示用户输入一个星期的薪水

printf("请输入一个星期的薪水(以美元为单位):");

scanf("%f", &salary);

// 提示用户输入工作时数

printf("请输入工作时数:");

scanf("%f", &hours);

// 计算每个小时的平均时薪

salary_per_hour = salary / hours;

// 输出结果

printf("Your average hourly pay rate is %d dollars and %d cents.\n", (int)salary_per_hour, (int)(salary_per_hour * 100) % 100);

return 0;

}

第3章 条件判断

习题3.1 编写一个程序,首先给用户以下两种选择:

(1)将温度从摄氏度转换为华氏度。

(2)将温度从华氏度转换为摄氏度。

接着,程序提示用户输入温度值,并输出转换后的数值。从摄氏度转换为华氏度,可以乘以 1.8 再加上 32。从华氏度转换为摄氏度,可以先减去 32 后,再乘以 5,除以 9。

#include

#include

int main()

{

float temperature = 0.0f; // 用户输入的温度值

char ch = '\0';

float result = 0.0f; // 转换后的温度值

// 提示用户都有哪种转换方式

printf("程序提供如下两种转换方式:\n");

printf(" A. 将温度从摄氏度转换为华氏度\n B. 将温度从华氏度转换为摄氏度\n");

// 提示用户输入选择的转换方式

printf("请选择转换方式(A or B):");

scanf("%c", &ch);

if (tolower(ch) == 'a')

{

printf("请输入温度值:");

scanf("%f", &temperature);

result = temperature * 1.8 + 32;

printf("%.2f摄氏度 = %.2f华氏度\n", temperature, result);

}

else if (tolower(ch) == 'b')

{

printf("请输入温度值:");

scanf("%f", &temperature);

result = (temperature - 32) * 5 / 9;

printf("%.2f华氏度 = %.2f摄氏度\n", temperature, result);

}

else

{

printf("选择错误\n");

}

return 0;

}

习题3.2 编写一个程序,提示用户输入3个整数值,分别代表月、日、年。例如用户输入了12、31、2003,程序就以31st December 2003 的格式输出该日期。

必须在日期值的后面加上th、nd、st 和 rd。例如1st、2nd、3rd、4th、11th、12th、13th、14th、21st、22nd、23rd、24th。

#include

int main()

{

int year = 0;

int month = 0;

int day = 0;

// 定义一个代表12个月份的枚举类型,枚举器值从1开始

enum Month { January = 1, February, March, April, May, June, July, August, September, October, November, December };

// 提示用户输入月、日、年

printf("请输入月、日、年:");

scanf("%d%d%d", &month, &day, &year);

// 输出日

if (day < 1 || day > 31)

{

printf("输入日期有误 ");

}

else if (day % 10 == 1 && day != 11)

{

printf("%dst ", day);

}

else if (day % 10 == 2 && day != 12)

{

printf("%dnd ", day);

}

else if (day % 10 == 3 && day != 13)

{

printf("%drd ", day);

}

else

{

printf("%dth ", day);

}

// 输出月

switch (month)

{

case January:

printf("January ");

break;

case February:

printf("February ");

break;

case March:

printf("March ");

break;

case April:

printf("April ");

break;

case May:

printf("May ");

break;

case June:

printf("June ");

break;

case July:

printf("July ");

break;

case August:

printf("August ");

break;

case September:

printf("September ");

break;

case October:

printf("October ");

break;

case November:

printf("November ");

break;

case December:

printf("December ");

break;

default:

printf("输入月份有误 ");

break;

}

// 输出年

printf("%d\n", year);

return 0;

}

习题3.3 编写一个程序,根据从键盘输入的一个数值,计算总价(单价是$5),数值超过30的折扣是10%,数值超过50的折扣是15%。

#include

int main()

{

float unit_price = 5.0f; // 商品单价

float discount = 0.0f; // 商品折扣

int number = 0; // 商品数量

// 提示用户输入商品数量

printf("请输入商品数量:");

scanf("%d", &number);

// 计算折扣

if (number <= 30)

{

discount = 0.0f;

}

else if (number <= 50)

{

discount = 0.1f;

}

else

{

discount = 0.15f;

}

// 输出总价

printf("商品的总价为:%.2f\n", number * unit_price * (1 - discount));

return 0;

}

习题3.4 修改本章最后的计算器例子,让用户选择输入y或Y,以执行另一个计算,输入n或N就结束程序。(注意:这需要实用goto语句,下一章将介绍一个更好的方法。)

#include

int main()

{

double number1 = 0.0;

double number2 = 0.0;

char operation = 0;

char choice = 0;

begin:

printf("Enter the caculation\n");

scanf("%lf %c %lf", &number1, &operation, &number2);

switch (operation)

{

case '+':

printf("= %lf\n", number1 + number2);

break;

case '-':

printf("= %lf\n", number1 - number2);

break;

case '*':

printf("= %lf\n", number1 * number2);

break;

case '/':

if (number2 == 0)

printf("\n\n\aDivision by zero error!\n");

else

printf("= %lf\n", number1 / number2);

break;

case '%':

if ((long)number1 == 0)

printf("\n\n\aDivision by zero error!\n");

else

printf("= %ld\n", (long)number1 % (long)number2);

break;

default:

printf("\n\n\aIllegal operation!\n");

break;

}

printf("Do you want to continue? (y or n): ");

scanf(" %c", &choice); // 注意:%c 前面的空格不能去掉

if (choice == 'y' || choice == 'Y')

goto begin;

return 0;

}

第4章 循环

习题4.1 编写一个程序,生成一个乘法表,其大小由用户输入来决定。例如,如果表的大小是4,该表就有4行4列。行和列标记为1~4.表中的每一个单元格都包含行列之积,因此第三行第4列的单元格包含12。

#include

int main()

{

int num_row = 0;

int num_col = 0;

printf("请输入行数和列数:");

scanf("%d%d", &num_row, &num_col);

printf(" ");

for (int i = 1; i <= num_col; i++)

{

printf("%3d ", i);

}

printf("\n");

for (int i = 1; i <= num_row; i++)

{

printf("%-4d", i);

for (int j = 1; j <= num_col; j++)

{

printf("%3d ", i * j);

}

printf("\n");

}

return 0;

}

习题4.2 编写一个程序,为0~127之间的字符码输出可打印的字符。输出每个字符码和它的符号,这两个字符占一行。列要对齐(提示:可以使用在ctype.h中声明的isgraph()函数,确定哪个字符是可以打印的)。

#include

#include

int main()

{

for (int i = 0; i <= 127; i++)

{

if (isgraph(i))

{

printf("%d %c\n", i, i);

}

}

return 0;

}

习题4.3 扩展上一题,给每个空白字符输出对应的名称,例如newline,space,tab等。

// 本题略

习题4.4 使用嵌套循环输出一个用星号绘制的盒子,与程序4.2类似,但是它的宽和高由用户输入。

#include

int main()

{

int height = 0;

int width = 0;

printf("请输入盒子的宽和高:");

scanf("%d%d", &width, &height);

for (int i = 1; i <= height; i++)

{

for (int j = 1; j <= width; j++)

{

if (i == 1 || i == height)

{

printf("*");

}

else

{

if (j == 1 || j == width)

{

printf("*");

}

else

{

printf(" ");

}

}

}

printf("\n");

}

return 0;

}

习题4.5 修改程序4.7的猜谜游戏,在玩家猜错数字后,可以用一个选项让玩家继续玩下去,且想玩多久就玩多久。

#include

#include

#include

#include

int main()

{

int chosen = 0;

int guess = 0;

int limit = 20;

char another_game = 'Y';

srand(time(NULL));

chosen = 1 + rand() % limit;

printf("\nThis is a guessing game."

"\nI have chosen a number between 1 and 20 which you must guess.\n");

do

{

printf("\nEnter a guess: ");

scanf("%d", &guess);

if (guess == chosen)

{

printf("\nYou guessed it!\n");

return 0;

}

if (guess < 1 || guess > 20)

{

printf("\nI said between 1 and 20.\n&

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值