Advertisement
Guest User

projš

a guest
Dec 6th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6.  
  7. #define strSize 1000 //  число строк/столбцов
  8.  
  9.  
  10. //  структура карты
  11. typedef struct
  12. {
  13.     int rows;
  14.     int cols;
  15.     unsigned char *cells;
  16. } Map;
  17.  
  18. //  является ли символ числом
  19. bool isCharNum(char *argument)
  20. {
  21.     int len = strlen(argument);
  22.     bool result = true;
  23.     for (int i = 0; i < len; i++)
  24.     {
  25.         if (!isdigit(argument[i]))
  26.         {
  27.             result = false;
  28.         }
  29.     }
  30.     return result;
  31. }
  32.  
  33. //  определение границы
  34. /*bool isborder (Map *map, int r, int c, int border)
  35. {
  36.    
  37. }*/
  38.  
  39. //  определение стартовой границы
  40. /*int startBorder (Map *map, int r, int c, int leftright)
  41. {
  42.  
  43. }*/
  44.  
  45. //  сколько слов в строке, без пробелов
  46. int wordsInStr(char *str)
  47. {
  48.     int i;
  49.     int words = 0;
  50.     for (i = 0; i < strlen(str) - 1; i++)
  51.     {
  52.         if (str[i] == ' ' && str[i+1] != ' ')
  53.         {
  54.             words++;
  55.         }
  56.         if (str[0] != ' ' && i == 0)
  57.         {
  58.             words++;
  59.         }
  60.     }
  61.     printf("Slov v 1 stroke %d\n", words);
  62.     return words;
  63. }
  64.  
  65. bool fileTxt(char *fileName)
  66. {
  67.     if ((fileName[strlen(fileName) - 4] == '.') && (fileName[strlen(fileName) - 3] == 't') && (fileName[strlen(fileName) - 2] == 'x') && (fileName[strlen(fileName) - 1] == 't'))   //strstr(fileName, ".txt") != NULL)
  68.     {
  69.         printf("%s\n", "File txt");
  70.         return true;
  71.     }
  72.     else
  73.     {
  74.         printf("%s\n", "File not txt");
  75.         return false;
  76.     }
  77. }
  78.  
  79. //  проверка карты
  80. bool *mapTest(char *fileName)
  81. {
  82.     //char *num1;
  83.     //char *num2;
  84.     //int rows, cols;
  85.     FILE *fp;
  86.     Map m;
  87.     fp = fopen(fileName, "r");
  88.     if (fp == NULL)
  89.     {
  90.         fprintf(stderr, "%s\n", "Can't open file");
  91.         return false;
  92.     }
  93.     else
  94.     {
  95.         char str[strSize] = "";
  96.         fgets(str, strSize, fp);
  97.         printf("%s\n", str);
  98.         if (wordsInStr(str) == 2)
  99.         {
  100.             char *firstword = strtok(str, " ");
  101.             printf("Firstword %s\n", firstword);
  102.             char *secondword = strtok(NULL, "\n");
  103.             printf("Secondword %s\n", secondword);
  104.             if (isCharNum(firstword) && isCharNum(secondword))
  105.             {
  106.                 m.rows = atoi(firstword);
  107.                 m.cols = atoi(secondword);
  108.                 printf("Rows %d\n", m.rows);
  109.                 printf("Cols %d\n", m.cols);
  110.                 m.cells = malloc(m.rows * m.cols * sizeof(unsigned char) + 1);
  111.                 int k = 0;
  112.                 while (fgets(str, strSize, fp) != NULL)
  113.                 {
  114.                     if (wordsInStr(str) == m.cols)
  115.                     {
  116.                         k++;
  117.                     }
  118.                     else
  119.                     {
  120.                         fprintf(stderr, "%s\n", "Invalid");
  121.                         return -1;
  122.                     }
  123.                     printf("%s", str);
  124.                    
  125.                 }
  126.                 printf("\n");
  127.                 if (k != m.rows)
  128.                 {
  129.                     printf("\n");
  130.                     fprintf(stderr, "%s\n", "Invalid");
  131.                     return -1;  
  132.                 }
  133.                
  134.                 free(m.cells);
  135.                 fprintf(stderr, "%s\n", "Valid");
  136.             }
  137.             else
  138.             {
  139.                 fprintf(stderr, "%s\n", "Invalid");
  140.             }
  141.        
  142.         }
  143.     }
  144.     printf("loh\n");
  145.     return 0;
  146. }
  147.  
  148.  
  149. int main()//int main (int argc, char *argv[])
  150. {
  151.     int argc = 3;
  152.     char *argv1 = "--test";
  153.     char *argv2 = "bludiste.txt";
  154.     if (argc == 2 && (strcmp(argv1, "--help") == 0))
  155.     {
  156.         printf("Napoveda!\nZadejte vstup, cislo radku a cislo sloupci!\nSpecialni komandy:\n --help - vypisuje napovedu\n --test nazev_souboru.txt - zkontroluje, ze soubor dany druhym argumentem programu obsahuje radnou definici mapy bludiste\n --rpath R C nazev_souboru.txt - hleda pruchod bludistem, zadanym z souboru txt, na vstupu na rádku R a sloupci C. Pruchod hleda pomoci pravidla prave ruky (prava ruka vzdy na zdi)\n --lpath R C nazev_souboru.txt - hleda pruchod bludistem, zadanym z souboru txt, na vstupu na rádku R a sloupci C. Pruchod hleda pomoci pravidla leve ruky (leva ruka vzdy na zdi)\n --shortest R C nazev_souboru.txt - hleda nejkratsi cestu z bludiste, zadanym z souboru txt, pri vstupu na radku R a sloupci C\n");
  157.         return 0;
  158.     }
  159.        
  160.     else if (argc == 3 && strcmp(argv1, "--test") == 0 && fileTxt(argv2))
  161.     {
  162.         if (mapTest(argv2))
  163.         {
  164.             printf("Valid\n");
  165.         }
  166.         //else
  167.         {
  168.             printf("Invalid\n");
  169.         }
  170.        
  171.            //    тестирование файла
  172.     }
  173.        
  174.     //  основной цикл
  175.     /*else if (argc == 5)
  176.     {
  177.         //int leftright;
  178.         //  правило правой руки
  179.         if (isCharNum(argv[2]) && isCharNum(argv[3]))
  180.         {
  181.             int startRows = atoi(argv[2]);
  182.             int startCols = atoi(argv[3]);
  183.             if ((strcmp(argv[1], "--rpath") == 0) && (argv2 < 0 && argv2 <= rows) && (argv3 > 0 && argv3 < cols) && (strcmp(argv[4], "bludiste.txt") == 0))
  184.             {
  185.                 //leftright = 0;
  186.                 printf("Ty pidor");
  187.                 return 0;
  188.             }
  189.             //  правило левой руки
  190.             else if ((strcmp(argv[1], "--lpath") == 0) && (argv2 > 0 && argv2 <= rows) && (argv3 > 0 && argv3< cols) && (strcmp(argv[4], "bludiste.txt") == 0))
  191.             {
  192.                 //leftright = 1;
  193.                 return 0;
  194.             }
  195.             //  самый короткий путь
  196.             else if((strcmp(argv[1], "--shortest") == 0) && (argv2 > 0 && argv2 <= rows) && (argv3 > 0 && argv3 < cols) && (strcmp(argv[4], "bludiste.txt") == 0))
  197.             {
  198.                 //leftright =1;
  199.                 return 0;
  200.             }
  201.             else
  202.             {
  203.             fprintf(stderr,"%s\n", "Error: chybna komanda\nPro napovedu napiste --help\n");
  204.             return -1;            
  205.             }
  206.         }
  207.         else
  208.         {
  209.             fprintf(stderr,"%s\n", "Error: chybna komanda\nPro napovedu napiste --help\n");
  210.             return -1;            
  211.         }  
  212.     }
  213.     else
  214.     {
  215.         fprintf(stderr,"%s\n", "Error: chybna komanda\nPro napovedu napiste --help\n");
  216.         return -1;
  217.     }*/
  218.     return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement