Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.69 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <locale>
  6. #include <windows.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <conio.h>
  10. #include "my_Header.h"
  11. #pragma warning(disable : 4996)
  12.  
  13. struct stack_words {
  14.     char inf[100];
  15.     stack_words *next;
  16. };
  17.  
  18. struct add_stack_words {
  19.     char info[100];
  20.     add_stack_words *next_word;
  21. };
  22.  
  23. stack_words *init_stack_words() {
  24.     return NULL;
  25. }
  26.  
  27. add_stack_words *init_add_stack_words() {
  28.     return NULL;
  29. }
  30.  
  31. void push_word(add_stack_words *&s, char *item) {
  32.     add_stack_words *r;
  33.     r = new add_stack_words;
  34.     strcpy(r->info, item);
  35.     r->next_word = s;
  36.     s = r;
  37. }
  38.  
  39. void push(stack_words *&s, char *item) {
  40.     stack_words *r;
  41.     r = new stack_words;
  42.     strcpy(r->inf, item);
  43.     r->next = s;
  44.     s = r;
  45. }
  46.  
  47. char* pop_word(add_stack_words *&s) {
  48.     add_stack_words *r = s;
  49.     char i[100];
  50.     strcpy(i, r->info);
  51.     s = r->next_word;
  52.     delete r;
  53.     return i;
  54. }
  55.  
  56. char* pop(stack_words *&s) {
  57.     stack_words *r = s;
  58.     char i[100];
  59.     strcpy(i, r->inf);
  60.     s = r->next;
  61.     delete r;
  62.     return i;
  63. }
  64.  
  65. char* peek(stack_words *s) {
  66.     return s->inf;
  67. }
  68.  
  69. int empty_stack_words(stack_words *s) {
  70.     return(s) ? 0 : 1;
  71. }
  72. int empty_add_stack_words(add_stack_words *s) {
  73.     return(s) ? 0 : 1;
  74. }
  75.  
  76. void task2_print(stack_words* list) {
  77.     printf("---------------------------------------------------------\n");
  78.     int i = 1;
  79.     printf("\n Список слов:");
  80.     while (list) {
  81.         printf("\n %d) %s\n ", i, list->inf);
  82.         list = list->next;
  83.         i++;
  84.     }
  85.     printf("---------------------------------------------------------\n");
  86. }
  87.  
  88. void task2_double(stack_words* list, add_stack_words* word_list) {
  89.     char str[100];
  90.     while (list) {
  91.         strcpy(str, list->inf);
  92.         if (strlen(list->inf) == 1) {
  93.             for (int j = 0; j <= strlen(list->inf); j++) {
  94.                 push_word(word_list, str);
  95.             }
  96.         }
  97.         else {
  98.             push_word(word_list, str);
  99.         }
  100.         list = list->next;
  101.     }
  102.     while (word_list) {
  103.         strcpy(str, word_list->info);
  104.         push(list, str);
  105.         word_list = word_list->next_word;
  106.         list = list->next;
  107.     }
  108. }
  109.  
  110. void task2_addnew() {
  111.     FILE *fp;
  112.     FILE *fp1;
  113.     char str[100];
  114.    
  115.     stack_words *head = init_stack_words();// Инициализация стека
  116.     add_stack_words *begin = init_add_stack_words();
  117.     if ((fp = fopen("read_text.txt", "r")) != NULL) { // Запись элементов из файла в стек
  118.         while (!feof(fp)) {
  119.             fscanf(fp, "%s", &str);
  120.             //printf("\n %d", head);
  121.             push(head, str);
  122.         }
  123.     }
  124.     task2_print(head);
  125.     task2_double(head, begin);
  126.     if ((fp1 = fopen("write_text.txt", "w")) != NULL) { // Печать в файл
  127.         while (!empty_stack_words(head)) {
  128.             strcpy(str,pop(head));
  129.             fprintf(fp1, "%s ", str);
  130.         }
  131.     }
  132.     fcloseall();
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement