본문 바로가기
C 언어

C언어로 만든 간단한 개인프로젝트

by treeCoder 2021. 9. 18.

C언어로 만든 간단한 개인프로젝트이다. 이런 프로그램을 다시 쓸 일이 있나 싶다.

이 것만 보면 무슨 프로그램일 까 도저히 알 수가 없을 것 같아서, 사진을 첨부 한다.

은행에서 송금내역을 엑셀로 다운받아서, 그 내용을 Copy하여 메모장에 붙여 넣어서 텍스트 파일로 저장한다.

그중에서 송금 받은 내역만 정리한다.

 

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

char *my_substr(char *str, int s,int e, char new[]) {

    strncpy(new, str + s, e - s + 1);
    new[e - s + 1] = 0;

    return new;
}

int main(int argc, char *argv[]) {

    int i;
    int amount; 
    int reportCount = 0;
    int lineCnt = 0;
    int monthTotal = 0;
    int yearTotal = 0;
    
    bool monthTotalWrite = false; 
    char *p;
    char buf[128];
    FILE *f;

    if( argc != 2 ) {
        puts("\n  This program sums up deposits from NH BANK data file in text format.\n");
	puts("\n  Usage is as follows:");
        printf("\n\t%s <FileName of NH Bank data>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    f = fopen(argv[1], "r"); // file encoding must be utf-8 to search Hangul string with strstr
    if( f == NULL ) {
        perror(argv[1]);
        exit(EXIT_FAILURE);
    }

    system("chcp 65001"); // to show Hangul right
    system("cls"); // clear screen

    char sav[10] = { 0 };
    char yearMonth[10] = { 0 };

    while( fgets(buf, sizeof(buf), f) != NULL ) {
        if( ++lineCnt == 1 )            continue; // skip the heading

        if( strstr(buf, ":" ) != NULL ) continue; // skip the line that has colon, for example 12:48:16, it's just time data

        if( strstr(buf, "엑*" )  ) continue;
        if( strstr(buf, "심*" )  ) continue;
        if( strstr(buf, "온*인" )  ) continue;
        if( strstr(buf, "가*" ) ) continue;
        if( strstr(buf, "서**" ) ) continue;
        if( strstr(buf, "박**" )  ) continue;
        if( strstr(buf, "박**" )  ) continue;
        if( strstr(buf, "지*****" )  ) continue;
        if( strstr(buf, "현***" )  ) continue;
        if( strstr(buf, "마******" )  ) continue;
        if( strstr(buf, "농*" )  ) continue;
        if( strstr(buf, "교**" )  ) continue;
        if( strstr(buf, "야****" )  ) continue;
        if( strstr(buf, "졸***" )  ) continue;
        if( strstr(buf, "환*" )  ) continue;

        if( strstr(buf, "현*1079304*" )  ) continue;
        if( strstr(buf, "B*7155610*" )  ) continue;
        if( strstr(buf, "롯*5614303*" )  ) continue;
        if( strstr(buf, "착***" )  ) continue;

        if( buf[10] == '\t' && isdigit(buf[11]) )  continue; //if tab and number it's a drawing transaction, skip drawing records
                                                             //we are only talking about deposits.

        //*************************** Here we go ***************************
        buf[strcspn(buf, "\n")] = '\0'; //remove new line char
       
        
        p = strtok(buf, "\t"); // each data field is separated by tabs.
        for(i = 1; p != NULL ; i++) { 
            if( i == 1 ) { // If it's date ***************************
               my_substr(p, 0, 6, yearMonth);
               if(  strcmp(yearMonth, sav) != 0  ) { // if not same, print year and month
                    if( monthTotalWrite ) { // if true, print  monthTotal (skips the 1st time when the program starts.)
                         printf("\n ** %s Monthly Total: %10d\n", sav, monthTotal ); 
                         monthTotal = 0;
                    }
                    printf("\n%s\n", yearMonth );
                    monthTotalWrite = true;
               }
               printf("%3d, %s, ", ++reportCount, p); 
               my_substr(p, 0, 6, sav);
         
            }
            if( i == 2 ) { // If it's amount ***************************
                amount = strtol(p, NULL, 10);
                printf("%10d, ", amount ); 
                monthTotal += amount;
                yearTotal += amount;
            }
            if( i == 5 ) { // If it's name ***************************
                printf("%s", p); 
            }
            p = strtok(NULL, "\t");
        }
        printf("\n", p);  
    }
    printf("\n ** %s Monthly Total: %10d\n", sav, monthTotal );
    printf("\n *** Yearly Total: %10d\n", yearTotal ); 

    fclose(f);
    return 0;
}

댓글