편집 기록

편집 기록
  • 프로필 알 수 없는 사용자님의 편집
    날짜2020.06.20

    숫자만 출력하는 코드를 짰는데 알파벳을 두개이상입력하면 변수를 알맞게 입력하라고 나오네요 이유를 잘 모르겠어요


    #include<stdio.h>
    
    int myatoi(char[]);
    
    void main()
    {
        char string[81];
        int re;
        printf("Enter the string: \n");
        gets_s(string,80);
        re=myatoi(string);
        printf("result is %d", re);
    }
    
    int myatoi(char str[]) {
        int length=0;
        while (str[length] != '\0') {
            length++;
        }
        int len, pull=0;
        for (len = 0; len < length; len++) {
            if (str[len] >= '0' && str[len] <= '9') {
                continue;
            }
            else {
                for(pull=0;pull<length;pull++) {
                    str[len] = str[len + 1];
                    len--;//+되기 때문에 -1을 해서 숫자 유지
                    length--;//앞으로 당겨서 길이 줄어듬
                    pull++;
                }
            }
        }
        int ten, num=1;
        for (ten = 0; ten < length-1; ten++) {
            num = num * 10;
        }
        int result=0;
        for (ten = 0; ten < length; ten++) {
            result = result + (str[ten]-48) * num;
            num = num / 10;//자릿수 줄이기
        }
        return result;
    }
    
  • 프로필 nowp님의 편집
    날짜2020.06.20

    숫자만 출력하는 코드를 짰는데 알파벳을 두개이상입력하면 변수를 알맞게 입력하라고 나오네요 이유를 잘 모르겠어요


    #include<stdio.h>
    
    int myatoi(char[]);
    
    void main()
    {
        char string[81];
        int re;
        printf("Enter the string: \n");
        gets_s(string,80);
        re=myatoi(string);
        printf("result is %d", re);
    }
    
    int myatoi(char str[]) {
        int length=0;
        while (str[length] != '\0') {
            length++;
        }
        int len, pull=0;
        for (len = 0; len < length; len++) {
            if (str[len] >= '0' && str[len] <= '9') {
                continue;
            }
            else {
                for(pull=0;pull<length;pull++) {
                    str[len] = str[len + 1];
                    len--;//+되기 때문에 -1을 해서 숫자 유지
                    length--;//앞으로 당겨서 길이 줄어듬
                    pull++;
                }
            }
        }
        int ten, num=1;
        for (ten = 0; ten < length-1; ten++) {
            num = num * 10;
        }
        int result=0;
        for (ten = 0; ten < length; ten++) {
            result = result + (str[ten]-48) * num;
            num = num / 10;//자릿수 줄이기
        }
        return result;
    }
    
  • 프로필 김태규님의 편집
    날짜2020.06.20

    숫자만 출력하는 코드를 짰는데 알파벳을 두개이상입력하면 변수를 알맞게 입력하라고 나오네요 이유를 잘 모르겠어요


    include

    int myatoi(char[]);

    void main() { char string[81]; int re; printf("Enter the string: \n"); gets_s(string,80); re=myatoi(string); printf("result is %d", re); }

    int myatoi(char str[]) { int length=0; while (str[length] != '\0') { length++; } int len, pull=0; for (len = 0; len < length; len++) { if (str[len] >= '0' && str[len] <= '9') { continue; } else { for(pull=0;pull<length;pull++) { str[len] = str[len + 1]; len--;//+되기 때문에 -1을 해서 숫자 유지 length--;//앞으로 당겨서 길이 줄어듬 pull++; } } } int ten, num=1; for (ten = 0; ten < length-1; ten++) { num = num * 10; } int result=0; for (ten = 0; ten < length; ten++) { result = result + (str[ten]-48) * num; num = num / 10;//자릿수 줄이기 } return result; }