Mouseartiom

pamuzhak_flta

Dec 5th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. void quickSort(int**, int);
  7. void msg(int);
  8. int searchNode(char, char*); //функция считает степень вершины
  9. int searchIndex(int, int*, int); //а эта ищет индекс для вершины с известной степенью
  10.  
  11. void main() {
  12.     char* temp = malloc(100);
  13.     char *input = malloc(1000), *startPos = input;
  14.     int inputLength, searchDegree, flag = 0;
  15.     int nodeAmount, i = 0, pos = 0, index;
  16.     printf("Enter amount of the nodes:\n");
  17.     scanf("%d", &nodeAmount); getchar();
  18.     msg(0); msg(1);
  19.  
  20.     //цикл считывания структуры графа в одну строку
  21.     while(1) {
  22.         fgets(temp, 100, stdin);
  23.         if (*temp == '\n') break;
  24.         strcpy(input+pos, temp);
  25.         pos += strlen(temp)-1;
  26.         *(input+pos) = ' ';
  27.         pos++; }
  28.     *(input+pos) = '\0';
  29.  
  30.     //создаём всякие массивы для чисел, в них хранятся степени
  31.     int *list[nodeAmount], node[nodeAmount], nodeDegree;
  32.     inputLength = strlen(input);
  33.  
  34.     //проверка корректности ввода графа
  35.     for (int i=0; i<inputLength-3;) {
  36.         if (!isalpha(input[i])) msg(3);
  37.         if (input[i+1] == ' ') i += 2;
  38.         else if ((input[i+1] != '-') || (input[i+2] != '-')) msg(3);
  39.         else i += 3; }
  40.  
  41.     //причёсываем массивы
  42.     //в массиве node хранятся числа в формате 203,
  43.     //где сотни - степень вершины, а единицы - индекс в массиве.
  44.     for (int i=0; i < nodeAmount; i++) {
  45.         nodeDegree = searchNode(i+'a', startPos);
  46.         node[i] = nodeDegree*100 + i;
  47.         list[i] = &node[i]; }
  48.        
  49.     msg(2);
  50.     scanf("%d", &searchDegree); getchar(); //ловим искомую степень
  51.  
  52.     //проверяем массив на наличие таких степеней
  53.     for (i=0; i<inputLength; i++) {
  54.         if (node[input[i]-'a']/100 == searchDegree) {
  55.             node[input[i]-'a'] *= -1; //клёвая система распознавания, у нужного числа меняется знак
  56.             flag++; //флаг нужен для определения существования вершин с искомой степенью
  57.         }
  58.     }
  59.    
  60.     if (!flag) {
  61.         printf("Вершин с такой степенью нет. Выход...\n");
  62.         exit(0); }
  63.  
  64.     //выводим результаты
  65.     printf("В введённом графе степень %d имеют следующие вершины:\n", searchDegree);
  66.     printf("Node  Degree\n");
  67.     for (i=0; i<nodeAmount; i++) {
  68.         index = searchIndex(*list[i], node, nodeAmount);
  69.         if (node[i] < 0)
  70.             //на -100 делим для получения положительного числа и чтобы узнать степень
  71.             printf("%c%6d\n", 'A'+index, *list[i]/-100);
  72.     }
  73. }
  74.  
  75. int searchNode(char node, char* input) {
  76.     int j = 0, i = 0;
  77.     while (*(input+i) != '\0') {
  78.         if (*(input+i) == node) {
  79.             if (*(input+i-1) == '-') j++;
  80.             if (*(input+i+1) == '-') j++; }
  81.         i++; }
  82.     return j;
  83. }
  84.  
  85. int searchIndex(int degree, int* array, int length) {
  86.     for (int i=0; i < length; i++)
  87.         if (array[i] == degree) return i;
  88.     return -1;
  89. }
  90.  
  91. void msg(int action) {
  92.     char* message[10] = {
  93.         "Введите структуру графа.\nПоддерживается мнгострочный ввод, ",
  94.         "используйте  '--'.\nНапример: a--b--c\nВведите пустую строку для завершения ввода.\n",
  95.         "Введите искомую степень:\n",
  96.         "Неверный ввод.\n"
  97.     };
  98.     printf("%s", message[action]);
  99.      if (action > 3) exit(0);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment