Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- #include <stdint.h>
- #include <stdlib.h>
- static char *C;
- static int stringSize;
- static char** AB;
- static int pairsSize;
- static int threadsSize;
- char genAlphabetic(){
- return rand()%26+65+rand()%2*32;
- }
- void genString()
- {
- C=(char*)malloc(stringSize*sizeof(char)+1);
- for(int i=0;i<stringSize;i++) {
- C[i]=genAlphabetic();
- }
- C[stringSize+1]='\0';
- }
- void printString(char* str)
- {
- printf("%s\n",str);
- }
- void genPairs(){
- AB=(char**)malloc(pairsSize*(sizeof(char*)));
- for(int i=0;i<pairsSize;i++) {
- AB[i]=(char*)malloc(3*sizeof(char));
- for(int j=0;j<2;j++){
- AB[i][j]=genAlphabetic();
- }
- AB[i][2]='\0';
- }
- }
- void *replace(void *threadNumber)
- {
- int index = (int)(intptr_t)threadNumber;
- for (; index < stringSize; index+=threadsSize) {
- for (int j=0;j<pairsSize;j++) {
- if(C[index]==AB[j][0]) {
- C[index]=AB[j][1];
- break;
- }
- }
- }
- }
- int main(int argc, char **argv)
- {
- //Обработка параметров
- threadsSize = atoi(argv[1]);
- stringSize = atoi(argv[2]);
- pairsSize = atoi(argv[3]);
- //Генерация строк и пар
- srand((unsigned)time(NULL));
- genString();
- genPairs();
- //Вывод пар
- printf("Пары замен:\n");
- for(int i=0;i<pairsSize;i++) {
- printString(AB[i]);
- }
- //Вывод строк
- printf("Первая строка:\n%s\n", C);
- pthread_t *threads = (pthread_t *)malloc(threadsSize * sizeof(pthread_t));
- for (int i = 0; i < threadsSize; i++) {
- pthread_create(&threads[i], NULL, replace, (void *)(intptr_t)i);
- }
- for (int i = 0; i<threadsSize; i++) {
- pthread_join(threads[i], NULL);
- }
- printf("Измененная строка:\n%s\n",C);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement