Advertisement
Ne-Biolog

Untitled

May 8th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <malloc/malloc.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. bool isLetter(char c) {
  7. if(c >= 'a' && c <= 'z') return true;
  8. if(c >= 'A' && c <= 'Z') return true;
  9. return false;
  10. }
  11.  
  12. bool stringСomparison(char *x, int sizeX, char *y, int sizeY) {
  13. if(sizeX != sizeY) return false;
  14. for(int i = 0; i < sizeX; ++i) {
  15. if(x[i] != y[i]) return false;
  16. }
  17. return true;
  18. }
  19.  
  20. void readTextFromFile(int *lenOfWords, char **listOfWords, int &numberOfWordsInFileN1, FILE *file) {
  21. char *currWord = (char*)malloc(sizeof(char) * 100);
  22. char ch = 'q';
  23. while(true) {
  24. if(ch == EOF) break;
  25. ch = fgetc(file);
  26.  
  27. if(isLetter(ch)) {
  28. int currLen = 0;
  29. currWord[currLen] = ch;
  30. while(true) {
  31. ch = fgetc(file);
  32. if(isLetter(ch)) {
  33. currLen++;
  34. currWord[currLen] = ch;
  35. } else break;
  36. }
  37.  
  38. currLen++;
  39. numberOfWordsInFileN1++;
  40.  
  41. lenOfWords = (int*)realloc(lenOfWords, numberOfWordsInFileN1 * sizeof(int));
  42. lenOfWords[numberOfWordsInFileN1 - 1] = currLen;
  43.  
  44. listOfWords = (char**)realloc(listOfWords, numberOfWordsInFileN1 * sizeof(char*));
  45. listOfWords[numberOfWordsInFileN1 - 1] = (char*)malloc(sizeof(char) * (currLen + 1));
  46.  
  47. for(int i = 0; i < currLen; ++i) {
  48. listOfWords[numberOfWordsInFileN1 - 1][i] = currWord[i];
  49. }
  50. }
  51. }
  52.  
  53. /*for(int i = 0; i < numberOfWordsInFileN1; ++i) {
  54. printf("%d ", lenOfWords[i]);
  55. for(int j = 0; j < lenOfWords[i]; ++j) {
  56. printf("%c", listOfWords[i][j]);
  57. }
  58. printf("\n");
  59. }*/
  60.  
  61. }
  62.  
  63. void searchForDuplicateWords(int *lenOfWordsInFileN1, char **listOfWordsInFileN1, int numberOfWordsInFileN1, int *lenOfWordsInFileN2, char **listOfWordsInFileN2, int numberOfWordsInFileN2) {
  64. /*for(int i = 0; i < numberOfWordsInFileN1; ++i) {
  65. int numberOfRepetitions = 0;
  66. for(int j = 0; j < numberOfWordsInFileN2; ++j) {
  67. if(stringСomparison(listOfWordsInFileN1[i], lenOfWordsInFileN1[i], listOfWordsInFileN2[j], lenOfWordsInFileN2[j])) {
  68. numberOfRepetitions++;
  69. }
  70. }
  71. //cout << listOfWordsInFileN2[i] << " in file N2 = " << numberOfRepetitions << endl;
  72. for(int j = 0; j < lenOfWordsInFileN1[i]; ++j) {
  73. //printf("%c", listOfWordsInFileN1[i][j]);
  74. }
  75. printf("\n");
  76. }*/
  77. }
  78.  
  79. int main ()
  80. {
  81.  
  82. int *lenOfWordsInFileN1 = NULL;
  83. char **listOfWordsInFileN1 = NULL;
  84. int numberOfWordsInFileN1 = 0;
  85.  
  86. int *lenOfWordsInFileN2 = NULL;
  87. char **listOfWordsInFileN2 = NULL;
  88. int numberOfWordsInFileN2 = 0;
  89.  
  90. FILE *file1 = fopen("input.txt", "r");
  91. FILE *file2 = fopen("output.txt", "r");
  92. readTextFromFile(lenOfWordsInFileN1, listOfWordsInFileN1, numberOfWordsInFileN1, file1);
  93. readTextFromFile(lenOfWordsInFileN2, listOfWordsInFileN2, numberOfWordsInFileN2, file2);
  94.  
  95. //freopen("output.txt" , "w" , stdout);
  96.  
  97. searchForDuplicateWords(lenOfWordsInFileN1, listOfWordsInFileN1, numberOfWordsInFileN1,
  98. lenOfWordsInFileN2, listOfWordsInFileN2, numberOfWordsInFileN2);
  99.  
  100.  
  101.  
  102. fclose(file1);
  103. fclose(file2);
  104.  
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement