- 分享
- 0
- 人气
- 0
- 主题
- 15
- 帖子
- 849
- UID
- 16303
- 积分
- 7006
- 阅读权限
- 23
- 注册时间
- 2005-8-13
- 最后登录
- 2015-12-27
- 在线时间
- 3849 小时
|
#include <stdio.h>
#include <math.h>
void input(double[]);
double calculate(double[]);
double roundingTotalPrice(double);
double productPrice[10],totalPrice,totalRoundedPrice;
void main () {
input(productPrice);
totalPrice=calculate(productPrice);
printf("Total Price is %.2f",totalPrice);
totalRoundedPrice=roundingTotalPrice(totalPrice);
printf("\nTotal Rounded Price is %.2f",totalRoundedPrice);
}
void input(double productPrice[]){
int i;
for(i=0;i<10;i++){
scanf("%lf",&productPrice);
}
}
double calculate(double productPrice[]){
int i;
for(i=0;i<10;i++){
totalPrice =totalPrice + productPrice;
}
return totalPrice;
}
double roundingTotalPrice(double totalPrice){
double price;
int number;
price =(totalPrice*10)-(int)(totalPrice*10);
number = price *10;
if(number==0){
totalRoundedPrice=totalPrice;
return totalRoundedPrice;
}
else if(number==1){
totalRoundedPrice = totalPrice - 0.01;
return totalRoundedPrice;
}
else if(number == 2){
totalRoundedPrice = totalPrice - 0.02;
return totalRoundedPrice;
}
else if(number==3){
totalRoundedPrice = totalPrice + 0.02;
return totalRoundedPrice;
}
else if(number==4){
totalRoundedPrice = totalPrice + 0.01;
return totalRoundedPrice;
}
else if(number==5){
totalRoundedPrice = totalPrice;
return totalRoundedPrice;
}
else if(number==6){
totalRoundedPrice = totalPrice - 0.01;
return totalRoundedPrice;
}
else if(number==7){
totalRoundedPrice = totalPrice - 0.02;
return totalRoundedPrice;
}
else if(number==8){
totalRoundedPrice = totalPrice + 0.02;
return totalRoundedPrice;
}
else{
totalRoundedPrice = totalPrice + 0.01;
return totalRoundedPrice;
}
}
有点问题
example:we enter
1.22
1
1
1
1
1
1
1
1
1
total price= 1.22
total rounded price=1.21
another example
1.32
1
1
1
1
1
1
1
1
1
total price 10.32
total rounded price 10.30
为甚么这个可以而上面那个却不可以??
是不是我做错什么了?? |
|