Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5.  
  6. static char *C;
  7. static int stringSize;
  8.  
  9. static char** AB;
  10. static int pairsSize;
  11.  
  12. static int threadsSize;
  13.  
  14. char genAlphabetic(){
  15.     return rand()%26+65+rand()%2*32;
  16. }
  17.  
  18. void genString()
  19. {
  20.     C=(char*)malloc(stringSize*sizeof(char)+1);
  21.     for(int i=0;i<stringSize;i++) {
  22.         C[i]=genAlphabetic();
  23.     }
  24.     C[stringSize+1]='\0';
  25. }
  26.  
  27. void printString(char* str)
  28. {
  29.     printf("%s\n",str);
  30. }
  31.  
  32. void genPairs(){
  33.     AB=(char**)malloc(pairsSize*(sizeof(char*)));
  34.     for(int i=0;i<pairsSize;i++) {
  35.         AB[i]=(char*)malloc(3*sizeof(char));
  36.         for(int j=0;j<2;j++){
  37.             AB[i][j]=genAlphabetic();
  38.         }
  39.         AB[i][2]='\0';
  40.     }
  41. }
  42.  
  43. void *replace(void *threadNumber)
  44. {
  45.     int index = (int)(intptr_t)threadNumber;
  46.     for (; index < stringSize; index+=threadsSize) {
  47.         for (int j=0;j<pairsSize;j++) {
  48.             if(C[index]==AB[j][0]) {
  49.                 C[index]=AB[j][1];
  50.                 break;
  51.             }
  52.         }
  53.     }
  54. }
  55.  
  56. int main(int argc, char **argv)
  57. {
  58.     //Обработка параметров
  59.     threadsSize = atoi(argv[1]);
  60.     stringSize = atoi(argv[2]);
  61.     pairsSize = atoi(argv[3]);
  62.  
  63.     //Генерация строк и пар
  64.     srand((unsigned)time(NULL));
  65.     genString();
  66.     genPairs();
  67.  
  68.     //Вывод пар
  69.     printf("Пары замен:\n");
  70.     for(int i=0;i<pairsSize;i++) {
  71.         printString(AB[i]);
  72.     }
  73.  
  74.     //Вывод строк
  75.     printf("Первая строка:\n%s\n", C);
  76.  
  77.     pthread_t *threads = (pthread_t *)malloc(threadsSize * sizeof(pthread_t));
  78.     for (int i = 0; i < threadsSize; i++) {
  79.         pthread_create(&threads[i], NULL, replace, (void *)(intptr_t)i);
  80.     }
  81.     for (int i = 0; i<threadsSize; i++) {
  82.         pthread_join(threads[i], NULL);
  83.     }
  84.     printf("Измененная строка:\n%s\n",C);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement