2014-09-20 70 views
0

美好的一天!在我爲學校寫的一個節目中,我們必須製作一個收銀機類型的節目,似乎很簡單,但對於我的生活,我無法讓它工作。在購買產品的數量後,所有的價格,程序必須要求現金支付,然後退還變更。但是變更必須以返還的金額(或者1美元的賬單)給出,然後纔是剩餘的美分。幫幫我?我已經得到了一些拉丁工作(有點),但我不知道如何去做這些改變。收銀機程序 - C

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    int itemNum, justChange; 
    double prodPrice, tax, cashGiven, change, purchasePrice, changeGiven, changeBack, cashBack; 
    float totalPrice; 

    //Num of items 
    printf ("Number of items: "); 
    scanf("%d", &itemNum); 

    //Price of items 
    printf("Please enter price of items: "); 
    scanf("%lf", &prodPrice); 

    //Math Stuff 
    purchasePrice = itemNum*prodPrice; 
    tax = purchasePrice * 0.13; 
    totalPrice = purchasePrice*1.13; 

    //find change alone 
    //justChange = totalPrice 


    //Price Output 
    printf("Purchase price is: %.2lf \n",purchasePrice); 
    printf("TAX (HST 13%):  %.2lf\n",tax); 
    printf("Total price is: %.2lf \n",totalPrice); 



    printf("Please Enter Cash: "); 
    scanf("%lf", &cashGiven); 

    printf("Please Enter Change: "); 
    scanf("%lf", &changeGiven); 

    //MAth stuuff again 

    double endCash; 
    double loony; 
    int yoloswag; 
    endCash = cashGiven - totalPrice; 
    loony = endCash/1; 
    loony = loony--; 

    if (loony<0) 
     printf ("Loonies: 0"); 
    else 
    printf("Loonies: %.0lf \n",loony); 

    printf("change: %d ", totalPrice-floor(totalPrice)); 

    return 0; 
} 
+0

對於某些特定的輸入,* expected *輸出和* actual *輸出是什麼? – 2014-09-20 14:40:24

+0

@JoachimPileborg輸入現金支付,然後輸出變回一個1的金額返回,然後在剩餘的美分變化。我沒有輸出錯誤,我只是不確定如何顯示剩餘的變化 – Andrew 2014-09-20 14:42:39

回答

1

用可能的改變值創建一個數組;

double cashValues[6] = {1, 0.5, 0.2, 0.1, 0.05, 0.01}; 

然後創建一個for循環,您可以嘗試從差值中減去可能的變化值,直到差值爲零。

double difference; 
difference = cashGiven - totalPrice; 
while (difference != 0) { 
    for(int i=0; i<6; i++) { 
     if(cashValues[i] <= difference) { 
       difference -= cashValues[i]; 
       printf("%f \n", cashValues[i]); 
       i=0; 
     } 
    } 
}