Guest User

Untitled

a guest
Nov 14th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>      //для isalpha
  4.  
  5.  
  6. int main(void){
  7.    char strings[100][100], str[100], buff[100];
  8.    bool fl_scob, fl_isfunc;
  9.    FILE *f;
  10.    int count = 0, i, j, k;
  11.    
  12.    if((f = fopen("input.txt", "r")) == NULL)
  13.       return 1;
  14.    //Считываем строки из файла
  15.    printf("Входной файл:\n");
  16.    while (fgets (str, sizeof(str), f)){                                        
  17.       printf("%s", str);
  18.       //Поиск закрывающей скобки в строке,
  19.       for(i = 0; i < strlen(str); i++){
  20.          fl_scob = false;
  21.          if(str[i] == ')'){
  22.             for(j = i; j > 0; j--)
  23.                if(str[j] == '('){
  24.                   fl_scob = true;
  25.                   j--;
  26.                   break;
  27.                }
  28.             if(fl_scob == true){
  29.                while((isalpha(str[j]) || str[j] == '_') && j != 0 && fl_scob == true)
  30.                   j--;
  31.                // is int func?
  32.                if(str[j] == ' ' && str[j-1] == 't' &&
  33.                   str[j-2] == 'n' && str[j-3] == 'i' &&
  34.                   (str[j-4] == ';' ||  str[j-4] == ' ' || j - 4 < 0)){            
  35.                   j = j - 3;
  36.                   fl_isfunc = true;
  37.                }
  38.                //is single func?
  39.                if(str[j] == ' ' && str[j-1] == 'e' &&
  40.                   str[j-2] == 'l' && str[j-3] == 'g' &&
  41.                   str[j-4] == 'n' && str[j-5] == 'i' &&
  42.                   str[j-6] == 's' &&
  43.                   (str[j-7] == ';' ||  str[j-7] == ' ' || j - 7 < 0)){            
  44.                   j = j - 6;
  45.                   fl_isfunc = true;
  46.                }
  47.                //is double func?
  48.                if(str[j] == ' ' && str[j-1] == 'e' &&
  49.                   str[j-2] == 'l' && str[j-3] == 'b' &&
  50.                   str[j-4] == 'u' && str[j-5] == 'o' &&
  51.                   str[j-6] == 'd' &&
  52.                   (str[j-7] == ';' ||  str[j-7] == ' '  || j - 7 < 0)){            
  53.                   j = j - 6;
  54.                   fl_isfunc = true;
  55.                }
  56.                //is void func?
  57.                if(str[j] == ' ' && str[j-1] == 'd' &&
  58.                   str[j-2] == 'i' && str[j-3] == 'o' &&
  59.                   str[j-4] == 'v' &&
  60.                   (str[j-5] == ';' ||  str[j-5] == ' ' || j - 5 < 0)){            
  61.                   j = j - 4;
  62.                   fl_isfunc = true;
  63.                }
  64.                //Если функция, то отправляем в символьный массив.
  65.                if(fl_isfunc == true){
  66.                   for(j, k=0; j <= i; j++, k++)
  67.                      buff[k] = str[j];
  68.                   strcpy(strings[count++], buff);
  69.                   memset(buff, 0, sizeof(buff));
  70.                   fl_isfunc = false;
  71.                }
  72.             }      
  73.          }
  74.       }
  75.    }
  76.    putchar('\n');
  77.    //Вывод перечня функций.
  78.    printf("Перечень функций:\n");
  79.    for(i = 0; i < count; i++)
  80.       printf("%s\n", strings[i]);
  81.    putchar('\n');
  82.    //Сортировка методом пузырька.
  83.    for(i = 1; i < count; i++)
  84.       for(j = 0; j < count - i; j++)
  85.          if(strcmp(strings[j], strings[j+1]) > 0){
  86.             strcpy(str, strings[j]);
  87.             strcpy(strings[j], strings[j+1]);
  88.             strcpy(strings[j+1], str);
  89.          }
  90.    //Выводим отсортированные функции.
  91.    printf("Выводим отсортированные функции:\n");
  92.    for(i = 0; i < count; i++)
  93.       printf("%s\n", strings[i]);
  94.    //Запись в файл
  95.    f = fopen("output.txt", "w");
  96.    for(i = 0; i < count; i++)
  97.       fprintf(f, "%s\n", strings[i]);
  98.    fclose(f);
  99.    return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment