2016년 6월 14일 화요일

이것이 c언어다. 17장 성적처리

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

typedef struct
{
    int stId;
    char name[10];
    int kor;
    int eng;
    int math;
    int total;
    float avg;
    enum{A=1,B,C,F}grade;
}STUDENTS;

void PrintStructure(STUDENTS *stList);
void SortStructure(STUDENTS *stList);
int CheckId(STUDENTS *stList,int tempId,int nNumber);
char GetGrade(float ar_avg);
int main(void)
{
    STUDENTS stList[5];
    int nNumber = 0;
    int tempId = 0;

    while(nNumber<5)
    {
        printf("student ID : ");
        scanf("%d",&tempId);
        fflush(stdin);              //버퍼에 남아있는 개행문자 제거


        if(nNumber == 0 || CheckId(stList,tempId,nNumber)) //Id검사에 통과되었을 경우 수행
        {
            stList[nNumber].stId = tempId;
        }
        else
        {
            printf("this ID can't use.");
            continue;
        }
        printf("name : ");
        gets(stList[nNumber].name);
        printf("kor, eng, math grade : (Ex. 99 99 99)");
        scanf("%d %d %d",&stList[nNumber].kor,&stList[nNumber].eng,&stList[nNumber].math);
        fflush(stdin);

        stList[nNumber].total = stList[nNumber].kor + stList[nNumber].eng + stList[nNumber].math;
        stList[nNumber].avg = (float)stList[nNumber].total/3.0;
        stList[nNumber].grade = GetGrade(stList[nNumber].avg);


        nNumber++;
    }

    printf("****Before Sort****\n");
    PrintStructure(stList);
    printf("****After Sort****\n");
    SortStructure(stList);
    PrintStructure(stList);
    return 0;
}
void PrintStructure(STUDENTS *stList)
{
    int i;
    for(i=0;i<5;i++)
    {
        printf("%d\t",stList[i].stId);
        printf("%s\t",stList[i].name);
        printf("%d\t",stList[i].kor);
        printf("%d\t",stList[i].eng);
        printf("%d\t",stList[i].math);
        printf("%d\t",stList[i].total);
        printf("%.1f\t",stList[i].avg);
        printf("%c\n",stList[i].grade);

    }
}
void SortStructure(STUDENTS *stList)
{
    int i,k;
    STUDENTS temp;


    for(i=0;i<5;i++)
    {
        for(k=0;k<4;k++)
        {
            if(stList[k].total < stList[k+1].total)
            {
                temp = stList[k];
                stList[k] = stList[k+1];
                stList[k+1] = temp;
            }
        }
    }
}
int CheckId(STUDENTS *stList,int tempId,int nNumber)
{
    int i;
    for(i=0;i<nNumber;i++)
    {
        if(stList[i].stId == tempId)
            return 0;
    }

    return 1;
}
char GetGrade(float ar_avg)
{
    int k = 0;

    k = (int)(ar_avg/10);
    switch(k)
    {
        case 10 :
        case 9 : return 'A'; break;
        case 8 : return 'B';break;
        case 7 : return 'C';break;
        default : return 'F';break;
    }//조금 이상한것이....'F'에 'D'를 넣어도 아무 이상없이 돌아감..
         //마음같아서는 enum에 있는것만 하게하고 없으면 오류를 띄어줫으면 좋겠는데 말이야..
}


2016년 6월 12일 일요일

이것이 c언어다. 14장 문자열 검색

//문자열을 입력하고, 문자열을 검색하여 중복을 확인함.
//근데..... szSavedVoca[0]에는  자꾸 이상한값이 들어가냐...;;
#include <stdio.h>
#include <string.h>
int main(void)
{
    char szSavedVoca[10][20] ;
    char pTempWord [20];
    int nCount = 0;
    int i;
    while(1)
    {
        printf("enter the Word (EXIT end) : ");
        gets(pTempWord);            //pTempWord  저장후 문자열 배열에 저장하는 이유는
                                    // 바로 문자열에 저장하게 되면 사용자가 10개 이상의 문자열입력시
                                    // 문자열 크기를 넘어가서 저장하게됨.
        if(strcmp(pTempWord, "end") == 0)
            break;
        else if(nCount >= 10)
            break;
        else
        {
            strcpy(szSavedVoca[nCount],pTempWord);
            for(i=0;i<nCount;i++)
                {
                    if(nCount == 0) break;
                    else if (strcmp(szSavedVoca[nCount],szSavedVoca[i]) == 0)
                    {
                        printf("this Word was entered. to %d\n",i+1);
                        nCount--;
                    }
                }
        }
        strcpy(pTempWord,""); //pTempWord 초기화.
        nCount++;
    }
    int flag = 1;
    printf("# total Words of %d are entered.\n",nCount);
    printf("enter the searching Word : ");
    while(strcmp(gets(pTempWord), "end") != 0)
    {
        for(i=0;i<=nCount;i++)
        {
            if (strcmp(szSavedVoca[i],pTempWord) == 0)
            {
                printf("this Word existed to %d\n",i+1);
                printf("enter the searching Word : ");
                flag = 0;
            }
        }
        if(flag == 1)
        {
            printf("this Word not exist\n");
            printf("enter the searching Word : ");
        }
        flag = 1;
    }
    return 0;
}


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;
    }
}


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;
}


이것이 c언어다. 17장 피트니스회원

회원별 몸무게와 이름, 회원번호를 저장하고 평균값과 최대값 구하기. 회원수는 제한 없음.

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


typedef struct
{
    int num;
    char name[20];
    double weight;
}Fitness;

double averageWeight(Fitness *pstList, int cnt);
int maxWeight(Fitness *pstList, int cnt);
void printInfo(Fitness *pstList, int mc);
void freeAty(Fitness *pstList, int cnt);

int main(void)
{
    int nSize = 2;
    static int sNIndex = 0;
    int nMaxCount = 0;
    double avg;

    Fitness stListTemp;               //임시list
    Fitness* pstList;       //동적할당 받을 list

    pstList = (Fitness*)malloc(sizeof(Fitness)*nSize);
    do
    {
        if(sNIndex  >= nSize)
        {
            nSize = nSize + 2;
            pstList = (Fitness*)realloc(pstList,nSize*sizeof(Fitness));
        }
        else
        {
            printf("User Number : %d\n",sNIndex+1);
            stListTemp.num = sNIndex;  
            fflush(stdin);
            printf("User Name : ");
            gets(stListTemp.name); 

            printf("enter the weight : ");
            scanf("%lf",&(stListTemp.weight));

            pstList[sNIndex] = stListTemp;   
            fflush(stdin);
            sNIndex++;
            printf("Anymore?(Y/N)");
        }
    }while(getchar() != 'N');

    printf("#total user : %d\n",sNIndex);

    avg = averageWeight(pstList,sNIndex);  
    printf("#average of weight : %.2f\n",avg);

    printf("the best pig!! : \n");
    nMaxCount = maxWeight(pstList,sNIndex);   
    printInfo(pstList,nMaxCount);


    return 0;
}

double averageWeight(Fitness *pstList, int cnt)
{
    int i;
    double total_weight = 0.0;
    for(i=0;i<=cnt;i++)
    {
        total_weight = total_weight + pstList[i].weight;
         //무게 총합 확인. 확인후 지우기
    }
    printf("total_weight : %.2f \n",total_weight);
    return total_weight / cnt;
}
int maxWeight(Fitness *pstList, int cnt)
{
    int i;
    int k = 0;
    double max = pstList[0].weight; //최대값 초기화
    for(i=1;i<cnt;i++)
    {
        if(max < pstList[i].weight)
        {
            max = pstList[i].weight;
            k = i;
        }
    }
    return k;
}
void printInfo(Fitness *pstList, int nMaxCount)
{
    printf("Number : %d\n",pstList[nMaxCount].num);
    printf("Name : %s\n",pstList[nMaxCount].name);
    printf("Weight : %.2lf\n",pstList[nMaxCount].weight);
}
void freeAty(Fitness *pstList, int sNIndex)
{
    int i;
    for(i=0;i<sNIndex;i++)
    {
        free(pstList);
    }
}


이것이 c언어다. 16장 끝말잇기

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

int compareLastChar(char *szSavedWord[100], char* szWordTemp,int nCount);
void printData(char *szSavedWord[100],int nCount);

int main(void)
{
    char *szSavedWord[100];
    char szWordTemp[80];
    int nCount = 0;
    int flag;

    int i;

    while(1)
    {
        printf("enter a Word (EXIT : end) : ");
        gets(szWordTemp);
        if(strcmp(szWordTemp,"end") == 0)break;

        if(nCount == 0)
        {
            szSavedWord[nCount] = (char*)malloc(sizeof(szWordTemp)+1);
            strcpy(szSavedWord[nCount],szWordTemp);
            nCount++;

        }
        else
        {
            flag = compareLastChar(szSavedWord,szWordTemp,nCount);
            if(flag == 0) // 끝말이 다르면
            {
                printf("this can't accept.\n");
            }
            else
            {
                szSavedWord[nCount] = (char*)malloc(sizeof(szWordTemp)+1);
                strcpy(szSavedWord[nCount],szWordTemp);

                for(i=0;i<nCount;i++)
                {
                    if ( strcmp(szSavedWord[i],szWordTemp) == 0) // 단어 중복검사
                    {
                        printf("this Word was entered.\n");
                        nCount--;
                    }
                }
                nCount++;
            }
        }
    }

    printData(szSavedWord,nCount);


    return 0;
}

int compareLastChar(char *szSavedWord[100], char* szWordTemp,int nCount)
{
    char chFirstch;
    char chLastch;
    int nLastWordSize;

    nLastWordSize = strlen(szSavedWord[nCount-1])-1;
    chFirstch = szWordTemp[0];
    chLastch = szSavedWord[nCount-1][nLastWordSize];

    if(chFirstch == chLastch)
        return 1;
    else
        return 0;

}
void printData(char *szSavedWord[100],int nCount)
{
    int i;
    for(i=0;i<nCount;i++)
    {
        printf("%s  ",szSavedWord[i]);
    }
    printf("\n");
}


이것이 c언어다. 16장 소수출력

사용자에게 정수를 입력받고, 정수의 -1까지의 소수들을 출력. 소수가 아닌수는 X로 표시

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

void saveData(int* nInteger, int nMax);
void printData (int* nInteger, int nMax);

int main(void)
{
    int *nInteger;
    int nMax;

    printf("enter the unsigned Integer : ");
    scanf("%d",&nMax);

    nInteger = (int *)calloc(nMax,sizeof(int));

    saveData(nInteger,nMax);
    printData(nInteger,nMax);

    return 0;
}
void saveData(int* nInteger, int nMax)
{
    int i,k;
    for ( i=0;i<nMax;i++)
    {
        nInteger[i] = 0;

    }
    for(i=2;i<nMax;i++)
    {
        for(k=2;k<i;k++)
        {
            if(i%k == 0) break;
        }
        if(i == k)
        {
            nInteger[i-2] = i; // i가 2부터 시작하기 때문에, i-2
        }
    }
}
void printData(int* nInteger, int nMax)
{
    int i;
    for(i=0;i<nMax;i++)
    {
        if(nInteger[i] == 0)
        {
        printf("X\t");
        }
        else
        {
            printf("%d\t",nInteger[i]);
        }

        if((i+1)%5 == 0) printf("\n");

    }
}