도와주세요 !! wrong answer

you88311 Reply 7 years 8 weeks ago
#include <stdio.h> #include <string.h> /* 문자열 중 32보다 작은 라인들을 갖는 문자열을 출력하는 프로그램*/ int main(void) { char string[7000]; //저장된 문자열 char temp_string[1200]; //현재 라인 문자열 저장 int string_length = 0; //저장된 문자열 길이 int temp_length = 0; //현재 라인 문자열 길이('\n'은 문자로 치지 않는다) char input; //입력받은 문자 while (1) { input = getchar(); //문자를 입력받는다. if (input == EOF || input=='\x1a') //EOF가 나오는 경우(라인이 종료) { if (temp_length > 31 || input == EOF) //라인이 31보다 크거나 EOF면 { break; } else //라인이 31보다 작거나 같고 문자열 뒤에 온 EOF면 { temp_string[temp_length] = NULL; //현재 라인 저장 배열 문자열화 string[string_length] = NULL; //저장된 라인 배열 문자열화 strncat(string, temp_string, strlen(temp_string)); //현재라인 붙여넣기(sting 뒤에 자동으로 NULL붙음) string_length += strlen(temp_string); //저장한 라인만큼 저장된 문자열 길이 추가 memset(temp_string, NULL, strlen(temp_string) + 1); //현재 라인 없애기 printf("%s\n", string); return 0; } } else if (input == '\n') // '\n'이 나오는 경우(라인이 바뀌면) { if (temp_length <= 31) //현재 라인을 저장!('\n'는 문자가 아닌걸로 가정) { if (temp_length == 0) { continue; } temp_string[temp_length] = input; //일단 개행 문자 저장 temp_string[temp_length + 1] = NULL; //현재 라인 저장 배열 문자열화 string[string_length] = NULL; //저장된 라인 배열 문자열화(이건 안해도 되지 않나?? 확인해보기) strncat(string, temp_string, strlen(temp_string)); //현재라인 붙여넣기(sting 뒤에 자동으로 NULL붙음) string_length += strlen(temp_string); //저장한 라인만큼 저장된 문자열 길이 추가 memset(temp_string, NULL, strlen(temp_string)); //현재 라인 없애기 temp_length = 0; //현재 라인 길이 없애기 } else //현재 라인 버려야 하는 경우 { temp_string[temp_length] = NULL; memset(temp_string, NULL, strlen(temp_string)); //현재 라인 없애기 temp_length = 0; //현재 라인 길이 없애기 } } else // EOF도 '\n'도 아닌(라인이 입력되는 경우) { if (temp_length < 31) //현재 라인이 31보다 작으면 { temp_string[temp_length++] = input; //현재 라인 배열에 저장, 현재 라인 길이 증가 } else //현재 라인이 31과 같거나 크면 { temp_length+=32; //길이를 32로 고정(32나 32이상이나 동일하게 취급) } } } printf("%s", string); return 0; } \n이 마지막이 아닌 경우면 '\x1a' 말씀하시는 건가요??
withcs1 Reply 7 years 8 weeks ago
라인처리를 \n을 이용해서 하고계시는데 그럼 맨 마지마라인은 어떻게되나요? \n이 없다고 하셨으니...EOF가...오면...
you88311 Reply 7 years 8 weeks ago
if (input == EOF || input=='\x1a') //EOF가 나오는 경우(라인이 종료) { if (temp_length > 31 || input == EOF) //라인이 31보다 크거나 EOF면 { break; } else //라인이 31보다 작거나 같고 문자열 뒤에 온 EOF면 { temp_string[temp_length] = NULL; //현재 라인 저장 배열 문자열화 string[string_length] = NULL; //저장된 라인 배열 문자열화 strncat(string, temp_string, strlen(temp_string)); //현재라인 붙여넣기(sting 뒤에 자동으로 NULL붙음) string_length += strlen(temp_string); //저장한 라인만큼 저장된 문자열 길이 추가 memset(temp_string, NULL, strlen(temp_string) + 1); //현재 라인 없애기 printf("%s\n", string); return 0; } } 여기 이부분이 문자열끝이 \n이 아닌 EOF(\x1a)로 끝나는 경우를 처리한 것 아닌가요??
withcs1 Reply 7 years 8 weeks ago
네 아니에요 or연산자는 둘중 하나의 조건만 만족하면 참을 반환합니당 템프렝스가 10이고 인풋이 eof면 어떻게처리되나요