Advertisement
JouJoy

Untitled

May 20th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. using namespace std;
  5. struct node
  6. {
  7.     char data;
  8.     node* next = NULL;
  9. };
  10. void create_list(node*& top, int n, char* S) //создание листа способом стэка
  11. {
  12.     int i;
  13.     node* p;
  14.     top = new node;
  15.     top = NULL;
  16.     for (i = 0; i < n; i++)
  17.     {
  18.         p = new node;
  19.  
  20.         p->data = S[i];
  21.         p->next = top;
  22.         top = p;
  23.     }
  24. }
  25. void show_list(node* top) //показать список
  26. {
  27.     node* p;
  28.     p = top;
  29.     while (p)
  30.     {
  31.         cout << p->data;
  32.         p = p->next;
  33.     }
  34.     cout << endl;
  35. }
  36. int find(node*& top, char* v2)
  37. {
  38.     int i = 0;
  39.     node* p = top;
  40.     bool t = false;
  41.     int numb;
  42.     int f = strlen(v2);
  43.     while ((p->next != NULL) && !t)
  44.     {
  45.         numb = i;
  46.         if ((v2[i] == p->data) && (p->data != ' ') && i < f)
  47.         {
  48.             i++;
  49.             p = p->next;
  50.  
  51.         }
  52.         else if ((p->data == ' ') && (i + 1 == f))
  53.         {
  54.             t = true;
  55.             return numb;
  56.         }
  57.         p = p->next;
  58.     }
  59.     return 0;
  60. }
  61. void zamena(char* v1, char* v2, node*& top)
  62. {
  63.     int i;
  64.     bool t = false;//слово не заменено
  65.     node* p=new node;
  66.     p->next = top->next;
  67.     int len = strlen(v1);
  68.     node* p1;
  69.     int address = find(top, v2);
  70.     for (i = 0; i < address; i++)
  71.     {
  72.         top = top->next;
  73.     }
  74.     i = 0;
  75.     while ((top->data != ' ') && i != len)
  76.     {
  77.         if (top->data == ' ')
  78.         {
  79.             p1 = new node;
  80.             p1->data = v1[i];
  81.             p1->next = top;
  82.             top = p->next;
  83.             i++;
  84.         }
  85.         else if (i < len)
  86.         {
  87.             while ((top->next->data!=' ')&&top->next!=NULL)
  88.                 top->next = top->next->next;
  89.         }
  90.         else
  91.         {
  92.             top->data = v1[i];
  93.             i++;
  94.             top = top->next;
  95.         }
  96.     }
  97.     top->next = p->next;
  98. }
  99.  
  100. int main()
  101. {
  102.    
  103.     setlocale(LC_ALL, "rus");
  104.     node* text;
  105.     char S[128] = ""; // для сохранения строки
  106.  
  107.     cout << "Введите текст латиницей (не больше 128 символов):\n";
  108.     cin.getline(S, 128);
  109.     int n;
  110.     int i;
  111.     n=strlen(S);
  112.     create_list(text, n, S);
  113.     cout << "Text t: ";
  114.     show_list(text);
  115.     cout << endl;
  116.     char v1[128];
  117.     cout << "Введите первое слово:\n";
  118.     cin.getline(v1, 128);
  119.     char v2[128];
  120.     cout << "Введите второе слово \n";
  121.     cin.getline(v2, 128);
  122.     zamena(v1, v2, text);
  123.     show_list(text);
  124.     return 0;
  125.     system("pause");
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement