想请问一道题中的两个难处 多谢
想请教一道题中的两个难处 谢谢!
编写一个程序,提示用户从键盘输入一个星期的薪水(以美元为单位)和工作时数,它们均为浮点数,然后计算并输出每小时的平均薪水,输出格式为:Your average hourly pay rate is 7 dollars and 54 cents.
#include <stdio.h>
int main(void)
{
double pay = 0.0; /* Weekly pay in dollars */
double hours = 0.0; /* hours worked */
int dollars = 0; /* Hourly rate - dollars */
int cents = 0; /* ... and cents */
/* Get the Weekly pay */
printf("Enter your weekly pay in dollars: ");
scanf("%lf", &pay);
/* Get the order quantity */
printf("Enter the hours worked: ");
scanf("%lf", &hours);
/* Calculate the average hourly rate - dollars first */
dollars = (int)(pay/hours);--------—————————————————这里加这个(int)是什么意思?
/* to get the cents we can subtract the dollars from the hourly rate */
/* and multiply by 100 to get cents. If we then add 0.5 and convert the result */
/* back to an integer, it will be to the nearest cent. */
cents = (int)(100.0*(pay/hours - dollars) +0.5);------------------------------这里加的这个(int)和+0.5是什么意思 /* Output the average hourly rate */
printf("Your average hourly pay rate is %d dollars and %d cents.\n", dollars, cents);
return 0;
}
注释我没看懂
------解决方案--------------------
(int)是强制类型转换,至于+0.5,应该是为了让100.0*(pay/hours - dollars)按四舍五入省略小数部分
------解决方案--------------------
同楼上
pay/hours的值是double的,赋值给int型需要类型转换啊,就是取整
dollars = pay/hours也可以,这是隐式转换,因为会导致精度损失,所以大多数编译器会给出警告
以下两种显示类型转换则没有警告:
(int)(pay/hours)这个是旧式强制类型转换,这种转换可视性比较差,难以跟踪错误的转换
还可以使用static_cast<int>(pay/hours),虽然比较麻烦,但是可视性好
+0.5 是为了四舍五入
比如100.0*(pay/hours - dollars)的值为5.5
如果不加0.5,转换的值就是5,加了0.5,得到的结果就是6了
------解决方案--------------------
(int)是强制把浮点型小数转换成整数
+0.5是四舍五入成整数~~
这种问题自己单步调试下就能知道什么意思的~
------解决方案--------------------
其实四舍五入可以用math.h里面的函数。
------解决方案--------------------
1, 向下取整.
2, 四舍五入.
编写一个程序,提示用户从键盘输入一个星期的薪水(以美元为单位)和工作时数,它们均为浮点数,然后计算并输出每小时的平均薪水,输出格式为:Your average hourly pay rate is 7 dollars and 54 cents.
#include <stdio.h>
int main(void)
{
double pay = 0.0; /* Weekly pay in dollars */
double hours = 0.0; /* hours worked */
int dollars = 0; /* Hourly rate - dollars */
int cents = 0; /* ... and cents */
/* Get the Weekly pay */
printf("Enter your weekly pay in dollars: ");
scanf("%lf", &pay);
/* Get the order quantity */
printf("Enter the hours worked: ");
scanf("%lf", &hours);
/* Calculate the average hourly rate - dollars first */
dollars = (int)(pay/hours);--------—————————————————这里加这个(int)是什么意思?
/* to get the cents we can subtract the dollars from the hourly rate */
/* and multiply by 100 to get cents. If we then add 0.5 and convert the result */
/* back to an integer, it will be to the nearest cent. */
cents = (int)(100.0*(pay/hours - dollars) +0.5);------------------------------这里加的这个(int)和+0.5是什么意思 /* Output the average hourly rate */
printf("Your average hourly pay rate is %d dollars and %d cents.\n", dollars, cents);
return 0;
}
注释我没看懂
------解决方案--------------------
(int)是强制类型转换,至于+0.5,应该是为了让100.0*(pay/hours - dollars)按四舍五入省略小数部分
------解决方案--------------------
同楼上
pay/hours的值是double的,赋值给int型需要类型转换啊,就是取整
dollars = pay/hours也可以,这是隐式转换,因为会导致精度损失,所以大多数编译器会给出警告
以下两种显示类型转换则没有警告:
(int)(pay/hours)这个是旧式强制类型转换,这种转换可视性比较差,难以跟踪错误的转换
还可以使用static_cast<int>(pay/hours),虽然比较麻烦,但是可视性好
+0.5 是为了四舍五入
比如100.0*(pay/hours - dollars)的值为5.5
如果不加0.5,转换的值就是5,加了0.5,得到的结果就是6了
------解决方案--------------------
(int)是强制把浮点型小数转换成整数
+0.5是四舍五入成整数~~
这种问题自己单步调试下就能知道什么意思的~
------解决方案--------------------
其实四舍五入可以用math.h里面的函数。
------解决方案--------------------
1, 向下取整.
2, 四舍五入.