Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- char *CreateStr(int N){
- char *mas = NULL;
- mas = (char*) malloc(sizeof(char)*N);
- if(mas == NULL) return NULL;
- for(int k = 0; k < N; k++)
- mas[k] = rand()%10 + 48;
- return mas;
- }
- int convert(char *str) // Функция конвертации строки в число
- {
- if(str == NULL) return -1;
- int k = 0;
- int count = 0;
- int flag = 0;
- while(str[k] > 13)
- {
- flag = 1; // На случай, если в строке будет 0, чтобы понимать, работал цикл или нет
- if(str[k] < 48 || str[k] > 57) return -1; // Если найдено не число, то ошибка
- count = count*10 + (str[k] - 48); // Формула преобразования в число
- k++; // Переход на следующий элемент строки
- }
- if(count == 0 && flag == 0) return -1; // Если не было найдено чисел
- return count;
- }
- int main() {
- srand(time(NULL));
- const int N = rand()%5 + 1;
- char * Str = CreateStr(N);
- if(Str == NULL){ // Проверка
- printf("Error.\n");
- return 0;
- }
- int a = convert(Str);
- if(a == -1) printf("Error.");
- else printf("%d\n", a);
- free(Str);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement