brunch

매거진 Sinclair

You can make anything
by writing

C.S.Lewis

by Sinclair Feb 10. 2016

제어문과 반복문

그리고 프로그램은 계속된다 V



fgets4()를 사용하여 정수형 입력 받는 함수 작성 i: 리턴하기  


#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>  

/*

* copyleft (l) 2006 - 2017 programmed by Sinclair

*/   

#define MAX_DIGIT 8 // 매크로 상수..  


int onlyInt0() {  

    char digit[MAX_DIGIT] = {0} ;

    register tag , i ;

    int size ;

    for (tag = 0 ; tag < 5 ; ++tag) {

        if( fgets4(digit , sizeof digit) ) {

            // 잘못된 입력..

            puts("입력이 잘못되었습니다.. 다시 한 번 입력해 주시와여..") ;

            continue ;

        } // end if

        if( !(size = strlen(digit)) ) {

            return 0X7FFFFFFF ; // 아싸 엔터~

        } // end if

        if( !( digit[0] ^ '+' && digit[0] ^ '-' ) ) {

              // 부호가 있는 경우

            if (!( size ^ 1)) {

                puts("부호만 입력하면 나보고 어쩌라고 잉?") ;

                continue ;

            } // end inner if

            i = 1 ;

        } // end outer if

        else // 부호가 없는 경우

            i = 0 ;  

        for ( ; i < size ; ++i ) {

            if (!isdigit(digit[i])) //<- <ctype.h>

                break ;

        } // end for  

        if( size ^ i ) { // 중간에 문자가 들어있다..

            puts("아쒸.. 문자를 입력하면 워쩌라구..") ;

            continue ;

        } // end if

        break ;

    } // end for  

    if( !(tag ^ 5) ) {

        puts("누굴 아주 쥑일 셈이더냐?") ;

        return 0XFFFFFFFF ;

    } // end if  

    return atoi(digit) ; //<- <stdlib.h>  

} // end onlyInt0()    




fgets4()를 사용하여 정수형 입력 받는 함수 작성 ii: 인자로 넘기기  


#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>  

/*

* copyleft (l) 2006 - 2017 programmed by Sinclair

*/   

#define MAX_DIGIT 8 // 매크로 상수..  

// 저는 scanf("%d", &data ) ; 와 같은 형태로 사용하고 싶었습니다.

// 당연히 주소 값을 넣으면 되겠죠?  


int onlyInt(int * data) {  

    char digit[MAX_DIGIT] = {0} ;

    register int tag , i ;

    int size ;

    tag = 0 ;

    while(-500) {

        if( !(tag++ ^ 5) ) {

            puts("누굴 아주 쥑일 셈이더냐?") ;

            return -1 ;

        } // end if


        if( fgets4(digit , sizeof digit) ) {

            // 잘못된 입력..

            puts("입력이 잘못되었습니다.. 다시 한번 입력해 주시와여..") ;

            continue ;

        } // end if

        if( !(size = strlen(digit)) ) {

            return 1 ; // 아싸 엔터~

        } // end if

        if( !( digit[0] ^ '+' && digit[0] ^ '-' ) ) {

             // 부호가 있는 경우  

            if (!( size ^ 1)) {

                puts("부호만 입력하면 나보고 어쩌라고 잉?") ;

                continue ;

            } // end inner if

            i = 1 ;

        } // end outer if

        else // 부호가 없는 경우

            i = 0 ;  


        for ( ; i < size ; ++i ) {

            if (!isdigit(digit[i])) //<- <ctype.h>

                break ;

        } // end for  

        if( size ^ i ) { // 중간에 문자가 들어있다..

            puts("아쒸.. 문자를 입력하면 워쩌라구..") ;

            continue ;

        } // end if

        break ;

    } // end while  

    *data = atoi(digit) ; //<- <stdlib.h>

    return 0 ;

} // end onlyInt()    




이차방정식의 근을 구하는 프로그램 ii: olnyInt()사용  


#include <stdio.h>

#include <ctype.h>

#include <math.h>  

/*

* copyleft (l) 2006 - 2017 programmed by Sinclair

*/   


// 정수형 만을 입력할 함수 input logic

extern int onlyInt( int * ) ;

// 입력 로직을 바꿨다고 나중에 다른 부분에 영향을 주지 않습니다.  


// 결과를 출력할 함수 presentation logic

int printResult( int , double , double ) ;  


// 근을 구하는 함수 business logic

int getResult(int , int , int ) ;  


int main() {  

    int aValue , bValue , cValue ;

    register int tag ;  

    puts("정수형 세 개를 입력하시면 이차방정식의 근을 구합니다.") ;  

    tag = 0 ;

    while(1004) {

        if( tag ++ == 5 ) return -4 ;

        if( onlyInt( &aValue ) ) {

            puts("프로그램을 종료합니다.") ;

            return -1 ;

        } // end if

        if( aValue ) break ;

        puts("a값을 0으로 입력하면 계산할 수 없습니다. 다시 입력하세요.") ;

    } // end while  

    if( onlyInt( &bValue ) ) {

        puts("프로그램을 종료합니다.") ;

        return -2 ;

    } // end if

    if( onlyInt( &cValue ) ) {

        puts("프로그램을 종료합니다.") ;

        return -3 ;

    } // end if  

    getResult( aValue , bValue , cValue ) ;

    return 0 ;

} // end main()  


int getResult( int a , int b , int c ) {  

    double result1 , result2 ;

    int discriminant; // 판별식 값

    result1 = -b / (2.0 * a) ;

    if(!(discriminant = b * b – 4 * a * c)) { // 중근일 때

        result2 = result1 ;

    } // end if

    else { // 실근과 허근일 때

        result2 = fabs( sqrt( discriminant > 0 ? discriminant :
                                  - discriminant ) / ( 2 * a ) ) ;

        /* result2 = sqrt( discriminant > 0 ? discriminant :
          *                                  - discriminant ) /
          *           ( 2 * ( a > 0 ? a : -a ) ) ;
          */

    } // end else

    printResult( discriminant , result1 , result2 ) ;  

    return discriminant ;  

} // end getResult()  


int printResult( int d , double r1 , double r2 ) {

    if( !d ) {

        printf("중근 : %f \n" , r1 ) ;

    } // end if

    else if ( d > 0 ) {

        printf("실근 : %f , %f\n" , r1 + r2 , r1 - r2 ) ;

    } // end else if

    else {

        printf("허근 : %f ± %f i\n" , r1 , r2 ) ;

        // 결국 허근은 i를 붙이는 일 아니겠습니까?

    } // end else  

    return 0 ;  

} // end printResult()    




다시 한번 말합니다. 정답은 없습니다. 정답을 찾지 말고 해답을 찾길 바랍니다.


수없이 많은 길들 가운데 한 가지 방법으로 여러분들께 소개 합니다. 부디 여러분들의 상상력을 일깨우는 데에 도움이 됐기를 바랍니다. 어떤 분은 이렇게 까지 해야 하나? 생각하겠지만 저는 감히 말하고 싶습니다. 이것보다 더 강력하게 작성하셔야 합니다. 그냥 취미로 프로그램을 작성할거라면 hello world나 계속 끄적거리면 됩니다. 하지만 프로그래머로 인생을 걸 분들이라면 이제는 안일했던 생각을 바꾸셔야 합니다. 앞서 얘기했던 레오나르도 다빈치의 낙하산을 꼭 기억하길 바랍니다.


여기저기에서 배열과 포인터가 등장했습니다. 이제 기다리고 기다리던 배열과 포인터의 실체를 밝혀야 할 때가 왔습니다. 그런데 진짜로 기다렸나요?


사실 저는 이 순간만을 기다려왔습니다. 정말입니다.


하지만 그전에 먼저 함수에 대한 모든 것을 정리해 보도록 하겠습니다.   











C언어 및 기타 프로그래밍 관련 질문은 오픈 카톡으로

group talk - https://is.gd/yourc

1:1 talk - https://is.gd/aboutc

#Sinclair #씽클레어 #싱클레어 #씽클레어도씨 #씨언어 #씨프로그래밍  #C언어 #Cprogramming #C_Programming #C #Programming #Clanguage #C_Language

매거진의 이전글 함수와 함수의 활용
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari