C++ strtok, strcpy 액세스 위반 예외 발생

조회수 649회
#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>

using namespace std;

class polynomial {
private:
    int arr[100];//계수 저장, 지수는 인덱스에 따라
public:
    polynomial() {};
    void setarr(int a, int b) {
        arr[a] = b;
    }
    void showarr(int champ) {
        for (int i = 0; i <= champ; i++) {
            if (arr[i] == 0) continue;
            else if (arr[i] == 1) printf("x^%d+", (champ - i));
            else if ((champ - i) == 1) printf("%dx+", arr[i]);
            else if (arr[i] == 1 && (champ - i) == 1) printf("x+");
            else if (i == champ) printf("%d+", arr[i]);
            else printf("%dx^%d+", arr[i], (champ - i));
        }
        printf("\b ");
    }
};

void cut(char* a, int arr[][10], char* c) {
    char* ptr = strtok(a, c);
    arr[0][0] = atoi(ptr);
    ptr = strtok(NULL, c);
    arr[0][1] = atoi(ptr);
    for (int i = 1; ptr != NULL; i++) {
        ptr = strtok(NULL, c);
        arr[i][0] = atoi(ptr);
        ptr = strtok(NULL, c);
        arr[i][1] = atoi(ptr);
    }
}

int getchamp(int arr[][10], int size) {//최고차항의 지수를 구함
    int champ = 0;
    for (int i = 0; i <= size; i++) {
        if (arr[i][2] > arr[champ][2]) champ = i;
    }
    int h = arr[champ][2];
    return h;
}

void setpolyarr(polynomial a, int input[][10], int size, int champ) {
    for (int i = 0; i <= size; i++) {
        int ind = champ - input[i][2];
        int num = input[i][1];
        a.setarr(ind, num);
    }
}

void main() {
    while (1) {
        polynomial a, b, c, d, t;
        int x;
        char input[1000];
        char ainput[1000];
        char binput[1000];
        char cinput[1000];
        int ainput2[100][10];
        int binput2[100][10];
        int cinput2[100][10];

        printf(">Input polynomials a, b, c: ");
        scanf("%s", input);
        if (input[0] == '#') break;
        else {
            char* ptr = strtok(input, " ");
            strcpy(ainput, ptr);
            ptr = strtok(NULL, " ");
            strcpy(binput, ptr);
            ptr = strtok(NULL, " ");
            strcpy(cinput, ptr);

            char del[10] = "(,)";

            cut(ainput, ainput2, del);
            cut(binput, binput2, del);
            cut(cinput, cinput2, del);

            int asize = sizeof(ainput2) / sizeof(ainput2[0]);
            int bsize = sizeof(binput2) / sizeof(binput2[0]);
            int csize = sizeof(cinput2) / sizeof(cinput2[0]);

            int achamp = getchamp(ainput2, asize);
            int bchamp = getchamp(binput2, bsize);
            int cchamp = getchamp(cinput2, csize);

            setpolyarr(a, ainput2, asize, achamp);
            setpolyarr(b, binput2, bsize, bchamp);
            setpolyarr(c, cinput2, csize, cchamp);

            printf("\nA(x) : ");
            a.showarr(achamp);
            printf("\nB(x) : ");
            b.showarr(bchamp);
            printf("\nC(x) : ");
            c.showarr(cchamp);
        }
    }
}

다항식을 (계수,지수)(계수,지수).. 형태로 입력받아 출력하는 프로그램입니다. 입력받을 때에 다항식끼리 구분은 " ", 공백으로 하였습니다. 다름이아니라 main함수의 첫 strtok에서 문제가 생깁니다. ainput에는 input에서 받은 내용 중 첫번째 공백까지의 내용이 잘 들어가는데 binput에는 깨진 글자들이 들어가고 액세스 위반 예외가 발생하더군요. 이유가 뭘까요? 예외 발생(0x7893ee43(ucrtbased.dll), project4.exe): 0xc0000005: 0x00000000 위치를 읽는 동안 액세스 위반이 발생했습니다.. 라는 내용이 뜹니다.

  • (•́ ✖ •̀)
    알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)