2016년 6월 10일 금요일

이것이 c언어다. 18장 출판사관리

#include <stdio.h>
#include <string.h>

typedef struct
{
    char szBookName[100];
    float faPrice[10];
    int naCount[10];
    int naTotalPrice[10];
    long nPriceIndex;
}BOOK;

int getData(BOOK stList[10], FILE *fpSaved);
void printData(BOOK stList[10],int nSizeOfList);
void iniData(BOOK stList[10]);
void saveData(FILE *fpResult, BOOK stList[], int nSizeOfList);

int main(void)
{
    FILE *fpSaved, *fpResult;
    BOOK stList[10];
    int nSizeOfList;

    iniData(stList); //구조체 요소 초기화.
    fpSaved = fopen("a.txt","r");
    if(fpSaved == NULL)
    {
        printf("can't open 'a.txt'");
        return 1;
    }
    fpResult = fopen("b.txt","w");

    nSizeOfList = getData(stList,fpSaved);
    fclose(fpSaved);
    if(fpResult == NULL)
    {
        printf("can't open 'b.txt'");
        return 1;
    }
    saveData(fpResult, stList, nSizeOfList);

    fclose(fpResult);

    printData(stList,nSizeOfList);


    return 0;
}
void iniData(BOOK stList[10])
{
    int i,k;
    for(i=0;i<10;i++)
    {
        stList[i].nPriceIndex = 0;
        for(k=0;k<10;k++)
        {
            stList[i].faPrice[k] = 0.0;
            stList[i].naCount[k] = 0;
            stList[i].naTotalPrice[k] = 0;
        }
    }
}

int getData(BOOK stList[], FILE *fpSaved)
{
    char *res;
    int nIndex=0; //stList구조체배열의 index값
    int nPriceIndex = 0;
    char chTemp[100];

    float fTemp = 0;
    int nTemp = 0;
    int flag = 1;
    int i;
    char chSpace[100];

    while(1)
    {
        res = fgets(chTemp,sizeof(chTemp),fpSaved);
        if(res == NULL)break;
        chTemp[strlen(chTemp)-1] = '\0';
        fscanf(fpSaved,"%f",&fTemp);
        fscanf(fpSaved,"%d",&nTemp);

        fgets(chSpace,sizeof(chSpace),fpSaved);
        for(i=0;i<nIndex;i++) //중복검사  중복시 중복되는 구조체에 값들을 저장.
        {
            if(strcmp( chTemp,stList[i].szBookName) == 0)
            {
                nPriceIndex = stList[i].nPriceIndex; // 맴버변수indxe를 구조체 안의index로 초기화
                nPriceIndex++;
                stList[i].faPrice[nPriceIndex] = fTemp;
                stList[i].naCount[nPriceIndex] = nTemp;
                stList[i].naTotalPrice[nPriceIndex] = fTemp * 10000 * nTemp;

                stList[i].nPriceIndex = nPriceIndex; // 구조체안의 index를 새로은 맴버변수로 초기화
                flag = 0;

                break;

            }
        }
        if (flag == 1)
        {
            nPriceIndex = stList[nIndex].nPriceIndex;
            strcpy(stList[nIndex].szBookName,chTemp);
            stList[nIndex].faPrice[0] = fTemp;
            stList[nIndex].naCount[0] = nTemp;
            stList[nIndex].naTotalPrice[0] = fTemp * 10000 * nTemp;

            nIndex++;
        }
        flag = 1;
    }
    return nIndex;
}
void printData(BOOK stList[10], int nSizeOfList)
{
    int i,k;
    int nPriceIndex;

    for(i=0;i<nSizeOfList;i++)
    {
        nPriceIndex = stList[i].nPriceIndex;
        printf("%s\n",stList[i].szBookName);
        for(k=0;k<nPriceIndex+1;k++) //nPriceIndex의 시작이 0이기 때문에 1을 더해서 출력해줌.
        {
            printf("%f\t",stList[i].faPrice[k]);
            printf("%d\t",stList[i].naCount[k]);
            printf("%d\n\n",stList[i].naTotalPrice[k]);
        }
    }

}
void saveData(FILE *fpResult, BOOK stList[10], int nSizeOfList)
{
    int i,k;
    int nTotalPrice = 0;
    int nTotalCount = 0;

    for(i=0;i<nSizeOfList;i++)
    {
        fputs(stList[i].szBookName,fpResult);
        for(k=0;k<stList[i].nPriceIndex+1;k++)
        {
            nTotalPrice += stList[i].naTotalPrice[k];
            nTotalCount += stList[i].naCount[k];
        }
        fprintf(fpResult,"\n%d갯수, %d 원을 팜.\n",nTotalCount, nTotalPrice);
        nTotalPrice = 0;
        nTotalCount = 0;
    }
}


댓글 없음:

댓글 쓰기