2016년 6월 9일 목요일

이것이 c언어다. 17장 저금통

동전 단위와, 갯수를 입력받아 총액을 출력한다.
#include <stdio.h>

typedef struct {
    int w500;
    int w100;
    int w50;
    int w10;
}MoneyBox;
void init(MoneyBox *s);
void save(MoneyBox *s, int unit, int cnt);
int total(MoneyBox *s);

int main(int argc, char *argv[])
{
    MoneyBox stMB;
    MoneyBox *pStMB = &stMB;

    int nTotalPrice = 0;
    int nCoin = 0;
    int nCount = 0;
    init(pStMB);
    while(nCoin != -1)
    {
        printf("coin's price and count (EXIT : -1).");
        scanf("%d",&nCoin);
        if(nCoin == -1)
            break;
        scanf("%d",&nCount);
        save(pStMB,nCoin,nCount);
    }
    nTotalPrice = total(pStMB);
    printf("total price : %d\n",nTotalPrice);
    printf("500 : %d, 100 : %d, 50 : %d, 10 : %d\n\n",pStMB->w500*500,pStMB->w100*100, pStMB->w50*50, pStMB->w10*10);
    return 0;
}
void init(MoneyBox *pStMB)
{
    pStMB->w500 = 0;
    pStMB->w100 = 0;
    pStMB->w50 = 0;
    pStMB->w10 = 0;
}
void save (MoneyBox *pStMB, int unit, int cnt)
{
    switch(unit)
    {
        case 500 : pStMB->w500 = pStMB->w500 + cnt;break;
    case 100 : pStMB->w100 = pStMB->w100 + cnt;break;
    case 50 : pStMB->w50 = pStMB->w50 + cnt;break;
    case 10 : pStMB->w10 = pStMB->w10 + cnt;break;
    }
}
int total(MoneyBox *pStMB)
{
    int m_total;
    m_total = pStMB->w500*500 + pStMB->w100*100 + pStMB->w50*50 + pStMB->w10*10;

    return m_total;
}


댓글 없음:

댓글 쓰기