练习解析与修正
1. 英寸转码、英尺、英寸
原代码:
#include <stdio.h>
int main(void)
{
printf("请输入英寸的值:");
int inch = 0;
scanf("%d",&inch);
printf("\n英寸:%d",inch);
printf("\n英尺:%lf",inch/12.0);
printf("\n码:%lf",inch/12.0/3.0);
return 0;
}Code language: PHP (php)
问题:
- 题目要求输出”码、英尺、和英寸的形式”,但代码只输出了小数形式的英尺和码,没有按整数分解。
- 应该用整数除法和取余来分解:总英寸 ÷ 12 = 总英尺,总英尺 ÷ 3 = 码,余数分别是英尺和英寸。
修正版本:
#include <stdio.h>
int main(void)
{
printf("请输入英寸的值:");
int total_inches = 0;
scanf("%d", &total_inches);
int yards = total_inches / 36; // 1码=36英寸
int remaining_inches = total_inches % 36;
int feet = remaining_inches / 12; // 剩余部分转英尺
int inches = remaining_inches % 12; // 最后剩余的英寸
printf("\n%d码 %d英尺 %d英寸\n", yards, feet, inches);
return 0;
}Code language: PHP (php)
2. 房间面积计算(平方码)
原代码:
#include <stdio.h>
int main(void)
{
printf("请输入房间的长(英寸):");
int inch = 0;
scanf("%d",&inch);
printf("\n请输入房间的宽(英尺):");
int ychi = 0;
scanf("%d",&ychi);
printf("\n房间的面积是%.2lf",(inch/12.0/3.0)*(ychi/3.0));
return 0;
}Code language: PHP (php)
问题:
- 变量命名混乱(
ychi实际是英尺),且提示语说”宽(英尺)”但变量名像”英寸”。 - 逻辑正确但可读性差,且未验证输入。
修正版本:
#include <stdio.h>
int main(void)
{
printf("请输入房间的长(英尺):");
double length_ft = 0.0;
scanf("%lf", &length_ft);
printf("请输入房间的宽(英尺):");
double width_ft = 0.0;
scanf("%lf", &width_ft);
double area_sq_yards = (length_ft / 3.0) * (width_ft / 3.0);
printf("\n房间的面积是%.2lf平方码\n", area_sq_yards);
return 0;
}Code language: PHP (php)
注意:题目说”用英尺和英寸输入”,但原代码只用了一种单位。若需分别输入英尺和英寸,可改为:
// 长:英尺 + 英寸
int length_ft, length_in;
printf("请输入长(英尺 英寸):");
scanf("%d %d", &length_ft, &length_in);
double total_length_ft = length_ft + length_in / 12.0;Code language: JavaScript (javascript)
3. 产品版本与价格计算
题目:标准版 3.5,豪华版5.5,输入版本和数量,计算总价。
暂放说明:因未学判断语句,可用数值映射(1=标准,2=豪华)配合算术技巧:
#include <stdio.h>
int main(void)
{
printf("请选择版本(1=标准版 $3.5,2=豪华版 $5.5):");
int version = 0;
scanf("%d", &version);
printf("请输入购买数量:");
int quantity = 0;
scanf("%d", &quantity);
// version=1 → price=3.5; version=2 → price=5.5
// 用算术映射:price = 3.5 + (version-1) * 2.0
double price = 3.5 + (version - 1) * 2.0;
double total = price * quantity;
printf("\n单价:$%.2f,总价:$%.2f\n", price, total);
return 0;
}Code language: PHP (php)
4. 时薪计算(美元+美分格式输出)
原代码:
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("请输入一个星期的薪水(美金):");
double salary = 0.0;
scanf("%lf",&salary);
printf("\n请输入工作的小时数:");
double hours = 0;
scanf("%lf",&hours);
int dolar = floor(salary/hours);
printf("\n你每个小时的薪水是%d美元%.0lf美分",dolar,(salary/hours-dolar)*100);
return 0;
}Code language: PHP (php)
问题:
floor(salary/hours)正确取整数部分,但(salary/hours - dolar) * 100可能因浮点误差输出49.999999被%.0lf截断为49而非50。- 建议用
round()或调整计算方式。
修正版本:
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("请输入一个星期的薪水(美金):");
double salary = 0.0;
scanf("%lf", &salary);
printf("\n请输入工作的小时数:");
double hours = 0;
scanf("%lf", &hours);
double hourly_rate = salary / hours;
int dollars = (int)hourly_rate; // 整数美元部分
int cents = (int)round((hourly_rate - dollars) * 100); // 美分部分(四舍五入)
// 处理进位(如 0.995 → 99.5 → 100美分)
if (cents >= 100) {
dollars += cents / 100;
cents = cents % 100;
}
printf("\n你每个小时的薪水是%d美元%d美分\n", dollars, cents);
return 0;
}Code language: PHP (php)
简化版(避免浮点误差):
#include <stdio.h>
int main(void)
{
double salary, hours;
printf("请输入一个星期的薪水(美金):");
scanf("%lf", &salary);
printf("请输入工作的小时数:");
scanf("%lf", &hours);
int total_cents = (int)round(salary * 100); // 总薪水转美分
double hourly_rate = total_cents / hours / 100.0;
int h_dollars = (int)hourly_rate;
int h_cents = (int)round((hourly_rate - h_dollars) * 100);
printf("\n你每个小时的薪水是%d美元%d美分\n", h_dollars, h_cents);
return 0;
}Code language: PHP (php)