Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <ctype.h>
- //vyhledávací tabulka jednotlivých znaků - jako na tlačítkovém telefonu
- const char key[10][5] = {
- {'0', '+', 0, 0, 0},
- {'1', 0, 0, 0, 0},
- {'2', 'a', 'b', 'c', 0},
- {'3', 'd', 'e', 'f', 0},
- {'4', 'g', 'h', 'i', 0},
- {'5', 'j', 'k', 'l', 0},
- {'6', 'm', 'n', 'o', 0},
- {'7', 'p', 'q', 'r', 's'},
- {'8', 't', 'u', 'v', 0},
- {'9', 'w', 'x', 'y', 'z'} };
- //definice konstanty MAX - maximální délka řádku (dáno zadáním)
- #define MAX 100
- //forward declarations of functions used in this code
- bool find(char* searched, char* wanted);
- int read(char *cur_buff);
- bool checkFromThis(char* data, char* pattern);
- int help_read(char *buffer);
- int main(int argc, char *argv[])
- {
- //two buffers of MAX size to store lines of contacts
- char line1[MAX];
- char line2[MAX];
- //variable to store count of matches
- int matches;
- //variables for storing readed lines
- int first;
- int second;
- //repeated reading into buffers
- while(true) {
- first = read(line1);
- second = read(line2);
- //checking return values, as mentioned in the function
- if(first == 1) {
- if(matches == 0) {
- //vypíše na standardní chybový výstup chybovou hlášku
- fprintf(stderr,"Not found\n");
- }
- return 0;
- }
- //návratová hodnota 2 je v pořádku - lze pokračovat ve vykonávání mainu
- else if(first == 2) {
- continue;
- }
- //stejná kontrola jako nahoře, jen u druhého načteného řádku
- if(second == 1) {
- if(matches == 0) {
- fprintf(stderr,"Not found\n");
- }
- return 0;
- }
- else if(second == 2) {
- continue;
- }
- //checking if there are no missing things
- //kdyby první znak načtený do bufferu byl prázdný řádek nebo znak nového řádku
- while(line1[0] == '\n' || line1[0] == '\0' || line2[0] == '\n' || line2[0] == '\0') {
- }
- //actual searching wanted pattern
- if(find(line1, argv[1]) || find(line2, argv[1])) {
- //deciding order of printing and incrementing matches variable
- if((line2[0] >= '0') && (line2[0] <= '9')) {
- matches++;
- //dodělání správného formátu výstupu - pokud by byl načten znak nového řádku, vypsaly by se řádky
- //se jménem a číslem pod sebe, což byla chyba - správný výpis je Jméno Příjmení, tel. číslo
- //tyhle řádky dělají to, že pokud v načteném řádku najdou znak nového řádku, nahradí ho za nulu
- //takže se pak vypíše správný formát
- for(int i = 0; i < 100; i++) {
- if(line1[i] == '\n') {
- line1[i] = '\0';
- }
- }
- printf("%s, %s\n", line1, line2);
- }
- //to samé pro druhý řádek
- else {
- matches++;
- for(int i = 0; i < 100; i++) {
- if(line2[i] == '\n') {
- line2[i] = '\0';
- }
- }
- printf("%s, %s\n", line2, line1);
- }
- }
- }
- (void)argc;
- return 0;
- }
- //solution of this project is based on different scenarios, thats why return type is int
- //návratový int pro snazší detkci a práci s chybami v mainu
- int read(char *cur_buff)
- {
- //this line reads line with max 100 characters, else program returns 1, if it get to the end of the file
- if (fgets(cur_buff, MAX, stdin) == NULL) {
- return 1;
- }
- //key line which checks length of input and decide if it matches
- // project requirements for pattern search
- if ((strlen(cur_buff) + 1) >= MAX) {
- //this is variable for purpose of checking if input is not too long
- char trash_buff[100];
- //this while cycle checks the length of input - in case it is longer returns 2
- while ((help_read(trash_buff) != 0)
- && (strlen(trash_buff) == sizeof(trash_buff) - 1)
- && (trash_buff[sizeof(trash_buff) - 1] != '\n')) {
- }
- return 2;
- }
- return 0;
- }
- //help for read function above
- int help_read(char *buffer)
- {
- if(fgets(buffer, sizeof(buffer),stdin) == NULL) {
- return 0;
- }
- return 1;
- }
- //searched is read line from stdin in which the wanted pattern will be tried to find
- bool find(char* searched, char* wanted){
- if(searched == NULL || wanted == NULL || searched == 0 || wanted == 0)
- return true;
- for(unsigned int i = 0; i < strlen(searched) - strlen(wanted); i++){
- if(checkFromThis(&searched[i], wanted))
- return true;
- }
- return false;
- }
- bool checkFromThis(char* data, char* pattern){
- //pomocná proměnná do které se uloží první znak vstupu, ve kterém se hledají shody překonvertovaný na malé písmeno
- int helper = tolower(data[0]);
- //kontrola zda hledané věci nejsou prázdné
- if(pattern == NULL || data == NULL || data[0] == 0 || pattern[0] == 0)
- return true;
- //hledání v tabulce znaků nahoře
- for(int i = 0; i < 5; i++){
- if(helper == key[pattern[0] - '0'][i]){
- return checkFromThis(&data[1], &pattern[1]);
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement