Advertisement
sp1d3o

IZP1

Dec 17th, 2022
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6.  
  7. //vyhledávací tabulka jednotlivých znaků - jako na tlačítkovém telefonu
  8. const char key[10][5] = {
  9.         {'0', '+', 0, 0, 0},
  10.         {'1', 0, 0, 0, 0},
  11.         {'2', 'a', 'b', 'c', 0},
  12.         {'3', 'd', 'e', 'f', 0},
  13.         {'4', 'g', 'h', 'i', 0},
  14.         {'5', 'j', 'k', 'l', 0},
  15.         {'6', 'm', 'n', 'o', 0},
  16.         {'7', 'p', 'q', 'r', 's'},
  17.         {'8', 't', 'u', 'v', 0},
  18.         {'9', 'w', 'x', 'y', 'z'} };
  19.  
  20. //definice konstanty MAX - maximální délka řádku (dáno zadáním)
  21. #define MAX 100
  22.  
  23.  
  24.  
  25. //forward declarations of functions used in this code
  26. bool find(char* searched, char* wanted);
  27. int read(char *cur_buff);
  28. bool checkFromThis(char* data, char* pattern);
  29. int help_read(char *buffer);
  30.  
  31.  
  32. int main(int argc, char *argv[])
  33. {
  34.     //two buffers of MAX size to store lines of contacts
  35.     char line1[MAX];
  36.     char line2[MAX];
  37.  
  38.  
  39.     //variable to store count of matches
  40.     int matches;
  41.  
  42.     //variables for storing readed lines
  43.     int first;
  44.     int second;
  45.  
  46.     //repeated reading into buffers
  47.     while(true) {
  48.         first = read(line1);
  49.         second = read(line2);
  50.  
  51.         //checking return values, as mentioned in the function
  52.         if(first == 1) {
  53.             if(matches == 0) {
  54.                 //vypíše na standardní chybový výstup chybovou hlášku
  55.                 fprintf(stderr,"Not found\n");
  56.             }
  57.             return 0;
  58.         }
  59.  
  60.         //návratová hodnota 2 je v pořádku - lze pokračovat ve vykonávání mainu
  61.         else if(first == 2) {
  62.             continue;
  63.         }
  64.  
  65.         //stejná kontrola jako nahoře, jen u druhého načteného řádku
  66.         if(second == 1) {
  67.             if(matches == 0) {
  68.                 fprintf(stderr,"Not found\n");
  69.             }
  70.             return 0;
  71.         }
  72.  
  73.         else if(second == 2) {
  74.             continue;
  75.         }
  76.  
  77.         //checking if there are no missing things
  78.         //kdyby první znak načtený do bufferu byl prázdný řádek nebo znak nového řádku
  79.         while(line1[0] == '\n' || line1[0] == '\0' || line2[0] == '\n' || line2[0] == '\0') {
  80.         }
  81.  
  82.  
  83.  
  84.         //actual searching wanted pattern
  85.         if(find(line1, argv[1]) || find(line2, argv[1])) {
  86.             //deciding order of printing and incrementing matches variable
  87.             if((line2[0] >= '0') && (line2[0] <= '9')) {
  88.                 matches++;
  89.                 //dodělání správného formátu výstupu - pokud by byl načten znak nového řádku, vypsaly by se řádky
  90.                 //se jménem a číslem pod sebe, což byla chyba - správný výpis je Jméno Příjmení, tel. číslo
  91.                 //tyhle řádky dělají to, že pokud v načteném řádku najdou znak nového řádku, nahradí ho za nulu
  92.                 //takže se pak vypíše správný formát
  93.                 for(int i = 0; i < 100; i++) {
  94.                     if(line1[i] == '\n') {
  95.                         line1[i] = '\0';
  96.                     }
  97.                 }
  98.                 printf("%s, %s\n", line1, line2);
  99.             }
  100.  
  101.             //to samé pro druhý řádek
  102.             else {
  103.                 matches++;
  104.                 for(int i = 0; i < 100; i++) {
  105.                     if(line2[i] == '\n') {
  106.                         line2[i] = '\0';
  107.                     }
  108.                 }
  109.  
  110.                 printf("%s, %s\n", line2, line1);
  111.             }
  112.         }
  113.     }
  114.  
  115.     (void)argc;
  116.  
  117.     return 0;
  118. }
  119.  
  120. //solution of this project is based  on different scenarios, thats why return type is int
  121. //návratový int pro snazší detkci a práci s chybami v mainu
  122. int read(char *cur_buff)
  123. {
  124.     //this line reads line with max 100 characters, else program returns 1, if it get to the end of the file
  125.     if (fgets(cur_buff, MAX, stdin) == NULL) {
  126.         return 1;
  127.     }
  128.  
  129.     //key line which checks length of input and decide if it matches
  130.     // project requirements for pattern search
  131.     if ((strlen(cur_buff) + 1) >= MAX) {
  132.         //this is variable for purpose of checking if input is not too long
  133.         char trash_buff[100];
  134.  
  135.         //this while cycle checks the length of input - in case it is longer returns 2
  136.         while ((help_read(trash_buff) != 0)
  137.         && (strlen(trash_buff) == sizeof(trash_buff) - 1)
  138.         && (trash_buff[sizeof(trash_buff) - 1] != '\n')) {
  139.         }
  140.         return 2;
  141.     }
  142.  
  143.     return 0;
  144. }
  145.  
  146. //help for read function above
  147. int help_read(char *buffer)
  148. {
  149.     if(fgets(buffer, sizeof(buffer),stdin) == NULL) {
  150.         return 0;
  151.     }
  152.  
  153.     return 1;
  154. }
  155.  
  156. //searched is read line from stdin in which the wanted pattern will be tried to find
  157. bool find(char* searched, char* wanted){
  158.     if(searched == NULL || wanted == NULL || searched == 0 || wanted == 0)
  159.         return true;
  160.  
  161.     for(unsigned int i = 0; i < strlen(searched) - strlen(wanted); i++){
  162.         if(checkFromThis(&searched[i], wanted))
  163.             return true;
  164.     }
  165.     return false;
  166. }
  167.  
  168. bool checkFromThis(char* data, char* pattern){
  169.     //pomocná proměnná do které se uloží první znak vstupu, ve kterém se hledají shody překonvertovaný na malé písmeno
  170.     int helper = tolower(data[0]);
  171.     //kontrola zda hledané věci nejsou prázdné
  172.     if(pattern == NULL || data  == NULL || data[0] == 0 || pattern[0] == 0)
  173.         return true;
  174.  
  175.     //hledání v tabulce znaků nahoře
  176.     for(int i = 0; i < 5; i++){
  177.         if(helper == key[pattern[0] - '0'][i]){
  178.             return checkFromThis(&data[1], &pattern[1]);
  179.         }
  180.     }
  181.     return false;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement