Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5.  
  6. #define MAX_NAME (128)
  7. #define MAX_NUM (64)
  8. #define MAX (128)
  9. typedef struct Contact {
  10.     char name[MAX_NAME];
  11.     char number[MAX_NUM];
  12. } Contact;
  13.  
  14. char matchTable[10][9] = {
  15.     "0+", "1", "2abcABC", "3defDEF", "4ghiGHI",
  16.     "5jklJKL", "6mnoMNO", "7pqrsPQRS", "8tuvTUV", "9wxyWXY"
  17. };
  18.  
  19.  
  20. bool find(char c, char key){
  21.  
  22.     int j = key - '0';
  23.     for (int i = 0; matchTable[j][i] != '\0'; i++){
  24.         if (c == matchTable[j][i])
  25.             return true;
  26.     }
  27.     return false;
  28.  
  29. }
  30.  
  31. bool matches(char* src, char* key){
  32.  
  33.     int i,j;
  34.  
  35.     for (i = 0; i < strlen(src); i++){
  36.         int tmp = i;
  37.         for (j = 0; j < strlen(key); j++){
  38.             if (find(src[tmp], key[j]))
  39.                 tmp++;
  40.             else
  41.                 break;
  42.         }
  43.         if (j == strlen(key))
  44.             return true;
  45.     }
  46.     return false;
  47. }
  48.  
  49. int main(int argc, char** argv){
  50.  
  51.     char key[MAX_NUM];
  52.     scanf("%s", key);
  53.     size_t arrSize = 32;
  54.  
  55.     Contact contacts[MAX];
  56.  
  57.     int k = 0;
  58.     size_t nameSize = MAX_NAME;
  59.     size_t numSize = MAX_NUM;
  60.  
  61.     FILE* f = fopen("1.txt", "r");
  62.     char nameBuf[MAX_NAME];
  63.     char numBuf[MAX_NUM];
  64.    
  65.     while (fgets(nameBuf, nameSize, f)
  66.             && fgets(numBuf, numSize, f)){
  67.         strcpy(contacts[k].name, nameBuf);
  68.         strcpy(contacts[k].number, numBuf);      
  69.         k++;
  70.     }
  71.  
  72.     for (int i = 0; i < k; i++){
  73.         bool matchesName = matches(contacts[i].name, key);
  74.         bool matchesNumber = matches(contacts[i].number, key);
  75.        
  76.         if (matchesName || matchesNumber)
  77.             printf("%s\n", contacts[i].name);
  78.     }
  79.  
  80.     fclose(f);
  81.    
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement