Advertisement
Usow_Maxim

Для лизы исправил лабу №7

Mar 2nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <locale.h>
  4. #include <cstring>
  5. #include <string.h>
  6. #include <stdbool.h>
  7. #include <windows.h>
  8. #include <ctype.h>
  9.  
  10. int strSize(char* str)
  11. {
  12.     int i = 0;
  13.     while(str[i] != '\0')
  14.         i++;
  15.     return i;
  16. }
  17.  
  18. char** FileRead(int &Size, char fileName[])
  19. {
  20.     char** str = NULL;
  21.     Size = 0;
  22.     FILE* f = fopen(fileName, "r");
  23.     if(f != NULL)
  24.     {
  25.         char buf[1000];
  26.         while(!feof(f))
  27.         {
  28.             if(fscanf(f, "%s ", buf))
  29.             {
  30.                 char** copyStr = new char*[Size + 1];
  31.                 for(int i = 0; i < Size; i++)
  32.                 {
  33.                     copyStr[i] = new char[strlen(str[i])];
  34.                     strcpy(copyStr[i], str[i]);
  35.                     delete[] str[i];
  36.                 }
  37.                 delete[] str;
  38.                 str = new char*[Size + 1];
  39.                 for(int i = 0; i < Size; i++)
  40.                 {
  41.                     str[i] = new char[strlen(copyStr[i])];
  42.                     strcpy(str[i], copyStr[i]);
  43.                     delete[] copyStr[i];
  44.                 }
  45.                 delete[] copyStr;
  46.                 str[Size] = new char[strlen(buf)];
  47.                 strcpy(str[Size], buf);
  48.                 Size++;
  49.                 printf("%s ", buf);
  50.             }
  51.         }
  52.     }
  53.     else
  54.     {
  55.         printf("Ошибка открытия файла!");
  56.     }
  57.     fclose(f);
  58.     printf("\n");
  59.     return str;
  60. }
  61.  
  62. int main()
  63. {
  64.     SetConsoleCP(1251);
  65.     SetConsoleOutputCP(1251);
  66.     int Size = 0;
  67.     char** str = NULL;
  68.     bool Menu = true;
  69.  
  70.     while(Menu)
  71.     {
  72.         printf("****************МЕНЮ*******************\n");
  73.         printf("1. Выбор файла по умолчанию \n");
  74.         printf("2. Выбор файла по желанию пользователя\n");
  75.         printf("3. Обработка текста\n");
  76.         printf("4. Вывод списка слов в алфавитном порядке\n");
  77.         printf("5. Выход\n");
  78.         printf("**************************************\n\n");
  79.         int sel = 0;
  80.         scanf("%d", &sel);
  81.  
  82.         switch (sel)
  83.         {
  84.             case 1:
  85.                 str = FileRead(Size, "test.txt");
  86.                 break;
  87.             case 2:
  88.             {
  89.                 char fileName[256];
  90.                 printf("Введите путь к файлу: ");
  91.                 scanf("%s", fileName);
  92.                 str = FileRead(Size, fileName);
  93.                 break;
  94.             }
  95.             case 3:
  96.             {
  97.                 for (int i = 0; i < Size; i++)
  98.                     if(strlen(str[i]) >= 3)
  99.                         str[i][0] = toupper(str[i][0]);
  100.                 char* buf = NULL;
  101.                 for(int i = 0; i < Size; i++)
  102.                     for(int j = i + 1; j < Size; j++)
  103.                         if(strcmp(str[i], str[j]) > 0)
  104.                         {
  105.                             buf = new char[strlen(str[i])];
  106.                             buf = str[i];
  107.                             str[i] = str[j];
  108.                             str[j] = buf;
  109.                             delete[] buf;
  110.                         }
  111.                 printf("Вывод:\n");
  112.                 for(int i = 0; i < Size; i++)
  113.                     printf("%s\n", str[i]);
  114.  
  115.                     /*for(int j = 0; j < strlen(str[i]); j++)
  116.                     {
  117.  
  118.                         if( strlen(str[i]) >= 3 )
  119.                         {
  120.  
  121.                             printf("size:%d\n", Size);
  122.                             for (int k = 0; k < Size; k++)
  123.                             {
  124.                                 for(int k = 0; k < strlen(str[k]); k++)
  125.                                     str[j][k] = toupper(str[j][k]);
  126.                                 printf("%s ", str[j]);
  127.                             }
  128.                         }
  129.                         printf("\n");
  130.                     }*/
  131.                 break;
  132.             }
  133.             case 4:
  134.             {
  135.                 char* buf = NULL;
  136.                 for(int i = 0; i < Size; i++)
  137.                     for(int j = i + 1; j < Size; j++)
  138.                         if(strcmp(str[i], str[j]) > 0)
  139.                         {
  140.                             buf = new char[strlen(str[i])];
  141.                             buf = str[i];
  142.                             str[i] = str[j];
  143.                             str[j] = buf;
  144.                             delete[] buf;
  145.                         }
  146.                 for(int i = 0; i < Size; i++)
  147.                     printf("%s\n", str[i]);
  148.                 break;
  149.             }
  150.             case 5:
  151.                 Menu = false;
  152.                 break;
  153.         }
  154.     }
  155.  
  156.     for(int i = 0; i < Size; i++)
  157.         delete[] str[i];
  158.     delete[] str;
  159.     return 0;
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement