Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Nhận xét
- Bài 4. Chẳng lẽ database chỉ có 8 chuỗi như em quy định hả
- // Code
- #include<stdio.h>
- #include<stdlib.h>
- #include<string>
- #define MAX 100
- void XoaXuongDong(char s[])
- {
- int n = strlen(s);
- if (n > 0 && s[n - 1] == '\n')
- s[n - 1] = '\0';
- }
- char** NhapData(char **data, FILE *f, int *pCount)
- {
- int i;
- // tao mang luu data
- data = (char**)malloc(MAX * sizeof(char*));
- for (i = 0; i < MAX; i++)
- data[i] = (char*)malloc(100);
- // luu data
- while(!feof(f))
- {
- fgets(data[*pCount], 100, f);
- XoaXuongDong(data[*pCount]);
- ++(*pCount);
- }
- return data;
- }
- int Google(char *s, char *s1, char **data, int Count)
- {
- int i;
- int dem = 0;
- char *p = NULL;
- for (i = 0; i < Count; i = i + 2)
- {
- strcpy(s1, s);
- p = strtok(s1, " ");
- while (p)
- {
- if (strstr(data[i], p) != NULL)
- {
- printf("%s \n", data[i]);
- printf("%s \n", data[i + 1]);
- printf("\n");
- ++dem;
- break;
- }
- p = strtok(NULL, " ");
- }
- }
- return dem;
- }
- int main()
- {
- // khai bao
- char s[50] = "";
- char s1[50] = "";
- char **data = NULL;
- FILE *f = NULL;
- int Count = 0;
- int i;
- // mo file
- f = fopen("data.txt", "r");
- if (f == NULL)
- {
- printf("Khong mo duoc file data.txt \n");
- return 1;
- }
- // xu li file
- data = NhapData(data, f, &Count);
- // dong file
- fclose(f);
- // nhap tu khoa
- printf("Ban muon tim kiem gi: ");
- gets_s(s);
- // google
- printf("------> Co %d ket qua tim thay \n", Google(s, s1, data, Count));
- // giai phong bo nho
- for (i = 0; i < MAX; i++)
- free(data[i]);
- free(data);
- data = NULL;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement