|
|
вернуться в форумошибка: Runtime error (access violation), подскажите как исправить? С++ #include <stdio.h> #include <tchar.h> #include <math.h> int charToNumber(char charValue) { if (charValue >= '0' && charValue <= '9') { return charValue - '0'; } } int main() { int CountOfAllNumbers = 1; int CountOfNumbers = 0; bool flag = 0; double *numbers = new double[4]; numbers[CountOfAllNumbers] = 0; char *A = new char [100]; scanf("%s", &A); for (int i = strlen(A)-1; i >= 0; i--) { if (((A[i] == ' ') || (A[i] == '\t'))|| (A[i] == '\n')) { CountOfNumbers = 0; if (flag) { CountOfAllNumbers++; numbers[CountOfAllNumbers] = 0; flag = 0; } } else { flag = 1; numbers[CountOfAllNumbers] = numbers[CountOfAllNumbers] + charToNumber(A[i])*pow(10.0, CountOfNumbers); CountOfNumbers++; } } for (int i = 1; i < CountOfAllNumbers; i++) { printf("%lf\n", sqrt(numbers[i])); } delete[] numbers; return 0; } Edited by author 14.03.2016 16:28 Re: ошибка: Runtime error (access violation), подскажите как исправить? С++ You are trying to read WHOLE input in one read, into 100 bytes buffer. But input is up to 256Kb; scanf "%s" reads non-white-space data and stops on the first white-space. You expect not more then 4 numbers. There are up to 256K/2 numbers. You should better use scanf "%lld" / scanf "%lf" in the cycle. You should allocate big enough "numbers" array (or use std::vector). You also should run locally at least test in the task sample and receive EXACTLY the sample output. |
|
|