|登录 |注册

查看: 3065|回复: 17
打印 上一主题 下一主题

c programming problem

[复制链接]
bmw
2009-8-18 06:38 PM
example
如果我要把 45.66 变成 45.65
或者是我要把 45.12 变成 45.10 然后我应该怎样做
它有点像rounding mechanism

问题是我应该使用什么东西来让它进位或退位(只要decimal进位或退位,integer没有变)?
应该不能使用ceil or floor 吧???
Super-Tomato
2009-8-18 06:43 PM
原帖由 bmw 于 2009-8-18 06:38 PM 发表
example
如果我要把 45.66 变成 45.65
或者是我要把 45.12 变成 45.10 然后我应该怎样做
它有点像rounding mechanism

问题是我应该使用什么东西来让它进位或退位(只要decimal进位或退位,integer没有变)? ...



你是指馬來西亞的小數點計算還是正常的進位運算??
goodday
2009-8-18 06:45 PM

你问的很模糊
小数点的 因该只有 decimal double 来处理吧?

45.66 - 0.01 = 45.65
ceil or floor 是 round up 吧??
4舍5入

回复 #2 Super-Tomato 的帖子

bmw
2009-8-18 07:05 PM
馬來西亞的小數點計算
bmw
2009-8-18 07:08 PM
decimal double 是什么??
Super-Tomato
2009-8-18 07:53 PM
原帖由 bmw 于 2009-8-18 07:05 PM 发表
馬來西亞的小數點計算



如果是馬來西亞的計算方式, 你就要自己寫個 function, 判斷小數點之後的小數是否達到要求而進位, 至於 function 內容怎麼寫就要靠你自己寫了
有甚麼問題的時候才把你的 coding 貼出來, 到時自然會有人指導你錯誤的地方
bmw
2009-8-19 08:45 PM
#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
为甚么这个可以而上面那个却不可以??
是不是我做错什么了??

回复 #7 bmw 的帖子

goodhermit95
2009-8-19 10:15 PM
你你你你你
那么多个 if elseif elseif
用switch case

switch(number)
{
case(0) //也就是 number = 0,要做什么东西
{
//写在这里
}
case(1)
{
}
}
returntotalroundedprice; /这个写在外面啊,为什么写那么多个在里面?
Super-Tomato
2009-8-19 10:58 PM
原帖由 bmw 于 2009-8-19 08:45 PM 发表
#include
#include
void input(double[]);
double calculate(double[]);
double roundingTotalPrice(double);
double productPrice[10],totalPrice,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
为甚么这个可以而上面那个却不可以??
是不是我做错什么了??


你的 coding 大致上看是沒錯, 但紅色的部分怎麼只是 1.22??? 我想應該是 10.22 吧
而你的進位只有 10.21 大概是編譯器的小數點計算問題, 有些編譯器會有這點不精準的問題
Super-Tomato
2009-8-19 11:05 PM
原帖由 bmw 于 2009-8-19 08:45 PM 发表
#include
#include
void input(double[]);
double calculate(double[]);
double roundingTotalPrice(double);
double productPrice[10],totalPrice,totalRoundedPrice;


void main () {


  inpu ...



剛用 GCC 編譯器和 VC++ .NET 測試過了, 後者有你這樣的問題, 而前者則沒問題
GCC 所編譯出來的執行檔
您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2026-1-1 06:53 AM , Processed in 0.089465 second(s), 24 queries .

Powered by Discuz! X2.5 © 2001-2012 Comsenz Inc.

本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved

Dedicated Server powered by iCore Technology Sdn. Bhd.

合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部