这一部分编程练习题可以学到箱子空间重量的计算方法,以及华氏温度转换为摄氏温度的方法。
箱子的空间重量,是相对于箱子的实际重量的一个概念。
/* Computes the dimensional weight */
#include
int main(void)
{
int height, length, width, volume, weight;
height = 8;
length = 12;
width = 10;
volume = height * length * width;
weight = (volume + 165) / 166;
printf("Volume (cubic inches): %d\n", volume);
printf("Dimensional weight (pounds): %d\n", weight);
return 0;
}
计算箱子的空间重量代码的改进版本中,用户能够自己输入箱子的长、宽、高。
/* Computes the dimensional weight of a
box from input provided by the user
*/
#include
int main(void)
{
int height, length, width, volume, weight, ret;
printf("Enter height of box: ");
ret = scanf("%d", &height);
printf("Enter length of box: ");
ret = scanf("%d", &length);
printf("Enter width of box: ");
ret = scanf("%d", &width);
volume = height * length * width;
weight = (volume + 165) / 166;
printf("Volume (cubic inches): %d\n", volume);
printf("Dimensional weight (pounds): %d\n", weight);
return 0;
}
通过定义常量的名字,使代码更容易理解。
/* Convert a Fahrenheit temperature to Celsius */
#include
#define FREEZING_PT 32.0f
#define SCALE_FACTOR (5.0f / 9.0f)
int main(void)
{
float fahrenheit, celsius;
int ret;
printf("Enter Fahrenheit temperature: ");
ret = scanf("%f", &fahrenheit);
celsius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;
printf("Celsius equivalent: %.1f\n", celsius);
return 0;
}
这一部分的练习能够增加对C语言基本概念的认识,提高使用C语言解决现实问题的能力。
多次使用printf可以显示比较复杂的图形。
/* 显示图形 */
#include
int main(void)
{
printf(" *\n");
printf(" *\n");
printf(" *\n");
printf("* *\n");
printf(" * *\n");
printf(" *\n");
return 0;
}
编写代码,通过公式,实现球体体积的计算。
/* 计算球体体积 */
#include
#define PI 3.1415926
int main(void)
{
int radius;
float volume;
radius = 10;
volume = (4.0f / 3.0f) * PI * radius * radius * radius;
printf("%.2f\n", volume);
return 0;
}
改进版代码中,用户可以自己输入球体的半径。
/* 计算球体体积(改进版) */
#include
#define PI 3.1415926
int main(void)
{
int radius, ret;
float volume;
printf("Enter radius of sphere: ");
ret = scanf("%d", &radius);
volume = (4.0f / 3.0f) * PI * radius * radius * radius;
printf("Volume (cubic meters): %.2f\n", volume);
return 0;
}
对于给定的美元数量,增加税率,显示增加税率后的美元数量。
#include
int main(void)
{
float amount;
int ret;
printf("Enter an amount: ");
ret = scanf("%f", &amount);
amount = amount + amount * 0.05f;
printf("With tax added: $%.2f\n", amount);
return 0;
}
通过多项式,逐项计算多项式的值。
/* 多项式求值 */
#include
int main(void)
{
int x, y, ret;
printf("Enter x: ");
ret = scanf("%d", &x);
y = 3 * x * x * x * x * x
+ 2 * x * x * x * x
- 5 * x * x * x
- x * x
+ 7 * x
- 6;
printf("Result: %d\n", y);
return 0;
}
改进版代码中,使用了Horner法则进行多项式的求值,减少了乘法次数。
/* Horner法则求多项式的值 */
#include
int main(void)
{
int x, y, ret;
printf("Enter x: ");
ret = scanf("%d", &x);
y = ((((3 * x + 2) * x - 5) * x - 1) * x + 7) * x - 6;
printf("Result: %d\n", y);
return 0;
}
给定一个金额,代码给出所给金额的最少支付张数。
#include
int main(void)
{
int amount, ret;
int bills_20, bills_10, bills_5, bills_1;
printf("Enter a dollar amount: ");
ret = scanf("%d", &amount);
bills_20 = amount / 20;
amount = amount - 20 * bills_20;
bills_10 = amount / 10;
amount = amount - 10 * bills_10;
bills_5 = amount / 5;
amount = amount - 5 * bills_5;
bills_1 = amount / 1;
printf("\n");
printf("$20 bills: %d\n", bills_20);
printf("$10 bills: %d\n", bills_10);
printf(" $5 bills: %d\n", bills_5);
printf(" $1 bills: %d\n", bills_1);
return 0;
}
给出每次还款之后的剩余还款金额。
#include
int main(void)
{
float loan, rate, payment;
int ret;
printf("Enter amount of loan: ");
ret = scanf("%f", &loan);
printf("Enter interest rate: ");
ret = scanf("%f", &rate);
printf("Enter monthly payment: ");
ret = scanf("%f", &payment);
printf("\n");
loan = (loan - payment) + loan * (rate / 100 / 12);
printf("Balance remaining after first payment: $%.2f\n", loan);
loan = (loan - payment) + loan * (rate / 100 / 12);
printf("Balance remaining after second payment: $%.2f\n", loan);
loan = (loan - payment) + loan * (rate / 100 / 12);
printf("Balance remaining after third payment: $%.2f\n", loan);
return 0;
}