fork download
  1. #include <math.h>
  2.  
  3. int prime(long int num){
  4. int i;
  5. int b=sqrt(num)+1;
  6. for (i = 1; i <= b; i++)
  7. if (num % i == 0)
  8. return 1;
  9. return 0;
  10. }
  11.  
  12. #include <string.h>
  13.  
  14. size_t custom_strlen(const char* str) {
  15. return sizeof(str);
  16. }
  17. // TODO: Implement `power of` function
  18. // int custom_pow(int base, int power);
  19. long int custom_pow(int base, int power) {
  20. if (power == 0){
  21. return 1;
  22. }
  23. if (power > 0){
  24. long int a = base, i;
  25. for (i = 2; i <= power; i++)
  26. a = a*base;
  27. return a;
  28. }
  29. return 0;
  30. }
  31.  
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34.  
  35. #define ERR_ARGS_COUNT (-1)
  36. #define ERR_WRONG_FLG (-2)
  37.  
  38. #define TST_FOO_FIX 1
  39. #define TST_FOO_IMPL 2
  40. #define TST_MOD_IMPL 3
  41.  
  42.  
  43. /* NOTE(stitaevskiy):
  44.  * We use `atoi` function just for simplification and code reducing.
  45.  * This function doesn't report conversation errors.
  46.  * For safety program we recommend using `strtol` and its analogs.
  47.  * (See `man atoi` and `man strtol` for more info).
  48.  *
  49.  * const char str_num[] = "1234";
  50.  * char* end = NULL;
  51.  * int val = (int) strtol(str_num, &end, 0);
  52.  * if (end != '\0') {
  53.  * //ERROR
  54.  * }
  55.  *
  56.  * */
  57.  
  58. int main(int argc, const char** argv) {
  59. if (argc > 3) {
  60. return ERR_ARGS_COUNT;
  61. }
  62. char *end = NULL;
  63. int test_case = (int) strtol (argv[1], &end, 10);
  64. if (*end != '\0') {
  65. printf("Error: number of case is not found\n");
  66. return 0;
  67. }
  68. const char* data;
  69. data = argv[2];
  70. switch (test_case) {
  71. case TST_FOO_FIX: {
  72. size_t res = custom_strlen(data);
  73. printf("%zu\n", res);
  74. break;
  75. }
  76. case TST_FOO_IMPL: {
  77. if (argc != 4){
  78. printf("The number of input parameters is exceeded\n");
  79. return ERR_ARGS_COUNT;
  80. }
  81. /* Comment to prevent `unused parameter` error
  82.   int base = atoi(data);
  83.   int pow = atoi(argv[3]);
  84.   int res = custom_pow(base, pow); // TODO: Implement me
  85.   printf("%i\n", res);
  86.   */
  87. char *end1, *end2;
  88. end1=NULL;
  89. end2=NULL;
  90. int base = (int) strtol (argv[2], &end1, 10);
  91. int pow = (int) strtol (argv[3], &end2, 10);
  92. if ((*end1 != '\n') && (*end2 != '\n')) {
  93. printf("DATA INPUT ERROR");
  94. }
  95. else {
  96. long int res = custom_pow(base,pow);
  97. printf("%ld\n", res);
  98. }
  99. break;
  100. }
  101. case TST_MOD_IMPL: {
  102. if (argc != 3) {
  103. printf("The number of input parameters is exceeded\n");
  104. }
  105. // Comment for prevent `unused variable` error
  106. // int num = atoi(data);
  107. char *end1;
  108. end1 = NULL;
  109. int num = (int) strtol (argv[2], &end1,0);
  110. if (*end1 != '\n')
  111. printf("DATA INPUT ERROR");
  112. else {
  113. int pri = prime (num);
  114. printf("%d", pri);
  115. }
  116. // TODO: Print to stdout `1` if `num` is prime number and `0` otherwise
  117. // This function MUST be implemented in
  118. // a separate C-module (not in `main` or `utils` module)
  119. break;
  120. }
  121. default: {
  122. return ERR_WRONG_FLG;
  123. }
  124. }
  125. return 0;
  126. }
Runtime error #stdin #stdout 0s 4392KB
stdin
Standard input is empty
stdout
Standard output is empty